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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines