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.3 by root, Thu Apr 6 20:15:10 2006 UTC vs.
Revision 1.57 by root, Sun Apr 9 23:11:16 2006 UTC

1#!/opt/bin/perl 1#!/opt/bin/perl
2 2
3use strict; 3use strict;
4use utf8;
4 5
5use Crossfire::Client; 6use Glib;
6use Crossfire::Protocol; 7use Gtk2 -init;
7
8package Crossfire::Client; # uh, yeah
9
10use strict;
11 8
12use SDL; 9use SDL;
13use SDL::App; 10use SDL::App;
14use SDL::Event; 11use SDL::Event;
15use SDL::Surface; 12use SDL::Surface;
16use SDL::OpenGL; 13use SDL::OpenGL;
17use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
18 15
19my $conn; 16use Crossfire;
20my $app; 17use Crossfire::Client;
18use Crossfire::Protocol;
21 19
22my $WIDTH = 640; 20use Crossfire::Client::Widget;
23my $HEIGHT = 480;
24 21
25sub glinit { 22our $FACECACHE;
26 # nuke all gl context data 23
27 24our $VERSION = '0.1';
25
26our %GL_EXT;
27
28our $CFG;
29our $CONN;
30
31our $WIDTH;
32our $HEIGHT;
33our $FULLSCREEN;
34
35our $FONTSIZE;
36
37our ($FOCUS, $HOVER, $GRAB); # various widgets
38
39our $SDL_TIMER;
40our $SDL_APP;
41our $SDL_EV = new SDL::Event;
42our %SDL_CB;
43
44our @GL_INIT; # hooks called on every gl init
45
46our $ALT_ENTER_MESSAGE;
47our $STATUS_LINE;
48
49our $TOPLEVEL;
50
51our $tw; # Test widget #d#
52
53my $last_refresh;
54my %ANIMATE;
55my $refresh_handler;
56
57sub init_screen {
28 $app = new SDL::App 58 $SDL_APP = new SDL::App
59 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
29 -title => "Crossfire+ Client", 60 -title => "Crossfire+ Client",
30 -width => $WIDTH, 61 -width => $WIDTH,
31 -height => $HEIGHT, 62 -height => $HEIGHT,
32 -depth => 24,
33 -opengl => 1, 63 -opengl => 1,
64 -red_size => 5,
65 -green_size => 5,
66 -blue_size => 5,
67 -alpha_size => 0,
34 -double_buffer => 1, 68 -double_buffer => 1,
69 -fullscreen => $FULLSCREEN,
35 -resizeable => 0; 70 -resizeable => 0;
71
72 $last_refresh = SDL::GetTicks;
73
74 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
75
76 $GL_EXT{GL_ARB_texture_non_power_of_two}
77 or warn "WARNING: non-power-of-two opengl extension required";
78
79 $FONTSIZE = int $HEIGHT / 50;
80
81 #############################################################################
82
83 glClearColor 0, 0, 0, 0;
36 84
37 glEnable GL_TEXTURE_2D; 85 glEnable GL_TEXTURE_2D;
86 glEnable GL_COLOR_MATERIAL;
38 glShadeModel GL_FLAT; 87 glShadeModel GL_FLAT;
39 glDisable GL_DEPTH_TEST; 88 glDisable GL_DEPTH_TEST;
89 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
90
91 $_->() for @GL_INIT;
92
93 #############################################################################
94
95 $STATUS_LINE = new Crossfire::Client::Widget::Label
96 0, $HEIGHT * 59 / 60 - $FONTSIZE, 1, $FONTSIZE,
97 "";
98 $TOPLEVEL->add ($STATUS_LINE);
99
100 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
101 0, $HEIGHT * 59 / 60, 1, $HEIGHT / 60,
102 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
103 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
104
105 # Test code #d#
106 unless ($tw) { # haha...
107 $tw = new Crossfire::Client::Widget::Animator;
108 my $lbl1 = new Crossfire::Client::Widget::Label
109 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
110 my $lbl2 = new Crossfire::Client::Widget::Label
111 0, 0, 10, $FONTSIZE, "LBL2";
112
113 my $vb = new Crossfire::Client::Widget::VBox;
114 my $f = new Crossfire::Client::Widget::FancyFrame;
115 my $f2 = new Crossfire::Client::Widget::FancyFrame;
116 $f->add ($lbl1);
117 $f2->add ($lbl2);
118 $vb->add ($f);
119 $vb->add ($f2, 1);
120
121 $tw->add ($vb);
122 $tw->w (400);
123 $tw->h (300);
124 $tw->move ($WIDTH - 200, 0);
125 $tw->moveto (0, 0);
126 $TOPLEVEL->add ($tw);
127
128# $f->move ($WIDTH - 200, 0);
129# $TOPLEVEL->add ($f);
130 }
131}
132
133sub start_game {
134 $SDL_TIMER = add Glib::Timeout 1000/50, sub {
135 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
136 while $SDL_EV->poll;
137
138 1
139 };
140
141 $WIDTH = $CFG->{width};
142 $HEIGHT = $CFG->{height};
143 $FULLSCREEN = 0;
144
145 init_screen;
146
147 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32;
148
149 $CONN = new conn
150 host => $CFG->{host},
151 port => $CFG->{port},
152 user => $CFG->{user},
153 pass => $CFG->{password},
154 mapw => $mapsize,
155 maph => $mapsize,
156 ;
157
158 Crossfire::Client::lowdelay fileno $CONN->{fh};
159}
160
161sub stop_game {
162 remove Glib::Source $SDL_TIMER;
163 remove Glib::Source $refresh_handler if $refresh_handler;
164 undef $refresh_handler;
165
166 undef $SDL_APP;
167 undef $CONN;
168 SDL::Quit;
169}
170
171
172sub force_refresh {
173 glViewport 0, 0, $WIDTH, $HEIGHT;
174
40 glMatrixMode GL_PROJECTION; 175 glMatrixMode GL_PROJECTION;
176 glLoadIdentity;
177 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
178 glMatrixMode GL_MODELVIEW;
41 179
42 #glViewport 0, 0, $WIDTH, $HEIGHT; 180 glClear GL_COLOR_BUFFER_BIT;
43 # re-bind all textures 181
182 $TOPLEVEL->draw;
183
184 SDL::GLSwapBuffers;
44} 185}
45 186
46sub refresh { 187sub refresh {
47 glClearColor 0.5, 0.5, 0.7, 0; 188 $refresh_handler ||= add Glib::Idle sub {
48 glClear GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT; 189 return unless $SDL_APP;
49 190
50 glLoadIdentity; 191 my $next_refresh = SDL::GetTicks;
51 glOrtho 0, $WIDTH / 32, $HEIGHT / 32, 0, -1 , 1; 192 my $interval = ($next_refresh - $last_refresh) * 0.001;
193 $last_refresh = $next_refresh;
52 194
53 my $map = $conn->{map}; 195 force_refresh;
196 $_->animate ($interval) for grep $_, values %ANIMATE;
54 197
55 for my $x (0 .. $conn->{mapw} - 1) { 198 if (%ANIMATE) {
56 for my $y (0 .. $conn->{maph} - 1) {
57
58 my $cell = $map->[$x][$y]
59 or next;
60
61 for my $num (grep $_, $cell->[0], $cell->[1], $cell->[2]) {
62 my $tex = $conn->{face}[$num]{texture} || 0;
63
64 glBindTexture GL_TEXTURE_2D, $tex;
65
66 glBegin GL_QUADS;
67 glTexCoord 0, 0; glVertex $x, $y;
68 glTexCoord 1, 0; glVertex $x + 1, $y;
69 glTexCoord 1, 1; glVertex $x + 1, $y + 1;
70 glTexCoord 0, 1; glVertex $x, $y + 1;
71 glEnd;
72 } 199 1
200 } else {
201 undef $refresh_handler;
202 0
73 } 203 }
74 } 204 };
75
76 SDL::GLSwapBuffers;
77} 205}
78 206
79my $ev = new SDL::Event; 207sub animation_start {
80my %ev_cb; 208 my ($widget) = @_;
209 $ANIMATE{$widget} = $widget;
210 Scalar::Util::weaken $ANIMATE{$widget};
81 211
82sub event(&$) { 212 refresh;
83 $ev_cb{$_[0]->()} = $_[1];
84} 213}
85 214
86sub does(&) { shift } 215sub animation_stop {
216 my ($widget) = @_;
217 delete $ANIMATE{$widget};
218}
87 219
88event {SDL_QUIT} does { 220%SDL_CB = (
89 exit; 221 SDL_QUIT() => sub {
90}; 222 main_quit Gtk2;
91 223 },
92event {SDL_VIDEORESIZE} does { 224 SDL_VIDEORESIZE() => sub {
93 print "resize\n"; 225 },
94}; 226 SDL_VIDEOEXPOSE() => sub {
95 227 refresh;
96event {SDL_KEYDOWN} does { 228 },
97 print "keypress\n"; 229 SDL_KEYDOWN() => sub {
98}; 230 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
99 231 # alt-enter
100event {SDL_KEYUP} does { 232 $FULLSCREEN = !$FULLSCREEN;
101 print "keyup\n";#d# 233 init_screen;
102}; 234 } else {
103 235 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
104event {SDL_MOUSEMOTION} does { 236 }
105 print "motion\n"; 237 },
106}; 238 SDL_KEYUP() => sub {
107 239 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
108event {SDL_MOUSEBUTTONDOWN} does { 240 },
109 print "button\n"; 241 SDL_MOUSEMOTION() => sub {
110}; 242 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y);
111 243 $HOVER = $GRAB || $TOPLEVEL->find_widget ($x, $y);
112event {SDL_MOUSEBUTTONUP} does { 244 ::refresh;
113 print "buttup\n"; 245 },
114}; 246 SDL_MOUSEBUTTONDOWN() => sub {
115 247 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
116event {SDL_ACTIVEEVENT} does { 248 },
249 SDL_MOUSEBUTTONUP() => sub {
250 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
251 },
252 SDL_ACTIVEEVENT() => sub {
117 print "active\n"; 253 warn "active\n";#d#
118}; 254 },
119 255);
120package Crossfire::Client;
121 256
122@conn::ISA = Crossfire::Protocol::; 257@conn::ISA = Crossfire::Protocol::;
123 258
124sub conn::map_update { 259sub conn::map_update {
125 my ($self, $dirty) = @_; 260 my ($self, $dirty) = @_;
128} 263}
129 264
130sub conn::map_scroll { 265sub conn::map_scroll {
131 my ($self, $dx, $dy) = @_; 266 my ($self, $dx, $dy) = @_;
132 267
133 refresh; 268# refresh;
134} 269}
135 270
136sub conn::map_clear { 271sub conn::map_clear {
137 my ($self) = @_; 272 my ($self) = @_;
138 273
139 refresh; 274# refresh;
275}
276
277sub conn::face_find {
278 my ($self, $face) = @_;
279
280 $FACECACHE->{"$face->{chksum},$face->{name}"}
140} 281}
141 282
142sub conn::face_update { 283sub conn::face_update {
143 my ($self, $num, $face) = @_; 284 my ($self, $face) = @_;
144 285
145 warn "up face $self,$num,$face\n";#d# 286 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
146 #TODO
147 open my $fh, ">:raw", "/tmp/x~";
148 syswrite $fh, $face->{image};
149 close $fh;
150 287
151 my $surface = new SDL::Surface -name => "/tmp/x~"; 288 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
289}
152 290
153 unlink "/tmp/x~"; 291sub conn::query {
292 my ($self, $flags, $prompt) = @_;
154 293
155 my ($tex) = @{glGenTextures 1}; 294 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
156 glGetError(); 295}
157 296
158 $face->{texture} = $tex; 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/],
159 330 );
160 glBindTexture GL_TEXTURE_2D, $tex; 331
161 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;
162 356 }
163# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR; 357 });
164# glTexParameter GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR;
165
166 $surface->rgba;
167 358
168 glTexImage2D GL_TEXTURE_2D, 0, 359 $cfg->{_i} = 1;
169 4, # components 360 for (@cfg) {
170 $surface->width, $surface->height, 361 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
362 }
363
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';
171 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';
172 GL_RGBA, 378 }
173 GL_UNSIGNED_BYTE, 379 my $cb = $events{login} || sub {};
174 $surface->pixels; 380 $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; 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 });
176} 393}
394
177 395
178############################################################################# 396#############################################################################
179 397
180use Event; 398SDL::Init SDL_INIT_EVERYTHING;
181 399
182glinit; 400$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
183 401
184$conn = new conn 402my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
403
404$TOPLEVEL->add ($mapwidget);
405$mapwidget->focus_in;
406
407Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
408
409$CFG ||= {
410 width => 640,
411 height => 480,
412 mapsize => 100,
413 fullscreen => 0,
185 host => "cf.schmorp.de", 414 host => "crossfire.schmorp.de",
186 port => 13327; 415 port => 13327,
187
188Event->timer (after => 0, interval => 1/20, hard => 1, cb => sub {
189 while ($ev->poll) {
190 ($ev_cb{$ev->type} || sub { warn "unhandled event ", $ev->type })->();
191 }
192}); 416};
193 417
194Event::loop; 418Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
195 419
420$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
196 421
422run_config_dialog
423 login => sub { start_game },
424 logout => sub { stop_game };
425
426main Gtk2;
427
428Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines