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.55 by root, Sun Apr 9 22:27:52 2006 UTC vs.
Revision 1.84 by root, Wed Apr 12 15:46:14 2006 UTC

12use SDL::Surface; 12use SDL::Surface;
13use SDL::OpenGL; 13use SDL::OpenGL;
14use SDL::OpenGL::Constants; 14use SDL::OpenGL::Constants;
15 15
16use Crossfire; 16use Crossfire;
17use Crossfire::Client;
18use Crossfire::Protocol; 17use Crossfire::Protocol;
19 18
20use Crossfire::Client::Widget; 19use CFClient;
20use CFClient::UI;
21
22our $VERSION = '0.1';
23
24my $MAX_FPS = 60;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
21 26
22our $FACECACHE; 27our $FACECACHE;
23
24our $VERSION = '0.1';
25
26our %GL_EXT;
27 28
28our $CFG; 29our $CFG;
29our $CONN; 30our $CONN;
31our $FAST; # fast, low-quality mode
30 32
33our @SDL_MODES;
31our $WIDTH; 34our $WIDTH;
32our $HEIGHT; 35our $HEIGHT;
33our $FULLSCREEN; 36our $FULLSCREEN;
34 37
38our $NOW;
39
40our $MAPWIDGET;
35our $FONTSIZE; 41our $FONTSIZE;
36our $FOCUS;
37our $HOVER;
38 42
39our $SDL_TIMER; 43our $SDL_TIMER;
40our $SDL_APP; 44our $SDL_APP;
41our $SDL_EV = new SDL::Event; 45our $SDL_EV;
42our %SDL_CB; 46our %SDL_CB;
43
44our @GL_INIT; # hooks called on every gl init
45 47
46our $ALT_ENTER_MESSAGE; 48our $ALT_ENTER_MESSAGE;
47our $STATUS_LINE; 49our $STATUS_LINE;
48 50our $DEBUG_STATUS;
49our $TOPLEVEL;
50
51our $tw; # Test widget #d#
52 51
53my $last_refresh; 52my $last_refresh;
54my %ANIMATE; 53my %ANIMATE;
55my $refresh_handler; 54my $refresh_handler;
56 55
56sub status {
57 $STATUS_LINE->set_text ($_[0]);
58 my ($w, $h) = $STATUS_LINE->size_request;
59 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h);
60}
61
62sub debug {
63 $DEBUG_STATUS->set_text ($_[0]);
64 my ($w, $h) = $DEBUG_STATUS->size_request;
65 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
66}
67
68sub start_game {
69 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
70
71 $CONN = new conn
72 host => $CFG->{host},
73 port => $CFG->{port},
74 user => $CFG->{user},
75 pass => $CFG->{password},
76 mapw => $mapsize,
77 maph => $mapsize,
78 ;
79
80 CFClient::lowdelay fileno $CONN->{fh};
81}
82
83sub stop_game {
84 undef $CONN;
85}
86
87sub config_dialog {
88 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
89 child => (my $vbox = new CFClient::UI::VBox);
90 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
91 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
92
93 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
94 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
95
96 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
97 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
98
99 $mode_slider->connect (changed => sub {
100 my ($self, $value) = @_;
101
102 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
103 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
104 });
105 $mode_slider->emit (changed => $mode_slider->{range}[0]);
106
107 $table->add (1, 1, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
108 destroy_screen ();
109 init_screen ();
110 });
111
112 $vbox->add (new CFClient::UI::Label align => 0, text => "Server");
113 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
114 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
115 $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
116
117 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
118 $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
119
120 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
121 $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
122
123 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
124 $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
125
126 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
127 $table->add (1, 6, new CFClient::UI::Slider
128 req_w => 100,
129 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
130 connect_changed => sub {
131 my ($self, $value) = @_;
132
133 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
134 },
135 );
136
137 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
138 start_game;
139 });
140
141 $vbox->add (my $hbox = new CFClient::UI::HBox);
142
143 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
144 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
145 status "Configuration Saved";
146 });
147 $CFClient::UI::TOPLEVEL->add ($dialog);
148}
149
57sub init_screen { 150sub init_screen {
151 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
152 $FULLSCREEN = $CFG->{fullscreen};
153
58 $SDL_APP = new SDL::App 154 $SDL_APP = new SDL::App
59 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 155 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
60 -title => "Crossfire+ Client", 156 -title => "Crossfire+ Client",
61 -width => $WIDTH, 157 -width => $WIDTH,
62 -height => $HEIGHT, 158 -height => $HEIGHT,
67 -alpha_size => 0, 163 -alpha_size => 0,
68 -double_buffer => 1, 164 -double_buffer => 1,
69 -fullscreen => $FULLSCREEN, 165 -fullscreen => $FULLSCREEN,
70 -resizeable => 0; 166 -resizeable => 0;
71 167
72 $last_refresh = SDL::GetTicks; 168 $SDL_EV = new SDL::Event;
169 $SDL_EV->set_unicode (1);
73 170
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
85 glEnable GL_TEXTURE_2D;
86 glEnable GL_COLOR_MATERIAL;
87 glShadeModel GL_FLAT;
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 { 171 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
135 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->() 172 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
136 while $SDL_EV->poll; 173 while $SDL_EV->poll;
137 174
138 1 175 1
139 }; 176 };
140 177
141 $WIDTH = $CFG->{width}; 178 $last_refresh = SDL::GetTicks;
142 $HEIGHT = $CFG->{height};
143 $FULLSCREEN = 0;
144 179
145 init_screen; 180 CFClient::gl_init;
146 181
147 my $mapsize = List::Util::min 64, List::Util::max 11, int $HEIGHT * $CFG->{mapsize} * 0.01 / 32; 182 $FONTSIZE = int $HEIGHT / 40;
148 183
149 $CONN = new conn 184 #############################################################################
150 host => $CFG->{host}, 185
151 port => $CFG->{port}, 186 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
152 user => $CFG->{user}, 187 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
153 pass => $CFG->{password},
154 mapw => $mapsize,
155 maph => $mapsize,
156 ; 188
189 $STATUS_LINE = new CFClient::UI::Label
190 padding => 0,
191 y => $HEIGHT * 49 / 50 - $FONTSIZE;
192 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
157 193
158 Crossfire::Client::lowdelay fileno $CONN->{fh}; 194 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
159} 195 padding => 0,
196 y => $HEIGHT * 49 / 50,
197 height => $HEIGHT / 50,
198 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
199 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
160 200
161sub stop_game { 201 $MAPWIDGET = new CFClient::UI::MapWidget;
202 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
203 $MAPWIDGET->focus_in;
204
205 config_dialog;
206}
207
208sub destroy_screen {
209 $CFClient::UI::TOPLEVEL->{children} = [];
162 remove Glib::Source $SDL_TIMER; 210 remove Glib::Source $SDL_TIMER;
163 remove Glib::Source $refresh_handler if $refresh_handler; 211 remove Glib::Source $refresh_handler if $refresh_handler;
164 undef $refresh_handler; 212 undef $refresh_handler;
165
166 undef $SDL_APP; 213 undef $SDL_APP;
167 undef $CONN; 214 undef $SDL_EV;
168 SDL::Quit; 215 SDL::Quit;
169} 216}
170
171 217
172sub force_refresh { 218sub force_refresh {
173 glViewport 0, 0, $WIDTH, $HEIGHT; 219 glViewport 0, 0, $WIDTH, $HEIGHT;
174 220
175 glMatrixMode GL_PROJECTION; 221 glMatrixMode GL_PROJECTION;
176 glLoadIdentity; 222 glLoadIdentity;
177 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000; 223 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
178 glMatrixMode GL_MODELVIEW; 224 glMatrixMode GL_MODELVIEW;
225 glLoadIdentity;
179 226
180 glClear GL_COLOR_BUFFER_BIT; 227 glClear GL_COLOR_BUFFER_BIT;
181 228
182 $TOPLEVEL->draw; 229 $CFClient::UI::TOPLEVEL->draw;
183 230
184 SDL::GLSwapBuffers; 231 SDL::GLSwapBuffers;
185} 232}
233
234my $FPS;
186 235
187sub refresh { 236sub refresh {
188 $refresh_handler ||= add Glib::Idle sub { 237 $refresh_handler ||= add Glib::Idle sub {
189 return unless $SDL_APP; 238 if ($SDL_APP) {
239 $NOW = SDL::GetTicks;
190 240
191 my $next_refresh = SDL::GetTicks; 241 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
242 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
243 $NOW = SDL::GetTicks;
244 }
245
192 my $interval = ($next_refresh - $last_refresh) * 0.001; 246 my $interval = ($NOW - $last_refresh) * 0.001;
193 $last_refresh = $next_refresh; 247 $last_refresh = $NOW;
194 248
249 if ($interval) {
250 $FPS ||= 1 / $interval;
251 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
252 debug sprintf "%5.02f", $FPS;
253 }
254
195 force_refresh; 255 force_refresh;
196 $_->animate ($interval) for grep $_, values %ANIMATE; 256 $_->animate ($interval) for grep $_, values %ANIMATE;
197 257
198 if (%ANIMATE) { 258 if (%ANIMATE) {
259 1
260 } else {
261 undef $refresh_handler;
262 0
199 1 263 }
200 } else { 264 } else {
201 undef $refresh_handler; 265 undef $refresh_handler;
202 0 266 0
203 } 267 }
204 }; 268 };
228 }, 292 },
229 SDL_KEYDOWN() => sub { 293 SDL_KEYDOWN() => sub {
230 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) { 294 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
231 # alt-enter 295 # alt-enter
232 $FULLSCREEN = !$FULLSCREEN; 296 $FULLSCREEN = !$FULLSCREEN;
297 destroy_screen;
233 init_screen; 298 init_screen;
234 } else { 299 } else {
235 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV); 300 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
236 } 301 }
237 }, 302 },
238 SDL_KEYUP() => sub { 303 SDL_KEYUP() => sub {
239 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV); 304 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
240 }, 305 },
241 SDL_MOUSEMOTION() => sub { 306 SDL_MOUSEMOTION() => sub {
242 my ($x, $y) = ($SDL_EV->motion_x, $SDL_EV->motion_y); 307 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
243 $HOVER = $TOPLEVEL->find_widget ($x, $y);
244 $self->refresh;
245
246 warn "mouse $x, $y = $HOVER\n";
247 }, 308 },
248 SDL_MOUSEBUTTONDOWN() => sub { 309 SDL_MOUSEBUTTONDOWN() => sub {
249 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV); 310 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
250 }, 311 },
251 SDL_MOUSEBUTTONUP() => sub { 312 SDL_MOUSEBUTTONUP() => sub {
252 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV); 313 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
253 }, 314 },
254 SDL_ACTIVEEVENT() => sub { 315 SDL_ACTIVEEVENT() => sub {
255 warn "active\n";#d# 316# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
256 }, 317 },
257); 318);
258 319
259@conn::ISA = Crossfire::Protocol::; 320@conn::ISA = Crossfire::Protocol::;
260 321
261sub conn::map_update { 322sub conn::map_update {
262 my ($self, $dirty) = @_; 323 my ($self, $dirty) = @_;
263 324
264 refresh; 325 $MAPWIDGET->update;
265} 326}
266 327
267sub conn::map_scroll { 328sub conn::map_scroll {
268 my ($self, $dx, $dy) = @_; 329 my ($self, $dx, $dy) = @_;
269 330
285sub conn::face_update { 346sub conn::face_update {
286 my ($self, $face) = @_; 347 my ($self, $face) = @_;
287 348
288 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image}; 349 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
289 350
290 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image}; 351 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
291} 352}
292 353
293sub conn::query { 354sub conn::query {
294 my ($self, $flags, $prompt) = @_; 355 my ($self, $flags, $prompt) = @_;
295 356
331 [qw/Password password/], 392 [qw/Password password/],
332 ); 393 );
333 394
334 my $cfg = {}; 395 my $cfg = {};
335 396
336 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
337 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
338
339 $w->add (my $vb = Gtk2::VBox->new); 397 $w->add (my $vb = Gtk2::VBox->new);
340 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0); 398 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
341 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; 399 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
342 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); 400 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
343 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1); 401 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
344 my $i = 0; 402 my $i = 0;
345 my $act = 0; 403 my $act = 0;
346 for (map { "$_->[0]x$_->[1]" } reverse @modes) { 404 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
347 if ($_ eq $selmode) { $act = $i } 405 if ($_ eq $selmode) { $act = $i }
348 $cb->append_text ($_); 406 $cb->append_text ($_);
349 $i++; 407 $i++;
350 } 408 }
351 $cb->set_active ($act); 409 $cb->set_active ($act);
368 $cb->signal_connect (clicked => sub { 426 $cb->signal_connect (clicked => sub {
369 for (keys %$cfg) { 427 for (keys %$cfg) {
370 $::CFG->{$_} = $cfg->{$_} 428 $::CFG->{$_} = $cfg->{$_}
371 if $_ ne '_i'; 429 if $_ ne '_i';
372 } 430 }
373 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc"; 431 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
374 }); 432 });
375 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5); 433 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
376 $cb->signal_connect (clicked => sub { 434 $cb->signal_connect (clicked => sub {
377 for (keys %$cfg) { 435 for (keys %$cfg) {
378 $::CFG->{$_} = $cfg->{$_} 436 $::CFG->{$_} = $cfg->{$_}
397 455
398############################################################################# 456#############################################################################
399 457
400SDL::Init SDL_INIT_EVERYTHING; 458SDL::Init SDL_INIT_EVERYTHING;
401 459
402$TOPLEVEL = Crossfire::Client::Widget::Toplevel->new; 460@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
461 @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
403 462
404my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
405
406$TOPLEVEL->add ($mapwidget);
407$mapwidget->focus_in;
408
409Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc"; 463CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
410 464
411$CFG ||= { 465$CFG ||= {
412 width => 640, 466 width => 640,
413 height => 480, 467 height => 480,
468 fullscreen => 0,
469 sdl_mode => 0,
414 mapsize => 100, 470 mapsize => 100,
415 fullscreen => 0,
416 host => "crossfire.schmorp.de", 471 host => "crossfire.schmorp.de",
417 port => 13327, 472 port => 13327,
418}; 473};
419 474
420Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf"; 475{
476 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
477
478 CFClient::add_font $_ for @fonts;
479 CFClient::set_font $fonts[0];
480}
421 481
422$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 482$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
423 483
424run_config_dialog 484init_screen;
425 login => sub { start_game },
426 logout => sub { stop_game };
427 485
428main Gtk2; 486main Gtk2;
429 487
430Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces"; 488Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
489

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines