ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.89
Committed: Wed Apr 12 21:20:42 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.88: +24 -6 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #!/opt/bin/perl
2
3 use strict;
4 use utf8;
5
6 use Time::HiRes 'time';
7 use Event;
8
9 use SDL;
10 use SDL::App;
11 use SDL::Event;
12 use SDL::Surface;
13 use SDL::OpenGL;
14
15 use Crossfire;
16 use Crossfire::Protocol;
17
18 use CFClient;
19 use CFClient::UI;
20
21 our $VERSION = '0.1';
22
23 my $MAX_FPS = 60;
24 my $MIN_FPS = 5; # unused
25
26 our $FACECACHE;
27
28 our $LAST_REFRESH;
29 our $NOW;
30
31 our $CFG;
32 our $CONN;
33 our $FAST; # fast, low-quality mode, possibly useful for software-rendering
34
35 our @SDL_MODES;
36 our $WIDTH;
37 our $HEIGHT;
38 our $FULLSCREEN;
39
40 our $MAPWIDGET;
41 our $FONTSIZE;
42
43 our $SDL_ACTIVE;
44 our $SDL_EV;
45 our %SDL_CB;
46
47 our $ALT_ENTER_MESSAGE;
48 our $STATUS_LINE;
49 our $DEBUG_STATUS;
50
51 sub status {
52 $STATUS_LINE->set_text ($_[0]);
53 my ($w, $h) = $STATUS_LINE->size_request;
54 $STATUS_LINE->size_allocate (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $h, $w, $h);
55 }
56
57 sub debug {
58 $DEBUG_STATUS->set_text ($_[0]);
59 my ($w, $h) = $DEBUG_STATUS->size_request;
60 $DEBUG_STATUS->size_allocate ($WIDTH - $w, 0, $w, $h);
61 }
62
63 sub start_game {
64 status "logging in...";
65
66 my $mapsize = List::Util::min 64, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
67
68 $CONN = new conn
69 host => $CFG->{host},
70 port => $CFG->{port},
71 user => $CFG->{user},
72 pass => $CFG->{password},
73 mapw => $mapsize,
74 maph => $mapsize,
75 ;
76
77 status "login successful";
78
79 CFClient::lowdelay fileno $CONN->{fh};
80 }
81
82 sub stop_game {
83 undef $CONN;
84 }
85
86 sub config_dialog {
87 my $dialog = new CFClient::UI::FancyFrame x => 300, y => 100,
88 child => (my $vbox = new CFClient::UI::VBox);
89 $vbox->add (new CFClient::UI::Label align => 0, text => "Client Setup");
90 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
91
92 $table->add (0, 0, new CFClient::UI::Label align => 1, text => "Video Mode");
93 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
94
95 $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
96 $hbox->add (my $mode_label = new CFClient::UI::Label height => $FONTSIZE * 0.8);
97
98 $mode_slider->connect (changed => sub {
99 my ($self, $value) = @_;
100
101 $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
102 $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
103 });
104 $mode_slider->emit (changed => $mode_slider->{range}[0]);
105
106 $table->add (0, 1, new CFClient::UI::Label align => 1, text => "Fullscreen");
107 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
108 my ($self, $value) = @_;
109 $CFG->{fullscreen} = $value;
110 });
111
112 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Fast & Ugly");
113 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
114 my ($self, $value) = @_;
115 $CFG->{fast} = $value;
116 });
117
118 $table->add (1, 3, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
119 destroy_screen ();
120 init_screen ();
121 });
122
123 $vbox->add (new CFClient::UI::Label align => 0, text => "Server Setup");
124 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
125 $table->add (0, 2, new CFClient::UI::Label align => 1, text => "Host");
126 $table->add (1, 2, my $host = new CFClient::UI::Entry text => $CFG->{host});
127
128 $table->add (0, 3, new CFClient::UI::Label align => 1, text => "Port");
129 $table->add (1, 3, my $port = new CFClient::UI::Entry text => $CFG->{port});
130
131 $table->add (0, 4, new CFClient::UI::Label align => 1, text => "Username");
132 $table->add (1, 4, my $user = new CFClient::UI::Entry text => $CFG->{user});
133
134 $table->add (0, 5, new CFClient::UI::Label align => 1, text => "Password");
135 $table->add (1, 5, my $pass = new CFClient::UI::Entry text => $CFG->{password}, hidden => 1);
136
137 $table->add (0, 6, new CFClient::UI::Label align => 1, text => "Map Size");
138 $table->add (1, 6, new CFClient::UI::Slider
139 req_w => 100,
140 range => [$CFG->{mapsize}, 10, 100 + 1, 1],
141 connect_changed => sub {
142 my ($self, $value) = @_;
143
144 $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
145 },
146 );
147
148 $table->add (1, 7, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
149 start_game;
150 });
151
152 $vbox->add (my $hbox = new CFClient::UI::HBox);
153
154 $hbox->add (new CFClient::UI::Button expand => 1, align => 0, text => "Save", connect_activate => sub {
155 CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
156 status "Configuration Saved";
157 });
158 $CFClient::UI::TOPLEVEL->add ($dialog);
159 }
160
161 sub sdl_init {
162 SDL::Init SDL_INIT_TIMER | SDL_INIT_AUDIO | SDL_INIT_VIDEO
163 and die "SDL::Init failed!\n";
164 }
165
166 sub init_screen {
167 sdl_init;
168
169 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
170 $FULLSCREEN = $CFG->{fullscreen};
171 $FAST = $CFG->{fast};
172
173 SDL::GLSetAttribute SDL_GL_RED_SIZE, 5;
174 SDL::GLSetAttribute SDL_GL_GREEN_SIZE, 5;
175 SDL::GLSetAttribute SDL_GL_BLUE_SIZE, 5;
176 SDL::GLSetAttribute SDL_GL_ALPHA_SIZE, 0;
177
178 SDL::GLSetAttribute SDL_GL_ACCUM_RED_SIZE, 0;
179 SDL::GLSetAttribute SDL_GL_ACCUM_GREEN_SIZE, 0;
180 SDL::GLSetAttribute SDL_GL_ACCUM_BLUE_SIZE, 0;
181 SDL::GLSetAttribute SDL_GL_ACCUM_ALPHA_SIZE, 0;
182
183 SDL::GLSetAttribute SDL_GL_DOUBLEBUFFER, 1;
184 SDL::GLSetAttribute SDL_GL_BUFFER_SIZE, 15;
185 SDL::GLSetAttribute SDL_GL_DEPTH_SIZE, 0;
186
187 SDL::SetVideoMode $WIDTH, $HEIGHT, 0,
188 SDL_HWSURFACE | SDL_ANYFORMAT | SDL_OPENGL | SDL_DOUBLEBUF
189 | ($FULLSCREEN ? SDL_FULLSCREEN : 0)
190 or die "SDL::SetVideoMode failed!\n";
191
192 SDL::WMSetCaption "Crossfire+ Client", "Crossfire+";
193
194 $SDL_EV = new SDL::Event;
195 $SDL_EV->set_unicode (1);
196
197 $SDL_ACTIVE = 1;
198
199 $LAST_REFRESH = time - 0.01;
200
201 CFClient::gl_init;
202
203 $FONTSIZE = int $HEIGHT / 40;
204
205 #############################################################################
206
207 $DEBUG_STATUS = new CFClient::UI::Label padding => 0;
208 $CFClient::UI::TOPLEVEL->add ($DEBUG_STATUS);
209
210 $STATUS_LINE = new CFClient::UI::Label
211 padding => 0,
212 y => $HEIGHT * 49 / 50 - $FONTSIZE;
213 $CFClient::UI::TOPLEVEL->add ($STATUS_LINE);
214
215 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
216 padding => 0,
217 y => $HEIGHT * 49 / 50,
218 height => $HEIGHT / 50,
219 text => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
220 $CFClient::UI::TOPLEVEL->add ($ALT_ENTER_MESSAGE);
221
222 $MAPWIDGET = new CFClient::UI::MapWidget;
223 $CFClient::UI::TOPLEVEL->add ($MAPWIDGET);
224 $MAPWIDGET->focus_in;
225
226 config_dialog;
227 }
228
229 sub destroy_screen {
230 $CFClient::UI::TOPLEVEL->{children} = [];
231 undef $SDL_ACTIVE;
232 undef $SDL_EV;
233 SDL::Quit;
234 }
235
236 my %animate_object;
237 my $animate_timer;
238
239 my $want_refresh;
240 my $can_refresh;
241
242 my $fps = 9;
243
244 sub force_refresh {
245 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
246 debug sprintf "%3.2f", $fps;
247
248 $want_refresh = 0;
249 $can_refresh = 0;
250
251 glViewport 0, 0, $WIDTH, $HEIGHT;
252
253 glMatrixMode GL_PROJECTION;
254 glLoadIdentity;
255 glOrtho 0, $WIDTH, $HEIGHT, 0, -10000 , 10000;
256 glMatrixMode GL_MODELVIEW;
257 glLoadIdentity;
258
259 glClear GL_COLOR_BUFFER_BIT;
260
261 $CFClient::UI::TOPLEVEL->draw;
262
263 SDL::GLSwapBuffers;
264
265 $LAST_REFRESH = $NOW;
266 }
267
268 my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
269 $NOW = time;
270
271 ($SDL_CB{$SDL_EV->type} || sub { warn "unhandled event ", $SDL_EV->type })->()
272 while $SDL_EV->poll;
273
274 if (%animate_object) {
275 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
276 $want_refresh++;
277 }
278
279 if ($want_refresh) {
280 force_refresh;
281 } else {
282 $can_refresh = 1;
283 }
284 });
285
286 sub refresh {
287 $want_refresh++;
288 }
289
290 sub animation_start {
291 my ($widget) = @_;
292 $animate_object{$widget} = $widget;
293 }
294
295 sub animation_stop {
296 my ($widget) = @_;
297 delete $animate_object{$widget};
298 }
299
300 @conn::ISA = Crossfire::Protocol::;
301
302 sub conn::user_send {
303 my ($self, $command) = @_;
304
305 $self->send ($command);
306 status $command;
307 }
308
309 sub conn::map_update {
310 my ($self, $dirty) = @_;
311
312 $MAPWIDGET->update;
313 }
314
315 sub conn::map_scroll {
316 my ($self, $dx, $dy) = @_;
317
318 # refresh;
319 }
320
321 sub conn::map_clear {
322 my ($self) = @_;
323
324 # refresh;
325 }
326
327 sub conn::face_find {
328 my ($self, $face) = @_;
329
330 $FACECACHE->{"$face->{chksum},$face->{name}"}
331 }
332
333 sub conn::face_update {
334 my ($self, $face) = @_;
335
336 $FACECACHE->{"$face->{chksum},$face->{name}"} = $face->{image};
337
338 $face->{texture} = new_from_image CFClient::Texture delete $face->{image};
339 }
340
341 sub conn::query {
342 my ($self, $flags, $prompt) = @_;
343
344 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
345 }
346
347 %SDL_CB = (
348 SDL_QUIT() => sub {
349 Event::unloop -1;
350 },
351 SDL_VIDEORESIZE() => sub {
352 },
353 SDL_VIDEOEXPOSE() => sub {
354 refresh;
355 },
356 SDL_KEYDOWN() => sub {
357 if ($SDL_EV->key_mod & KMOD_ALT && $SDL_EV->key_sym == SDLK_RETURN) {
358 # alt-enter
359 $FULLSCREEN = !$FULLSCREEN;
360 destroy_screen;
361 init_screen;
362 } else {
363 CFClient::UI::feed_sdl_key_down_event ($SDL_EV);
364 }
365 },
366 SDL_KEYUP() => sub {
367 CFClient::UI::feed_sdl_key_up_event ($SDL_EV);
368 },
369 SDL_MOUSEMOTION() => sub {
370 CFClient::UI::feed_sdl_motion_event ($SDL_EV);
371 },
372 SDL_MOUSEBUTTONDOWN() => sub {
373 CFClient::UI::feed_sdl_button_down_event ($SDL_EV);
374 },
375 SDL_MOUSEBUTTONUP() => sub {
376 CFClient::UI::feed_sdl_button_up_event ($SDL_EV);
377 },
378 SDL_ACTIVEEVENT() => sub {
379 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
380 },
381 );
382
383 #############################################################################
384
385 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
386
387 $CFG ||= {
388 width => 640,
389 height => 480,
390 fast => 0,
391 fullscreen => 0,
392 sdl_mode => 0,
393 mapsize => 100,
394 host => "crossfire.schmorp.de",
395 port => 13327,
396 };
397
398 sdl_init;
399
400 @SDL_MODES = reverse map [SDL::RectW ($_), SDL::RectH ($_)],
401 @{ SDL::ListModes 0, SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL };
402
403 @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
404
405 $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
406
407 init_screen;
408
409 {
410 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
411
412 CFClient::add_font $_ for @fonts;
413 CFClient::set_font $fonts[0];
414 }
415
416 $FACECACHE = eval { Crossfire::load_ref "$Crossfire::VARDIR/pclient.faces" } || {};
417
418 Event::loop;
419
420 Crossfire::save_ref $FACECACHE, "$Crossfire::VARDIR/pclient.faces";
421