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.68 by root, Tue Apr 11 13:38:22 2006 UTC vs.
Revision 1.84 by root, Wed Apr 12 15:46:14 2006 UTC

15 15
16use Crossfire; 16use Crossfire;
17use Crossfire::Protocol; 17use Crossfire::Protocol;
18 18
19use CFClient; 19use CFClient;
20use CFClient::Widget; 20use CFClient::UI;
21 21
22our $VERSION = '0.1'; 22our $VERSION = '0.1';
23 23
24my $MAX_FPS = 30; 24my $MAX_FPS = 60;
25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame 25my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26 26
27our $FACECACHE; 27our $FACECACHE;
28 28
29our $CFG; 29our $CFG;
30our $CONN; 30our $CONN;
31our $FAST; # fast, low-quality mode
31 32
33our @SDL_MODES;
32our $WIDTH; 34our $WIDTH;
33our $HEIGHT; 35our $HEIGHT;
34our $FULLSCREEN; 36our $FULLSCREEN;
35 37
38our $NOW;
39
40our $MAPWIDGET;
36our $FONTSIZE; 41our $FONTSIZE;
37 42
38our $SDL_TIMER; 43our $SDL_TIMER;
39our $SDL_APP; 44our $SDL_APP;
40our $SDL_EV; 45our $SDL_EV;
46 51
47my $last_refresh; 52my $last_refresh;
48my %ANIMATE; 53my %ANIMATE;
49my $refresh_handler; 54my $refresh_handler;
50 55
51our ($tw, $te); # Test widget #d# 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}
52 149
53sub init_screen { 150sub init_screen {
151 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
152 $FULLSCREEN = $CFG->{fullscreen};
153
54 $SDL_APP = new SDL::App 154 $SDL_APP = new SDL::App
55 -flags => SDL_ANYFORMAT | SDL_HWSURFACE, 155 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
56 -title => "Crossfire+ Client", 156 -title => "Crossfire+ Client",
57 -width => $WIDTH, 157 -width => $WIDTH,
58 -height => $HEIGHT, 158 -height => $HEIGHT,
66 -resizeable => 0; 166 -resizeable => 0;
67 167
68 $SDL_EV = new SDL::Event; 168 $SDL_EV = new SDL::Event;
69 $SDL_EV->set_unicode (1); 169 $SDL_EV->set_unicode (1);
70 170
71 $SDL_TIMER = add Glib::Timeout 1000/50, sub { 171 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
72 ($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 })->()
73 while $SDL_EV->poll; 173 while $SDL_EV->poll;
74 174
75 1 175 1
76 }; 176 };
77 177
78 $last_refresh = SDL::GetTicks; 178 $last_refresh = SDL::GetTicks;
79 179
80 CFClient::gl_init; 180 CFClient::gl_init;
81 181
82 $FONTSIZE = int $HEIGHT / 50; 182 $FONTSIZE = int $HEIGHT / 40;
83 183
84 ############################################################################# 184 #############################################################################
85 185
86 glClearColor 0.45, 0.45, 0.45, 1;
87
88 glEnable GL_TEXTURE_2D;
89 glEnable GL_COLOR_MATERIAL;
90 glShadeModel GL_FLAT;
91 glDisable GL_DEPTH_TEST;
92 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
93
94 #############################################################################
95
96 $DEBUG_STATUS = new CFClient::Widget::Label; 186 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
97 $CFClient::Widget::TOPLEVEL->add ($DEBUG_STATUS); 187 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
98 188
99 $STATUS_LINE = new CFClient::Widget::Label 189 $STATUS_LINE = new CFClient::UI::Label
190 padding => 0,
100 y => $HEIGHT * 59 / 60 - $FONTSIZE; 191 y => $HEIGHT * 49 / 50 - $FONTSIZE;
101 $CFClient::Widget::TOPLEVEL->add ($STATUS_LINE); 192 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
102 193
103 $ALT_ENTER_MESSAGE = new CFClient::Widget::Label 194 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
195 padding => 0,
104 y => $HEIGHT * 59 / 60, 196 y => $HEIGHT * 49 / 50,
105 height => $HEIGHT / 60, 197 height => $HEIGHT / 50,
106 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode"; 198 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
107 $CFClient::Widget::TOPLEVEL->add ($ALT_ENTER_MESSAGE); 199 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
108 200
109 # Test code #d# 201 $MAPWIDGET = new CFClient::UI::MapWidget;
110 unless ($tw) { # haha...
111 $te = new CFClient::Widget::FancyFrame x => 300, z => 1;
112 $te->add (new CFClient::Widget::Entry);
113 $CFClient::Widget::TOPLEVEL->add ($te);
114
115 $tw = new CFClient::Widget::Animator x => $WIDTH - 200, w => 400, h => 300;
116 my $lbl1 = new CFClient::Widget::Label text => "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
117 my $lbl2 = new CFClient::Widget::Label text => "LBL2";
118 my $vb = new CFClient::Widget::VBox;
119 my $f = new CFClient::Widget::FancyFrame;
120 my $f2 = new CFClient::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->moveto (0, 0);
128 $CFClient::Widget::TOPLEVEL->add ($tw);
129
130# $f->move ($WIDTH - 200, 0);
131# $CFClient::Widget::TOPLEVEL->add ($f); 202 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
132 } 203 $MAPWIDGET->focus_in;
204
205 config_dialog;
133} 206}
134 207
135sub destroy_screen { 208sub destroy_screen {
209 $CFClient::UI::TOPLEVEL->{children} = [];
136 remove Glib::Source $SDL_TIMER; 210 remove Glib::Source $SDL_TIMER;
211 remove Glib::Source $refresh_handler if $refresh_handler;
212 undef $refresh_handler;
137 undef $SDL_APP; 213 undef $SDL_APP;
138 undef $SDL_EV; 214 undef $SDL_EV;
139 SDL::Quit; 215 SDL::Quit;
140}
141
142sub start_game {
143 $WIDTH = $CFG->{width};
144 $HEIGHT = $CFG->{height};
145 $FULLSCREEN = 0;
146
147 init_screen;
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} 216}
170 217
171sub force_refresh { 218sub force_refresh {
172 glViewport 0, 0, $WIDTH, $HEIGHT; 219 glViewport 0, 0, $WIDTH, $HEIGHT;
173 220
174 glMatrixMode GL_PROJECTION; 221 glMatrixMode GL_PROJECTION;
175 glLoadIdentity; 222 glLoadIdentity;
176 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000; 223 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 glMatrixMode GL_MODELVIEW; 224 glMatrixMode GL_MODELVIEW;
225 glLoadIdentity;
178 226
179 glClear GL_COLOR_BUFFER_BIT; 227 glClear GL_COLOR_BUFFER_BIT;
180 228
181 $CFClient::Widget::TOPLEVEL->draw; 229 $CFClient::UI::TOPLEVEL->draw;
182 230
183 SDL::GLSwapBuffers; 231 SDL::GLSwapBuffers;
184}
185
186sub debug {
187 $DEBUG_STATUS->set_text ($_[0]);
188 $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
189 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
190} 232}
191 233
192my $FPS; 234my $FPS;
193 235
194sub refresh { 236sub refresh {
195 $refresh_handler ||= add Glib::Idle sub { 237 $refresh_handler ||= add Glib::Idle sub {
196 if ($SDL_APP) { 238 if ($SDL_APP) {
197 my $next_refresh = SDL::GetTicks; 239 $NOW = SDL::GetTicks;
198 240
199 if ($next_refresh - $last_refresh < $TICKS_PER_FRAME) { 241 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
200 SDL::Delay $TICKS_PER_FRAME - ($next_refresh - $last_refresh); 242 SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
201 $next_refresh = SDL::GetTicks; 243 $NOW = SDL::GetTicks;
202 } 244 }
203 245
204 my $interval = ($next_refresh - $last_refresh) * 0.001; 246 my $interval = ($NOW - $last_refresh) * 0.001;
205 $last_refresh = $next_refresh; 247 $last_refresh = $NOW;
206 248
207 if ($interval) { 249 if ($interval) {
208 $FPS ||= 1 / $interval; 250 $FPS ||= 1 / $interval;
209 $FPS = $FPS * 0.96 + (1 / $interval) * 0.04; 251 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
210 debug sprintf "%5.02f", $FPS; 252 debug sprintf "%5.02f", $FPS;
211 } 253 }
212 254
213 force_refresh; 255 force_refresh;
214 $_->animate ($interval) for grep $_, values %ANIMATE; 256 $_->animate ($interval) for grep $_, values %ANIMATE;
250 }, 292 },
251 SDL_KEYDOWN() => sub { 293 SDL_KEYDOWN() => sub {
252 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) {
253 # alt-enter 295 # alt-enter
254 $FULLSCREEN = !$FULLSCREEN; 296 $FULLSCREEN = !$FULLSCREEN;
297 destroy_screen;
255 init_screen; 298 init_screen;
256 } else { 299 } else {
257 CFClient::Widget::feed_sdl_key_down_event ($SDL_EV); 300 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
258 } 301 }
259 }, 302 },
260 SDL_KEYUP() => sub { 303 SDL_KEYUP() => sub {
261 CFClient::Widget::feed_sdl_key_up_event ($SDL_EV); 304 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
262 }, 305 },
263 SDL_MOUSEMOTION() => sub { 306 SDL_MOUSEMOTION() => sub {
264 CFClient::Widget::feed_sdl_motion_event ($SDL_EV); 307 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
265 }, 308 },
266 SDL_MOUSEBUTTONDOWN() => sub { 309 SDL_MOUSEBUTTONDOWN() => sub {
267 CFClient::Widget::feed_sdl_button_down_event ($SDL_EV); 310 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
268 }, 311 },
269 SDL_MOUSEBUTTONUP() => sub { 312 SDL_MOUSEBUTTONUP() => sub {
270 CFClient::Widget::feed_sdl_button_up_event ($SDL_EV); 313 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
271 }, 314 },
272 SDL_ACTIVEEVENT() => sub { 315 SDL_ACTIVEEVENT() => sub {
273 printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d# 316# printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
274 }, 317 },
275); 318);
276 319
277@conn::ISA = Crossfire::Protocol::; 320@conn::ISA = Crossfire::Protocol::;
278 321
279sub conn::map_update { 322sub conn::map_update {
280 my ($self, $dirty) = @_; 323 my ($self, $dirty) = @_;
281 324
282 refresh; 325 $MAPWIDGET->update;
283} 326}
284 327
285sub conn::map_scroll { 328sub conn::map_scroll {
286 my ($self, $dx, $dy) = @_; 329 my ($self, $dx, $dy) = @_;
287 330
349 [qw/Password password/], 392 [qw/Password password/],
350 ); 393 );
351 394
352 my $cfg = {}; 395 my $cfg = {};
353 396
354 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
355 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
356
357 $w->add (my $vb = Gtk2::VBox->new); 397 $w->add (my $vb = Gtk2::VBox->new);
358 $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);
359 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height}; 399 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
360 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1); 400 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
361 $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);
362 my $i = 0; 402 my $i = 0;
363 my $act = 0; 403 my $act = 0;
364 for (map { "$_->[0]x$_->[1]" } reverse @modes) { 404 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
365 if ($_ eq $selmode) { $act = $i } 405 if ($_ eq $selmode) { $act = $i }
366 $cb->append_text ($_); 406 $cb->append_text ($_);
367 $i++; 407 $i++;
368 } 408 }
369 $cb->set_active ($act); 409 $cb->set_active ($act);
415 455
416############################################################################# 456#############################################################################
417 457
418SDL::Init SDL_INIT_EVERYTHING; 458SDL::Init SDL_INIT_EVERYTHING;
419 459
420my $mapwidget = CFClient::Widget::MapWidget->new; 460@SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
421 461 @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
422$CFClient::Widget::TOPLEVEL->add ($mapwidget);
423$mapwidget->focus_in;
424 462
425CFClient::read_cfg "$Crossfire::VARDIR/pclientrc"; 463CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
426 464
427$CFG ||= { 465$CFG ||= {
428 width => 640, 466 width => 640,
429 height => 480, 467 height => 480,
468 fullscreen => 0,
469 sdl_mode => 0,
430 mapsize => 100, 470 mapsize => 100,
431 fullscreen => 0,
432 host => "crossfire.schmorp.de", 471 host => "crossfire.schmorp.de",
433 port => 13327, 472 port => 13327,
434}; 473};
435 474
436{ 475{
440 CFClient::set_font $fonts[0]; 479 CFClient::set_font $fonts[0];
441} 480}
442 481
443$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {}; 482$FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
444 483
445run_config_dialog 484init_screen;
446 login => sub { start_game },
447 logout => sub { stop_game };
448 485
449main Gtk2; 486main Gtk2;
450 487
451Crossfire::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