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.18 by root, Fri Apr 7 18:49:53 2006 UTC vs.
Revision 1.63 by root, Mon Apr 10 22:53:50 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;
17use Crossfire::Protocol;
18
15use Crossfire::Client; 19use Crossfire::Client;
16use Crossfire::Protocol;
17
18use Client::Util;
19use Client::Widget; 20use Crossfire::Client::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 $FONTSIZE;
37
26our $SDL_TIMER; 38our $SDL_TIMER;
27our $SDL_APP; 39our $SDL_APP;
28our $SDL_EV = new SDL::Event; 40our $SDL_EV;
29our %SDL_CB; 41our %SDL_CB;
30 42
31our @GL_INIT; # hooks called on every gl init 43our $ALT_ENTER_MESSAGE;
44our $STATUS_LINE;
45
46my $last_refresh;
47my %ANIMATE;
48my $refresh_handler;
49
50our ($tw, $te); # Test widget #d#
32 51
33sub init_screen { 52sub init_screen {
34 $SDL_APP = new SDL::App 53 $SDL_APP = new SDL::App
35 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
36 -title => "Crossfire+ Client", 55 -title => "Crossfire+ Client",
37 -width => $CFG->{width}, 56 -width => $WIDTH,
38 -height => $CFG->{height}, 57 -height => $HEIGHT,
39 -opengl => 1, 58 -opengl => 1,
40 -red_size => 8, 59 -red_size => 5,
41 -green_size => 8, 60 -green_size => 5,
42 -blue_size => 8, 61 -blue_size => 5,
62 -alpha_size => 0,
43 -double_buffer => 1, 63 -double_buffer => 1,
44 -fullscreen => $CFG->{fullscreen}, 64 -fullscreen => $FULLSCREEN,
45 -resizeable => 0; 65 -resizeable => 0;
46 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
47 glEnable GL_TEXTURE_2D; 87 glEnable GL_TEXTURE_2D;
48 glTexEnv GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE; 88 glEnable GL_COLOR_MATERIAL;
49 glShadeModel GL_FLAT; 89 glShadeModel GL_FLAT;
50 glDisable GL_DEPTH_TEST; 90 glDisable GL_DEPTH_TEST;
51 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA; 91 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
52 glEnable GL_BLEND; 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
138sub destroy_screen {
139 remove Glib::Source $SDL_TIMER;
140 undef $SDL_APP;
141 undef $SDL_EV;
142 SDL::Quit;
143}
144
145sub 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
166sub stop_game {
167 remove Glib::Source $refresh_handler if $refresh_handler;
168 undef $refresh_handler;
169
170 undef $CONN;
171 destroy_screen;
172}
173
174sub force_refresh {
175 glViewport 0, 0, $WIDTH, $HEIGHT;
53 176
54 glMatrixMode GL_PROJECTION; 177 glMatrixMode GL_PROJECTION;
55 glLoadIdentity; 178 glLoadIdentity;
56 glOrtho 0, $CFG->{width} / 32, $CFG->{height} / 32, 0, -1 , 1; 179 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
180 glMatrixMode GL_MODELVIEW;
57 181
58 $_->() for @GL_INIT; 182 glClear GL_COLOR_BUFFER_BIT;
59}
60 183
61sub start_game { 184 $Crossfire::Client::Widget::TOPLEVEL->draw;
62 $SDL_TIMER = add Glib::Timeout 1000/20, sub { 185
63 while ($SDL_EV->poll) { 186 SDL::GLSwapBuffers;
64 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->(); 187}
188
189sub 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
65 } 214 }
66
67 1
68 }; 215 };
69
70 init_screen;
71
72 $CONN = new conn
73 host => $CFG->{host},
74 port => $CFG->{port};
75} 216}
76 217
77sub stop_game { 218sub animation_start {
78 remove Glib::Source $SDL_TIMER; 219 my ($widget) = @_;
220 $ANIMATE{$widget} = $widget;
221 Scalar::Util::weaken $ANIMATE{$widget};
79 222
80 undef $SDL_APP; 223 refresh;
81 SDL::Quit;
82} 224}
83 225
84sub refresh { 226sub animation_stop {
85 glClearColor 0, 0, 0, 0; 227 my ($widget) = @_;
86 glClear GL_COLOR_BUFFER_BIT; 228 delete $ANIMATE{$widget};
87
88 for (values %Client::Widget::ACTIVE_WIDGETS) {
89 $_->draw
90 }
91
92 SDL::GLSwapBuffers;
93} 229}
94 230
95%SDL_CB = ( 231%SDL_CB = (
96 SDL_QUIT() => sub { 232 SDL_QUIT() => sub {
97 warn "sdl quit\n";#d# 233 main_quit Gtk2;
98 exit;
99 }, 234 },
100 SDL_VIDEORESIZE() => sub { 235 SDL_VIDEORESIZE() => sub {
101 }, 236 },
102 SDL_VIDEOEXPOSE() => sub { 237 SDL_VIDEOEXPOSE() => sub {
103 refresh; 238 refresh;
104 }, 239 },
105 SDL_KEYDOWN() => sub { 240 SDL_KEYDOWN() => sub {
106 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 241 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
107 # alt-enter 242 # alt-enter
108 $CFG->{fullscreen} = !$CFG->{fullscreen}; 243 $FULLSCREEN = !$FULLSCREEN;
109 init_screen; 244 init_screen;
110 } else { 245 } else {
111 Client::Widget::feed_sdl_key_down_event ($SDL_EV); 246 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
112 } 247 }
113 }, 248 },
114 SDL_KEYUP() => sub { 249 SDL_KEYUP() => sub {
115 Client::Widget::feed_sdl_key_up_event ($SDL_EV); 250 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
116 }, 251 },
117 SDL_MOUSEMOTION() => sub { 252 SDL_MOUSEMOTION() => sub {
118 warn "sdl motion\n";#d# 253 Crossfire::Client::Widget::feed_sdl_motion_event ($SDL_EV);
119 }, 254 },
120 SDL_MOUSEBUTTONDOWN() => sub { 255 SDL_MOUSEBUTTONDOWN() => sub {
121 Client::Widget::feed_sdl_button_down_event ($SDL_EV); 256 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
122 }, 257 },
123 SDL_MOUSEBUTTONUP() => sub { 258 SDL_MOUSEBUTTONUP() => sub {
124 Client::Widget::feed_sdl_button_up_event ($SDL_EV); 259 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
125 }, 260 },
126 SDL_ACTIVEEVENT() => sub { 261 SDL_ACTIVEEVENT() => sub {
127 warn "active\n";#d# 262 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
128 }, 263 },
129); 264);
130 265
131@conn::ISA = Crossfire::Protocol::; 266@conn::ISA = Crossfire::Protocol::;
132 267
137} 272}
138 273
139sub conn::map_scroll { 274sub conn::map_scroll {
140 my ($self, $dx, $dy) = @_; 275 my ($self, $dx, $dy) = @_;
141 276
142 refresh; 277# refresh;
143} 278}
144 279
145sub conn::map_clear { 280sub conn::map_clear {
146 my ($self) = @_; 281 my ($self) = @_;
147 282
148 refresh; 283# refresh;
284}
285
286sub conn::face_find {
287 my ($self, $face) = @_;
288
289 $FACECACHE->{"$face->{chksum},$face->{name}"}
149} 290}
150 291
151sub conn::face_update { 292sub conn::face_update {
152 my ($self, $num, $face) = @_; 293 my ($self, $face) = @_;
294
295 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
153 296
154 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 297 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
155} 298}
156 299
300sub conn::query {
301 my ($self, $flags, $prompt) = @_;
302
303 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
304}
305
306sub 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
328sub 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
157############################################################################# 405#############################################################################
158 406
407SDL::Init SDL_INIT_EVERYTHING;
408
159my $mapwidget = Client::MapWidget->new; 409my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
160 410
161$mapwidget->activate; 411$Crossfire::Client::Widget::TOPLEVEL->add ($mapwidget);
162$mapwidget->focus_in; 412$mapwidget->focus_in;
163 413
164Client::Util::read_cfg "$Crossfire::VARDIR/pclientrc"; 414Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
165 415
166$CFG ||= { 416$CFG ||= {
167 width => 640, 417 width => 640,
168 height => 480, 418 height => 480,
419 mapsize => 100,
169 fullscreen => 0, 420 fullscreen => 0,
170 host => "crossfire.schmorp.de", 421 host => "crossfire.schmorp.de",
171 port => 13327, 422 port => 13327,
172}; 423};
173 424
174Client::Util::run_config_dialog 425Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
426
427$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
428
429run_config_dialog
175 login => sub { start_game }, 430 login => sub { start_game },
176 logout => sub { stop_game }; 431 logout => sub { stop_game };
177 432
178main Gtk2; 433main Gtk2;
434
435Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines