ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.86
Committed: Wed Apr 12 18:09:22 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.85: +28 -17 lines
Log Message:
less SDL more SDL

File Contents

# User Rev Content
1 root 1.1 #!/opt/bin/perl
2 root 1.25
3 root 1.2 use strict;
4 root 1.25 use utf8;
5 root 1.2
6 root 1.13 use Glib;
7     use Gtk2 -init;
8    
9     use SDL;
10     use SDL::App;
11     use SDL::Event;
12     use SDL::Surface;
13     use SDL::OpenGL;
14    
15 elmex 1.11 use Crossfire;
16 root 1.2 use Crossfire::Protocol;
17    
18 root 1.67 use CFClient;
19 root 1.72 use CFClient::UI;
20 elmex 1.10
21 root 1.63 our $VERSION = '0.1';
22    
23 root 1.75 my $MAX_FPS = 60;
24 root 1.85 my $MIN_FPS = 5; # unused
25 root 1.63 my $TICKS_PER_FRAME = int 1000 / $MAX_FPS - 1; # min ticks per frame
26    
27 root 1.19 our $FACECACHE;
28    
29 elmex 1.10 our $CFG;
30 root 1.13 our $CONN;
31 root 1.85 our $FAST; # fast, low-quality mode, possibly useful for software-rendering
32 root 1.2
33 root 1.75 our @SDL_MODES;
34 root 1.30 our $WIDTH;
35     our $HEIGHT;
36     our $FULLSCREEN;
37    
38 root 1.71 our $NOW;
39    
40 root 1.69 our $MAPWIDGET;
41 root 1.39 our $FONTSIZE;
42 root 1.57
43 root 1.13 our $SDL_TIMER;
44 root 1.86 our $SDL_ACTIVE;
45 root 1.58 our $SDL_EV;
46 root 1.13 our %SDL_CB;
47 root 1.18
48 root 1.30 our $ALT_ENTER_MESSAGE;
49 root 1.51 our $STATUS_LINE;
50 root 1.64 our $DEBUG_STATUS;
51 root 1.30
52 root 1.45 my $last_refresh;
53 root 1.50 my %ANIMATE;
54     my $refresh_handler;
55 root 1.45
56 root 1.82 sub 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    
62     sub 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    
68 root 1.84 sub start_game {
69 root 1.85 status "logging in...";
70    
71 root 1.84 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
72    
73     $CONN = new conn
74     host => $CFG->{host},
75     port => $CFG->{port},
76     user => $CFG->{user},
77     pass => $CFG->{password},
78     mapw => $mapsize,
79     maph => $mapsize,
80     ;
81    
82 root 1.85 status "login successful";
83    
84 root 1.84 CFClient::lowdelay fileno $CONN->{fh};
85     }
86    
87     sub stop_game {
88     undef $CONN;
89     }
90    
91 root 1.81 sub config_dialog {
92     my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
93     child => (my $vbox = new CFClient::UI::VBox);
94 root 1.82 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
95 root 1.81 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
96    
97     $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
98     $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
99    
100     $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
101     $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
102    
103     $mode_slider->connect (changed => sub {
104     my ($self, $value) = @_;
105    
106     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
107     $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
108     });
109     $mode_slider->emit (changed => $mode_slider->{range}[0]);
110 root 1.82
111 root 1.85 $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen");
112     $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
113     my ($self, $value) = @_;
114     $CFG->{fullscreen} = $value;
115     });
116    
117     $table->add (1, 2, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
118 root 1.83 destroy_screen ();
119     init_screen ();
120 root 1.82 });
121 root 1.81
122 root 1.82 $vbox->add (new CFClient::UI::Label align => 0, text => "Server");
123     $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
124     $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
125     $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
126 root 1.81
127 root 1.82 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
128     $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
129 root 1.81
130 root 1.82 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
131     $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
132 root 1.81
133 root 1.82 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
134     $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
135 root 1.81
136 root 1.82 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
137     $table->add (1, 6, new CFClient::UI::Slider
138 root 1.81 req_w => 100,
139     range => [$CFG->{mapsize}, 10, 100 + 1, 1],
140     connect_changed => sub {
141     my ($self, $value) = @_;
142    
143     $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
144     },
145     );
146    
147 root 1.82 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
148 root 1.84 start_game;
149 root 1.82 });
150    
151 root 1.81 $vbox->add (my $hbox = new CFClient::UI::HBox);
152 root 1.82
153 root 1.81 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
154 root 1.84 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
155 root 1.82 status "Configuration Saved";
156 root 1.81 });
157     $CFClient::UI::TOPLEVEL->add ($dialog);
158     }
159 root 1.58
160 root 1.13 sub init_screen {
161 root 1.84 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
162     $FULLSCREEN = $CFG->{fullscreen};
163    
164 root 1.86 SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO
165     and die "SDL::Init failed!\n";
166    
167     SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
168     SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
169     SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
170     SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0;
171    
172     SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
173     SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
174     SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
175     SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
176    
177     SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1;
178     SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15;
179     SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0;
180    
181     SDL::SetVideoMode $WIDTH, $HEIGHT, 0,
182     SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF
183     | ($FULLSCREEN ? SDL_FULLSCREEN : 0)
184     or die "SDL::SetVideoMode failed!\n";
185    
186     SDL::WMSetCaption "Crossfire+ Client", "Crossfire+";
187 root 1.2
188 root 1.58 $SDL_EV = new SDL::Event;
189     $SDL_EV->set_unicode (1);
190    
191 root 1.86 $SDL_ACTIVE = 1;
192    
193 root 1.76 $SDL_TIMER = add Glib::Timeout 1000 / $MAX_FPS, sub {
194 root 1.62 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
195     while $SDL_EV->poll;
196    
197     1
198     };
199    
200 root 1.45 $last_refresh = SDL::GetTicks;
201    
202 root 1.67 CFClient::gl_init;
203 root 1.30
204 root 1.77 $FONTSIZE = int $HEIGHT / 40;
205 root 1.39
206 root 1.52 #############################################################################
207    
208 root 1.77 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
209 root 1.72 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
210 root 1.52
211 root 1.72 $STATUS_LINE = new CFClient::UI::Label
212 root 1.77 padding => 0,
213     y => $HEIGHT * 49 / 50 - $FONTSIZE;
214 root 1.72 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
215 root 1.51
216 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
217 root 1.77 padding => 0,
218     y => $HEIGHT * 49 / 50,
219     height => $HEIGHT / 50,
220 root 1.68 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
221 root 1.72 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
222 root 1.30
223 root 1.72 $MAPWIDGET = new CFClient::UI::MapWidget;
224     $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
225 root 1.69 $MAPWIDGET->focus_in;
226 root 1.81
227     config_dialog;
228 root 1.2 }
229    
230 root 1.62 sub destroy_screen {
231 root 1.81 $CFClient::UI::TOPLEVEL->{children} = [];
232 root 1.62 remove Glib::Source $SDL_TIMER;
233 root 1.84 remove Glib::Source $refresh_handler if $refresh_handler;
234     undef $refresh_handler;
235 root 1.86 undef $SDL_ACTIVE;
236 root 1.62 undef $SDL_EV;
237     SDL::Quit;
238     }
239    
240 root 1.30 sub force_refresh {
241     glViewport 0, 0, $WIDTH, $HEIGHT;
242 root 1.35
243     glMatrixMode GL_PROJECTION;
244     glLoadIdentity;
245 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
246 root 1.35 glMatrixMode GL_MODELVIEW;
247 root 1.73 glLoadIdentity;
248 root 1.35
249 root 1.5 glClear GL_COLOR_BUFFER_BIT;
250 root 1.2
251 root 1.72 $CFClient::UI::TOPLEVEL->draw;
252 root 1.1
253 root 1.2 SDL::GLSwapBuffers;
254 root 1.1 }
255    
256 root 1.64 my $FPS;
257    
258 root 1.30 sub refresh {
259     $refresh_handler ||= add Glib::Idle sub {
260 root 1.86 if ($SDL_ACTIVE) {
261 root 1.71 $NOW = SDL::GetTicks;
262 root 1.63
263 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
264     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
265     $NOW = SDL::GetTicks;
266 root 1.63 }
267    
268 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
269     $last_refresh = $NOW;
270 root 1.58
271 root 1.64 if ($interval) {
272     $FPS ||= 1 / $interval;
273 root 1.77 $FPS = $FPS * 0.9 + (1 / $interval) * 0.1;
274 root 1.66 debug sprintf "%5.02f", $FPS;
275 root 1.64 }
276    
277 root 1.58 force_refresh;
278     $_->animate ($interval) for grep $_, values %ANIMATE;
279    
280     if (%ANIMATE) {
281     1
282     } else {
283     undef $refresh_handler;
284     0
285     }
286 root 1.45 } else {
287     undef $refresh_handler;
288     0
289     }
290 root 1.30 };
291     }
292    
293 root 1.45 sub animation_start {
294     my ($widget) = @_;
295     $ANIMATE{$widget} = $widget;
296     Scalar::Util::weaken $ANIMATE{$widget};
297    
298     refresh;
299     }
300    
301     sub animation_stop {
302     my ($widget) = @_;
303     delete $ANIMATE{$widget};
304     }
305    
306 root 1.13 %SDL_CB = (
307     SDL_QUIT() => sub {
308 root 1.19 main_quit Gtk2;
309 root 1.13 },
310     SDL_VIDEORESIZE() => sub {
311     },
312     SDL_VIDEOEXPOSE() => sub {
313     refresh;
314     },
315     SDL_KEYDOWN() => sub {
316 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
317     # alt-enter
318 root 1.30 $FULLSCREEN = !$FULLSCREEN;
319 root 1.81 destroy_screen;
320 root 1.18 init_screen;
321     } else {
322 root 1.72 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
323 root 1.18 }
324 root 1.13 },
325     SDL_KEYUP() => sub {
326 root 1.72 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
327 root 1.13 },
328     SDL_MOUSEMOTION() => sub {
329 root 1.72 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
330 root 1.13 },
331     SDL_MOUSEBUTTONDOWN() => sub {
332 root 1.72 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
333 root 1.13 },
334     SDL_MOUSEBUTTONUP() => sub {
335 root 1.72 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
336 root 1.13 },
337     SDL_ACTIVEEVENT() => sub {
338 root 1.70 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
339 root 1.13 },
340     );
341 root 1.1
342 root 1.2 @conn::ISA = Crossfire::Protocol::;
343 root 1.1
344 root 1.2 sub conn::map_update {
345 root 1.1 my ($self, $dirty) = @_;
346    
347 root 1.69 $MAPWIDGET->update;
348 root 1.1 }
349    
350 root 1.2 sub conn::map_scroll {
351 root 1.1 my ($self, $dx, $dy) = @_;
352    
353 root 1.45 # refresh;
354 root 1.1 }
355    
356 root 1.2 sub conn::map_clear {
357 root 1.1 my ($self) = @_;
358    
359 root 1.45 # refresh;
360 root 1.1 }
361    
362 root 1.19 sub conn::face_find {
363     my ($self, $face) = @_;
364    
365     $FACECACHE->{"$face->{chksum},$face->{name}"}
366     }
367    
368 root 1.2 sub conn::face_update {
369 root 1.19 my ($self, $face) = @_;
370    
371     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
372 root 1.1
373 root 1.67 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
374 root 1.1 }
375    
376 root 1.33 sub conn::query {
377     my ($self, $flags, $prompt) = @_;
378    
379     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
380     }
381    
382 elmex 1.23 sub gtk_add_cfg_field {
383     my ($tbl, $cfg, $klbl, $key, $value) = @_;
384     my $i = $cfg->{_i}++;
385     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
386     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
387     if ($key eq 'password') {
388     $ent->set_invisible_char ("*");
389     $ent->set (visibility => 0)
390     }
391     $ent->set_text ($value);
392     $ent->signal_connect (changed => sub {
393     my ($ent) = @_;
394     $cfg->{$key} = $ent->get_text;
395 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
396 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
397     $cfg->{$key} = 100;
398 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
399     $cfg->{$key} = 50;
400 elmex 1.47 }
401 elmex 1.23 });
402     }
403    
404     sub run_config_dialog {
405     my (%events) = @_;
406    
407     my $w = Gtk2::Window->new;
408    
409     my @cfg = (
410     [qw/Host host/],
411     [qw/Port port/],
412 elmex 1.47 [qw/Mapsize% mapsize/],
413 elmex 1.23 [qw/Username user/],
414     [qw/Password password/],
415     );
416    
417     my $cfg = {};
418    
419     $w->add (my $vb = Gtk2::VBox->new);
420     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
421     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
422     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
423     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
424     my $i = 0;
425     my $act = 0;
426 root 1.75 for (map { "$_->[0]x$_->[1]" } @SDL_MODES) {
427 elmex 1.23 if ($_ eq $selmode) { $act = $i }
428     $cb->append_text ($_);
429     $i++;
430     }
431     $cb->set_active ($act);
432     $cb->signal_connect (changed => sub {
433     my ($cb) = @_;
434     my $txt = $cb->get_active_text;
435     if ($txt =~ m/(\d+)x(\d+)/) {
436     $::CFG->{width} = $1;
437     $::CFG->{height} = $2;
438     }
439     });
440    
441     $cfg->{_i} = 1;
442     for (@cfg) {
443     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
444     }
445    
446     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
447     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
448     $cb->signal_connect (clicked => sub {
449     for (keys %$cfg) {
450     $::CFG->{$_} = $cfg->{$_}
451     if $_ ne '_i';
452     }
453 root 1.67 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
454 elmex 1.23 });
455     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
456     $cb->signal_connect (clicked => sub {
457     for (keys %$cfg) {
458     $::CFG->{$_} = $cfg->{$_}
459     if $_ ne '_i';
460     }
461     my $cb = $events{login} || sub {};
462     $cb->($::CFG->{user}, $::CFG->{password});
463     });
464     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
465     $cb->signal_connect (clicked => sub {
466 root 1.50 my $cb = $events{logout} || sub {};
467 elmex 1.23 $cb->();
468     });
469     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
470     $cb->signal_connect (clicked => sub { $w->destroy });
471    
472     $w->show_all;
473    
474     $w->signal_connect (destroy => sub { Gtk2->main_quit });
475     }
476    
477    
478 root 1.1 #############################################################################
479    
480 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
481 elmex 1.20
482 root 1.75 @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
483     @{ SDL::ListModes 0, SDL_FULLSCREEN|SDL_HWSURFACE };
484    
485 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
486 elmex 1.10
487 root 1.13 $CFG ||= {
488     width => 640,
489     height => 480,
490 root 1.82 fullscreen => 0,
491     sdl_mode => 0,
492 elmex 1.47 mapsize => 100,
493 root 1.13 host => "crossfire.schmorp.de",
494     port => 13327,
495     };
496 elmex 1.12
497 root 1.65 {
498 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
499 root 1.65
500 root 1.67 CFClient::add_font $_ for @fonts;
501     CFClient::set_font $fonts[0];
502 root 1.65 }
503 root 1.40
504 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
505    
506 root 1.84 init_screen;
507 root 1.1
508 root 1.13 main Gtk2;
509 root 1.19
510 root 1.68 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
511 root 1.82