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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines