ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.9
Committed: Fri Apr 7 13:53:38 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.8: +16 -17 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4
5 use Crossfire::Client;
6 use Crossfire::Protocol;
7
8 package Crossfire::Client; # uh, yeah
9
10 use strict;
11
12 use SDL;
13 use SDL::App;
14 use SDL::Event;
15 use SDL::Surface;
16 use SDL::OpenGL;
17 use SDL::OpenGL::Constants;
18
19 my $conn;
20 my $app;
21
22 my $WIDTH = 640;
23 my $HEIGHT = 480;
24
25 sub glinit {
26 # nuke all gl context data
27
28 $app = new SDL::App
29 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
30 -title => "Crossfire+ Client",
31 -width => $WIDTH,
32 -height => $HEIGHT,
33 -opengl => 1,
34 -red_size => 8,
35 -green_size => 8,
36 -blue_size => 8,
37 -double_buffer => 1,
38 -resizeable => 0;
39
40 glEnable GL_TEXTURE_2D;
41 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE;
42 glShadeModel GL_FLAT;
43 glDisable GL_DEPTH_TEST;
44 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
45 glEnable GL_BLEND;
46
47 glMatrixMode GL_PROJECTION;
48 glLoadIdentity;
49 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1;
50
51 # re-bind all textures
52 }
53
54 sub refresh {
55 glClearColor 0, 0, 0, 0;
56 glClear GL_COLOR_BUFFER_BIT;
57
58 my $map = $conn->{map};
59
60 for my $x (0 .. $conn->{mapw} - 1) {
61 for my $y (0 .. $conn->{maph} - 1) {
62
63 my $cell = $map->[$x][$y]
64 or next;
65
66 my $darkness = $cell->[3] * (1 / 255);
67 my $darkness = 0.8 + 0.2*rand;
68 glColor $darkness, $darkness, $darkness;
69
70 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
71 my $tex = $conn->{face}[$num]{texture} || 0;
72
73 glBindTexture GL_TEXTURE_2D, $tex;
74
75 glBegin GL_QUADS;
76 glTexCoord 0, 0; glVertex $x, $y;
77 glTexCoord 0, 1; glVertex $x, $y + 1;
78 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
79 glTexCoord 1, 0; glVertex $x + 1, $y;
80 glEnd;
81 }
82 }
83 }
84
85 SDL::GLSwapBuffers;
86 }
87
88 my $ev = new SDL::Event;
89 my %ev_cb;
90
91 sub event(&$) {
92 $ev_cb{$_[0]->()} = $_[1];
93 }
94
95 sub does(&) { shift }
96
97 event {SDL_QUIT} does {
98 exit;
99 };
100
101 event {SDL_VIDEORESIZE} does {
102 print "resize\n";
103 };
104
105 event {SDL_VIDEOEXPOSE} does {
106 refresh;
107 };
108
109 event {SDL_KEYDOWN} does {
110 print "keypress\n";
111 };
112
113 event {SDL_KEYUP} does {
114 print "keyup\n";#d#
115 };
116
117 event {SDL_MOUSEMOTION} does {
118 print "motion\n";
119 };
120
121 event {SDL_MOUSEBUTTONDOWN} does {
122 print "button\n";
123 };
124
125 event {SDL_MOUSEBUTTONUP} does {
126 print "buttup\n";
127 };
128
129 event {SDL_ACTIVEEVENT} does {
130 print "active\n";
131 };
132
133 package Crossfire::Client;
134
135 @conn::ISA = Crossfire::Protocol::;
136
137 sub conn::map_update {
138 my ($self, $dirty) = @_;
139
140 refresh;
141 }
142
143 sub conn::map_scroll {
144 my ($self, $dx, $dy) = @_;
145
146 refresh;
147 }
148
149 sub conn::map_clear {
150 my ($self) = @_;
151
152 refresh;
153 }
154
155 sub conn::face_update {
156 my ($self, $num, $face) = @_;
157
158 warn "up face $self,$num,$face\n";#d#
159 use Gtk2;
160
161 my $pb = new Gtk2::Gdk::PixbufLoader;
162 $pb->write ($face->{image});
163 $pb->close;
164
165 $pb = $pb->get_pixbuf;
166 $pb = $pb->add_alpha (0, 0, 0, 0);
167
168 glGetError();
169 my ($tex) = @{glGenTextures 1};
170
171 $face->{texture} = $tex;
172
173 glBindTexture GL_TEXTURE_2D, $tex;
174 my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr;
175
176 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR;
177 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR;#_MIPMAP_LINEAR;
178 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP;
179 glTexParameter GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP;
180
181 glTexImage2D GL_TEXTURE_2D, 0,
182 GL_RGBA8,
183 $pb->get_width, $pb->get_height,
184 0,
185 GL_RGBA,
186 GL_UNSIGNED_BYTE,
187 $pb->get_pixels;
188 my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr;
189 }
190
191 #############################################################################
192
193 use Event;
194
195 glinit;
196
197 $conn = new conn
198 host => "cf.schmorp.de",
199 port => 13327;
200
201 Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
202 while ($ev->poll) {
203 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
204 }
205 });
206
207 Event::loop;
208
209