ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.47
Committed: Sun Apr 9 18:28:23 2006 UTC (18 years, 1 month ago) by elmex
Branch: MAIN
Changes since 1.46: +9 -0 lines
Log Message:
added mapsize

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 if ($key eq 'mapsize' and $cfg->{$key} > 100) {
292 $cfg->{$key} = 100;
293 $ent->set_text ('100');
294 } elsif ($key eq 'mapsize' and $cfg->{$key} < 0) {
295 $cfg->{$key} = 0;
296 $ent->set_text ('0');
297 }
298 });
299 }
300
301 sub run_config_dialog {
302 my (%events) = @_;
303
304 my $w = Gtk2::Window->new;
305
306 my @cfg = (
307 [qw/Host host/],
308 [qw/Port port/],
309 [qw/Mapsize% mapsize/],
310 [qw/Username user/],
311 [qw/Password password/],
312 );
313
314 my $cfg = {};
315
316 my $a = SDL::ListModes (0, SDL_FULLSCREEN|SDL_HWSURFACE);
317 my @modes = map { [SDL::RectW ($_), SDL::RectH ($_)] } @$a;
318
319 $w->add (my $vb = Gtk2::VBox->new);
320 $vb->pack_start (my $t = Gtk2::Table->new (2, scalar @cfg), 0, 0, 0);
321 my $selmode = $::CFG->{width} . 'x' . $::CFG->{height};
322 $t->attach_defaults (Gtk2::Label->new ("Modes"), 0, 1, 0, 1);
323 $t->attach_defaults (my $cb = Gtk2::ComboBox->new_text, 1, 2, 0, 1);
324 my $i = 0;
325 my $act = 0;
326 for (map { "$_->[0]x$_->[1]" } reverse @modes) {
327 if ($_ eq $selmode) { $act = $i }
328 $cb->append_text ($_);
329 $i++;
330 }
331 $cb->set_active ($act);
332 $cb->signal_connect (changed => sub {
333 my ($cb) = @_;
334 my $txt = $cb->get_active_text;
335 if ($txt =~ m/(\d+)x(\d+)/) {
336 $::CFG->{width} = $1;
337 $::CFG->{height} = $2;
338 }
339 });
340
341 $cfg->{_i} = 1;
342 for (@cfg) {
343 gtk_add_cfg_field ($t, $cfg, $_->[0], $_->[1], $::CFG->{$_->[1]});
344 }
345
346 $vb->pack_start (my $hb = Gtk2::HBox->new, 0, 0, 0);
347 $hb->pack_start (my $cb = Gtk2::Button->new ("save"), 1, 1, 5);
348 $cb->signal_connect (clicked => sub {
349 for (keys %$cfg) {
350 $::CFG->{$_} = $cfg->{$_}
351 if $_ ne '_i';
352 }
353 Crossfire::Client::write_cfg "$Crossfire::VARDIR/pclientrc";
354 });
355 $hb->pack_start (my $cb = Gtk2::Button->new ("login"), 1, 1, 5);
356 $cb->signal_connect (clicked => sub {
357 for (keys %$cfg) {
358 $::CFG->{$_} = $cfg->{$_}
359 if $_ ne '_i';
360 }
361 my $cb = $events{login} || sub {};
362 $cb->($::CFG->{user}, $::CFG->{password});
363 });
364 $hb->pack_start (my $cb = Gtk2::Button->new ("logout"), 1, 1, 5);
365 $cb->signal_connect (clicked => sub {
366 my $cb = $events{login} || sub {};
367 $cb->();
368 });
369 $hb->pack_start (my $cb = Gtk2::Button->new ("quit"), 1, 1, 5);
370 $cb->signal_connect (clicked => sub { $w->destroy });
371
372 $w->show_all;
373
374 $w->signal_connect (destroy => sub { Gtk2->main_quit });
375 }
376
377
378 #############################################################################
379
380 SDL::Init SDL_INIT_EVERYTHING;
381
382 $TOPLEVEL = Crossfire::Client::Widget::Toplevel->new;
383
384 my $mapwidget = Crossfire::Client::Widget::MapWidget->new;
385
386 $TOPLEVEL->add ($mapwidget);
387 $mapwidget->focus_in;
388
389 Crossfire::Client::read_cfg "$Crossfire::VARDIR/pclientrc";
390
391 $CFG ||= {
392 width => 640,
393 height => 480,
394 mapsize => 100,
395 fullscreen => 0,
396 host => "crossfire.schmorp.de",
397 port => 13327,
398 };
399
400 Crossfire::Client::set_font Crossfire::Client::find_rcfile "uifont.ttf";
401
402 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
403
404 run_config_dialog
405 login => sub { start_game },
406 logout => sub { stop_game };
407
408 main Gtk2;
409
410 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";