ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.73
Committed: Tue Apr 11 19:31:19 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.72: +1 -31 lines
Log Message:
*** empty log message ***

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    
129     $CFClient::UI::TOPLEVEL->add ($dialog);
130     }
131    
132 root 1.13 sub start_game {
133 root 1.30 $WIDTH = $CFG->{width};
134     $HEIGHT = $CFG->{height};
135     $FULLSCREEN = 0;
136    
137 root 1.13 init_screen;
138    
139 root 1.72 config_dialog;
140    
141 root 1.59 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
142 root 1.50
143 root 1.13 $CONN = new conn
144     host => $CFG->{host},
145 root 1.28 port => $CFG->{port},
146     user => $CFG->{user},
147 root 1.37 pass => $CFG->{password},
148 root 1.50 mapw => $mapsize,
149     maph => $mapsize,
150 root 1.37 ;
151 root 1.45
152 root 1.67 CFClient::lowdelay fileno $CONN->{fh};
153 root 1.13 }
154    
155     sub stop_game {
156 root 1.51 remove Glib::Source $refresh_handler if $refresh_handler;
157 root 1.50 undef $refresh_handler;
158 root 1.13
159 root 1.50 undef $CONN;
160 root 1.62 destroy_screen;
161 root 1.13 }
162    
163 root 1.30 sub force_refresh {
164     glViewport 0, 0, $WIDTH, $HEIGHT;
165 root 1.35
166     glMatrixMode GL_PROJECTION;
167     glLoadIdentity;
168 root 1.59 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
169 root 1.35 glMatrixMode GL_MODELVIEW;
170 root 1.73 glLoadIdentity;
171 root 1.35
172 root 1.5 glClear GL_COLOR_BUFFER_BIT;
173 root 1.2
174 root 1.72 $CFClient::UI::TOPLEVEL->draw;
175 root 1.1
176 root 1.2 SDL::GLSwapBuffers;
177 root 1.1 }
178    
179 root 1.64 sub debug {
180     $DEBUG_STATUS->set_text ($_[0]);
181     $DEBUG_STATUS->size_allocate ($DEBUG_STATUS->size_request);
182     $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0);
183     }
184    
185     my $FPS;
186    
187 root 1.30 sub refresh {
188     $refresh_handler ||= add Glib::Idle sub {
189 root 1.58 if ($SDL_APP) {
190 root 1.71 $NOW = SDL::GetTicks;
191 root 1.63
192 root 1.71 if ($NOW - $last_refresh < $TICKS_PER_FRAME) {
193     SDL::Delay $TICKS_PER_FRAME - ($NOW - $last_refresh);
194     $NOW = SDL::GetTicks;
195 root 1.63 }
196    
197 root 1.71 my $interval = ($NOW - $last_refresh) * 0.001;
198     $last_refresh = $NOW;
199 root 1.58
200 root 1.64 if ($interval) {
201     $FPS ||= 1 / $interval;
202     $FPS = $FPS * 0.96 + (1 / $interval) * 0.04;
203 root 1.66 debug sprintf "%5.02f", $FPS;
204 root 1.64 }
205    
206 root 1.58 force_refresh;
207     $_->animate ($interval) for grep $_, values %ANIMATE;
208    
209     if (%ANIMATE) {
210     1
211     } else {
212     undef $refresh_handler;
213     0
214     }
215 root 1.45 } else {
216     undef $refresh_handler;
217     0
218     }
219 root 1.30 };
220     }
221    
222 root 1.45 sub animation_start {
223     my ($widget) = @_;
224     $ANIMATE{$widget} = $widget;
225     Scalar::Util::weaken $ANIMATE{$widget};
226    
227     refresh;
228     }
229    
230     sub animation_stop {
231     my ($widget) = @_;
232     delete $ANIMATE{$widget};
233     }
234    
235 root 1.13 %SDL_CB = (
236     SDL_QUIT() => sub {
237 root 1.19 main_quit Gtk2;
238 root 1.13 },
239     SDL_VIDEORESIZE() => sub {
240     },
241     SDL_VIDEOEXPOSE() => sub {
242     refresh;
243     },
244     SDL_KEYDOWN() => sub {
245 root 1.18 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
246     # alt-enter
247 root 1.30 $FULLSCREEN = !$FULLSCREEN;
248 root 1.18 init_screen;
249     } else {
250 root 1.72 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
251 root 1.18 }
252 root 1.13 },
253     SDL_KEYUP() => sub {
254 root 1.72 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
255 root 1.13 },
256     SDL_MOUSEMOTION() => sub {
257 root 1.72 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
258 root 1.13 },
259     SDL_MOUSEBUTTONDOWN() => sub {
260 root 1.72 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
261 root 1.13 },
262     SDL_MOUSEBUTTONUP() => sub {
263 root 1.72 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
264 root 1.13 },
265     SDL_ACTIVEEVENT() => sub {
266 root 1.70 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
267 root 1.13 },
268     );
269 root 1.1
270 root 1.2 @conn::ISA = Crossfire::Protocol::;
271 root 1.1
272 root 1.2 sub conn::map_update {
273 root 1.1 my ($self, $dirty) = @_;
274    
275 root 1.69 $MAPWIDGET->update;
276 root 1.1 }
277    
278 root 1.2 sub conn::map_scroll {
279 root 1.1 my ($self, $dx, $dy) = @_;
280    
281 root 1.45 # refresh;
282 root 1.1 }
283    
284 root 1.2 sub conn::map_clear {
285 root 1.1 my ($self) = @_;
286    
287 root 1.45 # refresh;
288 root 1.1 }
289    
290 root 1.19 sub conn::face_find {
291     my ($self, $face) = @_;
292    
293     $FACECACHE->{"$face->{chksum},$face->{name}"}
294     }
295    
296 root 1.2 sub conn::face_update {
297 root 1.19 my ($self, $face) = @_;
298    
299     $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
300 root 1.1
301 root 1.67 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
302 root 1.1 }
303    
304 root 1.33 sub conn::query {
305     my ($self, $flags, $prompt) = @_;
306    
307     warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
308     }
309    
310 elmex 1.23 sub gtk_add_cfg_field {
311     my ($tbl, $cfg, $klbl, $key, $value) = @_;
312     my $i = $cfg->{_i}++;
313     $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
314     $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
315     if ($key eq 'password') {
316     $ent->set_invisible_char ("*");
317     $ent->set (visibility => 0)
318     }
319     $ent->set_text ($value);
320     $ent->signal_connect (changed => sub {
321     my ($ent) = @_;
322     $cfg->{$key} = $ent->get_text;
323 elmex 1.49 # TODO: Mapsize should be a slider in the game gui
324 elmex 1.47 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
325     $cfg->{$key} = 100;
326 elmex 1.48 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
327     $cfg->{$key} = 50;
328 elmex 1.47 }
329 elmex 1.23 });
330     }
331    
332     sub run_config_dialog {
333     my (%events) = @_;
334    
335     my $w = Gtk2::Window->new;
336    
337     my @cfg = (
338     [qw/Host host/],
339     [qw/Port port/],
340 elmex 1.47 [qw/Mapsize% mapsize/],
341 elmex 1.23 [qw/Username user/],
342     [qw/Password password/],
343     );
344    
345     my $cfg = {};
346    
347     my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
348     my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
349    
350     $w->add (my $vb = Gtk2::VBox->new);
351     $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
352     my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
353     $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
354     $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
355     my $i = 0;
356     my $act = 0;
357     for (map { "$_->[0]x$_->[1]" } reverse @modes) {
358     if ($_ eq $selmode) { $act = $i }
359     $cb->append_text ($_);
360     $i++;
361     }
362     $cb->set_active ($act);
363     $cb->signal_connect (changed => sub {
364     my ($cb) = @_;
365     my $txt = $cb->get_active_text;
366     if ($txt =~ m/(\d+)x(\d+)/) {
367     $::CFG->{width} = $1;
368     $::CFG->{height} = $2;
369     }
370     });
371    
372     $cfg->{_i} = 1;
373     for (@cfg) {
374     gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
375     }
376    
377     $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
378     $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
379     $cb->signal_connect (clicked => sub {
380     for (keys %$cfg) {
381     $::CFG->{$_} = $cfg->{$_}
382     if $_ ne '_i';
383     }
384 root 1.67 CFClient::write_cfg "$CFrossfire::VARDIR/pclientrc";
385 elmex 1.23 });
386     $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
387     $cb->signal_connect (clicked => sub {
388     for (keys %$cfg) {
389     $::CFG->{$_} = $cfg->{$_}
390     if $_ ne '_i';
391     }
392     my $cb = $events{login} || sub {};
393     $cb->($::CFG->{user}, $::CFG->{password});
394     });
395     $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
396     $cb->signal_connect (clicked => sub {
397 root 1.50 my $cb = $events{logout} || sub {};
398 elmex 1.23 $cb->();
399     });
400     $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
401     $cb->signal_connect (clicked => sub { $w->destroy });
402    
403     $w->show_all;
404    
405     $w->signal_connect (destroy => sub { Gtk2->main_quit });
406     }
407    
408    
409 root 1.1 #############################################################################
410    
411 root 1.24 SDL::Init SDL_INIT_EVERYTHING;
412 elmex 1.20
413 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
414 elmex 1.10
415 root 1.13 $CFG ||= {
416     width => 640,
417     height => 480,
418 elmex 1.47 mapsize => 100,
419 root 1.13 fullscreen => 0,
420     host => "crossfire.schmorp.de",
421     port => 13327,
422     };
423 elmex 1.12
424 root 1.65 {
425 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
426 root 1.65
427 root 1.67 CFClient::add_font $_ for @fonts;
428     CFClient::set_font $fonts[0];
429 root 1.65 }
430 root 1.40
431 root 1.24 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
432    
433 elmex 1.23 run_config_dialog
434 elmex 1.16 login => sub { start_game },
435     logout => sub { stop_game };
436 root 1.1
437 root 1.13 main Gtk2;
438 root 1.19
439 root 1.68 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";