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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines