… | |
… | |
12 | use SDL::Surface; |
12 | use SDL::Surface; |
13 | use SDL::OpenGL; |
13 | use SDL::OpenGL; |
14 | use SDL::OpenGL::Constants; |
14 | use SDL::OpenGL::Constants; |
15 | |
15 | |
16 | use Crossfire; |
16 | use Crossfire; |
|
|
17 | use Crossfire::Protocol; |
|
|
18 | |
17 | use Crossfire::Client; |
19 | use Crossfire::Client; |
18 | use Crossfire::Protocol; |
|
|
19 | |
|
|
20 | use Crossfire::Client::Widget; |
20 | use Crossfire::Client::Widget; |
21 | |
21 | |
|
|
22 | our $VERSION = '0.1'; |
|
|
23 | |
|
|
24 | my $MAX_FPS = 30; |
|
|
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
|
|
26 | |
22 | our $FACECACHE; |
27 | our $FACECACHE; |
23 | |
|
|
24 | our $VERSION = '0.1'; |
|
|
25 | |
|
|
26 | our %GL_EXT; |
|
|
27 | |
28 | |
28 | our $CFG; |
29 | our $CFG; |
29 | our $CONN; |
30 | our $CONN; |
30 | |
31 | |
31 | our $WIDTH; |
32 | our $WIDTH; |
… | |
… | |
34 | |
35 | |
35 | our $FONTSIZE; |
36 | our $FONTSIZE; |
36 | |
37 | |
37 | our $SDL_TIMER; |
38 | our $SDL_TIMER; |
38 | our $SDL_APP; |
39 | our $SDL_APP; |
39 | our $SDL_EV = new SDL::Event; |
40 | our $SDL_EV; |
40 | our %SDL_CB; |
41 | our %SDL_CB; |
41 | |
42 | |
42 | our @GL_INIT; # hooks called on every gl init |
|
|
43 | |
|
|
44 | our $ALT_ENTER_MESSAGE; |
43 | our $ALT_ENTER_MESSAGE; |
45 | |
44 | our $STATUS_LINE; |
46 | our $TOPLEVEL; |
45 | our $DEBUG_STATUS; |
47 | |
|
|
48 | our $tw; # Test widget #d# |
|
|
49 | |
46 | |
50 | my $last_refresh; |
47 | my $last_refresh; |
|
|
48 | my %ANIMATE; |
|
|
49 | my $refresh_handler; |
|
|
50 | |
|
|
51 | our ($tw, $te); # Test widget #d# |
51 | |
52 | |
52 | sub init_screen { |
53 | sub init_screen { |
53 | $SDL_APP = new SDL::App |
54 | $SDL_APP = new SDL::App |
54 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
55 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
55 | -title => "Crossfire+ Client", |
56 | -title => "Crossfire+ Client", |
56 | -width => $WIDTH, |
57 | -width => $WIDTH, |
57 | -height => $HEIGHT, |
58 | -height => $HEIGHT, |
58 | -opengl => 1, |
59 | -opengl => 1, |
59 | -red_size => 8, |
60 | -red_size => 5, |
60 | -green_size => 8, |
61 | -green_size => 5, |
61 | -blue_size => 8, |
62 | -blue_size => 5, |
|
|
63 | -alpha_size => 0, |
62 | -double_buffer => 1, |
64 | -double_buffer => 1, |
63 | -fullscreen => $FULLSCREEN, |
65 | -fullscreen => $FULLSCREEN, |
64 | -resizeable => 0; |
66 | -resizeable => 0; |
65 | |
67 | |
|
|
68 | $SDL_EV = new SDL::Event; |
|
|
69 | $SDL_EV->set_unicode (1); |
|
|
70 | |
|
|
71 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
|
|
72 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
73 | while $SDL_EV->poll; |
|
|
74 | |
|
|
75 | 1 |
|
|
76 | }; |
|
|
77 | |
66 | $last_refresh = SDL::GetTicks; |
78 | $last_refresh = SDL::GetTicks; |
67 | |
79 | |
68 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
80 | Crossfire::Client::gl_init; |
69 | |
|
|
70 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
71 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
72 | |
81 | |
73 | $FONTSIZE = int $HEIGHT / 50; |
82 | $FONTSIZE = int $HEIGHT / 50; |
74 | |
83 | |
|
|
84 | ############################################################################# |
|
|
85 | |
|
|
86 | glClearColor 0.45, 0.45, 0.45, 1; |
|
|
87 | |
|
|
88 | glEnable GL_TEXTURE_2D; |
|
|
89 | glEnable GL_COLOR_MATERIAL; |
|
|
90 | glShadeModel GL_FLAT; |
|
|
91 | glDisable GL_DEPTH_TEST; |
|
|
92 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
93 | |
|
|
94 | ############################################################################# |
|
|
95 | |
|
|
96 | $DEBUG_STATUS = new Crossfire::Client::Widget::Label 0, 0, 1, $FONTSIZE, ""; |
|
|
97 | $Crossfire::Client::Widget::TOPLEVEL->add ($DEBUG_STATUS); |
|
|
98 | |
|
|
99 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
|
|
100 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
|
|
101 | ""; |
|
|
102 | $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE); |
|
|
103 | |
75 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
104 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
76 | 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE, |
105 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
77 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
106 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
78 | $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]); |
107 | $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
79 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
|
|
80 | |
108 | |
81 | # Test code #d# |
109 | # Test code #d# |
82 | unless ($tw) { # haha... |
110 | unless ($tw) { # haha... |
|
|
111 | $te = new Crossfire::Client::Widget::FancyFrame; |
|
|
112 | $te->add (new Crossfire::Client::Widget::Entry); |
|
|
113 | $te->move (300, 0, 2); |
|
|
114 | $Crossfire::Client::Widget::TOPLEVEL->add ($te); |
|
|
115 | |
83 | $tw = new Crossfire::Client::Widget::Animator; |
116 | $tw = new Crossfire::Client::Widget::Animator; |
84 | my $lbl1 = new Crossfire::Client::Widget::Label |
117 | my $lbl1 = new Crossfire::Client::Widget::Label |
85 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
118 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
86 | my $lbl2 = new Crossfire::Client::Widget::Label |
119 | my $lbl2 = new Crossfire::Client::Widget::Label |
87 | 0, 0, 10, $FONTSIZE, "LBL2"; |
120 | 0, 0, 10, $FONTSIZE, "LBL2"; |
… | |
… | |
97 | $tw->add ($vb); |
130 | $tw->add ($vb); |
98 | $tw->w (400); |
131 | $tw->w (400); |
99 | $tw->h (300); |
132 | $tw->h (300); |
100 | $tw->move ($WIDTH - 200, 0); |
133 | $tw->move ($WIDTH - 200, 0); |
101 | $tw->moveto (0, 0); |
134 | $tw->moveto (0, 0); |
102 | $TOPLEVEL->add ($tw); |
135 | $Crossfire::Client::Widget::TOPLEVEL->add ($tw); |
103 | |
136 | |
104 | # $f->move ($WIDTH - 200, 0); |
137 | # $f->move ($WIDTH - 200, 0); |
105 | # $TOPLEVEL->add ($f); |
138 | # $Crossfire::Client::Widget::TOPLEVEL->add ($f); |
106 | } |
139 | } |
|
|
140 | } |
107 | |
141 | |
108 | glClearColor 0, 0, 0, 0; |
142 | sub destroy_screen { |
109 | |
143 | remove Glib::Source $SDL_TIMER; |
110 | glEnable GL_TEXTURE_2D; |
144 | undef $SDL_APP; |
111 | glEnable GL_COLOR_MATERIAL; |
145 | undef $SDL_EV; |
112 | glShadeModel GL_FLAT; |
146 | SDL::Quit; |
113 | glDisable GL_DEPTH_TEST; |
|
|
114 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
|
|
115 | |
|
|
116 | $_->() for @GL_INIT; |
|
|
117 | } |
147 | } |
118 | |
148 | |
119 | sub start_game { |
149 | sub start_game { |
120 | $SDL_TIMER = add Glib::Timeout 1000/100, sub { |
|
|
121 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
122 | while $SDL_EV->poll; |
|
|
123 | |
|
|
124 | 1 |
|
|
125 | }; |
|
|
126 | |
|
|
127 | $WIDTH = $CFG->{width}; |
150 | $WIDTH = $CFG->{width}; |
128 | $HEIGHT = $CFG->{height}; |
151 | $HEIGHT = $CFG->{height}; |
129 | $FULLSCREEN = 0; |
152 | $FULLSCREEN = 0; |
130 | |
153 | |
131 | init_screen; |
154 | init_screen; |
|
|
155 | |
|
|
156 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
132 | |
157 | |
133 | $CONN = new conn |
158 | $CONN = new conn |
134 | host => $CFG->{host}, |
159 | host => $CFG->{host}, |
135 | port => $CFG->{port}, |
160 | port => $CFG->{port}, |
136 | user => $CFG->{user}, |
161 | user => $CFG->{user}, |
137 | pass => $CFG->{password}, |
162 | pass => $CFG->{password}, |
138 | mapw => 50, |
163 | mapw => $mapsize, |
139 | maph => 37, |
164 | maph => $mapsize, |
140 | ; |
165 | ; |
141 | |
166 | |
142 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
167 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
143 | } |
168 | } |
144 | |
169 | |
145 | sub stop_game { |
170 | sub stop_game { |
146 | remove Glib::Source $SDL_TIMER; |
171 | remove Glib::Source $refresh_handler if $refresh_handler; |
|
|
172 | undef $refresh_handler; |
147 | |
173 | |
148 | undef $SDL_APP; |
174 | undef $CONN; |
149 | SDL::Quit; |
175 | destroy_screen; |
150 | } |
176 | } |
151 | |
|
|
152 | |
177 | |
153 | sub force_refresh { |
178 | sub force_refresh { |
154 | glViewport 0, 0, $WIDTH, $HEIGHT; |
179 | glViewport 0, 0, $WIDTH, $HEIGHT; |
155 | |
180 | |
156 | glMatrixMode GL_PROJECTION; |
181 | glMatrixMode GL_PROJECTION; |
157 | glLoadIdentity; |
182 | glLoadIdentity; |
158 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
183 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
159 | glMatrixMode GL_MODELVIEW; |
184 | glMatrixMode GL_MODELVIEW; |
160 | |
185 | |
161 | glClear GL_COLOR_BUFFER_BIT; |
186 | glClear GL_COLOR_BUFFER_BIT; |
162 | |
187 | |
163 | $TOPLEVEL->draw; |
188 | $Crossfire::Client::Widget::TOPLEVEL->draw; |
164 | |
189 | |
165 | SDL::GLSwapBuffers; |
190 | SDL::GLSwapBuffers; |
166 | } |
191 | } |
167 | |
192 | |
168 | my %ANIMATE; |
193 | sub debug { |
|
|
194 | $DEBUG_STATUS->set_text ($_[0]); |
|
|
195 | $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); |
|
|
196 | $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); |
|
|
197 | } |
169 | |
198 | |
170 | my $refresh_handler; |
199 | my $FPS; |
171 | |
200 | |
172 | sub refresh { |
201 | sub refresh { |
173 | $refresh_handler ||= add Glib::Idle sub { |
202 | $refresh_handler ||= add Glib::Idle sub { |
|
|
203 | if ($SDL_APP) { |
174 | my $next_refresh = SDL::GetTicks; |
204 | my $next_refresh = SDL::GetTicks; |
|
|
205 | |
|
|
206 | if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { |
|
|
207 | SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); |
|
|
208 | $next_refresh = SDL::GetTicks; |
|
|
209 | } |
|
|
210 | |
175 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
211 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
176 | $last_refresh = $next_refresh; |
212 | $last_refresh = $next_refresh; |
177 | |
213 | |
|
|
214 | if ($interval) { |
|
|
215 | $FPS ||= 1 / $interval; |
|
|
216 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
|
|
217 | debug sprintf "%5.02f\n", $FPS; |
|
|
218 | } |
|
|
219 | |
178 | force_refresh; |
220 | force_refresh; |
179 | $_->animate ($interval) for grep $_, values %ANIMATE; |
221 | $_->animate ($interval) for grep $_, values %ANIMATE; |
180 | |
222 | |
181 | if (%ANIMATE) { |
223 | if (%ANIMATE) { |
|
|
224 | 1 |
|
|
225 | } else { |
|
|
226 | undef $refresh_handler; |
|
|
227 | 0 |
182 | 1 |
228 | } |
183 | } else { |
229 | } else { |
184 | undef $refresh_handler; |
230 | undef $refresh_handler; |
185 | 0 |
231 | 0 |
186 | } |
232 | } |
187 | }; |
233 | }; |
… | |
… | |
220 | }, |
266 | }, |
221 | SDL_KEYUP() => sub { |
267 | SDL_KEYUP() => sub { |
222 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
268 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
223 | }, |
269 | }, |
224 | SDL_MOUSEMOTION() => sub { |
270 | SDL_MOUSEMOTION() => sub { |
225 | warn "sdl motion\n";#d# |
271 | Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); |
226 | }, |
272 | }, |
227 | SDL_MOUSEBUTTONDOWN() => sub { |
273 | SDL_MOUSEBUTTONDOWN() => sub { |
228 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
274 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
229 | }, |
275 | }, |
230 | SDL_MOUSEBUTTONUP() => sub { |
276 | SDL_MOUSEBUTTONUP() => sub { |
231 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
277 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
232 | }, |
278 | }, |
233 | SDL_ACTIVEEVENT() => sub { |
279 | SDL_ACTIVEEVENT() => sub { |
234 | warn "active\n";#d# |
280 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
235 | }, |
281 | }, |
236 | ); |
282 | ); |
237 | |
283 | |
238 | @conn::ISA = Crossfire::Protocol::; |
284 | @conn::ISA = Crossfire::Protocol::; |
239 | |
285 | |
… | |
… | |
286 | } |
332 | } |
287 | $ent->set_text ($value); |
333 | $ent->set_text ($value); |
288 | $ent->signal_connect (changed => sub { |
334 | $ent->signal_connect (changed => sub { |
289 | my ($ent) = @_; |
335 | my ($ent) = @_; |
290 | $cfg->{$key} = $ent->get_text; |
336 | $cfg->{$key} = $ent->get_text; |
|
|
337 | # TODO: Mapsize should be a slider in the game gui |
|
|
338 | if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
|
|
339 | $cfg->{$key} = 100; |
|
|
340 | } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
|
|
341 | $cfg->{$key} = 50; |
|
|
342 | } |
291 | }); |
343 | }); |
292 | } |
344 | } |
293 | |
345 | |
294 | sub run_config_dialog { |
346 | sub run_config_dialog { |
295 | my (%events) = @_; |
347 | my (%events) = @_; |
… | |
… | |
297 | my $w = Gtk2::Window->new; |
349 | my $w = Gtk2::Window->new; |
298 | |
350 | |
299 | my @cfg = ( |
351 | my @cfg = ( |
300 | [qw/Host host/], |
352 | [qw/Host host/], |
301 | [qw/Port port/], |
353 | [qw/Port port/], |
|
|
354 | [qw/Mapsize% mapsize/], |
302 | [qw/Username user/], |
355 | [qw/Username user/], |
303 | [qw/Password password/], |
356 | [qw/Password password/], |
304 | ); |
357 | ); |
305 | |
358 | |
306 | my $cfg = {}; |
359 | my $cfg = {}; |
… | |
… | |
353 | my $cb = $events{login} || sub {}; |
406 | my $cb = $events{login} || sub {}; |
354 | $cb->($::CFG->{user}, $::CFG->{password}); |
407 | $cb->($::CFG->{user}, $::CFG->{password}); |
355 | }); |
408 | }); |
356 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
409 | $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
357 | $cb->signal_connect (clicked => sub { |
410 | $cb->signal_connect (clicked => sub { |
358 | my $cb = $events{login} || sub {}; |
411 | my $cb = $events{logout} || sub {}; |
359 | $cb->(); |
412 | $cb->(); |
360 | }); |
413 | }); |
361 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
414 | $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
362 | $cb->signal_connect (clicked => sub { $w->destroy }); |
415 | $cb->signal_connect (clicked => sub { $w->destroy }); |
363 | |
416 | |
… | |
… | |
369 | |
422 | |
370 | ############################################################################# |
423 | ############################################################################# |
371 | |
424 | |
372 | SDL::Init SDL_INIT_EVERYTHING; |
425 | SDL::Init SDL_INIT_EVERYTHING; |
373 | |
426 | |
374 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
|
|
375 | |
|
|
376 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
427 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
377 | |
428 | |
378 | $TOPLEVEL->add ($mapwidget); |
429 | $Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget); |
379 | $mapwidget->focus_in; |
430 | $mapwidget->focus_in; |
380 | |
431 | |
381 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
432 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
382 | |
433 | |
383 | $CFG ||= { |
434 | $CFG ||= { |
384 | width => 640, |
435 | width => 640, |
385 | height => 480, |
436 | height => 480, |
|
|
437 | mapsize => 100, |
386 | fullscreen => 0, |
438 | fullscreen => 0, |
387 | host => "crossfire.schmorp.de", |
439 | host => "crossfire.schmorp.de", |
388 | port => 13327, |
440 | port => 13327, |
389 | }; |
441 | }; |
390 | |
442 | |
391 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
443 | { |
|
|
444 | my @fonts = map Crossfire::Client::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
|
|
445 | |
|
|
446 | Crossfire::Client::add_font $_ for @fonts; |
|
|
447 | Crossfire::Client::set_font $fonts[0]; |
|
|
448 | } |
392 | |
449 | |
393 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
450 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
394 | |
451 | |
395 | run_config_dialog |
452 | run_config_dialog |
396 | login => sub { start_game }, |
453 | login => sub { start_game }, |