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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines