1 |
#!/opt/bin/perl |
2 |
|
3 |
use strict; |
4 |
use utf8; |
5 |
|
6 |
use Glib; |
7 |
use Gtk2 -init; |
8 |
|
9 |
use SDL; |
10 |
use SDL::App; |
11 |
use SDL::Event; |
12 |
use SDL::Surface; |
13 |
use SDL::OpenGL; |
14 |
use SDL::OpenGL::Constants; |
15 |
|
16 |
use Crossfire; |
17 |
use Crossfire::Protocol; |
18 |
|
19 |
use CFClient; |
20 |
use CFClient::UI; |
21 |
|
22 |
our $VERSION = '0.1'; |
23 |
|
24 |
my $MAX_FPS = 60; |
25 |
my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame |
26 |
|
27 |
our $FACECACHE; |
28 |
|
29 |
our $CFG; |
30 |
our $CONN; |
31 |
|
32 |
our @SDL_MODES; |
33 |
our $WIDTH; |
34 |
our $HEIGHT; |
35 |
our $FULLSCREEN; |
36 |
|
37 |
our $NOW; |
38 |
|
39 |
our $MAPWIDGET; |
40 |
our $FONTSIZE; |
41 |
|
42 |
our $SDL_TIMER; |
43 |
our $SDL_APP; |
44 |
our $SDL_EV; |
45 |
our %SDL_CB; |
46 |
|
47 |
our $ALT_ENTER_MESSAGE; |
48 |
our $STATUS_LINE; |
49 |
our $DEBUG_STATUS; |
50 |
|
51 |
my $last_refresh; |
52 |
my %ANIMATE; |
53 |
my $refresh_handler; |
54 |
|
55 |
our ($tw, $te); # Test widget #d# |
56 |
|
57 |
sub init_screen { |
58 |
$SDL_APP = new SDL::App |
59 |
-flags => SDL_ANYFORMAT | SDL_HWSURFACE, |
60 |
-title => "Crossfire+ Client", |
61 |
-width => $WIDTH, |
62 |
-height => $HEIGHT, |
63 |
-opengl => 1, |
64 |
-red_size => 5, |
65 |
-green_size => 5, |
66 |
-blue_size => 5, |
67 |
-alpha_size => 0, |
68 |
-double_buffer => 1, |
69 |
-fullscreen => $FULLSCREEN, |
70 |
-resizeable => 0; |
71 |
|
72 |
$SDL_EV = new SDL::Event; |
73 |
$SDL_EV->set_unicode (1); |
74 |
|
75 |
$SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub { |
76 |
($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() |
77 |
while $SDL_EV->poll; |
78 |
|
79 |
1 |
80 |
}; |
81 |
|
82 |
$last_refresh = SDL::GetTicks; |
83 |
|
84 |
CFClient::gl_init; |
85 |
|
86 |
$FONTSIZE = int $HEIGHT / 40; |
87 |
|
88 |
############################################################################# |
89 |
|
90 |
glClearColor 0.45, 0.45, 0.45, 1; |
91 |
|
92 |
glEnable GL_TEXTURE_2D; |
93 |
glEnable GL_COLOR_MATERIAL; |
94 |
glShadeModel GL_FLAT; |
95 |
glDisable GL_DEPTH_TEST; |
96 |
glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; |
97 |
|
98 |
############################################################################# |
99 |
|
100 |
$DEBUG_STATUS = new CFClient::UI::Label padding => 0; |
101 |
$CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS); |
102 |
|
103 |
$STATUS_LINE = new CFClient::UI::Label |
104 |
padding => 0, |
105 |
y => $HEIGHT * 49 / 50 - $FONTSIZE; |
106 |
$CFClient::UI::TOPLEVEL->add ($STATUS_LINE); |
107 |
|
108 |
$ALT_ENTER_MESSAGE = new CFClient::UI::Label |
109 |
padding => 0, |
110 |
y => $HEIGHT * 49 / 50, |
111 |
height => $HEIGHT / 50, |
112 |
text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; |
113 |
$CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE); |
114 |
|
115 |
$MAPWIDGET = new CFClient::UI::MapWidget; |
116 |
$CFClient::UI::TOPLEVEL->add ($MAPWIDGET); |
117 |
$MAPWIDGET->focus_in; |
118 |
} |
119 |
|
120 |
sub destroy_screen { |
121 |
remove Glib::Source $SDL_TIMER; |
122 |
undef $SDL_APP; |
123 |
undef $SDL_EV; |
124 |
SDL::Quit; |
125 |
} |
126 |
|
127 |
sub config_dialog { |
128 |
my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100, |
129 |
child => (my $vbox = new CFClient::UI::VBox); |
130 |
$vbox->add (new CFClient::UI::Label align => 0, text => "Setup"); |
131 |
$vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]); |
132 |
|
133 |
$table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode"); |
134 |
$table->add (1, 0, my $hbox = new CFClient::UI::HBox); |
135 |
|
136 |
$hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
137 |
$hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8); |
138 |
|
139 |
$mode_slider->connect (changed => sub { |
140 |
my ($self, $value) = @_; |
141 |
|
142 |
$CFG->{sdl_mode} = $self->{range}[0] = $value = int $value; |
143 |
$mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]}); |
144 |
}); |
145 |
$mode_slider->emit (changed => $mode_slider->{range}[0]); |
146 |
|
147 |
$table->add (0, 1, new CFClient::UI::Label align => 1, text => "Host"); |
148 |
$table->add (1, 1, my $host = new CFClient::UI::Entry text => $CFG->{host}); |
149 |
|
150 |
$table->add (0, 2, new CFClient::UI::Label align => 1, text => "Port"); |
151 |
$table->add (1, 2, my $port = new CFClient::UI::Entry text => $CFG->{port}); |
152 |
|
153 |
$table->add (0, 3, new CFClient::UI::Label align => 1, text => "Username"); |
154 |
$table->add (1, 3, my $user = new CFClient::UI::Entry text => $CFG->{user}); |
155 |
|
156 |
$table->add (0, 4, new CFClient::UI::Label align => 1, text => "Password"); |
157 |
$table->add (1, 4, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1); |
158 |
|
159 |
$table->add (0, 5, new CFClient::UI::Label align => 1, text => "Map Size"); |
160 |
$table->add (1, 5, new CFClient::UI::Slider |
161 |
req_w => 100, |
162 |
range => [$CFG->{mapsize}, 10, 100 + 1, 1], |
163 |
connect_changed => sub { |
164 |
my ($self, $value) = @_; |
165 |
|
166 |
$CFG->{mapsize} = $self->{range}[0] = $value = int $value; |
167 |
}, |
168 |
); |
169 |
|
170 |
$vbox->add (my $hbox = new CFClient::UI::HBox); |
171 |
$hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub { |
172 |
warn "save\n"; |
173 |
}); |
174 |
$hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub { |
175 |
warn "login\n"; |
176 |
}); |
177 |
$hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub { |
178 |
warn "apply\n"; |
179 |
}); |
180 |
|
181 |
$CFClient::UI::TOPLEVEL->add ($dialog); |
182 |
} |
183 |
|
184 |
sub start_game { |
185 |
$WIDTH = $CFG->{width}; |
186 |
$HEIGHT = $CFG->{height}; |
187 |
$FULLSCREEN = 0; |
188 |
|
189 |
init_screen; |
190 |
|
191 |
config_dialog; |
192 |
|
193 |
my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
194 |
|
195 |
$CONN = new conn |
196 |
host => $CFG->{host}, |
197 |
port => $CFG->{port}, |
198 |
user => $CFG->{user}, |
199 |
pass => $CFG->{password}, |
200 |
mapw => $mapsize, |
201 |
maph => $mapsize, |
202 |
; |
203 |
|
204 |
CFClient::lowdelay fileno $CONN->{fh}; |
205 |
} |
206 |
|
207 |
sub stop_game { |
208 |
remove Glib::Source $refresh_handler if $refresh_handler; |
209 |
undef $refresh_handler; |
210 |
|
211 |
undef $CONN; |
212 |
destroy_screen; |
213 |
} |
214 |
|
215 |
sub force_refresh { |
216 |
glViewport 0, 0, $WIDTH, $HEIGHT; |
217 |
|
218 |
glMatrixMode GL_PROJECTION; |
219 |
glLoadIdentity; |
220 |
glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
221 |
glMatrixMode GL_MODELVIEW; |
222 |
glLoadIdentity; |
223 |
|
224 |
glClear GL_COLOR_BUFFER_BIT; |
225 |
|
226 |
$CFClient::UI::TOPLEVEL->draw; |
227 |
|
228 |
SDL::GLSwapBuffers; |
229 |
} |
230 |
|
231 |
sub debug { |
232 |
$DEBUG_STATUS->set_text ($_[0]); |
233 |
my ($w, $h) = $DEBUG_STATUS->size_request; |
234 |
$DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
235 |
} |
236 |
|
237 |
my $FPS; |
238 |
|
239 |
sub refresh { |
240 |
$refresh_handler ||= add Glib::Idle sub { |
241 |
if ($SDL_APP) { |
242 |
$NOW = SDL::GetTicks; |
243 |
|
244 |
if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
245 |
SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
246 |
$NOW = SDL::GetTicks; |
247 |
} |
248 |
|
249 |
my $interval = ($NOW - $last_refresh) * 0.001; |
250 |
$last_refresh = $NOW; |
251 |
|
252 |
if ($interval) { |
253 |
$FPS ||= 1 / $interval; |
254 |
$FPS = $FPS * 0.9 + (1 / $interval) * 0.1; |
255 |
debug sprintf "%5.02f", $FPS; |
256 |
} |
257 |
|
258 |
force_refresh; |
259 |
$_->animate ($interval) for grep $_, values %ANIMATE; |
260 |
|
261 |
if (%ANIMATE) { |
262 |
1 |
263 |
} else { |
264 |
undef $refresh_handler; |
265 |
0 |
266 |
} |
267 |
} else { |
268 |
undef $refresh_handler; |
269 |
0 |
270 |
} |
271 |
}; |
272 |
} |
273 |
|
274 |
sub animation_start { |
275 |
my ($widget) = @_; |
276 |
$ANIMATE{$widget} = $widget; |
277 |
Scalar::Util::weaken $ANIMATE{$widget}; |
278 |
|
279 |
refresh; |
280 |
} |
281 |
|
282 |
sub animation_stop { |
283 |
my ($widget) = @_; |
284 |
delete $ANIMATE{$widget}; |
285 |
} |
286 |
|
287 |
%SDL_CB = ( |
288 |
SDL_QUIT() => sub { |
289 |
main_quit Gtk2; |
290 |
}, |
291 |
SDL_VIDEORESIZE() => sub { |
292 |
}, |
293 |
SDL_VIDEOEXPOSE() => sub { |
294 |
refresh; |
295 |
}, |
296 |
SDL_KEYDOWN() => sub { |
297 |
if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
298 |
# alt-enter |
299 |
$FULLSCREEN = !$FULLSCREEN; |
300 |
init_screen; |
301 |
} else { |
302 |
CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
303 |
} |
304 |
}, |
305 |
SDL_KEYUP() => sub { |
306 |
CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
307 |
}, |
308 |
SDL_MOUSEMOTION() => sub { |
309 |
CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
310 |
}, |
311 |
SDL_MOUSEBUTTONDOWN() => sub { |
312 |
CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
313 |
}, |
314 |
SDL_MOUSEBUTTONUP() => sub { |
315 |
CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
316 |
}, |
317 |
SDL_ACTIVEEVENT() => sub { |
318 |
# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
319 |
}, |
320 |
); |
321 |
|
322 |
@conn::ISA = Crossfire::Protocol::; |
323 |
|
324 |
sub conn::map_update { |
325 |
my ($self, $dirty) = @_; |
326 |
|
327 |
$MAPWIDGET->update; |
328 |
} |
329 |
|
330 |
sub conn::map_scroll { |
331 |
my ($self, $dx, $dy) = @_; |
332 |
|
333 |
# refresh; |
334 |
} |
335 |
|
336 |
sub conn::map_clear { |
337 |
my ($self) = @_; |
338 |
|
339 |
# refresh; |
340 |
} |
341 |
|
342 |
sub conn::face_find { |
343 |
my ($self, $face) = @_; |
344 |
|
345 |
$FACECACHE->{"$face->{chksum},$face->{name}"} |
346 |
} |
347 |
|
348 |
sub conn::face_update { |
349 |
my ($self, $face) = @_; |
350 |
|
351 |
$FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
352 |
|
353 |
$face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
354 |
} |
355 |
|
356 |
sub conn::query { |
357 |
my ($self, $flags, $prompt) = @_; |
358 |
|
359 |
warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
360 |
} |
361 |
|
362 |
sub gtk_add_cfg_field { |
363 |
my ($tbl, $cfg, $klbl, $key, $value) = @_; |
364 |
my $i = $cfg->{_i}++; |
365 |
$tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
366 |
$tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
367 |
if ($key eq 'password') { |
368 |
$ent->set_invisible_char ("*"); |
369 |
$ent->set (visibility => 0) |
370 |
} |
371 |
$ent->set_text ($value); |
372 |
$ent->signal_connect (changed => sub { |
373 |
my ($ent) = @_; |
374 |
$cfg->{$key} = $ent->get_text; |
375 |
# TODO: Mapsize should be a slider in the game gui |
376 |
if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
377 |
$cfg->{$key} = 100; |
378 |
} elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
379 |
$cfg->{$key} = 50; |
380 |
} |
381 |
}); |
382 |
} |
383 |
|
384 |
sub run_config_dialog { |
385 |
my (%events) = @_; |
386 |
|
387 |
my $w = Gtk2::Window->new; |
388 |
|
389 |
my @cfg = ( |
390 |
[qw/Host host/], |
391 |
[qw/Port port/], |
392 |
[qw/Mapsize% mapsize/], |
393 |
[qw/Username user/], |
394 |
[qw/Password password/], |
395 |
); |
396 |
|
397 |
my $cfg = {}; |
398 |
|
399 |
$w->add (my $vb = Gtk2::VBox->new); |
400 |
$vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
401 |
my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
402 |
$t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
403 |
$t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
404 |
my $i = 0; |
405 |
my $act = 0; |
406 |
for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { |
407 |
if ($_ eq $selmode) { $act = $i } |
408 |
$cb->append_text ($_); |
409 |
$i++; |
410 |
} |
411 |
$cb->set_active ($act); |
412 |
$cb->signal_connect (changed => sub { |
413 |
my ($cb) = @_; |
414 |
my $txt = $cb->get_active_text; |
415 |
if ($txt =~ m/(\d+)x(\d+)/) { |
416 |
$::CFG->{width} = $1; |
417 |
$::CFG->{height} = $2; |
418 |
} |
419 |
}); |
420 |
|
421 |
$cfg->{_i} = 1; |
422 |
for (@cfg) { |
423 |
gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
424 |
} |
425 |
|
426 |
$vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
427 |
$hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
428 |
$cb->signal_connect (clicked => sub { |
429 |
for (keys %$cfg) { |
430 |
$::CFG->{$_} = $cfg->{$_} |
431 |
if $_ ne '_i'; |
432 |
} |
433 |
CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
434 |
}); |
435 |
$hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
436 |
$cb->signal_connect (clicked => sub { |
437 |
for (keys %$cfg) { |
438 |
$::CFG->{$_} = $cfg->{$_} |
439 |
if $_ ne '_i'; |
440 |
} |
441 |
my $cb = $events{login} || sub {}; |
442 |
$cb->($::CFG->{user}, $::CFG->{password}); |
443 |
}); |
444 |
$hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
445 |
$cb->signal_connect (clicked => sub { |
446 |
my $cb = $events{logout} || sub {}; |
447 |
$cb->(); |
448 |
}); |
449 |
$hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
450 |
$cb->signal_connect (clicked => sub { $w->destroy }); |
451 |
|
452 |
$w->show_all; |
453 |
|
454 |
$w->signal_connect (destroy => sub { Gtk2->main_quit }); |
455 |
} |
456 |
|
457 |
|
458 |
############################################################################# |
459 |
|
460 |
SDL::Init SDL_INIT_EVERYTHING; |
461 |
|
462 |
@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], |
463 |
@{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; |
464 |
|
465 |
CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
466 |
|
467 |
$CFG ||= { |
468 |
width => 640, |
469 |
height => 480, |
470 |
mapsize => 100, |
471 |
fullscreen => 0, |
472 |
host => "crossfire.schmorp.de", |
473 |
port => 13327, |
474 |
}; |
475 |
|
476 |
{ |
477 |
my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
478 |
|
479 |
CFClient::add_font $_ for @fonts; |
480 |
CFClient::set_font $fonts[0]; |
481 |
} |
482 |
|
483 |
$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
484 |
|
485 |
run_config_dialog |
486 |
login => sub { start_game }, |
487 |
logout => sub { stop_game }; |
488 |
|
489 |
main Gtk2; |
490 |
|
491 |
Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |