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.14 by elmex, Fri Apr 7 16:36:44 2006 UTC vs.
Revision 1.70 by root, Tue Apr 11 14:36:02 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines