ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.49
Committed: Sun Apr 9 18:35:09 2006 UTC (18 years, 2 months ago) by elmex
Branch: MAIN
Changes since 1.48: +1 -2 lines
Log Message:
removed crash in mapsize... gmrhl

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4 use utf8;
5
6 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 use Crossfire;
17 use Crossfire::Client;
18 use Crossfire::Protocol;
19
20 use Crossfire::Client::Widget;
21
22 our $FACECACHE;
23
24 our $VERSION = '0.1';
25
26 our %GL_EXT;
27
28 our $CFG;
29 our $CONN;
30
31 our $WIDTH;
32 our $HEIGHT;
33 our $FULLSCREEN;
34
35 our $FONTSIZE;
36
37 our $SDL_TIMER;
38 our $SDL_APP;
39 our $SDL_EV = new SDL::Event;
40 our %SDL_CB;
41
42 our @GL_INIT; # hooks called on every gl init
43
44 our $ALT_ENTER_MESSAGE;
45
46 our $TOPLEVEL;
47
48 our $tw; # Test widget #d#
49
50 my $last_refresh;
51
52 sub init_screen {
53 $SDL_APP = new SDL::App
54 -flags => SDL_ANYFORMAT | SDL_HWSURFACE,
55 -title => "Crossfire+ Client",
56 -width => $WIDTH,
57 -height => $HEIGHT,
58 -opengl => 1,
59 -red_size => 8,
60 -green_size => 8,
61 -blue_size => 8,
62 -double_buffer => 1,
63 -fullscreen => $FULLSCREEN,
64 -resizeable => 0;
65
66 $last_refresh = SDL::GetTicks;
67
68 %GL_EXT = map +($_ => 1), split /\s+/, Crossfire::Client::gl_extensions;
69
70 $GL_EXT{GL_ARB_texture_non_power_of_two}
71 or warn "WARNING: non-power-of-two opengl extension required";
72
73 $FONTSIZE = int $HEIGHT / 50;
74
75 $ALT_ENTER_MESSAGE = new Crossfire::Client::Widget::Label
76 0, $FONTSIZE, $HEIGHT - $FONTSIZE, $FONTSIZE,
77 "Use <b>Alt-Enter</b> to toggle fullscreen mode";
78 $ALT_ENTER_MESSAGE->move (0, $HEIGHT - ($ALT_ENTER_MESSAGE->size_request)[1]);
79 $TOPLEVEL->add ($ALT_ENTER_MESSAGE);
80
81 # Test code #d#
82 unless ($tw) { # haha...
83 $tw = new Crossfire::Client::Widget::Animator;
84 my $lbl1 = new Crossfire::Client::Widget::Label
85 0, 0, 10, $FONTSIZE, "<i>This</i> is a\n<u>TEST</u>!\nOf a themed\nFrame!";
86 my $lbl2 = new Crossfire::Client::Widget::Label
87 0, 0, 10, $FONTSIZE, "LBL2";
88
89 my $vb = new Crossfire::Client::Widget::VBox;
90 my $f = new Crossfire::Client::Widget::FancyFrame;
91 my $f2 = new Crossfire::Client::Widget::FancyFrame;
92 $f->add ($lbl1);
93 $f2->add ($lbl2);
94 $vb->add ($f);
95 $vb->add ($f2, 1);
96
97 $tw->add ($vb);
98 $tw->w (400);
99 $tw->h (300);
100 $tw->move ($WIDTH - 200, 0);
101 $tw->moveto (0, 0);
102 $TOPLEVEL->add ($tw);
103
104 # $f->move ($WIDTH - 200, 0);
105 # $TOPLEVEL->add ($f);
106 }
107
108 glClearColor 0, 0, 0, 0;
109
110 glEnable GL_TEXTURE_2D;
111 glEnable GL_COLOR_MATERIAL;
112 glShadeModel GL_FLAT;
113 glDisable GL_DEPTH_TEST;
114 glBlendFunc GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA;
115
116 $_->() for @GL_INIT;
117 }
118
119 sub start_game {
120 $SDL_TIMER = add Glib::Timeout 1000/100, sub {
121 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
122 while $SDL_EV->poll;
123
124 1
125 };
126
127 $WIDTH = $CFG->{width};
128 $HEIGHT = $CFG->{height};
129 $FULLSCREEN = 0;
130
131 init_screen;
132
133 $CONN = new conn
134 host => $CFG->{host},
135 port => $CFG->{port},
136 user => $CFG->{user},
137 pass => $CFG->{password},
138 mapw => 50,
139 maph => 37,
140 ;
141
142 Crossfire::Client::lowdelay fileno $CONN->{fh};
143 }
144
145 sub stop_game {
146 remove Glib::Source $SDL_TIMER;
147
148 undef $SDL_APP;
149 SDL::Quit;
150 }
151
152
153 sub force_refresh {
154 glViewport 0, 0, $WIDTH, $HEIGHT;
155
156 glMatrixMode GL_PROJECTION;
157 glLoadIdentity;
158 glOrtho 0, $WIDTH, $HEIGHT, 0, -6000 , 6000;
159 glMatrixMode GL_MODELVIEW;
160
161 glClear GL_COLOR_BUFFER_BIT;
162
163 $TOPLEVEL->draw;
164
165 SDL::GLSwapBuffers;
166 }
167
168 my %ANIMATE;
169
170 my $refresh_handler;
171
172 sub refresh {
173 $refresh_handler ||= add Glib::Idle sub {
174 my $next_refresh = SDL::GetTicks;
175 my $interval = ($next_refresh - $last_refresh) * 0.001;
176 $last_refresh = $next_refresh;
177
178 force_refresh;
179 $_->animate ($interval) for grep $_, values %ANIMATE;
180
181 if (%ANIMATE) {
182 1
183 } else {
184 undef $refresh_handler;
185 0
186 }
187 };
188 }
189
190 sub animation_start {
191 my ($widget) = @_;
192 $ANIMATE{$widget} = $widget;
193 Scalar::Util::weaken $ANIMATE{$widget};
194
195 refresh;
196 }
197
198 sub animation_stop {
199 my ($widget) = @_;
200 delete $ANIMATE{$widget};
201 }
202
203 %SDL_CB = (
204 SDL_QUIT() => sub {
205 main_quit Gtk2;
206 },
207 SDL_VIDEORESIZE() => sub {
208 },
209 SDL_VIDEOEXPOSE() => sub {
210 refresh;
211 },
212 SDL_KEYDOWN() => sub {
213 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
214 # alt-enter
215 $FULLSCREEN = !$FULLSCREEN;
216 init_screen;
217 } else {
218 Crossfire::Client::Widget::feed_sdl_key_down_event ($SDL_EV);
219 }
220 },
221 SDL_KEYUP() => sub {
222 Crossfire::Client::Widget::feed_sdl_key_up_event ($SDL_EV);
223 },
224 SDL_MOUSEMOTION() => sub {
225 warn "sdl motion\n";#d#
226 },
227 SDL_MOUSEBUTTONDOWN() => sub {
228 Crossfire::Client::Widget::feed_sdl_button_down_event ($SDL_EV);
229 },
230 SDL_MOUSEBUTTONUP() => sub {
231 Crossfire::Client::Widget::feed_sdl_button_up_event ($SDL_EV);
232 },
233 SDL_ACTIVEEVENT() => sub {
234 warn "active\n";#d#
235 },
236 );
237
238 @conn::ISA = Crossfire::Protocol::;
239
240 sub conn::map_update {
241 my ($self, $dirty) = @_;
242
243 refresh;
244 }
245
246 sub conn::map_scroll {
247 my ($self, $dx, $dy) = @_;
248
249 # refresh;
250 }
251
252 sub conn::map_clear {
253 my ($self) = @_;
254
255 # refresh;
256 }
257
258 sub conn::face_find {
259 my ($self, $face) = @_;
260
261 $FACECACHE->{"$face->{chksum},$face->{name}"}
262 }
263
264 sub conn::face_update {
265 my ($self, $face) = @_;
266
267 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
268
269 $face->{texture} = new_from_image Crossfire::Client::Texture delete $face->{image};
270 }
271
272 sub conn::query {
273 my ($self, $flags, $prompt) = @_;
274
275 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
276 }
277
278 sub gtk_add_cfg_field {
279 my ($tbl, $cfg, $klbl, $key, $value) = @_;
280 my $i = $cfg->{_i}++;
281 $tbl->attach_defaults (my $lbl = Gtk2::Label->new ($klbl), 0, 1, $i, $i + 1);
282 $tbl->attach_defaults (my $ent = Gtk2::Entry->new, 1, 2, $i, $i + 1);
283 if ($key eq 'password') {
284 $ent->set_invisible_char ("*");
285 $ent->set (visibility => 0)
286 }
287 $ent->set_text ($value);
288 $ent->signal_connect (changed => sub {
289 my ($ent) = @_;
290 $cfg->{$key} = $ent->get_text;
291 # TODO: Mapsize should be a slider in the game gui
292 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
293 $cfg->{$key} = 100;
294 } elsif ($key eq 'mapsize' and $cfg->{$key} < 50) {
295 $cfg->{$key} = 50;
296 }
297 });
298 }
299
300 sub run_config_dialog {
301 my (%events) = @_;
302
303 my $w = Gtk2::Window->new;
304
305 my @cfg = (
306 [qw/Host host/],
307 [qw/Port port/],
308 [qw/Mapsize% mapsize/],
309 [qw/Username user/],
310 [qw/Password password/],
311 );
312
313 my $cfg = {};
314
315 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
316 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
317
318 $w->add (my $vb = Gtk2::VBox->new);
319 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
320 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
321 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
322 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
323 my $i = 0;
324 my $act = 0;
325 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
326 if ($_ eq $selmode) { $act = $i }
327 $cb->append_text ($_);
328 $i++;
329 }
330 $cb->set_active ($act);
331 $cb->signal_connect (changed => sub {
332 my ($cb) = @_;
333 my $txt = $cb->get_active_text;
334 if ($txt =~ m/(\d+)x(\d+)/) {
335 $::CFG->{width} = $1;
336 $::CFG->{height} = $2;
337 }
338 });
339
340 $cfg->{_i} = 1;
341 for (@cfg) {
342 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
343 }
344
345 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
346 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
347 $cb->signal_connect (clicked => sub {
348 for (keys %$cfg) {
349 $::CFG->{$_} = $cfg->{$_}
350 if $_ ne '_i';
351 }
352 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
353 });
354 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
355 $cb->signal_connect (clicked => sub {
356 for (keys %$cfg) {
357 $::CFG->{$_} = $cfg->{$_}
358 if $_ ne '_i';
359 }
360 my $cb = $events{login} || sub {};
361 $cb->($::CFG->{user}, $::CFG->{password});
362 });
363 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
364 $cb->signal_connect (clicked => sub {
365 my $cb = $events{login} || sub {};
366 $cb->();
367 });
368 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
369 $cb->signal_connect (clicked => sub { $w->destroy });
370
371 $w->show_all;
372
373 $w->signal_connect (destroy => sub { Gtk2->main_quit });
374 }
375
376
377 #############################################################################
378
379 SDL::Init SDL_INIT_EVERYTHING;
380
381 $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
382
383 my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
384
385 $TOPLEVEL->add ($mapwidget);
386 $mapwidget->focus_in;
387
388 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
389
390 $CFG ||= {
391 width => 640,
392 height => 480,
393 mapsize => 100,
394 fullscreen => 0,
395 host => "crossfire.schmorp.de",
396 port => 13327,
397 };
398
399 Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
400
401 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
402
403 run_config_dialog
404 login => sub { start_game },
405 logout => sub { stop_game };
406
407 main Gtk2;
408
409 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";