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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines