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