… | |
… | |
15 | |
15 | |
16 | use Crossfire; |
16 | use Crossfire; |
17 | use Crossfire::Protocol; |
17 | use Crossfire::Protocol; |
18 | |
18 | |
19 | use CFClient; |
19 | use CFClient; |
20 | use CFClient::Widget; |
20 | use CFClient::UI; |
21 | |
21 | |
22 | our $VERSION = '0.1'; |
22 | our $VERSION = '0.1'; |
23 | |
23 | |
24 | my $MAX_FPS = 30; |
24 | my $MAX_FPS = 60; |
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
25 | my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
26 | |
26 | |
27 | our $FACECACHE; |
27 | our $FACECACHE; |
28 | |
28 | |
29 | our $CFG; |
29 | our $CFG; |
30 | our $CONN; |
30 | our $CONN; |
31 | |
31 | |
|
|
32 | our @SDL_MODES; |
32 | our $WIDTH; |
33 | our $WIDTH; |
33 | our $HEIGHT; |
34 | our $HEIGHT; |
34 | our $FULLSCREEN; |
35 | our $FULLSCREEN; |
|
|
36 | |
|
|
37 | our $NOW; |
35 | |
38 | |
36 | our $MAPWIDGET; |
39 | our $MAPWIDGET; |
37 | our $FONTSIZE; |
40 | our $FONTSIZE; |
38 | |
41 | |
39 | our $SDL_TIMER; |
42 | our $SDL_TIMER; |
… | |
… | |
67 | -resizeable => 0; |
70 | -resizeable => 0; |
68 | |
71 | |
69 | $SDL_EV = new SDL::Event; |
72 | $SDL_EV = new SDL::Event; |
70 | $SDL_EV->set_unicode (1); |
73 | $SDL_EV->set_unicode (1); |
71 | |
74 | |
72 | $SDL_TIMER = add Glib::Timeout 1000/50, sub { |
75 | $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { |
73 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
76 | ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
74 | while $SDL_EV->poll; |
77 | while $SDL_EV->poll; |
75 | |
78 | |
76 | 1 |
79 | 1 |
77 | }; |
80 | }; |
… | |
… | |
92 | glDisable GL_DEPTH_TEST; |
95 | glDisable GL_DEPTH_TEST; |
93 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
96 | glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
94 | |
97 | |
95 | ############################################################################# |
98 | ############################################################################# |
96 | |
99 | |
97 | $DEBUG_STATUS = new CFClient::Widget::Label; |
100 | $DEBUG_STATUS = new CFClient::UI::Label; |
98 | $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); |
101 | $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
99 | |
102 | |
100 | $STATUS_LINE = new CFClient::Widget::Label |
103 | $STATUS_LINE = new CFClient::UI::Label |
101 | y => $HEIGHT * 59 / 60 - $FONTSIZE; |
104 | y => $HEIGHT * 59 / 60 - $FONTSIZE; |
102 | $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); |
105 | $CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
103 | |
106 | |
104 | $ALT_ENTER_MESSAGE = new CFClient::Widget::Label |
107 | $ALT_ENTER_MESSAGE = new CFClient::UI::Label |
105 | y => $HEIGHT * 59 / 60, |
108 | y => $HEIGHT * 59 / 60, |
106 | height => $HEIGHT / 60, |
109 | height => $HEIGHT / 60, |
107 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
110 | text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
108 | $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
111 | $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
109 | |
112 | |
110 | $MAPWIDGET = new CFClient::Widget::MapWidget; |
113 | $MAPWIDGET = new CFClient::UI::MapWidget; |
111 | $CFClient::Widget::TOPLEVEL->add ($MAPWIDGET); |
114 | $CFClient::UI::TOPLEVEL->add ($MAPWIDGET); |
112 | $MAPWIDGET->focus_in; |
115 | $MAPWIDGET->focus_in; |
113 | |
|
|
114 | # Test code #d# |
|
|
115 | unless ($tw) { # haha... |
|
|
116 | $te = new CFClient::Widget::FancyFrame x => 300, z => 1; |
|
|
117 | $te->add (new CFClient::Widget::Entry); |
|
|
118 | $CFClient::Widget::TOPLEVEL->add ($te); |
|
|
119 | |
|
|
120 | $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 600, h => 300; |
|
|
121 | my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!"; |
|
|
122 | my $lbl2 = new CFClient::Widget::Label text => "LBL2"; |
|
|
123 | my $vb = new CFClient::Widget::VBox; |
|
|
124 | my $f = new CFClient::Widget::FancyFrame; |
|
|
125 | my $f2 = new CFClient::Widget::FancyFrame; |
|
|
126 | $f->add ($lbl1); |
|
|
127 | $f2->add ($lbl2); |
|
|
128 | $vb->add ($f); |
|
|
129 | $vb->add ($f2, 1); |
|
|
130 | |
|
|
131 | $tw->add ($vb); |
|
|
132 | $tw->moveto (0, 0); |
|
|
133 | $CFClient::Widget::TOPLEVEL->add ($tw); |
|
|
134 | |
|
|
135 | # $f->move ($WIDTH - 200, 0); |
|
|
136 | # $CFClient::Widget::TOPLEVEL->add ($f); |
|
|
137 | } |
|
|
138 | } |
116 | } |
139 | |
117 | |
140 | sub destroy_screen { |
118 | sub destroy_screen { |
141 | remove Glib::Source $SDL_TIMER; |
119 | remove Glib::Source $SDL_TIMER; |
142 | undef $SDL_APP; |
120 | undef $SDL_APP; |
143 | undef $SDL_EV; |
121 | undef $SDL_EV; |
144 | SDL::Quit; |
122 | SDL::Quit; |
145 | } |
123 | } |
146 | |
124 | |
|
|
125 | sub config_dialog { |
|
|
126 | my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, |
|
|
127 | child => (my $vbox = new CFClient::UI::VBox); |
|
|
128 | $vbox->add (new CFClient::UI::Label align => 0, text => "Setup"); |
|
|
129 | $vbox->add (my $table = new CFClient::UI::Table expand => 1); |
|
|
130 | |
|
|
131 | $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); |
|
|
132 | $table->add (1, 0, my $hbox = new CFClient::UI::HBox); |
|
|
133 | |
|
|
134 | $hbox->add (my $mode_slider = new CFClient::UI::Slider req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
|
|
135 | $hbox->add (my $mode_label = new CFClient::UI::Label); |
|
|
136 | |
|
|
137 | $mode_slider->connect (changed => sub { |
|
|
138 | my (undef, $value) = @_; |
|
|
139 | |
|
|
140 | $CFG->{sdl_mode} = $value = int $value + 0.5; |
|
|
141 | $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); |
|
|
142 | }); |
|
|
143 | $mode_slider->emit (changed => $mode_slider->{range}[0]); |
|
|
144 | |
|
|
145 | $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Host"); |
|
|
146 | $table->add (1, 1, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
|
|
147 | |
|
|
148 | $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Port"); |
|
|
149 | $table->add (1, 2, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
|
|
150 | |
|
|
151 | $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Username"); |
|
|
152 | $table->add (1, 3, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
|
|
153 | |
|
|
154 | $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Password"); |
|
|
155 | $table->add (1, 4, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
|
|
156 | |
|
|
157 | $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Map Size"); |
|
|
158 | $table->add (1, 5, my $mapsize_slider = new CFClient::UI::Slider req_w => 100, range => [$CFG->{mapsize}, 10, 100 + 1, 1]); |
|
|
159 | |
|
|
160 | $CFClient::UI::TOPLEVEL->add ($dialog); |
|
|
161 | } |
|
|
162 | |
147 | sub start_game { |
163 | sub start_game { |
148 | $WIDTH = $CFG->{width}; |
164 | $WIDTH = $CFG->{width}; |
149 | $HEIGHT = $CFG->{height}; |
165 | $HEIGHT = $CFG->{height}; |
150 | $FULLSCREEN = 0; |
166 | $FULLSCREEN = 0; |
151 | |
167 | |
152 | init_screen; |
168 | init_screen; |
|
|
169 | |
|
|
170 | config_dialog; |
153 | |
171 | |
154 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
172 | my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
155 | |
173 | |
156 | $CONN = new conn |
174 | $CONN = new conn |
157 | host => $CFG->{host}, |
175 | host => $CFG->{host}, |
… | |
… | |
178 | |
196 | |
179 | glMatrixMode GL_PROJECTION; |
197 | glMatrixMode GL_PROJECTION; |
180 | glLoadIdentity; |
198 | glLoadIdentity; |
181 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
199 | glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
182 | glMatrixMode GL_MODELVIEW; |
200 | glMatrixMode GL_MODELVIEW; |
|
|
201 | glLoadIdentity; |
183 | |
202 | |
184 | glClear GL_COLOR_BUFFER_BIT; |
203 | glClear GL_COLOR_BUFFER_BIT; |
185 | |
204 | |
186 | $CFClient::Widget::TOPLEVEL->draw; |
205 | $CFClient::UI::TOPLEVEL->draw; |
187 | |
206 | |
188 | SDL::GLSwapBuffers; |
207 | SDL::GLSwapBuffers; |
189 | } |
208 | } |
190 | |
209 | |
191 | sub debug { |
210 | sub debug { |
192 | $DEBUG_STATUS->set_text ($_[0]); |
211 | $DEBUG_STATUS->set_text ($_[0]); |
193 | $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request); |
212 | my ($w, $h) = $DEBUG_STATUS->size_request; |
194 | $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0); |
213 | $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
195 | } |
214 | } |
196 | |
215 | |
197 | my $FPS; |
216 | my $FPS; |
198 | |
217 | |
199 | sub refresh { |
218 | sub refresh { |
200 | $refresh_handler ||= add Glib::Idle sub { |
219 | $refresh_handler ||= add Glib::Idle sub { |
201 | if ($SDL_APP) { |
220 | if ($SDL_APP) { |
202 | my $next_refresh = SDL::GetTicks; |
221 | $NOW = SDL::GetTicks; |
203 | |
222 | |
204 | if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { |
223 | if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
205 | SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); |
224 | SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
206 | $next_refresh = SDL::GetTicks; |
225 | $NOW = SDL::GetTicks; |
207 | } |
226 | } |
208 | |
227 | |
209 | my $interval = ($next_refresh - $last_refresh) * 0.001; |
228 | my $interval = ($NOW - $last_refresh) * 0.001; |
210 | $last_refresh = $next_refresh; |
229 | $last_refresh = $NOW; |
211 | |
230 | |
212 | if ($interval) { |
231 | if ($interval) { |
213 | $FPS ||= 1 / $interval; |
232 | $FPS ||= 1 / $interval; |
214 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
233 | $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; |
215 | debug sprintf "%5.02f", $FPS; |
234 | debug sprintf "%5.02f", $FPS; |
… | |
… | |
257 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
276 | if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
258 | # alt-enter |
277 | # alt-enter |
259 | $FULLSCREEN = !$FULLSCREEN; |
278 | $FULLSCREEN = !$FULLSCREEN; |
260 | init_screen; |
279 | init_screen; |
261 | } else { |
280 | } else { |
262 | CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); |
281 | CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
263 | } |
282 | } |
264 | }, |
283 | }, |
265 | SDL_KEYUP() => sub { |
284 | SDL_KEYUP() => sub { |
266 | CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); |
285 | CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
267 | }, |
286 | }, |
268 | SDL_MOUSEMOTION() => sub { |
287 | SDL_MOUSEMOTION() => sub { |
269 | CFClient::Widget::feed_sdl_motion_event ($SDL_EV); |
288 | CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
270 | }, |
289 | }, |
271 | SDL_MOUSEBUTTONDOWN() => sub { |
290 | SDL_MOUSEBUTTONDOWN() => sub { |
272 | CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); |
291 | CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
273 | }, |
292 | }, |
274 | SDL_MOUSEBUTTONUP() => sub { |
293 | SDL_MOUSEBUTTONUP() => sub { |
275 | CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); |
294 | CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
276 | }, |
295 | }, |
277 | SDL_ACTIVEEVENT() => sub { |
296 | SDL_ACTIVEEVENT() => sub { |
278 | printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
297 | # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
279 | }, |
298 | }, |
280 | ); |
299 | ); |
281 | |
300 | |
282 | @conn::ISA = Crossfire::Protocol::; |
301 | @conn::ISA = Crossfire::Protocol::; |
283 | |
302 | |
… | |
… | |
354 | [qw/Password password/], |
373 | [qw/Password password/], |
355 | ); |
374 | ); |
356 | |
375 | |
357 | my $cfg = {}; |
376 | my $cfg = {}; |
358 | |
377 | |
359 | my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE); |
|
|
360 | my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a; |
|
|
361 | |
|
|
362 | $w->add (my $vb = Gtk2::VBox->new); |
378 | $w->add (my $vb = Gtk2::VBox->new); |
363 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
379 | $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
364 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
380 | my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
365 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
381 | $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
366 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
382 | $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
367 | my $i = 0; |
383 | my $i = 0; |
368 | my $act = 0; |
384 | my $act = 0; |
369 | for (map { "$_->[0]x$_->[1]" } reverse @modes) { |
385 | for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { |
370 | if ($_ eq $selmode) { $act = $i } |
386 | if ($_ eq $selmode) { $act = $i } |
371 | $cb->append_text ($_); |
387 | $cb->append_text ($_); |
372 | $i++; |
388 | $i++; |
373 | } |
389 | } |
374 | $cb->set_active ($act); |
390 | $cb->set_active ($act); |
… | |
… | |
420 | |
436 | |
421 | ############################################################################# |
437 | ############################################################################# |
422 | |
438 | |
423 | SDL::Init SDL_INIT_EVERYTHING; |
439 | SDL::Init SDL_INIT_EVERYTHING; |
424 | |
440 | |
|
|
441 | @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], |
|
|
442 | @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; |
|
|
443 | |
425 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
444 | CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
426 | |
445 | |
427 | $CFG ||= { |
446 | $CFG ||= { |
428 | width => 640, |
447 | width => 640, |
429 | height => 480, |
448 | height => 480, |