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 req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]); |
137 |
$hbox->add (my $mode_label = new CFClient::UI::Label); |
138 |
|
139 |
$mode_slider->connect (changed => sub { |
140 |
my (undef, $value) = @_; |
141 |
|
142 |
$CFG->{sdl_mode} = $value = int $value + 0.5; |
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, my $mapsize_slider = new CFClient::UI::Slider req_w => 100, range => [$CFG->{mapsize}, 10, 100 + 1, 1]); |
161 |
|
162 |
$CFClient::UI::TOPLEVEL->add ($dialog); |
163 |
} |
164 |
|
165 |
sub start_game { |
166 |
$WIDTH = $CFG->{width}; |
167 |
$HEIGHT = $CFG->{height}; |
168 |
$FULLSCREEN = 0; |
169 |
|
170 |
init_screen; |
171 |
|
172 |
config_dialog; |
173 |
|
174 |
my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32; |
175 |
|
176 |
$CONN = new conn |
177 |
host => $CFG->{host}, |
178 |
port => $CFG->{port}, |
179 |
user => $CFG->{user}, |
180 |
pass => $CFG->{password}, |
181 |
mapw => $mapsize, |
182 |
maph => $mapsize, |
183 |
; |
184 |
|
185 |
CFClient::lowdelay fileno $CONN->{fh}; |
186 |
} |
187 |
|
188 |
sub stop_game { |
189 |
remove Glib::Source $refresh_handler if $refresh_handler; |
190 |
undef $refresh_handler; |
191 |
|
192 |
undef $CONN; |
193 |
destroy_screen; |
194 |
} |
195 |
|
196 |
sub force_refresh { |
197 |
glViewport 0, 0, $WIDTH, $HEIGHT; |
198 |
|
199 |
glMatrixMode GL_PROJECTION; |
200 |
glLoadIdentity; |
201 |
glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; |
202 |
glMatrixMode GL_MODELVIEW; |
203 |
glLoadIdentity; |
204 |
|
205 |
glClear GL_COLOR_BUFFER_BIT; |
206 |
|
207 |
$CFClient::UI::TOPLEVEL->draw; |
208 |
|
209 |
SDL::GLSwapBuffers; |
210 |
} |
211 |
|
212 |
sub debug { |
213 |
$DEBUG_STATUS->set_text ($_[0]); |
214 |
my ($w, $h) = $DEBUG_STATUS->size_request; |
215 |
$DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h); |
216 |
} |
217 |
|
218 |
my $FPS; |
219 |
|
220 |
sub refresh { |
221 |
$refresh_handler ||= add Glib::Idle sub { |
222 |
if ($SDL_APP) { |
223 |
$NOW = SDL::GetTicks; |
224 |
|
225 |
if ($NOW - $last_refresh < $TICKS_PER_FRAME) { |
226 |
SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh); |
227 |
$NOW = SDL::GetTicks; |
228 |
} |
229 |
|
230 |
my $interval = ($NOW - $last_refresh) * 0.001; |
231 |
$last_refresh = $NOW; |
232 |
|
233 |
if ($interval) { |
234 |
$FPS ||= 1 / $interval; |
235 |
$FPS = $FPS * 0.9 + (1 / $interval) * 0.1; |
236 |
debug sprintf "%5.02f", $FPS; |
237 |
} |
238 |
|
239 |
force_refresh; |
240 |
$_->animate ($interval) for grep $_, values %ANIMATE; |
241 |
|
242 |
if (%ANIMATE) { |
243 |
1 |
244 |
} else { |
245 |
undef $refresh_handler; |
246 |
0 |
247 |
} |
248 |
} else { |
249 |
undef $refresh_handler; |
250 |
0 |
251 |
} |
252 |
}; |
253 |
} |
254 |
|
255 |
sub animation_start { |
256 |
my ($widget) = @_; |
257 |
$ANIMATE{$widget} = $widget; |
258 |
Scalar::Util::weaken $ANIMATE{$widget}; |
259 |
|
260 |
refresh; |
261 |
} |
262 |
|
263 |
sub animation_stop { |
264 |
my ($widget) = @_; |
265 |
delete $ANIMATE{$widget}; |
266 |
} |
267 |
|
268 |
%SDL_CB = ( |
269 |
SDL_QUIT() => sub { |
270 |
main_quit Gtk2; |
271 |
}, |
272 |
SDL_VIDEORESIZE() => sub { |
273 |
}, |
274 |
SDL_VIDEOEXPOSE() => sub { |
275 |
refresh; |
276 |
}, |
277 |
SDL_KEYDOWN() => sub { |
278 |
if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { |
279 |
# alt-enter |
280 |
$FULLSCREEN = !$FULLSCREEN; |
281 |
init_screen; |
282 |
} else { |
283 |
CFClient::UI::feed_sdl_key_down_event ($SDL_EV); |
284 |
} |
285 |
}, |
286 |
SDL_KEYUP() => sub { |
287 |
CFClient::UI::feed_sdl_key_up_event ($SDL_EV); |
288 |
}, |
289 |
SDL_MOUSEMOTION() => sub { |
290 |
CFClient::UI::feed_sdl_motion_event ($SDL_EV); |
291 |
}, |
292 |
SDL_MOUSEBUTTONDOWN() => sub { |
293 |
CFClient::UI::feed_sdl_button_down_event ($SDL_EV); |
294 |
}, |
295 |
SDL_MOUSEBUTTONUP() => sub { |
296 |
CFClient::UI::feed_sdl_button_up_event ($SDL_EV); |
297 |
}, |
298 |
SDL_ACTIVEEVENT() => sub { |
299 |
# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# |
300 |
}, |
301 |
); |
302 |
|
303 |
@conn::ISA = Crossfire::Protocol::; |
304 |
|
305 |
sub conn::map_update { |
306 |
my ($self, $dirty) = @_; |
307 |
|
308 |
$MAPWIDGET->update; |
309 |
} |
310 |
|
311 |
sub conn::map_scroll { |
312 |
my ($self, $dx, $dy) = @_; |
313 |
|
314 |
# refresh; |
315 |
} |
316 |
|
317 |
sub conn::map_clear { |
318 |
my ($self) = @_; |
319 |
|
320 |
# refresh; |
321 |
} |
322 |
|
323 |
sub conn::face_find { |
324 |
my ($self, $face) = @_; |
325 |
|
326 |
$FACECACHE->{"$face->{chksum},$face->{name}"} |
327 |
} |
328 |
|
329 |
sub conn::face_update { |
330 |
my ($self, $face) = @_; |
331 |
|
332 |
$FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; |
333 |
|
334 |
$face->{texture} = new_from_image CFClient::Texture delete $face->{image}; |
335 |
} |
336 |
|
337 |
sub conn::query { |
338 |
my ($self, $flags, $prompt) = @_; |
339 |
|
340 |
warn "<<<<QUERY:$flags:$prompt>>>\n";#d# |
341 |
} |
342 |
|
343 |
sub gtk_add_cfg_field { |
344 |
my ($tbl, $cfg, $klbl, $key, $value) = @_; |
345 |
my $i = $cfg->{_i}++; |
346 |
$tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1); |
347 |
$tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1); |
348 |
if ($key eq 'password') { |
349 |
$ent->set_invisible_char ("*"); |
350 |
$ent->set (visibility => 0) |
351 |
} |
352 |
$ent->set_text ($value); |
353 |
$ent->signal_connect (changed => sub { |
354 |
my ($ent) = @_; |
355 |
$cfg->{$key} = $ent->get_text; |
356 |
# TODO: Mapsize should be a slider in the game gui |
357 |
if ($key eq 'mapsize' and $cfg->{$key} > 100) { |
358 |
$cfg->{$key} = 100; |
359 |
} elsif ($key eq 'mapsize' and $cfg->{$key} < 50) { |
360 |
$cfg->{$key} = 50; |
361 |
} |
362 |
}); |
363 |
} |
364 |
|
365 |
sub run_config_dialog { |
366 |
my (%events) = @_; |
367 |
|
368 |
my $w = Gtk2::Window->new; |
369 |
|
370 |
my @cfg = ( |
371 |
[qw/Host host/], |
372 |
[qw/Port port/], |
373 |
[qw/Mapsize% mapsize/], |
374 |
[qw/Username user/], |
375 |
[qw/Password password/], |
376 |
); |
377 |
|
378 |
my $cfg = {}; |
379 |
|
380 |
$w->add (my $vb = Gtk2::VBox->new); |
381 |
$vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); |
382 |
my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; |
383 |
$t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); |
384 |
$t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); |
385 |
my $i = 0; |
386 |
my $act = 0; |
387 |
for (map { "$_->[0]x$_->[1]" } @SDL_MODES) { |
388 |
if ($_ eq $selmode) { $act = $i } |
389 |
$cb->append_text ($_); |
390 |
$i++; |
391 |
} |
392 |
$cb->set_active ($act); |
393 |
$cb->signal_connect (changed => sub { |
394 |
my ($cb) = @_; |
395 |
my $txt = $cb->get_active_text; |
396 |
if ($txt =~ m/(\d+)x(\d+)/) { |
397 |
$::CFG->{width} = $1; |
398 |
$::CFG->{height} = $2; |
399 |
} |
400 |
}); |
401 |
|
402 |
$cfg->{_i} = 1; |
403 |
for (@cfg) { |
404 |
gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]}); |
405 |
} |
406 |
|
407 |
$vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0); |
408 |
$hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5); |
409 |
$cb->signal_connect (clicked => sub { |
410 |
for (keys %$cfg) { |
411 |
$::CFG->{$_} = $cfg->{$_} |
412 |
if $_ ne '_i'; |
413 |
} |
414 |
CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc"; |
415 |
}); |
416 |
$hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); |
417 |
$cb->signal_connect (clicked => sub { |
418 |
for (keys %$cfg) { |
419 |
$::CFG->{$_} = $cfg->{$_} |
420 |
if $_ ne '_i'; |
421 |
} |
422 |
my $cb = $events{login} || sub {}; |
423 |
$cb->($::CFG->{user}, $::CFG->{password}); |
424 |
}); |
425 |
$hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5); |
426 |
$cb->signal_connect (clicked => sub { |
427 |
my $cb = $events{logout} || sub {}; |
428 |
$cb->(); |
429 |
}); |
430 |
$hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5); |
431 |
$cb->signal_connect (clicked => sub { $w->destroy }); |
432 |
|
433 |
$w->show_all; |
434 |
|
435 |
$w->signal_connect (destroy => sub { Gtk2->main_quit }); |
436 |
} |
437 |
|
438 |
|
439 |
############################################################################# |
440 |
|
441 |
SDL::Init SDL_INIT_EVERYTHING; |
442 |
|
443 |
@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)], |
444 |
@{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE }; |
445 |
|
446 |
CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; |
447 |
|
448 |
$CFG ||= { |
449 |
width => 640, |
450 |
height => 480, |
451 |
mapsize => 100, |
452 |
fullscreen => 0, |
453 |
host => "crossfire.schmorp.de", |
454 |
port => 13327, |
455 |
}; |
456 |
|
457 |
{ |
458 |
my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf); |
459 |
|
460 |
CFClient::add_font $_ for @fonts; |
461 |
CFClient::set_font $fonts[0]; |
462 |
} |
463 |
|
464 |
$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; |
465 |
|
466 |
run_config_dialog |
467 |
login => sub { start_game }, |
468 |
logout => sub { stop_game }; |
469 |
|
470 |
main Gtk2; |
471 |
|
472 |
Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; |