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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines