1 | #!/opt/bin/perl |
1 | #!/opt/bin/perl |
|
|
2 | |
2 | use strict; |
3 | use strict; |
|
|
4 | use utf8; |
3 | |
5 | |
4 | use Glib; |
6 | use Glib; |
5 | use Gtk2 -init; |
7 | use Gtk2 -init; |
6 | |
8 | |
7 | use SDL; |
9 | use SDL; |
… | |
… | |
13 | |
15 | |
14 | use Crossfire; |
16 | use Crossfire; |
15 | use Crossfire::Client; |
17 | use Crossfire::Client; |
16 | use Crossfire::Protocol; |
18 | use Crossfire::Protocol; |
17 | |
19 | |
18 | use Crossfire::Client::Util; |
|
|
19 | use Crossfire::Client::Widget; |
20 | use Crossfire::Client::Widget; |
20 | |
21 | |
21 | our $FACECACHE; |
22 | our $FACECACHE; |
22 | |
23 | |
23 | our $VERSION = '0.1'; |
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
24 | |
27 | |
25 | our $CFG; |
28 | our $CFG; |
26 | our $CONN; |
29 | our $CONN; |
|
|
30 | |
|
|
31 | our $WIDTH; |
|
|
32 | our $HEIGHT; |
|
|
33 | our $FULLSCREEN; |
|
|
34 | |
|
|
35 | our $FONTSIZE; |
|
|
36 | our $FOCUS; |
|
|
37 | our $HOVER; |
27 | |
38 | |
28 | our $SDL_TIMER; |
39 | our $SDL_TIMER; |
29 | our $SDL_APP; |
40 | our $SDL_APP; |
30 | our $SDL_EV = new SDL::Event; |
41 | our $SDL_EV = new SDL::Event; |
31 | our %SDL_CB; |
42 | our %SDL_CB; |
32 | |
43 | |
33 | our @GL_INIT; # hooks called on every gl init |
44 | our @GL_INIT; # hooks called on every gl init |
34 | |
45 | |
|
|
46 | our $ALT_ENTER_MESSAGE; |
|
|
47 | our $STATUS_LINE; |
|
|
48 | |
|
|
49 | our $TOPLEVEL; |
|
|
50 | |
|
|
51 | our $tw; # Test widget #d# |
|
|
52 | |
|
|
53 | my $last_refresh; |
|
|
54 | my %ANIMATE; |
|
|
55 | my $refresh_handler; |
|
|
56 | |
35 | sub init_screen { |
57 | sub init_screen { |
36 | $SDL_APP = new SDL::App |
58 | $SDL_APP = new SDL::App |
37 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
59 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
38 | -title => "Crossfire+ Client", |
60 | -title => "Crossfire+ Client", |
39 | -width => $CFG->{width}, |
61 | -width => $WIDTH, |
40 | -height => $CFG->{height}, |
62 | -height => $HEIGHT, |
41 | -opengl => 1, |
63 | -opengl => 1, |
42 | -red_size => 8, |
64 | -red_size => 5, |
43 | -green_size => 8, |
65 | -green_size => 5, |
44 | -blue_size => 8, |
66 | -blue_size => 5, |
|
|
67 | -alpha_size => 0, |
45 | -double_buffer => 1, |
68 | -double_buffer => 1, |
46 | -fullscreen => $CFG->{fullscreen}, |
69 | -fullscreen => $FULLSCREEN, |
47 | -resizeable => 0; |
70 | -resizeable => 0; |
48 | |
71 | |
|
|
72 | $last_refresh = SDL::GetTicks; |
|
|
73 | |
|
|
74 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
|
|
75 | |
|
|
76 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
77 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
78 | |
|
|
79 | $FONTSIZE = int $HEIGHT / 50; |
|
|
80 | |
|
|
81 | ############################################################################# |
|
|
82 | |
49 | glClearColor 0, 0, 0, 0; |
83 | glClearColor 0, 0, 0, 0; |
50 | |
84 | |
51 | glEnable GL_TEXTURE_2D; |
85 | glEnable GL_TEXTURE_2D; |
52 | glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; |
86 | glEnable GL_COLOR_MATERIAL; |
53 | glShadeModel GL_FLAT; |
87 | glShadeModel GL_FLAT; |
54 | glDisable GL_DEPTH_TEST; |
88 | glDisable GL_DEPTH_TEST; |
55 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
56 | glEnable GL_BLEND; |
90 | |
|
|
91 | $_->() for @GL_INIT; |
|
|
92 | |
|
|
93 | ############################################################################# |
|
|
94 | |
|
|
95 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
|
|
96 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
|
|
97 | ""; |
|
|
98 | $TOPLEVEL->add ($STATUS_LINE); |
|
|
99 | |
|
|
100 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
|
|
101 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
|
|
102 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
|
|
103 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
104 | |
|
|
105 | # Test code #d# |
|
|
106 | unless ($tw) { # haha... |
|
|
107 | $tw = new Crossfire::Client::Widget::Animator; |
|
|
108 | my $lbl1 = new Crossfire::Client::Widget::Label |
|
|
109 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
110 | my $lbl2 = new Crossfire::Client::Widget::Label |
|
|
111 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
112 | |
|
|
113 | my $vb = new Crossfire::Client::Widget::VBox; |
|
|
114 | my $f = new Crossfire::Client::Widget::FancyFrame; |
|
|
115 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
|
|
116 | $f->add ($lbl1); |
|
|
117 | $f2->add ($lbl2); |
|
|
118 | $vb->add ($f); |
|
|
119 | $vb->add ($f2, 1); |
|
|
120 | |
|
|
121 | $tw->add ($vb); |
|
|
122 | $tw->w (400); |
|
|
123 | $tw->h (300); |
|
|
124 | $tw->move ($WIDTH - 200, 0); |
|
|
125 | $tw->moveto (0, 0); |
|
|
126 | $TOPLEVEL->add ($tw); |
|
|
127 | |
|
|
128 | # $f->move ($WIDTH - 200, 0); |
|
|
129 | # $TOPLEVEL->add ($f); |
|
|
130 | } |
|
|
131 | } |
|
|
132 | |
|
|
133 | sub start_game { |
|
|
134 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
|
|
135 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
136 | while $SDL_EV->poll; |
|
|
137 | |
|
|
138 | 1 |
|
|
139 | }; |
|
|
140 | |
|
|
141 | $WIDTH = $CFG->{width}; |
|
|
142 | $HEIGHT = $CFG->{height}; |
|
|
143 | $FULLSCREEN = 0; |
|
|
144 | |
|
|
145 | init_screen; |
|
|
146 | |
|
|
147 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
|
|
148 | |
|
|
149 | $CONN = new conn |
|
|
150 | host => $CFG->{host}, |
|
|
151 | port => $CFG->{port}, |
|
|
152 | user => $CFG->{user}, |
|
|
153 | pass => $CFG->{password}, |
|
|
154 | mapw => $mapsize, |
|
|
155 | maph => $mapsize, |
|
|
156 | ; |
|
|
157 | |
|
|
158 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
|
|
159 | } |
|
|
160 | |
|
|
161 | sub stop_game { |
|
|
162 | remove Glib::Source $SDL_TIMER; |
|
|
163 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
164 | undef $refresh_handler; |
|
|
165 | |
|
|
166 | undef $SDL_APP; |
|
|
167 | undef $CONN; |
|
|
168 | SDL::Quit; |
|
|
169 | } |
|
|
170 | |
|
|
171 | |
|
|
172 | sub force_refresh { |
|
|
173 | glViewport 0, 0, $WIDTH, $HEIGHT; |
57 | |
174 | |
58 | glMatrixMode GL_PROJECTION; |
175 | glMatrixMode GL_PROJECTION; |
59 | glLoadIdentity; |
176 | glLoadIdentity; |
60 | glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -100 , 100; |
177 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
61 | |
|
|
62 | glMatrixMode GL_MODELVIEW; |
178 | glMatrixMode GL_MODELVIEW; |
63 | |
179 | |
64 | $_->() for @GL_INIT; |
180 | glClear GL_COLOR_BUFFER_BIT; |
65 | } |
|
|
66 | |
181 | |
67 | sub start_game { |
182 | $TOPLEVEL->draw; |
68 | $SDL_TIMER = add Glib::Timeout 1000/20, sub { |
183 | |
69 | while ($SDL_EV->poll) { |
184 | SDL::GLSwapBuffers; |
70 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); |
185 | } |
|
|
186 | |
|
|
187 | sub refresh { |
|
|
188 | $refresh_handler ||= add Glib::Idle sub { |
|
|
189 | return unless $SDL_APP; |
|
|
190 | |
|
|
191 | my $next_refresh = SDL::GetTicks; |
|
|
192 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
|
|
193 | $last_refresh = $next_refresh; |
|
|
194 | |
|
|
195 | force_refresh; |
|
|
196 | $_->animate ($interval) for grep $_, values %ANIMATE; |
|
|
197 | |
|
|
198 | if (%ANIMATE) { |
|
|
199 | 1 |
|
|
200 | } else { |
|
|
201 | undef $refresh_handler; |
|
|
202 | 0 |
71 | } |
203 | } |
72 | |
|
|
73 | 1 |
|
|
74 | }; |
204 | }; |
75 | |
|
|
76 | init_screen; |
|
|
77 | |
|
|
78 | $CONN = new conn |
|
|
79 | host => $CFG->{host}, |
|
|
80 | port => $CFG->{port}; |
|
|
81 | } |
205 | } |
82 | |
206 | |
83 | sub stop_game { |
207 | sub animation_start { |
84 | remove Glib::Source $SDL_TIMER; |
208 | my ($widget) = @_; |
|
|
209 | $ANIMATE{$widget} = $widget; |
|
|
210 | Scalar::Util::weaken $ANIMATE{$widget}; |
85 | |
211 | |
86 | undef $SDL_APP; |
212 | refresh; |
87 | SDL::Quit; |
|
|
88 | } |
213 | } |
89 | |
214 | |
90 | sub refresh { |
215 | sub animation_stop { |
91 | glClear GL_COLOR_BUFFER_BIT; |
216 | my ($widget) = @_; |
92 | |
217 | delete $ANIMATE{$widget}; |
93 | $_->draw for values %Crossfire::Client::Widget::ACTIVE_WIDGETS; |
|
|
94 | |
|
|
95 | SDL::GLSwapBuffers; |
|
|
96 | } |
218 | } |
97 | |
219 | |
98 | %SDL_CB = ( |
220 | %SDL_CB = ( |
99 | SDL_QUIT() => sub { |
221 | SDL_QUIT() => sub { |
100 | main_quit Gtk2; |
222 | main_quit Gtk2; |
… | |
… | |
105 | refresh; |
227 | refresh; |
106 | }, |
228 | }, |
107 | SDL_KEYDOWN() => sub { |
229 | SDL_KEYDOWN() => sub { |
108 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
230 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
109 | # alt-enter |
231 | # alt-enter |
110 | $CFG->{fullscreen} = !$CFG->{fullscreen}; |
232 | $FULLSCREEN = !$FULLSCREEN; |
111 | init_screen; |
233 | init_screen; |
112 | } else { |
234 | } else { |
113 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
235 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
114 | } |
236 | } |
115 | }, |
237 | }, |
116 | SDL_KEYUP() => sub { |
238 | SDL_KEYUP() => sub { |
117 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
239 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
118 | }, |
240 | }, |
119 | SDL_MOUSEMOTION() => sub { |
241 | SDL_MOUSEMOTION() => sub { |
120 | warn "sdl motion\n";#d# |
242 | my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); |
|
|
243 | $HOVER = $TOPLEVEL->find_widget ($x, $y); |
|
|
244 | ::refresh; |
|
|
245 | |
|
|
246 | warn "mouse $x, $y = $HOVER\n"; |
121 | }, |
247 | }, |
122 | SDL_MOUSEBUTTONDOWN() => sub { |
248 | SDL_MOUSEBUTTONDOWN() => sub { |
123 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
249 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
124 | }, |
250 | }, |
125 | SDL_MOUSEBUTTONUP() => sub { |
251 | SDL_MOUSEBUTTONUP() => sub { |
… | |
… | |
139 | } |
265 | } |
140 | |
266 | |
141 | sub conn::map_scroll { |
267 | sub conn::map_scroll { |
142 | my ($self, $dx, $dy) = @_; |
268 | my ($self, $dx, $dy) = @_; |
143 | |
269 | |
144 | refresh; |
270 | # refresh; |
145 | } |
271 | } |
146 | |
272 | |
147 | sub conn::map_clear { |
273 | sub conn::map_clear { |
148 | my ($self) = @_; |
274 | my ($self) = @_; |
149 | |
275 | |
150 | refresh; |
276 | # refresh; |
151 | } |
277 | } |
152 | |
278 | |
153 | sub conn::face_find { |
279 | sub conn::face_find { |
154 | my ($self, $face) = @_; |
280 | my ($self, $face) = @_; |
155 | |
281 | |
… | |
… | |
160 | my ($self, $face) = @_; |
286 | my ($self, $face) = @_; |
161 | |
287 | |
162 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
288 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
163 | |
289 | |
164 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
290 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
|
|
291 | } |
|
|
292 | |
|
|
293 | sub conn::query { |
|
|
294 | my ($self, $flags, $prompt) = @_; |
|
|
295 | |
|
|
296 | warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
165 | } |
297 | } |
166 | |
298 | |
167 | sub gtk_add_cfg_field { |
299 | sub gtk_add_cfg_field { |
168 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
300 | my ($tbl, $cfg, $klbl, $key, $value) = @_; |
169 | my $i = $cfg->{_i}++; |
301 | my $i = $cfg->{_i}++; |
… | |
… | |
175 | } |
307 | } |
176 | $ent->set_text ($value); |
308 | $ent->set_text ($value); |
177 | $ent->signal_connect (changed => sub { |
309 | $ent->signal_connect (changed => sub { |
178 | my ($ent) = @_; |
310 | my ($ent) = @_; |
179 | $cfg->{$key} = $ent->get_text; |
311 | $cfg->{$key} = $ent->get_text; |
|
|
312 | # TODO: Mapsize should be a slider in the game gui |
|
|
313 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
314 | $cfg->{$key} = 100; |
|
|
315 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
316 | $cfg->{$key} = 50; |
|
|
317 | } |
180 | }); |
318 | }); |
181 | } |
319 | } |
182 | |
320 | |
183 | sub run_config_dialog { |
321 | sub run_config_dialog { |
184 | my (%events) = @_; |
322 | my (%events) = @_; |
… | |
… | |
186 | my $w = Gtk2::Window->new; |
324 | my $w = Gtk2::Window->new; |
187 | |
325 | |
188 | my @cfg = ( |
326 | my @cfg = ( |
189 | [qw/Host host/], |
327 | [qw/Host host/], |
190 | [qw/Port port/], |
328 | [qw/Port port/], |
|
|
329 | [qw/Mapsize% mapsize/], |
191 | [qw/Username user/], |
330 | [qw/Username user/], |
192 | [qw/Password password/], |
331 | [qw/Password password/], |
193 | ); |
332 | ); |
194 | |
333 | |
195 | my $cfg = {}; |
334 | my $cfg = {}; |
… | |
… | |
242 | my $cb = $events{login} || sub {}; |
381 | my $cb = $events{login} || sub {}; |
243 | $cb->($::CFG->{user}, $::CFG->{password}); |
382 | $cb->($::CFG->{user}, $::CFG->{password}); |
244 | }); |
383 | }); |
245 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
384 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
246 | $cb->signal_connect (clicked => sub { |
385 | $cb->signal_connect (clicked => sub { |
247 | my $cb = $events{login} || sub {}; |
386 | my $cb = $events{logout} || sub {}; |
248 | $cb->(); |
387 | $cb->(); |
249 | }); |
388 | }); |
250 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
389 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
251 | $cb->signal_connect (clicked => sub { $w->destroy }); |
390 | $cb->signal_connect (clicked => sub { $w->destroy }); |
252 | |
391 | |
… | |
… | |
256 | } |
395 | } |
257 | |
396 | |
258 | |
397 | |
259 | ############################################################################# |
398 | ############################################################################# |
260 | |
399 | |
261 | SDL::Init(SDL_INIT_EVERYTHING()); |
400 | SDL::Init SDL_INIT_EVERYTHING; |
|
|
401 | |
|
|
402 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
262 | |
403 | |
263 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
404 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
264 | |
405 | |
265 | find_rcfile "uifont.ttf"; |
406 | $TOPLEVEL->add ($mapwidget); |
266 | |
|
|
267 | $mapwidget->activate; |
|
|
268 | $mapwidget->focus_in; |
407 | $mapwidget->focus_in; |
269 | |
408 | |
270 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
409 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
271 | |
|
|
272 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
|
|
273 | |
410 | |
274 | $CFG ||= { |
411 | $CFG ||= { |
275 | width => 640, |
412 | width => 640, |
276 | height => 480, |
413 | height => 480, |
|
|
414 | mapsize => 100, |
277 | fullscreen => 0, |
415 | fullscreen => 0, |
278 | host => "crossfire.schmorp.de", |
416 | host => "crossfire.schmorp.de", |
279 | port => 13327, |
417 | port => 13327, |
280 | }; |
418 | }; |
281 | |
419 | |
|
|
420 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
|
|
421 | |
|
|
422 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
|
|
423 | |
282 | run_config_dialog |
424 | run_config_dialog |
283 | login => sub { start_game }, |
425 | login => sub { start_game }, |
284 | logout => sub { stop_game }; |
426 | logout => sub { stop_game }; |
285 | |
427 | |
286 | main Gtk2; |
428 | main Gtk2; |