1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
2 | |
2 | |
3 | use strict; |
3 | use strict; |
|
|
4 | use utf8; |
4 | |
5 | |
5 | use Crossfire::Client; |
6 | use Glib; |
6 | use Crossfire::Protocol; |
7 | use Gtk2 -init; |
7 | |
|
|
8 | package Crossfire::Client; # uh, yeah |
|
|
9 | |
|
|
10 | use strict; |
|
|
11 | |
8 | |
12 | use SDL; |
9 | use SDL; |
13 | use SDL::App; |
10 | use SDL::App; |
14 | use SDL::Event; |
11 | use SDL::Event; |
15 | use SDL::Surface; |
12 | use SDL::Surface; |
16 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
17 | use SDL::OpenGL::Constants; |
14 | use SDL::OpenGL::Constants; |
18 | |
15 | |
19 | my $conn; |
16 | use Crossfire; |
20 | my $app; |
17 | use Crossfire::Client; |
|
|
18 | use Crossfire::Protocol; |
21 | |
19 | |
22 | my $WIDTH = 640; |
20 | use Crossfire::Client::Widget; |
23 | my $HEIGHT = 480; |
|
|
24 | |
21 | |
25 | sub glinit { |
22 | our $FACECACHE; |
26 | # nuke all gl context data |
23 | |
27 | |
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
|
|
27 | |
|
|
28 | our $CFG; |
|
|
29 | our $CONN; |
|
|
30 | |
|
|
31 | our $WIDTH; |
|
|
32 | our $HEIGHT; |
|
|
33 | our $FULLSCREEN; |
|
|
34 | |
|
|
35 | our $FONTSIZE; |
|
|
36 | |
|
|
37 | our $SDL_TIMER; |
|
|
38 | our $SDL_APP; |
|
|
39 | our $SDL_EV = new SDL::Event; |
|
|
40 | our %SDL_CB; |
|
|
41 | |
|
|
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
|
|
45 | |
|
|
46 | our $tw; # Test widget #d# |
|
|
47 | |
|
|
48 | sub init_screen { |
28 | $app = new SDL::App |
49 | $SDL_APP = new SDL::App |
|
|
50 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
29 | -title => "Crossfire+ Client", |
51 | -title => "Crossfire+ Client", |
30 | -width => $WIDTH, |
52 | -width => $WIDTH, |
31 | -height => $HEIGHT, |
53 | -height => $HEIGHT, |
32 | -depth => 24, |
|
|
33 | -opengl => 1, |
54 | -opengl => 1, |
|
|
55 | -red_size => 8, |
|
|
56 | -green_size => 8, |
|
|
57 | -blue_size => 8, |
34 | -double_buffer => 1, |
58 | -double_buffer => 1, |
|
|
59 | -fullscreen => $FULLSCREEN, |
35 | -resizeable => 0; |
60 | -resizeable => 0; |
|
|
61 | |
|
|
62 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
|
|
63 | |
|
|
64 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
65 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
66 | |
|
|
67 | $FONTSIZE = int $HEIGHT / 40; |
|
|
68 | |
|
|
69 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, "Alt-Enter toggles fullscreen mode"; |
|
|
70 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
|
|
71 | $ALT_ENTER_MESSAGE->activate; |
|
|
72 | |
|
|
73 | # Test code #d# |
|
|
74 | { |
|
|
75 | my $frm = new Crossfire::Client::Widget::Frame; |
|
|
76 | $tw = new Crossfire::Client::Widget::Window; |
|
|
77 | $frm->add (new Crossfire::Client::Widget::Label 0, 0, 10, $FONTSIZE, "Foo in the garden!"); |
|
|
78 | $tw->add ($frm); |
|
|
79 | |
|
|
80 | $tw->move (0, $HEIGHT - 120); |
|
|
81 | $tw->activate; |
|
|
82 | } |
|
|
83 | |
|
|
84 | glClearColor 0, 0, 0, 0; |
36 | |
85 | |
37 | glEnable GL_TEXTURE_2D; |
86 | glEnable GL_TEXTURE_2D; |
|
|
87 | glEnable GL_COLOR_MATERIAL; |
38 | glShadeModel GL_FLAT; |
88 | glShadeModel GL_FLAT; |
39 | glDisable GL_DEPTH_TEST; |
89 | glDisable GL_DEPTH_TEST; |
|
|
90 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
91 | |
|
|
92 | $_->() for @GL_INIT; |
|
|
93 | } |
|
|
94 | |
|
|
95 | sub start_game { |
|
|
96 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
|
|
97 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
98 | while $SDL_EV->poll; |
|
|
99 | |
|
|
100 | 1 |
|
|
101 | }; |
|
|
102 | |
|
|
103 | $WIDTH = $CFG->{width}; |
|
|
104 | $HEIGHT = $CFG->{height}; |
|
|
105 | $FULLSCREEN = 0; |
|
|
106 | |
|
|
107 | init_screen; |
|
|
108 | |
|
|
109 | $CONN = new conn |
|
|
110 | host => $CFG->{host}, |
|
|
111 | port => $CFG->{port}, |
|
|
112 | user => $CFG->{user}, |
|
|
113 | pass => $CFG->{password}, |
|
|
114 | mapw => 50, |
|
|
115 | maph => 37, |
|
|
116 | ; |
|
|
117 | } |
|
|
118 | |
|
|
119 | sub stop_game { |
|
|
120 | remove Glib::Source $SDL_TIMER; |
|
|
121 | |
|
|
122 | undef $SDL_APP; |
|
|
123 | SDL::Quit; |
|
|
124 | } |
|
|
125 | |
|
|
126 | |
|
|
127 | sub force_refresh { |
|
|
128 | glViewport 0, 0, $WIDTH, $HEIGHT; |
|
|
129 | |
40 | glMatrixMode GL_PROJECTION; |
130 | glMatrixMode GL_PROJECTION; |
|
|
131 | glLoadIdentity; |
|
|
132 | glOrtho 0, $WIDTH, $HEIGHT, 0, -100 , 100; |
|
|
133 | glMatrixMode GL_MODELVIEW; |
41 | |
134 | |
42 | #glViewport 0, 0, $WIDTH, $HEIGHT; |
135 | glClear GL_COLOR_BUFFER_BIT; |
43 | # re-bind all textures |
136 | |
|
|
137 | $_->draw for @Crossfire::Client::Widget::ACTIVE_WIDGETS; |
|
|
138 | |
|
|
139 | SDL::GLSwapBuffers; |
44 | } |
140 | } |
|
|
141 | |
|
|
142 | my $refresh_handler; |
45 | |
143 | |
46 | sub refresh { |
144 | sub refresh { |
47 | glClearColor 0.5, 0.5, 0.7, 0; |
145 | $refresh_handler ||= add Glib::Idle sub { |
48 | glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; |
146 | force_refresh; |
|
|
147 | undef $refresh_handler; |
|
|
148 | 0 |
|
|
149 | }; |
|
|
150 | } |
49 | |
151 | |
50 | glLoadIdentity; |
152 | %SDL_CB = ( |
51 | glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; |
153 | SDL_QUIT() => sub { |
52 | |
154 | main_quit Gtk2; |
53 | my $map = $conn->{map}; |
155 | }, |
54 | |
156 | SDL_VIDEORESIZE() => sub { |
55 | for my $x (0 .. $conn->{mapw} - 1) { |
157 | }, |
56 | for my $y (0 .. $conn->{maph} - 1) { |
158 | SDL_VIDEOEXPOSE() => sub { |
57 | |
159 | refresh; |
58 | my $cell = $map->[$x][$y] |
160 | }, |
59 | or next; |
161 | SDL_KEYDOWN() => sub { |
60 | |
162 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
61 | for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) { |
163 | # alt-enter |
62 | my $tex = $conn->{face}[$num]{texture} || 0; |
164 | $FULLSCREEN = !$FULLSCREEN; |
63 | |
165 | init_screen; |
64 | glBindTexture GL_TEXTURE_2D, $tex; |
166 | } else { |
65 | |
167 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
66 | glBegin GL_QUADS; |
|
|
67 | glTexCoord 0, 0; glVertex $x, $y; |
|
|
68 | glTexCoord 1, 0; glVertex $x + 1, $y; |
|
|
69 | glTexCoord 1, 1; glVertex $x + 1, $y + 1; |
|
|
70 | glTexCoord 0, 1; glVertex $x, $y + 1; |
|
|
71 | glEnd; |
|
|
72 | } |
|
|
73 | } |
168 | } |
74 | } |
169 | }, |
75 | |
170 | SDL_KEYUP() => sub { |
76 | SDL::GLSwapBuffers; |
171 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
77 | } |
172 | }, |
78 | |
173 | SDL_MOUSEMOTION() => sub { |
79 | my $ev = new SDL::Event; |
174 | warn "sdl motion\n";#d# |
80 | my %ev_cb; |
175 | }, |
81 | |
176 | SDL_MOUSEBUTTONDOWN() => sub { |
82 | sub event(&$) { |
177 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
83 | $ev_cb{$_[0]->()} = $_[1]; |
178 | }, |
84 | } |
179 | SDL_MOUSEBUTTONUP() => sub { |
85 | |
180 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
86 | sub does(&) { shift } |
181 | }, |
87 | |
182 | SDL_ACTIVEEVENT() => sub { |
88 | event {SDL_QUIT} does { |
|
|
89 | exit; |
|
|
90 | }; |
|
|
91 | |
|
|
92 | event {SDL_VIDEORESIZE} does { |
|
|
93 | print "resize\n"; |
|
|
94 | }; |
|
|
95 | |
|
|
96 | event {SDL_KEYDOWN} does { |
|
|
97 | print "keypress\n"; |
|
|
98 | }; |
|
|
99 | |
|
|
100 | event {SDL_KEYUP} does { |
|
|
101 | print "keyup\n";#d# |
|
|
102 | }; |
|
|
103 | |
|
|
104 | event {SDL_MOUSEMOTION} does { |
|
|
105 | print "motion\n"; |
|
|
106 | }; |
|
|
107 | |
|
|
108 | event {SDL_MOUSEBUTTONDOWN} does { |
|
|
109 | print "button\n"; |
|
|
110 | }; |
|
|
111 | |
|
|
112 | event {SDL_MOUSEBUTTONUP} does { |
|
|
113 | print "buttup\n"; |
|
|
114 | }; |
|
|
115 | |
|
|
116 | event {SDL_ACTIVEEVENT} does { |
|
|
117 | print "active\n"; |
183 | warn "active\n";#d# |
118 | }; |
184 | }, |
119 | |
185 | ); |
120 | package Crossfire::Client; |
|
|
121 | |
186 | |
122 | @conn::ISA = Crossfire::Protocol::; |
187 | @conn::ISA = Crossfire::Protocol::; |
123 | |
188 | |
124 | sub conn::map_update { |
189 | sub conn::map_update { |
125 | my ($self, $dirty) = @_; |
190 | my ($self, $dirty) = @_; |
… | |
… | |
137 | my ($self) = @_; |
202 | my ($self) = @_; |
138 | |
203 | |
139 | refresh; |
204 | refresh; |
140 | } |
205 | } |
141 | |
206 | |
|
|
207 | sub conn::face_find { |
|
|
208 | my ($self, $face) = @_; |
|
|
209 | |
|
|
210 | $FACECACHE->{"$face->{chksum},$face->{name}"} |
|
|
211 | } |
|
|
212 | |
142 | sub conn::face_update { |
213 | sub conn::face_update { |
143 | my ($self, $num, $face) = @_; |
214 | my ($self, $face) = @_; |
144 | |
215 | |
145 | warn "up face $self,$num,$face\n";#d# |
216 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
146 | #TODO |
|
|
147 | open my $fh, ">:raw", "/tmp/x~"; |
|
|
148 | syswrite $fh, $face->{image}; |
|
|
149 | close $fh; |
|
|
150 | |
217 | |
151 | my $surface = new SDL::Surface -name => "/tmp/x~"; |
218 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
|
|
219 | } |
152 | |
220 | |
153 | unlink "/tmp/x~"; |
221 | sub conn::query { |
|
|
222 | my ($self, $flags, $prompt) = @_; |
154 | |
223 | |
155 | my ($tex) = @{glGenTextures 1}; |
224 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
156 | glGetError(); |
225 | } |
157 | |
226 | |
158 | $face->{texture} = $tex; |
227 | sub gtk_add_cfg_field { |
|
|
228 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
|
|
229 | my $i = $cfg->{_i}++; |
|
|
230 | $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
|
|
231 | $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
|
|
232 | if ($key eq 'password') { |
|
|
233 | $ent->set_invisible_char ("*"); |
|
|
234 | $ent->set (visibility => 0) |
|
|
235 | } |
|
|
236 | $ent->set_text ($value); |
|
|
237 | $ent->signal_connect (changed => sub { |
|
|
238 | my ($ent) = @_; |
|
|
239 | $cfg->{$key} = $ent->get_text; |
|
|
240 | }); |
|
|
241 | } |
|
|
242 | |
|
|
243 | sub run_config_dialog { |
|
|
244 | my (%events) = @_; |
|
|
245 | |
|
|
246 | my $w = Gtk2::Window->new; |
|
|
247 | |
|
|
248 | my @cfg = ( |
|
|
249 | [qw/Host host/], |
|
|
250 | [qw/Port port/], |
|
|
251 | [qw/Username user/], |
|
|
252 | [qw/Password password/], |
159 | |
253 | ); |
160 | glBindTexture GL_TEXTURE_2D, $tex; |
254 | |
161 | my $glerr=glGetError(); die "a: ".gluErrorString($glerr)."\n" if $glerr; |
255 | my $cfg = {}; |
|
|
256 | |
|
|
257 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
258 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
259 | |
|
|
260 | $w->add (my $vb = Gtk2::VBox->new); |
|
|
261 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
|
|
262 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
|
|
263 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
|
|
264 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
|
|
265 | my $i = 0; |
|
|
266 | my $act = 0; |
|
|
267 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
|
|
268 | if ($_ eq $selmode) { $act = $i } |
|
|
269 | $cb->append_text ($_); |
|
|
270 | $i++; |
|
|
271 | } |
|
|
272 | $cb->set_active ($act); |
|
|
273 | $cb->signal_connect (changed => sub { |
|
|
274 | my ($cb) = @_; |
|
|
275 | my $txt = $cb->get_active_text; |
|
|
276 | if ($txt =~ m/(\d+)x(\d+)/) { |
|
|
277 | $::CFG->{width} = $1; |
|
|
278 | $::CFG->{height} = $2; |
162 | |
279 | } |
163 | # glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; |
280 | }); |
164 | # glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR; |
|
|
165 | |
|
|
166 | $surface->rgba; |
|
|
167 | |
281 | |
168 | glTexImage2D GL_TEXTURE_2D, 0, |
282 | $cfg->{_i} = 1; |
169 | 4, # components |
283 | for (@cfg) { |
170 | $surface->width, $surface->height, |
284 | gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
|
|
285 | } |
|
|
286 | |
|
|
287 | $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
|
|
288 | $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
|
|
289 | $cb->signal_connect (clicked => sub { |
|
|
290 | for (keys %$cfg) { |
|
|
291 | $::CFG->{$_} = $cfg->{$_} |
|
|
292 | if $_ ne '_i'; |
171 | 0, |
293 | } |
|
|
294 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
295 | }); |
|
|
296 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
|
|
297 | $cb->signal_connect (clicked => sub { |
|
|
298 | for (keys %$cfg) { |
|
|
299 | $::CFG->{$_} = $cfg->{$_} |
|
|
300 | if $_ ne '_i'; |
172 | GL_RGBA, |
301 | } |
173 | GL_UNSIGNED_BYTE, |
302 | my $cb = $events{login} || sub {}; |
174 | $surface->pixels; |
303 | $cb->($::CFG->{user}, $::CFG->{password}); |
175 | my $glerr=glGetError(); die "Problem setting up 2d Texture (dimensions not a power of 2?)):".gluErrorString($glerr)."\n" if $glerr; |
304 | }); |
|
|
305 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
|
|
306 | $cb->signal_connect (clicked => sub { |
|
|
307 | my $cb = $events{login} || sub {}; |
|
|
308 | $cb->(); |
|
|
309 | }); |
|
|
310 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
|
|
311 | $cb->signal_connect (clicked => sub { $w->destroy }); |
|
|
312 | |
|
|
313 | $w->show_all; |
|
|
314 | |
|
|
315 | $w->signal_connect (destroy => sub { Gtk2->main_quit }); |
176 | } |
316 | } |
|
|
317 | |
177 | |
318 | |
178 | ############################################################################# |
319 | ############################################################################# |
179 | |
320 | |
180 | use Event; |
321 | SDL::Init SDL_INIT_EVERYTHING; |
181 | |
322 | |
182 | glinit; |
323 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
183 | |
324 | |
184 | $conn = new conn |
325 | $mapwidget->activate; |
|
|
326 | $mapwidget->focus_in; |
|
|
327 | |
|
|
328 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
|
|
329 | |
|
|
330 | $CFG ||= { |
|
|
331 | width => 640, |
|
|
332 | height => 480, |
|
|
333 | fullscreen => 0, |
185 | host => "cf.schmorp.de", |
334 | host => "crossfire.schmorp.de", |
186 | port => 13327; |
335 | port => 13327, |
187 | |
|
|
188 | Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub { |
|
|
189 | while ($ev->poll) { |
|
|
190 | ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->(); |
|
|
191 | } |
|
|
192 | }); |
336 | }; |
193 | |
337 | |
194 | Event::loop; |
338 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
195 | |
339 | |
|
|
340 | run_config_dialog |
|
|
341 | login => sub { start_game }, |
|
|
342 | logout => sub { stop_game }; |
196 | |
343 | |
|
|
344 | main Gtk2; |
|
|
345 | |
|
|
346 | Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |