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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines