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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines