ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.63
Committed: Mon Apr 10 22:53:50 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.62: +11 -2 lines
Log Message:
*** empty log message ***

File Contents

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