… | |
… | |
14 | use SDL::OpenGL::Constants; |
14 | use SDL::OpenGL::Constants; |
15 | |
15 | |
16 | use Crossfire; |
16 | use Crossfire; |
17 | use Crossfire::Protocol; |
17 | use Crossfire::Protocol; |
18 | |
18 | |
19 | use Crossfire::Client; |
19 | use CFClient; |
20 | use Crossfire::Client::Widget; |
20 | use CFClient::Widget; |
|
|
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 |
21 | |
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; |
… | |
… | |
37 | our $SDL_TIMER; |
38 | our $SDL_TIMER; |
38 | our $SDL_APP; |
39 | our $SDL_APP; |
39 | our $SDL_EV; |
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 | our $STATUS_LINE; |
44 | our $STATUS_LINE; |
|
|
45 | our $DEBUG_STATUS; |
46 | |
46 | |
47 | my $last_refresh; |
47 | my $last_refresh; |
48 | my %ANIMATE; |
48 | my %ANIMATE; |
49 | my $refresh_handler; |
49 | my $refresh_handler; |
50 | |
50 | |
… | |
… | |
66 | -resizeable => 0; |
66 | -resizeable => 0; |
67 | |
67 | |
68 | $SDL_EV = new SDL::Event; |
68 | $SDL_EV = new SDL::Event; |
69 | $SDL_EV->set_unicode (1); |
69 | $SDL_EV->set_unicode (1); |
70 | |
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 | |
71 | $last_refresh = SDL::GetTicks; |
78 | $last_refresh = SDL::GetTicks; |
72 | |
79 | |
73 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
80 | CFClient::gl_init; |
74 | |
|
|
75 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
76 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
77 | |
81 | |
78 | $FONTSIZE = int $HEIGHT / 50; |
82 | $FONTSIZE = int $HEIGHT / 50; |
79 | |
83 | |
80 | ############################################################################# |
84 | ############################################################################# |
81 | |
85 | |
82 | glClearColor 1, 1, 1, 1; |
86 | glClearColor 0.45, 0.45, 0.45, 1; |
83 | |
87 | |
84 | glEnable GL_TEXTURE_2D; |
88 | glEnable GL_TEXTURE_2D; |
85 | glEnable GL_COLOR_MATERIAL; |
89 | glEnable GL_COLOR_MATERIAL; |
86 | glShadeModel GL_FLAT; |
90 | glShadeModel GL_FLAT; |
87 | glDisable GL_DEPTH_TEST; |
91 | glDisable GL_DEPTH_TEST; |
88 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
92 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
89 | |
93 | |
90 | $_->() for @GL_INIT; |
|
|
91 | |
|
|
92 | ############################################################################# |
94 | ############################################################################# |
|
|
95 | |
|
|
96 | $DEBUG_STATUS = new CFClient::Widget::Label; |
|
|
97 | $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); |
93 | |
98 | |
94 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
99 | $STATUS_LINE = new CFClient::Widget::Label |
95 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
100 | y => $HEIGHT * 59 / 60 - $FONTSIZE; |
96 | ""; |
|
|
97 | $Crossfire::Client::Widget::TOPLEVEL->add ($STATUS_LINE); |
101 | $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); |
98 | |
102 | |
99 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
103 | $ALT_ENTER_MESSAGE = new CFClient::Widget::Label |
100 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
104 | y => $HEIGHT * 59 / 60, |
|
|
105 | height => $HEIGHT / 60, |
101 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
106 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
102 | $Crossfire::Client::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
107 | $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
103 | |
108 | |
104 | # Test code #d# |
109 | # Test code #d# |
105 | unless ($tw) { # haha... |
110 | unless ($tw) { # haha... |
106 | $te = new Crossfire::Client::Widget::FancyFrame; |
111 | $te = new CFClient::Widget::FancyFrame x => 300, z => 1; |
107 | $te->add (new Crossfire::Client::Widget::Entry); |
112 | $te->add (new CFClient::Widget::Entry); |
108 | $te->move (300, 0, 2); |
|
|
109 | $Crossfire::Client::Widget::TOPLEVEL->add ($te); |
113 | $CFClient::Widget::TOPLEVEL->add ($te); |
110 | |
114 | |
111 | $tw = new Crossfire::Client::Widget::Animator; |
115 | $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 400, h => 300; |
112 | my $lbl1 = new Crossfire::Client::Widget::Label |
116 | my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
113 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
114 | my $lbl2 = new Crossfire::Client::Widget::Label |
117 | my $lbl2 = new CFClient::Widget::Label text => "LBL2"; |
115 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
116 | |
|
|
117 | my $vb = new Crossfire::Client::Widget::VBox; |
118 | my $vb = new CFClient::Widget::VBox; |
118 | my $f = new Crossfire::Client::Widget::FancyFrame; |
119 | my $f = new CFClient::Widget::FancyFrame; |
119 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
120 | my $f2 = new CFClient::Widget::FancyFrame; |
120 | $f->add ($lbl1); |
121 | $f->add ($lbl1); |
121 | $f2->add ($lbl2); |
122 | $f2->add ($lbl2); |
122 | $vb->add ($f); |
123 | $vb->add ($f); |
123 | $vb->add ($f2, 1); |
124 | $vb->add ($f2, 1); |
124 | |
125 | |
125 | $tw->add ($vb); |
126 | $tw->add ($vb); |
126 | $tw->w (400); |
|
|
127 | $tw->h (300); |
|
|
128 | $tw->move ($WIDTH - 200, 0); |
|
|
129 | $tw->moveto (0, 0); |
127 | $tw->moveto (0, 0); |
130 | $Crossfire::Client::Widget::TOPLEVEL->add ($tw); |
128 | $CFClient::Widget::TOPLEVEL->add ($tw); |
131 | |
129 | |
132 | # $f->move ($WIDTH - 200, 0); |
130 | # $f->move ($WIDTH - 200, 0); |
133 | # $Crossfire::Client::Widget::TOPLEVEL->add ($f); |
131 | # $CFClient::Widget::TOPLEVEL->add ($f); |
134 | } |
132 | } |
135 | } |
133 | } |
136 | |
134 | |
|
|
135 | sub destroy_screen { |
|
|
136 | remove Glib::Source $SDL_TIMER; |
|
|
137 | undef $SDL_APP; |
|
|
138 | undef $SDL_EV; |
|
|
139 | SDL::Quit; |
|
|
140 | } |
|
|
141 | |
137 | sub start_game { |
142 | sub start_game { |
138 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
|
|
139 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
|
|
140 | while $SDL_EV->poll; |
|
|
141 | |
|
|
142 | 1 |
|
|
143 | }; |
|
|
144 | |
|
|
145 | $WIDTH = $CFG->{width}; |
143 | $WIDTH = $CFG->{width}; |
146 | $HEIGHT = $CFG->{height}; |
144 | $HEIGHT = $CFG->{height}; |
147 | $FULLSCREEN = 0; |
145 | $FULLSCREEN = 0; |
148 | |
146 | |
149 | init_screen; |
147 | init_screen; |
… | |
… | |
157 | pass => $CFG->{password}, |
155 | pass => $CFG->{password}, |
158 | mapw => $mapsize, |
156 | mapw => $mapsize, |
159 | maph => $mapsize, |
157 | maph => $mapsize, |
160 | ; |
158 | ; |
161 | |
159 | |
162 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
160 | CFClient::lowdelay fileno $CONN->{fh}; |
163 | } |
161 | } |
164 | |
162 | |
165 | sub stop_game { |
163 | sub stop_game { |
166 | remove Glib::Source $SDL_TIMER; |
|
|
167 | remove Glib::Source $refresh_handler if $refresh_handler; |
164 | remove Glib::Source $refresh_handler if $refresh_handler; |
168 | undef $refresh_handler; |
165 | undef $refresh_handler; |
169 | |
166 | |
170 | undef $SDL_APP; |
|
|
171 | undef $CONN; |
167 | undef $CONN; |
172 | SDL::Quit; |
168 | destroy_screen; |
173 | } |
169 | } |
174 | |
170 | |
175 | sub force_refresh { |
171 | sub force_refresh { |
176 | glViewport 0, 0, $WIDTH, $HEIGHT; |
172 | glViewport 0, 0, $WIDTH, $HEIGHT; |
177 | |
173 | |
… | |
… | |
180 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
176 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
181 | glMatrixMode GL_MODELVIEW; |
177 | glMatrixMode GL_MODELVIEW; |
182 | |
178 | |
183 | glClear GL_COLOR_BUFFER_BIT; |
179 | glClear GL_COLOR_BUFFER_BIT; |
184 | |
180 | |
185 | $Crossfire::Client::Widget::TOPLEVEL->draw; |
181 | $CFClient::Widget::TOPLEVEL->draw; |
186 | |
182 | |
187 | SDL::GLSwapBuffers; |
183 | SDL::GLSwapBuffers; |
188 | } |
184 | } |
|
|
185 | |
|
|
186 | sub debug { |
|
|
187 | $DEBUG_STATUS->set_text ($_[0]); |
|
|
188 | $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); |
|
|
189 | $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); |
|
|
190 | } |
|
|
191 | |
|
|
192 | my $FPS; |
189 | |
193 | |
190 | sub refresh { |
194 | sub refresh { |
191 | $refresh_handler ||= add Glib::Idle sub { |
195 | $refresh_handler ||= add Glib::Idle sub { |
192 | if ($SDL_APP) { |
196 | if ($SDL_APP) { |
193 | my $next_refresh = SDL::GetTicks; |
197 | my $next_refresh = SDL::GetTicks; |
|
|
198 | |
|
|
199 | if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { |
|
|
200 | SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); |
|
|
201 | $next_refresh = SDL::GetTicks; |
|
|
202 | } |
|
|
203 | |
194 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
204 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
195 | $last_refresh = $next_refresh; |
205 | $last_refresh = $next_refresh; |
|
|
206 | |
|
|
207 | if ($interval) { |
|
|
208 | $FPS ||= 1 / $interval; |
|
|
209 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
|
|
210 | debug sprintf "%5.02f", $FPS; |
|
|
211 | } |
196 | |
212 | |
197 | force_refresh; |
213 | force_refresh; |
198 | $_->animate ($interval) for grep $_, values %ANIMATE; |
214 | $_->animate ($interval) for grep $_, values %ANIMATE; |
199 | |
215 | |
200 | if (%ANIMATE) { |
216 | if (%ANIMATE) { |
… | |
… | |
236 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
252 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
237 | # alt-enter |
253 | # alt-enter |
238 | $FULLSCREEN = !$FULLSCREEN; |
254 | $FULLSCREEN = !$FULLSCREEN; |
239 | init_screen; |
255 | init_screen; |
240 | } else { |
256 | } else { |
241 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
257 | CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); |
242 | } |
258 | } |
243 | }, |
259 | }, |
244 | SDL_KEYUP() => sub { |
260 | SDL_KEYUP() => sub { |
245 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
261 | CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); |
246 | }, |
262 | }, |
247 | SDL_MOUSEMOTION() => sub { |
263 | SDL_MOUSEMOTION() => sub { |
248 | Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV); |
264 | CFClient::Widget::feed_sdl_motion_event ($SDL_EV); |
249 | }, |
265 | }, |
250 | SDL_MOUSEBUTTONDOWN() => sub { |
266 | SDL_MOUSEBUTTONDOWN() => sub { |
251 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
267 | CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); |
252 | }, |
268 | }, |
253 | SDL_MOUSEBUTTONUP() => sub { |
269 | SDL_MOUSEBUTTONUP() => sub { |
254 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
270 | CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); |
255 | }, |
271 | }, |
256 | SDL_ACTIVEEVENT() => sub { |
272 | SDL_ACTIVEEVENT() => sub { |
257 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
273 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
258 | }, |
274 | }, |
259 | ); |
275 | ); |
… | |
… | |
287 | sub conn::face_update { |
303 | sub conn::face_update { |
288 | my ($self, $face) = @_; |
304 | my ($self, $face) = @_; |
289 | |
305 | |
290 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
306 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
291 | |
307 | |
292 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
308 | $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
293 | } |
309 | } |
294 | |
310 | |
295 | sub conn::query { |
311 | sub conn::query { |
296 | my ($self, $flags, $prompt) = @_; |
312 | my ($self, $flags, $prompt) = @_; |
297 | |
313 | |
… | |
… | |
370 | $cb->signal_connect (clicked => sub { |
386 | $cb->signal_connect (clicked => sub { |
371 | for (keys %$cfg) { |
387 | for (keys %$cfg) { |
372 | $::CFG->{$_} = $cfg->{$_} |
388 | $::CFG->{$_} = $cfg->{$_} |
373 | if $_ ne '_i'; |
389 | if $_ ne '_i'; |
374 | } |
390 | } |
375 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
391 | CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
376 | }); |
392 | }); |
377 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
393 | $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
378 | $cb->signal_connect (clicked => sub { |
394 | $cb->signal_connect (clicked => sub { |
379 | for (keys %$cfg) { |
395 | for (keys %$cfg) { |
380 | $::CFG->{$_} = $cfg->{$_} |
396 | $::CFG->{$_} = $cfg->{$_} |
… | |
… | |
399 | |
415 | |
400 | ############################################################################# |
416 | ############################################################################# |
401 | |
417 | |
402 | SDL::Init SDL_INIT_EVERYTHING; |
418 | SDL::Init SDL_INIT_EVERYTHING; |
403 | |
419 | |
404 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
420 | my $mapwidget = CFClient::Widget::MapWidget->new; |
405 | |
421 | |
406 | $Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget); |
422 | $CFClient::Widget::TOPLEVEL->add ($mapwidget); |
407 | $mapwidget->focus_in; |
423 | $mapwidget->focus_in; |
408 | |
424 | |
409 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
425 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
410 | |
426 | |
411 | $CFG ||= { |
427 | $CFG ||= { |
412 | width => 640, |
428 | width => 640, |
413 | height => 480, |
429 | height => 480, |
414 | mapsize => 100, |
430 | mapsize => 100, |
415 | fullscreen => 0, |
431 | fullscreen => 0, |
416 | host => "crossfire.schmorp.de", |
432 | host => "crossfire.schmorp.de", |
417 | port => 13327, |
433 | port => 13327, |
418 | }; |
434 | }; |
419 | |
435 | |
420 | Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; |
436 | { |
|
|
437 | my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
|
|
438 | |
|
|
439 | CFClient::add_font $_ for @fonts; |
|
|
440 | CFClient::set_font $fonts[0]; |
|
|
441 | } |
421 | |
442 | |
422 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
443 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
423 | |
444 | |
424 | run_config_dialog |
445 | run_config_dialog |
425 | login => sub { start_game }, |
446 | login => sub { start_game }, |