… | |
… | |
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::Client; |
|
|
18 | use Crossfire::Protocol; |
17 | use Crossfire::Protocol; |
19 | |
18 | |
|
|
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; |
32 | our $HEIGHT; |
33 | our $HEIGHT; |
33 | our $FULLSCREEN; |
34 | our $FULLSCREEN; |
34 | |
35 | |
35 | our $FONTSIZE; |
36 | our $FONTSIZE; |
36 | our $FOCUS; |
|
|
37 | our $HOVER; |
|
|
38 | |
37 | |
39 | our $SDL_TIMER; |
38 | our $SDL_TIMER; |
40 | our $SDL_APP; |
39 | our $SDL_APP; |
41 | our $SDL_EV = new SDL::Event; |
40 | our $SDL_EV; |
42 | our %SDL_CB; |
41 | our %SDL_CB; |
43 | |
|
|
44 | our @GL_INIT; # hooks called on every gl init |
|
|
45 | |
42 | |
46 | our $ALT_ENTER_MESSAGE; |
43 | our $ALT_ENTER_MESSAGE; |
47 | our $STATUS_LINE; |
44 | our $STATUS_LINE; |
48 | |
45 | our $DEBUG_STATUS; |
49 | our $TOPLEVEL; |
|
|
50 | |
|
|
51 | our $tw; # Test widget #d# |
|
|
52 | |
46 | |
53 | my $last_refresh; |
47 | my $last_refresh; |
54 | my %ANIMATE; |
48 | my %ANIMATE; |
55 | my $refresh_handler; |
49 | my $refresh_handler; |
|
|
50 | |
|
|
51 | our ($tw, $te); # Test widget #d# |
56 | |
52 | |
57 | sub init_screen { |
53 | sub init_screen { |
58 | $SDL_APP = new SDL::App |
54 | $SDL_APP = new SDL::App |
59 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
55 | -flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
60 | -title => "Crossfire+ Client", |
56 | -title => "Crossfire+ Client", |
… | |
… | |
67 | -alpha_size => 0, |
63 | -alpha_size => 0, |
68 | -double_buffer => 1, |
64 | -double_buffer => 1, |
69 | -fullscreen => $FULLSCREEN, |
65 | -fullscreen => $FULLSCREEN, |
70 | -resizeable => 0; |
66 | -resizeable => 0; |
71 | |
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 | |
72 | $last_refresh = SDL::GetTicks; |
78 | $last_refresh = SDL::GetTicks; |
73 | |
79 | |
74 | %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions; |
80 | CFClient::gl_init; |
75 | |
|
|
76 | $GL_EXT{GL_ARB_texture_non_power_of_two} |
|
|
77 | or warn "WARNING: non-power-of-two opengl extension required"; |
|
|
78 | |
81 | |
79 | $FONTSIZE = int $HEIGHT / 50; |
82 | $FONTSIZE = int $HEIGHT / 50; |
80 | |
83 | |
81 | ############################################################################# |
84 | ############################################################################# |
82 | |
85 | |
83 | glClearColor 0, 0, 0, 0; |
86 | glClearColor 0.45, 0.45, 0.45, 1; |
84 | |
87 | |
85 | glEnable GL_TEXTURE_2D; |
88 | glEnable GL_TEXTURE_2D; |
86 | glEnable GL_COLOR_MATERIAL; |
89 | glEnable GL_COLOR_MATERIAL; |
87 | glShadeModel GL_FLAT; |
90 | glShadeModel GL_FLAT; |
88 | glDisable GL_DEPTH_TEST; |
91 | glDisable GL_DEPTH_TEST; |
89 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
92 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
90 | |
93 | |
91 | $_->() for @GL_INIT; |
|
|
92 | |
|
|
93 | ############################################################################# |
94 | ############################################################################# |
|
|
95 | |
|
|
96 | $DEBUG_STATUS = new CFClient::Widget::Label; |
|
|
97 | $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); |
94 | |
98 | |
95 | $STATUS_LINE = new Crossfire::Client::Widget::Label |
99 | $STATUS_LINE = new CFClient::Widget::Label |
96 | 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE, |
100 | y => $HEIGHT * 59 / 60 - $FONTSIZE; |
97 | ""; |
|
|
98 | $TOPLEVEL->add ($STATUS_LINE); |
101 | $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); |
99 | |
102 | |
100 | $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label |
103 | $ALT_ENTER_MESSAGE = new CFClient::Widget::Label |
101 | 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60, |
104 | y => $HEIGHT * 59 / 60, |
|
|
105 | height => $HEIGHT / 60, |
102 | "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
106 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
103 | $TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
107 | $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
104 | |
108 | |
105 | # Test code #d# |
109 | # Test code #d# |
106 | unless ($tw) { # haha... |
110 | unless ($tw) { # haha... |
107 | $tw = new Crossfire::Client::Widget::Animator; |
111 | $te = new CFClient::Widget::FancyFrame x => 300, z => 1; |
108 | my $lbl1 = new Crossfire::Client::Widget::Label |
112 | $te->add (new CFClient::Widget::Entry); |
109 | 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
113 | $CFClient::Widget::TOPLEVEL->add ($te); |
|
|
114 | |
|
|
115 | $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 400, h => 300; |
|
|
116 | my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
110 | my $lbl2 = new Crossfire::Client::Widget::Label |
117 | my $lbl2 = new CFClient::Widget::Label text => "LBL2"; |
111 | 0, 0, 10, $FONTSIZE, "LBL2"; |
|
|
112 | |
|
|
113 | my $vb = new Crossfire::Client::Widget::VBox; |
118 | my $vb = new CFClient::Widget::VBox; |
114 | my $f = new Crossfire::Client::Widget::FancyFrame; |
119 | my $f = new CFClient::Widget::FancyFrame; |
115 | my $f2 = new Crossfire::Client::Widget::FancyFrame; |
120 | my $f2 = new CFClient::Widget::FancyFrame; |
116 | $f->add ($lbl1); |
121 | $f->add ($lbl1); |
117 | $f2->add ($lbl2); |
122 | $f2->add ($lbl2); |
118 | $vb->add ($f); |
123 | $vb->add ($f); |
119 | $vb->add ($f2, 1); |
124 | $vb->add ($f2, 1); |
120 | |
125 | |
121 | $tw->add ($vb); |
126 | $tw->add ($vb); |
122 | $tw->w (400); |
|
|
123 | $tw->h (300); |
|
|
124 | $tw->move ($WIDTH - 200, 0); |
|
|
125 | $tw->moveto (0, 0); |
127 | $tw->moveto (0, 0); |
126 | $TOPLEVEL->add ($tw); |
128 | $CFClient::Widget::TOPLEVEL->add ($tw); |
127 | |
129 | |
128 | # $f->move ($WIDTH - 200, 0); |
130 | # $f->move ($WIDTH - 200, 0); |
129 | # $TOPLEVEL->add ($f); |
131 | # $CFClient::Widget::TOPLEVEL->add ($f); |
130 | } |
132 | } |
131 | } |
133 | } |
132 | |
134 | |
|
|
135 | sub destroy_screen { |
|
|
136 | remove Glib::Source $SDL_TIMER; |
|
|
137 | undef $SDL_APP; |
|
|
138 | undef $SDL_EV; |
|
|
139 | SDL::Quit; |
|
|
140 | } |
|
|
141 | |
133 | sub start_game { |
142 | 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}; |
143 | $WIDTH = $CFG->{width}; |
142 | $HEIGHT = $CFG->{height}; |
144 | $HEIGHT = $CFG->{height}; |
143 | $FULLSCREEN = 0; |
145 | $FULLSCREEN = 0; |
144 | |
146 | |
145 | init_screen; |
147 | init_screen; |
146 | |
148 | |
147 | my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; |
149 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
148 | |
150 | |
149 | $CONN = new conn |
151 | $CONN = new conn |
150 | host => $CFG->{host}, |
152 | host => $CFG->{host}, |
151 | port => $CFG->{port}, |
153 | port => $CFG->{port}, |
152 | user => $CFG->{user}, |
154 | user => $CFG->{user}, |
153 | pass => $CFG->{password}, |
155 | pass => $CFG->{password}, |
154 | mapw => $mapsize, |
156 | mapw => $mapsize, |
155 | maph => $mapsize, |
157 | maph => $mapsize, |
156 | ; |
158 | ; |
157 | |
159 | |
158 | Crossfire::Client::lowdelay fileno $CONN->{fh}; |
160 | CFClient::lowdelay fileno $CONN->{fh}; |
159 | } |
161 | } |
160 | |
162 | |
161 | sub stop_game { |
163 | sub stop_game { |
162 | remove Glib::Source $SDL_TIMER; |
|
|
163 | remove Glib::Source $refresh_handler if $refresh_handler; |
164 | remove Glib::Source $refresh_handler if $refresh_handler; |
164 | undef $refresh_handler; |
165 | undef $refresh_handler; |
165 | |
166 | |
166 | undef $SDL_APP; |
|
|
167 | undef $CONN; |
167 | undef $CONN; |
168 | SDL::Quit; |
168 | destroy_screen; |
169 | } |
169 | } |
170 | |
|
|
171 | |
170 | |
172 | sub force_refresh { |
171 | sub force_refresh { |
173 | glViewport 0, 0, $WIDTH, $HEIGHT; |
172 | glViewport 0, 0, $WIDTH, $HEIGHT; |
174 | |
173 | |
175 | glMatrixMode GL_PROJECTION; |
174 | glMatrixMode GL_PROJECTION; |
176 | glLoadIdentity; |
175 | glLoadIdentity; |
177 | glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; |
176 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
178 | glMatrixMode GL_MODELVIEW; |
177 | glMatrixMode GL_MODELVIEW; |
179 | |
178 | |
180 | glClear GL_COLOR_BUFFER_BIT; |
179 | glClear GL_COLOR_BUFFER_BIT; |
181 | |
180 | |
182 | $TOPLEVEL->draw; |
181 | $CFClient::Widget::TOPLEVEL->draw; |
183 | |
182 | |
184 | SDL::GLSwapBuffers; |
183 | SDL::GLSwapBuffers; |
185 | } |
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; |
186 | |
193 | |
187 | sub refresh { |
194 | sub refresh { |
188 | $refresh_handler ||= add Glib::Idle sub { |
195 | $refresh_handler ||= add Glib::Idle sub { |
189 | return unless $SDL_APP; |
196 | if ($SDL_APP) { |
190 | |
|
|
191 | 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 | |
192 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
204 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
193 | $last_refresh = $next_refresh; |
205 | $last_refresh = $next_refresh; |
194 | |
206 | |
|
|
207 | if ($interval) { |
|
|
208 | $FPS ||= 1 / $interval; |
|
|
209 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
|
|
210 | debug sprintf "%5.02f", $FPS; |
|
|
211 | } |
|
|
212 | |
195 | force_refresh; |
213 | force_refresh; |
196 | $_->animate ($interval) for grep $_, values %ANIMATE; |
214 | $_->animate ($interval) for grep $_, values %ANIMATE; |
197 | |
215 | |
198 | if (%ANIMATE) { |
216 | if (%ANIMATE) { |
|
|
217 | 1 |
|
|
218 | } else { |
|
|
219 | undef $refresh_handler; |
|
|
220 | 0 |
199 | 1 |
221 | } |
200 | } else { |
222 | } else { |
201 | undef $refresh_handler; |
223 | undef $refresh_handler; |
202 | 0 |
224 | 0 |
203 | } |
225 | } |
204 | }; |
226 | }; |
… | |
… | |
230 | 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) { |
231 | # alt-enter |
253 | # alt-enter |
232 | $FULLSCREEN = !$FULLSCREEN; |
254 | $FULLSCREEN = !$FULLSCREEN; |
233 | init_screen; |
255 | init_screen; |
234 | } else { |
256 | } else { |
235 | Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); |
257 | CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); |
236 | } |
258 | } |
237 | }, |
259 | }, |
238 | SDL_KEYUP() => sub { |
260 | SDL_KEYUP() => sub { |
239 | Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); |
261 | CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); |
240 | }, |
262 | }, |
241 | SDL_MOUSEMOTION() => sub { |
263 | SDL_MOUSEMOTION() => sub { |
242 | my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); |
264 | CFClient::Widget::feed_sdl_motion_event ($SDL_EV); |
243 | $HOVER = $TOPLEVEL->find_widget ($x, $y); |
|
|
244 | |
|
|
245 | warn "mouse $x, $y = $HOVER\n"; |
|
|
246 | }, |
265 | }, |
247 | SDL_MOUSEBUTTONDOWN() => sub { |
266 | SDL_MOUSEBUTTONDOWN() => sub { |
248 | Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); |
267 | CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); |
249 | }, |
268 | }, |
250 | SDL_MOUSEBUTTONUP() => sub { |
269 | SDL_MOUSEBUTTONUP() => sub { |
251 | Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); |
270 | CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); |
252 | }, |
271 | }, |
253 | SDL_ACTIVEEVENT() => sub { |
272 | SDL_ACTIVEEVENT() => sub { |
254 | warn "active\n";#d# |
273 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
255 | }, |
274 | }, |
256 | ); |
275 | ); |
257 | |
276 | |
258 | @conn::ISA = Crossfire::Protocol::; |
277 | @conn::ISA = Crossfire::Protocol::; |
259 | |
278 | |
… | |
… | |
284 | sub conn::face_update { |
303 | sub conn::face_update { |
285 | my ($self, $face) = @_; |
304 | my ($self, $face) = @_; |
286 | |
305 | |
287 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
306 | $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
288 | |
307 | |
289 | $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; |
308 | $face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
290 | } |
309 | } |
291 | |
310 | |
292 | sub conn::query { |
311 | sub conn::query { |
293 | my ($self, $flags, $prompt) = @_; |
312 | my ($self, $flags, $prompt) = @_; |
294 | |
313 | |
… | |
… | |
367 | $cb->signal_connect (clicked => sub { |
386 | $cb->signal_connect (clicked => sub { |
368 | for (keys %$cfg) { |
387 | for (keys %$cfg) { |
369 | $::CFG->{$_} = $cfg->{$_} |
388 | $::CFG->{$_} = $cfg->{$_} |
370 | if $_ ne '_i'; |
389 | if $_ ne '_i'; |
371 | } |
390 | } |
372 | Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; |
391 | CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
373 | }); |
392 | }); |
374 | $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); |
375 | $cb->signal_connect (clicked => sub { |
394 | $cb->signal_connect (clicked => sub { |
376 | for (keys %$cfg) { |
395 | for (keys %$cfg) { |
377 | $::CFG->{$_} = $cfg->{$_} |
396 | $::CFG->{$_} = $cfg->{$_} |
… | |
… | |
396 | |
415 | |
397 | ############################################################################# |
416 | ############################################################################# |
398 | |
417 | |
399 | SDL::Init SDL_INIT_EVERYTHING; |
418 | SDL::Init SDL_INIT_EVERYTHING; |
400 | |
419 | |
401 | $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; |
|
|
402 | |
|
|
403 | my $mapwidget = Crossfire::Client::Widget::MapWidget->new; |
420 | my $mapwidget = CFClient::Widget::MapWidget->new; |
404 | |
421 | |
405 | $TOPLEVEL->add ($mapwidget); |
422 | $CFClient::Widget::TOPLEVEL->add ($mapwidget); |
406 | $mapwidget->focus_in; |
423 | $mapwidget->focus_in; |
407 | |
424 | |
408 | Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; |
425 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
409 | |
426 | |
410 | $CFG ||= { |
427 | $CFG ||= { |
411 | width => 640, |
428 | width => 640, |
412 | height => 480, |
429 | height => 480, |
413 | mapsize => 100, |
430 | mapsize => 100, |
414 | fullscreen => 0, |
431 | fullscreen => 0, |
415 | host => "crossfire.schmorp.de", |
432 | host => "crossfire.schmorp.de", |
416 | port => 13327, |
433 | port => 13327, |
417 | }; |
434 | }; |
418 | |
435 | |
419 | 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 | } |
420 | |
442 | |
421 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
443 | $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
422 | |
444 | |
423 | run_config_dialog |
445 | run_config_dialog |
424 | login => sub { start_game }, |
446 | login => sub { start_game }, |