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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines