ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.74
Committed: Tue Apr 11 20:44:49 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.73: +8 -0 lines
Log Message:
fix(?) table

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     use SDL::OpenGL::Constants;
15    
16 elmex 1.11 use Crossfire;
17 root 1.2 use Crossfire::Protocol;
18    
19 root 1.67 use CFClient;
20 root 1.72 use CFClient::UI;
21 elmex 1.10
22 root 1.63 our $VERSION = '0.1';
23    
24     my $MAX_FPS = 30;
25     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.2
32 root 1.30 our $WIDTH;
33     our $HEIGHT;
34     our $FULLSCREEN;
35    
36 root 1.71 our $NOW;
37    
38 root 1.69 our $MAPWIDGET;
39 root 1.39 our $FONTSIZE;
40 root 1.57
41 root 1.13 our $SDL_TIMER;
42     our $SDL_APP;
43 root 1.58 our $SDL_EV;
44 root 1.13 our %SDL_CB;
45 root 1.18
46 root 1.30 our $ALT_ENTER_MESSAGE;
47 root 1.51 our $STATUS_LINE;
48 root 1.64 our $DEBUG_STATUS;
49 root 1.30
50 root 1.45 my $last_refresh;
51 root 1.50 my %ANIMATE;
52     my $refresh_handler;
53 root 1.45
54 root 1.58 our ($tw, $te); # Test widget #d#
55    
56 root 1.13 sub init_screen {
57     $SDL_APP = new SDL::App
58 root 1.5 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
59     -title => "Crossfire+ Client",
60 root 1.30 -width => $WIDTH,
61     -height => $HEIGHT,
62 root 1.5 -opengl => 1,
63 root 1.51 -red_size => 5,
64     -green_size => 5,
65     -blue_size => 5,
66     -alpha_size => 0,
67 root 1.2 -double_buffer => 1,
68 root 1.30 -fullscreen => $FULLSCREEN,
69 root 1.5 -resizeable => 0;
70 root 1.2
71 root 1.58 $SDL_EV = new SDL::Event;
72     $SDL_EV->set_unicode (1);
73    
74 root 1.62 $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 root 1.45 $last_refresh = SDL::GetTicks;
82    
83 root 1.67 CFClient::gl_init;
84 root 1.30
85 root 1.40 $FONTSIZE = int $HEIGHT / 50;
86 root 1.39
87 root 1.52 #############################################################################
88    
89 root 1.62 glClearColor 0.45, 0.45, 0.45, 1;
90 root 1.52
91     glEnable GL_TEXTURE_2D;
92     glEnable GL_COLOR_MATERIAL;
93     glShadeModel GL_FLAT;
94     glDisable GL_DEPTH_TEST;
95     glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
96    
97     #############################################################################
98 root 1.64
99 root 1.72 $DEBUG_STATUS = new CFClient::UI::Label;
100     $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
101 root 1.52
102 root 1.72 $STATUS_LINE = new CFClient::UI::Label
103 root 1.68 y => $HEIGHT * 59 / 60 - $FONTSIZE;
104 root 1.72 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
105 root 1.51
106 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
107 root 1.68 y => $HEIGHT * 59 / 60,
108     height => $HEIGHT / 60,
109     text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
110 root 1.72 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
111 root 1.30
112 root 1.72 $MAPWIDGET = new CFClient::UI::MapWidget;
113     $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
114 root 1.69 $MAPWIDGET->focus_in;
115 root 1.2 }
116    
117 root 1.62 sub destroy_screen {
118     remove Glib::Source $SDL_TIMER;
119     undef $SDL_APP;
120     undef $SDL_EV;
121     SDL::Quit;
122     }
123    
124 root 1.72 sub 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 root 1.74 $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 root 1.72
137     $CFClient::UI::TOPLEVEL->add ($dialog);
138     }
139    
140 root 1.13 sub start_game {
141 root 1.30 $WIDTH = $CFG->{width};
142     $HEIGHT = $CFG->{height};
143     $FULLSCREEN = 0;
144    
145 root 1.13 init_screen;
146    
147 root 1.72 config_dialog;
148    
149 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
150 root 1.50
151 root 1.13 $CONN = new conn
152     host => $CFG->{host},
153 root 1.28 port => $CFG->{port},
154     user => $CFG->{user},
155 root 1.37 pass => $CFG->{password},
156 root 1.50 mapw => $mapsize,
157     maph => $mapsize,
158 root 1.37 ;
159 root 1.45
160 root 1.67 CFClient::lowdelay fileno $CONN->{fh};
161 root 1.13 }
162    
163     sub stop_game {
164 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
165 root 1.50 undef $refresh_handler;
166 root 1.13
167 root 1.50 undef $CONN;
168 root 1.62 destroy_screen;
169 root 1.13 }
170    
171 root 1.30 sub force_refresh {
172     glViewport 0, 0, $WIDTH, $HEIGHT;
173 root 1.35
174     glMatrixMode GL_PROJECTION;
175     glLoadIdentity;
176 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
177 root 1.35 glMatrixMode GL_MODELVIEW;
178 root 1.73 glLoadIdentity;
179 root 1.35
180 root 1.5 glClear GL_COLOR_BUFFER_BIT;
181 root 1.2
182 root 1.72 $CFClient::UI::TOPLEVEL->draw;
183 root 1.1
184 root 1.2 SDL::GLSwapBuffers;
185 root 1.1 }
186    
187 root 1.64 sub 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    
193     my $FPS;
194    
195 root 1.30 sub refresh {
196     $refresh_handler ||= add Glib::Idle sub {
197 root 1.58 if ($SDL_APP) {
198 root 1.71 $NOW = SDL::GetTicks;
199 root 1.63
200 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
201     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
202     $NOW = SDL::GetTicks;
203 root 1.63 }
204    
205 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
206     $last_refresh = $NOW;
207 root 1.58
208 root 1.64 if ($interval) {
209     $FPS ||= 1 / $interval;
210     $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
211 root 1.66 debug sprintf "%5.02f", $FPS;
212 root 1.64 }
213    
214 root 1.58 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 root 1.45 } else {
224     undef $refresh_handler;
225     0
226     }
227 root 1.30 };
228     }
229    
230 root 1.45 sub animation_start {
231     my ($widget) = @_;
232     $ANIMATE{$widget} = $widget;
233     Scalar::Util::weaken $ANIMATE{$widget};
234    
235     refresh;
236     }
237    
238     sub animation_stop {
239     my ($widget) = @_;
240     delete $ANIMATE{$widget};
241     }
242    
243 root 1.13 %SDL_CB = (
244     SDL_QUIT() => sub {
245 root 1.19 main_quit Gtk2;
246 root 1.13 },
247     SDL_VIDEORESIZE() => sub {
248     },
249     SDL_VIDEOEXPOSE() => sub {
250     refresh;
251     },
252     SDL_KEYDOWN() => sub {
253 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
254     # alt-enter
255 root 1.30 $FULLSCREEN = !$FULLSCREEN;
256 root 1.18 init_screen;
257     } else {
258 root 1.72 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
259 root 1.18 }
260 root 1.13 },
261     SDL_KEYUP() => sub {
262 root 1.72 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
263 root 1.13 },
264     SDL_MOUSEMOTION() => sub {
265 root 1.72 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
266 root 1.13 },
267     SDL_MOUSEBUTTONDOWN() => sub {
268 root 1.72 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
269 root 1.13 },
270     SDL_MOUSEBUTTONUP() => sub {
271 root 1.72 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
272 root 1.13 },
273     SDL_ACTIVEEVENT() => sub {
274 root 1.70 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
275 root 1.13 },
276     );
277 root 1.1
278 root 1.2 @conn::ISA = Crossfire::Protocol::;
279 root 1.1
280 root 1.2 sub conn::map_update {
281 root 1.1 my ($self, $dirty) = @_;
282    
283 root 1.69 $MAPWIDGET->update;
284 root 1.1 }
285    
286 root 1.2 sub conn::map_scroll {
287 root 1.1 my ($self, $dx, $dy) = @_;
288    
289 root 1.45 # refresh;
290 root 1.1 }
291    
292 root 1.2 sub conn::map_clear {
293 root 1.1 my ($self) = @_;
294    
295 root 1.45 # refresh;
296 root 1.1 }
297    
298 root 1.19 sub conn::face_find {
299     my ($self, $face) = @_;
300    
301     $FACECACHE->{"$face->{chksum},$face->{name}"}
302     }
303    
304 root 1.2 sub conn::face_update {
305 root 1.19 my ($self, $face) = @_;
306    
307     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
308 root 1.1
309 root 1.67 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
310 root 1.1 }
311    
312 root 1.33 sub conn::query {
313     my ($self, $flags, $prompt) = @_;
314    
315     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
316     }
317    
318 elmex 1.23 sub 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 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
332 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
333     $cfg->{$key} = 100;
334 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
335     $cfg->{$key} = 50;
336 elmex 1.47 }
337 elmex 1.23 });
338     }
339    
340     sub 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 elmex 1.47 [qw/Mapsize% mapsize/],
349 elmex 1.23 [qw/Username user/],
350     [qw/Password password/],
351     );
352    
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;
377     }
378     });
379    
380     $cfg->{_i} = 1;
381     for (@cfg) {
382     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
383     }
384    
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';
391     }
392 root 1.67 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
393 elmex 1.23 });
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';
399     }
400     my $cb = $events{login} || sub {};
401     $cb->($::CFG->{user}, $::CFG->{password});
402     });
403     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
404     $cb->signal_connect (clicked => sub {
405 root 1.50 my $cb = $events{logout} || sub {};
406 elmex 1.23 $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 });
414     }
415    
416    
417 root 1.1 #############################################################################
418    
419 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
420 elmex 1.20
421 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
422 elmex 1.10
423 root 1.13 $CFG ||= {
424     width => 640,
425     height => 480,
426 elmex 1.47 mapsize => 100,
427 root 1.13 fullscreen => 0,
428     host => "crossfire.schmorp.de",
429     port => 13327,
430     };
431 elmex 1.12
432 root 1.65 {
433 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
434 root 1.65
435 root 1.67 CFClient::add_font $_ for @fonts;
436     CFClient::set_font $fonts[0];
437 root 1.65 }
438 root 1.40
439 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
440    
441 elmex 1.23 run_config_dialog
442 elmex 1.16 login => sub { start_game },
443     logout => sub { stop_game };
444 root 1.1
445 root 1.13 main Gtk2;
446 root 1.19
447 root 1.68 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";