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