ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/pclient (file contents):
Revision 1.17 by root, Fri Apr 7 18:20:13 2006 UTC vs.
Revision 1.71 by root, Tue Apr 11 17:02:36 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines