ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.147
Committed: Wed Apr 19 21:38:05 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.146: +8 -15 lines
Log Message:
got rid of SDL::Event

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.87 use Time::HiRes 'time';
7     use Event;
8 root 1.13
9     use SDL::OpenGL;
10    
11 elmex 1.11 use Crossfire;
12 root 1.2 use Crossfire::Protocol;
13    
14 root 1.116 use Compress::LZF;
15    
16 root 1.67 use CFClient;
17 root 1.72 use CFClient::UI;
18 root 1.141 use CFClient::MapWidget;
19 elmex 1.10
20 root 1.63 our $VERSION = '0.1';
21    
22 root 1.96 my $MAX_FPS = 60;
23 root 1.90 my $MIN_FPS = 5; # unused as of yet
24 root 1.63
25 root 1.112 our $META_SERVER = "crossfire.real-time.com:13326";
26    
27 root 1.116 our $FACEMAP;
28     our $TILECACHE;
29     our $MAPCACHE;
30 root 1.19
31 root 1.87 our $LAST_REFRESH;
32     our $NOW;
33    
34 elmex 1.10 our $CFG;
35 root 1.13 our $CONN;
36 root 1.85 our $FAST; # fast, low-quality mode, possibly useful for software-rendering
37 root 1.2
38 root 1.75 our @SDL_MODES;
39 root 1.30 our $WIDTH;
40     our $HEIGHT;
41     our $FULLSCREEN;
42 root 1.99 our $FONTSIZE;
43 root 1.30
44 root 1.95 our $MAP;
45 root 1.69 our $MAPWIDGET;
46 root 1.112 our $BUTTONBAR;
47     our $LOGVIEW;
48     our $CONSOLE;
49     our $METASERVER;
50 root 1.57
51 elmex 1.125 our $GAUGES;
52    
53 root 1.86 our $SDL_ACTIVE;
54 root 1.13 our %SDL_CB;
55 root 1.18
56 root 1.134 our $SDL_MIXER;
57     our @SOUNDS; # event => file mapping
58     our %AUDIO_CHUNKS; # audio files
59    
60 root 1.30 our $ALT_ENTER_MESSAGE;
61 root 1.51 our $STATUS_LINE;
62 root 1.64 our $DEBUG_STATUS;
63 root 1.30
64 root 1.82 sub status {
65     $STATUS_LINE->set_text ($_[0]);
66 root 1.128 $STATUS_LINE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h} - $STATUS_LINE->{h});
67 root 1.82 }
68    
69     sub debug {
70     $DEBUG_STATUS->set_text ($_[0]);
71 root 1.128 $DEBUG_STATUS->move ($WIDTH - $DEBUG_STATUS->{w}, 0, $DEBUG_STATUS->{w}, $DEBUG_STATUS->{h});
72 root 1.82 }
73    
74 root 1.84 sub start_game {
75 root 1.85 status "logging in...";
76    
77 root 1.106 my $mapsize = List::Util::min 32, List::Util::max 11, int $WIDTH * $CFG->{mapsize} * 0.01 / 32;
78 root 1.84
79 root 1.116 $MAPCACHE = CFClient::db_table "mapcache_$CFG->{host}";
80    
81 root 1.95 $MAP = new CFClient::Map $mapsize, $mapsize;
82 root 1.112
83     my ($host, $port) = split /:/, $CFG->{host};
84 root 1.95
85 root 1.84 $CONN = new conn
86 root 1.112 host => $host,
87     port => $port || 13327,
88 root 1.84 user => $CFG->{user},
89     pass => $CFG->{password},
90     mapw => $mapsize,
91     maph => $mapsize,
92     ;
93    
94 root 1.85 status "login successful";
95    
96 root 1.84 CFClient::lowdelay fileno $CONN->{fh};
97     }
98    
99     sub stop_game {
100     undef $CONN;
101     }
102    
103 root 1.111 sub client_setup {
104 root 1.99 my $dialog = new CFClient::UI::FancyFrame
105 root 1.81 child => (my $vbox = new CFClient::UI::VBox);
106 root 1.140 $vbox->add (new CFClient::UI::Label valign => 0, align => 0, text => "Client Setup");
107 root 1.81 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
108    
109 root 1.140 $table->add (0, 0, new CFClient::UI::Label valign => 0, align => 1, text => "Video Mode");
110 root 1.81 $table->add (1, 0, my $hbox = new CFClient::UI::HBox);
111    
112     $hbox->add (my $mode_slider = new CFClient::UI::Slider expand => 1, req_w => 100, range => [$CFG->{sdl_mode}, 0, scalar @SDL_MODES, 1]);
113 root 1.140 $hbox->add (my $mode_label = new CFClient::UI::Label valign => 0, height => 0.8);
114 root 1.81
115     $mode_slider->connect (changed => sub {
116     my ($self, $value) = @_;
117    
118     $CFG->{sdl_mode} = $self->{range}[0] = $value = int $value;
119     $mode_label->set_text (sprintf "%dx%d", @{$SDL_MODES[$value]});
120     });
121     $mode_slider->emit (changed => $mode_slider->{range}[0]);
122 root 1.82
123 root 1.140 $table->add (0, 1, new CFClient::UI::Label valign => 0, align => 1, text => "Fullscreen");
124 root 1.85 $table->add (1, 1, new CFClient::UI::CheckBox state => $CFG->{fullscreen}, connect_changed => sub {
125     my ($self, $value) = @_;
126     $CFG->{fullscreen} = $value;
127     });
128    
129 root 1.140 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Fast & Ugly");
130 root 1.89 $table->add (1, 2, new CFClient::UI::CheckBox state => $CFG->{fast}, connect_changed => sub {
131     my ($self, $value) = @_;
132     $CFG->{fast} = $value;
133     });
134    
135 root 1.140 $table->add (0, 3, new CFClient::UI::Label valign => 0, align => 1, text => "Fog of War");
136 root 1.97 $table->add (1, 3, new CFClient::UI::CheckBox state => $CFG->{fow_enable}, connect_changed => sub {
137     my ($self, $value) = @_;
138     $CFG->{fow_enable} = $value;
139     });
140    
141 root 1.140 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Intensity");
142 root 1.97 $table->add (1, 4, new CFClient::UI::Slider range => [$CFG->{fow_intensity}, 0, 1 + 0.001, 0.001], connect_changed => sub {
143 root 1.90 my ($self, $value) = @_;
144     $CFG->{fow_intensity} = $value;
145     });
146    
147 root 1.140 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "FoW Smooth");
148 root 1.97 $table->add (1, 5, new CFClient::UI::CheckBox state => $CFG->{fow_smooth}, connect_changed => sub {
149 root 1.91 my ($self, $value) = @_;
150     $CFG->{fow_smooth} = $value;
151 root 1.132 status "Fog of War smoothing requires OpenGL 1.2 or higher" if $CFClient::GL_VERSION < 1.2;
152 root 1.91 });
153    
154 root 1.141 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "GUI Fontsize");
155     $table->add (1, 6, new CFClient::UI::Slider range => [$CFG->{gui_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
156     $CFG->{gui_fontsize} = 0.1 * int $_[1] * 10;
157 root 1.140 # $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
158     });
159    
160 root 1.141 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Server Log Fontsize");
161     $table->add (1, 7, new CFClient::UI::Slider range => [$CFG->{log_fontsize}, 0.7, 1.7, 0.1], connect_changed => sub {
162     $LOGVIEW->set_fontsize ($CFG->{log_fontsize} = 0.1 * int $_[1] * 10);
163 root 1.105 });
164    
165 root 1.140 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
166 root 1.134 video_shutdown ();
167     video_init ();
168 root 1.82 });
169 root 1.111
170 root 1.140 $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Audio Enable");
171     $table->add (1, 9, new CFClient::UI::CheckBox state => $CFG->{audio_enable}, connect_changed => sub {
172     $CFG->{audio_enable} = $_[1];
173     });
174     # $table->add (0, 9, new CFClient::UI::Label valign => 0, align => 1, text => "Effects Volume");
175     # $table->add (1, 8, new CFClient::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], connect_changed => sub {
176     # $CFG->{effects_volume} = $_[1];
177     # });
178 root 1.141 $table->add (0, 10, new CFClient::UI::Label valign => 0, align => 1, text => "Background Music");
179 root 1.140 $table->add (1, 10, my $hbox = new CFClient::UI::HBox);
180     $hbox->add (new CFClient::UI::CheckBox expand => 1, state => $CFG->{bgm_enable}, connect_changed => sub {
181     $CFG->{bgm_enable} = $_[1];
182     });
183 root 1.146 $hbox->add (new CFClient::UI::Slider expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0.1], connect_changed => sub {
184 root 1.140 $CFG->{bgm_volume} = $_[1];
185 root 1.146 CFClient::Mix_VolumeMusic ($_[1]);
186 elmex 1.137 });
187 root 1.140
188     $table->add (1, 11, new CFClient::UI::Button expand => 1, align => 0, text => "Apply", connect_activate => sub {
189 elmex 1.137 audio_shutdown ();
190     audio_init ();
191     });
192    
193 root 1.111 $dialog
194     }
195    
196 root 1.112 sub metaserver_dialog {
197     my $dialog = new CFClient::UI::FancyFrame
198     child => (my $vbox = new CFClient::UI::VBox);
199    
200     $vbox->add ($dialog->{table} = new CFClient::UI::Table);
201    
202     $dialog
203     }
204    
205     sub update_metaserver {
206 root 1.114 my ($HOST) = @_;
207    
208 root 1.112 status "fetching metaserver list...";
209    
210     my $buf;
211    
212     my $fh = new IO::Socket::INET PeerHost => $META_SERVER, Blocking => 0;
213    
214     Event->io (fd => $fh, poll => 'r', cb => sub {
215     my $res = sysread $fh, $buf, 8192, length $buf;
216    
217     if (!defined $res) {
218     $_[0]->w->cancel;
219     status "metaserver: $!";
220     } elsif ($res == 0) {
221     $_[0]->w->cancel;
222     status "server list retrieved";
223 root 1.113
224     my $table = $METASERVER->{table};
225    
226     $table->clear;
227    
228 root 1.114 my @col = qw(Use #Users Host Uptime Version Description);
229 root 1.113 $table->add ($_, 0, new CFClient::UI::Label align => 0, fg => [1, 1, 0], text => $col[$_])
230     for 0 .. $#col;
231    
232     my @align = qw(1 0 1 1 -1);
233    
234     my $y = 0;
235 root 1.114 for my $m (sort { $b->[3] <=> $a->[3] } map [split /\|/], split /\015?\012/, $buf) {
236 root 1.113 my ($ip, $last, $host, $users, $version, $desc, $ibytes, $obytes, $uptime) = @$m;
237    
238     for ($desc) {
239     s/<br>/\n/gi;
240     s/<li>/\n· /gi;
241     s/<.*?>//sgi;
242     s/&/&amp;/g;
243     s/</&lt;/g;
244     s/>/&gt;/g;
245     }
246    
247     $uptime = sprintf "%dd %02d:%02d:%02d",
248     (int $m->[8] / 86400),
249     (int $m->[8] / 3600) % 24,
250     (int $m->[8] / 60) % 60,
251     $m->[8] % 60;
252    
253     $m = [$users, $host, $uptime, $version, $desc];
254    
255     $y++;
256 root 1.114
257     $table->add (0, $y, new CFClient::UI::VBox children => [
258     (new CFClient::UI::Button text => " ", connect_activate => sub {
259     $HOST->set_text ($CFG->{host} = $host);
260     }),
261     (new CFClient::UI::Empty expand => 1),
262     ]);
263    
264 root 1.140 $table->add ($_ + 1, $y, new CFClient::UI::Label align => $align[$_], text => $m->[$_], fontsize => 0.8)
265 root 1.113 for 0 .. $#$m;
266     }
267 root 1.112 }
268     });
269     }
270    
271 root 1.111 sub server_setup {
272     my $dialog = new CFClient::UI::FancyFrame
273     child => (my $vbox = new CFClient::UI::VBox);
274 root 1.81
275 root 1.141 $vbox->add (new CFClient::UI::Label valign => 0, align => 0, text => "Server Setup");
276 root 1.82 $vbox->add (my $table = new CFClient::UI::Table expand => 1, col_expand => [0, 1]);
277 root 1.141 $table->add (0, 2, new CFClient::UI::Label valign => 0, align => 1, text => "Host:Port");
278 root 1.112
279     {
280     $table->add (1, 2, my $vbox = new CFClient::UI::VBox);
281    
282 root 1.141 $vbox->add (my $HOST = new CFClient::UI::Entry expand => 1, text => $CFG->{host}, connect_changed => sub {
283 root 1.112 my ($self, $value) = @_;
284     $CFG->{host} = $value;
285     });
286    
287     $METASERVER = metaserver_dialog;
288 elmex 1.101
289 root 1.141 $vbox->add (new CFClient::UI::Flopper expand => 1, text => "Metaserver", other => $METASERVER, connect_open => sub {
290 root 1.114 update_metaserver $HOST;
291 root 1.112 });
292     }
293 root 1.81
294 root 1.141 $table->add (0, 4, new CFClient::UI::Label valign => 0, align => 1, text => "Username");
295 root 1.114 $table->add (1, 4, new CFClient::UI::Entry text => $CFG->{user}, connect_changed => sub {
296 elmex 1.101 my ($self, $value) = @_;
297     $CFG->{user} = $value;
298     });
299 root 1.81
300 root 1.141 $table->add (0, 5, new CFClient::UI::Label valign => 0, align => 1, text => "Password");
301 root 1.114 $table->add (1, 5, new CFClient::UI::Entry text => $CFG->{password}, hidden => 1, connect_changed => sub {
302 elmex 1.101 my ($self, $value) = @_;
303     $CFG->{password} = $value;
304     });
305    
306 root 1.141 $table->add (0, 6, new CFClient::UI::Label valign => 0, align => 1, text => "Def. say cmd");
307 elmex 1.101 $table->add (1, 6, my $saycmd = new CFClient::UI::Entry text => $CFG->{say_command}, connect_changed => sub {
308     my ($self, $value) = @_;
309     $CFG->{say_command} = $value;
310     });
311 root 1.81
312 root 1.141 $table->add (0, 7, new CFClient::UI::Label valign => 0, align => 1, text => "Map Size");
313 elmex 1.101 $table->add (1, 7, new CFClient::UI::Slider
314 root 1.81 req_w => 100,
315     range => [$CFG->{mapsize}, 10, 100 + 1, 1],
316     connect_changed => sub {
317     my ($self, $value) = @_;
318    
319     $CFG->{mapsize} = $self->{range}[0] = $value = int $value;
320     },
321     );
322    
323 elmex 1.101 $table->add (1, 8, new CFClient::UI::Button expand => 1, align => 0, text => "Login", connect_activate => sub {
324 root 1.84 start_game;
325 root 1.82 });
326    
327 root 1.98 $dialog
328 root 1.81 }
329 root 1.58
330 root 1.111 sub message_window {
331 root 1.99 my $window = new CFClient::UI::FancyFrame
332     border_bg => [1, 1, 1, 0.5],
333     bg => [0.3, 0.3, 0.3, 0.8],
334 root 1.124 user_w => int $::WIDTH / 3,
335     user_h => int $::HEIGHT / 5,
336 root 1.99 child => (my $vbox = new CFClient::UI::VBox);
337    
338 root 1.105 $vbox->add ($LOGVIEW = new CFClient::UI::TextView
339     expand => 1,
340     fontsize => $::CFG->{log_fontsize},
341     );
342    
343 root 1.122 $vbox->add (my $input = new CFClient::UI::Entry
344 elmex 1.118 connect_focus_in => sub {
345     my ($input, $prev_focus) = @_;
346    
347     delete $input->{refocus_map};
348    
349     if ($prev_focus == $MAPWIDGET && $input->{auto_activated}) {
350     $input->{refocus_map} = 1;
351     }
352     delete $input->{auto_activated};
353     },
354 root 1.116 connect_activate => sub {
355 elmex 1.100 my ($input, $text) = @_;
356     $input->set_text ('');
357    
358     if ($text =~ /^\/(.*)/) {
359 root 1.123 $::CONN->user_send ($1);
360 elmex 1.100 } else {
361 elmex 1.101 my $say_cmd = $::CFG->{say_command} || 'say';
362 root 1.123 $::CONN->user_send ("$say_cmd $text");
363 elmex 1.100 }
364 elmex 1.118 if ($input->{refocus_map}) {
365     delete $input->{refocus_map};
366     $MAPWIDGET->focus_in
367     }
368 root 1.116 },
369     connect_escape => sub {
370 elmex 1.102 $MAPWIDGET->focus_in
371 root 1.116 },
372     );
373 elmex 1.102
374     $CONSOLE = {
375     window => $window,
376     input => $input
377     };
378 root 1.99
379     $window
380     }
381    
382 root 1.89 sub sdl_init {
383 root 1.145 CFClient::SDL_Init
384 root 1.89 and die "SDL::Init failed!\n";
385     }
386    
387 root 1.134 sub video_init {
388 root 1.89 sdl_init;
389    
390 root 1.84 ($WIDTH, $HEIGHT) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
391     $FULLSCREEN = $CFG->{fullscreen};
392 root 1.89 $FAST = $CFG->{fast};
393 root 1.84
394 root 1.145 CFClient::SDL_SetVideoMode $WIDTH, $HEIGHT, $FULLSCREEN
395     or die "SDL_SetVideoMode failed!\n";
396 root 1.2
397 root 1.86 $SDL_ACTIVE = 1;
398    
399 root 1.87 $LAST_REFRESH = time - 0.01;
400 root 1.45
401 root 1.67 CFClient::gl_init;
402 root 1.30
403 root 1.140 $FONTSIZE = int $HEIGHT / 40 * $CFG->{gui_fontsize};
404 root 1.39
405 root 1.52 #############################################################################
406    
407 root 1.99 $DEBUG_STATUS = new CFClient::UI::Label padding => 0, z => 100;
408 root 1.140 $DEBUG_STATUS->show;
409 root 1.52
410 root 1.72 $STATUS_LINE = new CFClient::UI::Label
411 root 1.77 padding => 0,
412 root 1.140 y => $HEIGHT - $FONTSIZE * 1.8;
413     $STATUS_LINE->show;
414 root 1.51
415 root 1.72 $ALT_ENTER_MESSAGE = new CFClient::UI::Label
416 root 1.123 padding => 0,
417 root 1.140 fontsize => 0.8,
418 root 1.123 markup => "Use <b>Alt-Enter</b> to toggle fullscreen mode";
419 root 1.140 $ALT_ENTER_MESSAGE->show;
420     $ALT_ENTER_MESSAGE->move (0, $HEIGHT - $ALT_ENTER_MESSAGE->{h});
421 root 1.30
422 root 1.141 $CFClient::UI::ROOT->add ($MAPWIDGET = new CFClient::MapWidget);
423 root 1.69 $MAPWIDGET->focus_in;
424 elmex 1.102 $MAPWIDGET->connect (activate_console => sub {
425 elmex 1.103 my ($mapwidget, $preset) = @_;
426    
427 elmex 1.102 if ($CONSOLE) {
428 elmex 1.118 $CONSOLE->{input}->{auto_activated} = 1;
429 elmex 1.102 $CONSOLE->{input}->focus_in;
430 elmex 1.103
431     if ($preset && $CONSOLE->{input}->get_text eq '') {
432     $CONSOLE->{input}->set_text ($preset);
433     }
434 elmex 1.102 }
435     });
436 root 1.81
437 root 1.111 $CFClient::UI::ROOT->add ($BUTTONBAR = new CFClient::UI::HBox);
438    
439     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Client Setup", other => client_setup);
440     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Server Setup", other => server_setup);
441     $BUTTONBAR->add (new CFClient::UI::Flopper text => "Message Window", other => message_window);
442    
443     $BUTTONBAR->add (new CFClient::UI::Button text => "Save Config", connect_activate => sub {
444     CFClient::write_cfg "$Crossfire::VARDIR/pclientrc";
445     status "Configuration Saved";
446     });
447 root 1.98
448 root 1.119 $BUTTONBAR->{children}[1]->emit ("activate"); # pop up server setup
449 elmex 1.125
450 elmex 1.127 my $tgw = new CFClient::UI::FancyFrame (x => $WIDTH - 300, y => 0);
451     $tgw->add (my $hbox = new CFClient::UI::HBox ());
452 root 1.126
453 root 1.129 $hbox->add (my $hg = new CFClient::UI::VGauge (gauge => 'hp'));
454     $hbox->add (my $mg = new CFClient::UI::VGauge (gauge => 'mana'));
455     $hbox->add (my $gg = new CFClient::UI::VGauge (gauge => 'grace'));
456     $hbox->add (my $fg = new CFClient::UI::VGauge (gauge => 'food'));
457 elmex 1.125
458     $GAUGES = { food => $fg, mana => $mg, hp => $hg, grace => $gg };
459     $CFClient::UI::ROOT->add ($tgw);
460 root 1.2 }
461    
462 root 1.134 sub video_shutdown {
463 root 1.111 $CFClient::UI::ROOT->{children} = [];
464 root 1.86 undef $SDL_ACTIVE;
465 root 1.134 }
466    
467 root 1.135 my $bgmusic;#TODO#hack#d#
468    
469 root 1.134 sub audio_init {
470 root 1.139 if ($CFG->{audio_enable}) {
471 root 1.134 if (open my $fh, "<:utf8", CFClient::find_rcfile "sounds/config") {
472 root 1.142
473 root 1.146 $SDL_MIXER = !CFClient::Mix_OpenAudio;
474     CFClient::Mix_AllocateChannels 8;
475     CFClient::MixMusic::volume $CFG->{bgm_volume};
476 root 1.134
477 root 1.139 # TODO: hack, do play loop and mood music
478     if ($CFG->{bgm_enable}) {
479 root 1.146 $bgmusic = new_from_file CFClient::MixMusic CFClient::find_rcfile "music/game3.ogg";
480     $bgmusic->play;
481 elmex 1.137 }
482 root 1.135
483 root 1.134 while (<$fh>) {
484     next if /^\s*#/;
485     next if /^\s*$/;
486    
487     my ($file, $volume, $event) = split /\s+/, $_, 3;
488    
489     push @SOUNDS, "$volume,$file";
490    
491     $AUDIO_CHUNKS{"$volume,$file"} ||= do {
492 root 1.146 my $chunk = new_from_file CFClient::MixChunk CFClient::find_rcfile "sounds/$file";
493 root 1.134 $chunk->volume ($volume * 128 / 100);
494     $chunk
495     };
496     }
497     } else {
498     status "unable to open sound config: $!";
499     }
500     }
501     }
502    
503     sub audio_shutdown {
504 root 1.146 CFClient::Mix_CloseAudio if $SDL_MIXER;
505 root 1.134 undef $SDL_MIXER;
506     @SOUNDS = ();
507     %AUDIO_CHUNKS = ();
508 root 1.62 }
509    
510 root 1.87 my %animate_object;
511     my $animate_timer;
512    
513     my $want_refresh;
514     my $can_refresh;
515    
516     my $fps = 9;
517    
518 root 1.30 sub force_refresh {
519 root 1.87 $fps = $fps * 0.95 + 1 / ($NOW - $LAST_REFRESH) * 0.05;
520     debug sprintf "%3.2f", $fps;
521    
522 root 1.96 $want_refresh = 0;
523 root 1.87 $can_refresh = 0;
524    
525 root 1.111 $CFClient::UI::ROOT->draw;
526 root 1.1
527 root 1.2 SDL::GLSwapBuffers;
528 root 1.87
529     $LAST_REFRESH = $NOW;
530 root 1.1 }
531    
532 root 1.87 my $refresh_watcher = Event->timer (after => 0, hard => 1, interval => 1 / $MAX_FPS, cb => sub {
533     $NOW = time;
534    
535 root 1.147 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
536     for CFClient::SDL_PollEvent;
537 root 1.87
538     if (%animate_object) {
539     $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
540     $want_refresh++;
541     }
542    
543     if ($want_refresh) {
544     force_refresh;
545     } else {
546     $can_refresh = 1;
547     }
548     });
549 root 1.64
550 root 1.30 sub refresh {
551 root 1.87 $want_refresh++;
552 root 1.30 }
553    
554 root 1.45 sub animation_start {
555     my ($widget) = @_;
556 root 1.87 $animate_object{$widget} = $widget;
557 root 1.45 }
558    
559     sub animation_stop {
560     my ($widget) = @_;
561 root 1.87 delete $animate_object{$widget};
562 root 1.45 }
563    
564 root 1.2 @conn::ISA = Crossfire::Protocol::;
565 root 1.1
566 elmex 1.125 sub conn::stats_update {
567     my ($self, $stats) = @_;
568    
569     # i love text protocols!!!
570     # FIXME: the stats are somehow weird
571     my $hp = $stats->{1};
572     my $hp_m = $stats->{2};
573     my $sp = $stats->{3};
574     my $sp_m = $stats->{4};
575     my $fo = $stats->{18};
576     my $fo_m = 1000;
577     my $gr = $stats->{23};
578     my $gr_m = $stats->{24};
579    
580     #d# warn "DATA $hp $hp_m $sp $sp_m $fo $fo_m $gr $gr_m\n";
581     $GAUGES->{hp}->set_value ($hp, $hp_m);
582     $GAUGES->{mana}->set_value ($sp, $sp_m);
583     $GAUGES->{food}->set_value ($fo, $fo_m);
584     $GAUGES->{grace}->set_value ($gr, $gr_m);
585     }
586    
587 root 1.89 sub conn::user_send {
588 root 1.88 my ($self, $command) = @_;
589    
590 root 1.123 $self->send_command ($command);
591 root 1.88 status $command;
592     }
593    
594 root 1.119 sub conn::map_scroll {
595     my ($self, $dx, $dy) = @_;
596    
597     $MAP->scroll ($dx, $dy);
598     }
599    
600 root 1.94 sub conn::feed_map1a {
601     my ($self, $data) = @_;
602    
603 root 1.95 # $self->Crossfire::Protocol::feed_map1a ($data);
604 root 1.1
605 root 1.95 $MAP->map1a_update ($data);
606 root 1.69 $MAPWIDGET->update;
607 root 1.1 }
608    
609 root 1.116 sub conn::flush_map {
610     my ($self) = @_;
611    
612     my $map_info = delete $self->{map_info}
613     or return;
614    
615     my ($hash, $x, $y, $w, $h) = @$map_info;
616    
617     my $data = $MAP->get_rect ($x, $y, $w, $h);
618     $MAPCACHE->put ($hash => Compress::LZF::compress $data);
619    
620     warn sprintf "SAVEmap[%s] length %d\n", $hash, length $data;#d#
621    
622     }
623 root 1.1
624 root 1.2 sub conn::map_clear {
625 root 1.1 my ($self) = @_;
626    
627 root 1.116 $self->flush_map;
628 root 1.119 delete $self->{neigh};
629 root 1.116
630 root 1.95 $MAP->clear;
631 root 1.1 }
632    
633 root 1.116
634 root 1.119 sub conn::load_map($$$) {
635     my ($self, $hash, $x, $y) = @_;
636 root 1.115
637 root 1.116 if (defined (my $data = $MAPCACHE->get ($hash))) {
638     $data = Compress::LZF::decompress $data;
639 root 1.119 warn sprintf "LOADmap[%s,%d,%d] length %d\n", $hash, $x, $y, length $data;#d#
640 root 1.116 for my $id ($MAP->set_rect ($x, $y, $data)) {
641     my $data = $TILECACHE->get ($id)
642     or next;
643    
644     $self->set_texture ($id => $data);
645     }
646     }
647 root 1.115 }
648    
649 root 1.119 sub conn::flood_fill {
650     my ($self, $path, $hash, $flags, $x0, $y0, $x1, $y1) = @_;
651    
652 root 1.121 # the server does not allow map paths > 6
653 root 1.120 return if 6 <= length $path;
654    
655 root 1.119 for my $tile (1..4) {
656     next if $self->{neigh}{$hash}[$tile];
657     next unless $flags & (1 << ($tile - 1));
658    
659     my $neigh = $self->{neigh}{$hash} ||= [];
660    
661     $self->send_mapinfo ("spatial $path$tile", sub {
662     my ($mode, $flags, $x, $y, $w, $h, $hash) = @_;
663    
664 root 1.122 #warn "map<$path>_$tile=<$mode,$x,$y,$w,$h,$hash>\n";#d#
665 root 1.119 return if $mode ne "spatial";
666    
667     $x += $MAP->ox;
668     $y += $MAP->oy;
669    
670 root 1.124 $self->load_map ($hash, $x, $y)
671     unless $self->{neigh}{$hash}[5]++;#d#
672 root 1.119
673     $neigh->[$tile] = [$x, $y, $w, $h];
674    
675     $self->flood_fill ("$path$tile", $hash, $flags, $x0, $y0, $x1, $y1)
676     if $x >= $x0 && $x + $w < $x1 && $y >= $y0 && $y + $h < $y1;
677     });
678     }
679     }
680    
681     sub conn::map_change {
682     my ($self, $mode, $flags, $x, $y, $w, $h, $hash) = @_;
683    
684     $self->flush_map;
685    
686     my ($ox, $oy) = ($::MAP->ox, $::MAP->oy);
687    
688     my $mapmapw = 250;
689     my $mapmaph = 250;
690    
691     $self->flood_fill ("", $hash, $flags,
692     $ox - $mapmapw * 0.5, $oy - $mapmapw * 0.5,
693     $ox + $mapmapw * 0.5, $oy + $mapmapw * 0.5);
694    
695     $x += $ox;
696     $y += $oy;
697    
698     $self->{map_info} = [$hash, $x, $y, $w, $h];
699    
700     $self->load_map ($hash, $x, $y);
701     }
702    
703 root 1.19 sub conn::face_find {
704 root 1.116 my ($self, $facenum, $face) = @_;
705    
706     my $hash = "$face->{chksum},$face->{name}";
707    
708     my $id = $FACEMAP->get ($hash);
709    
710     unless ($id) {
711     # create new id for face
712     # i love transactions
713     for (1..100) {
714     my $txn = $CFClient::DB_ENV->txn_begin;
715     my $status = $FACEMAP->db_get (id => $id, BerkeleyDB::DB_RMW);
716     if ($status == 0 || $status == BerkeleyDB::DB_NOTFOUND) {
717     $id++;
718     if ($FACEMAP->put (id => $id) == 0
719     && $FACEMAP->put ($hash => $id) == 0) {
720     $txn->txn_commit;
721    
722     goto gotid;
723     }
724     }
725     $txn->abort;
726     }
727 root 1.19
728 root 1.116 CFClient::fatal "maximum number of transaction retries reached - database problems?";
729     }
730 root 1.114
731 root 1.116 gotid:
732     $face->{id} = $id;
733     $MAP->set_face ($facenum => $id);
734     $TILECACHE->get ($id)
735 root 1.19 }
736    
737 root 1.2 sub conn::face_update {
738 root 1.95 my ($self, $facenum, $face) = @_;
739 root 1.19
740 root 1.116 $TILECACHE->put ($face->{id} => $face->{image}); #TODO: try to avoid duplicate writes
741    
742     $self->set_texture ($face->{id} => delete $face->{image});
743     }
744 root 1.1
745 root 1.116 sub conn::set_texture {
746     my ($self, $id, $data) = @_;
747 root 1.95
748 root 1.116 $self->{texture}[$id] ||= do {
749     my $tex =
750     new_from_image CFClient::Texture
751     $data, minify => 1;
752    
753     $MAP->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}});
754     $MAPWIDGET->update;
755    
756     $tex
757     };
758 root 1.1 }
759    
760 root 1.134 sub conn::sound_play {
761     my ($self, $x, $y, $soundnum, $type) = @_;
762    
763 root 1.139 $SDL_MIXER
764     or return;
765    
766 root 1.134 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
767     or return;
768    
769 root 1.146 $chunk->play;
770 root 1.143 # warn "sound $x,$y,$soundnum,$type\n";#d#
771 root 1.134 }
772    
773 root 1.33 sub conn::query {
774     my ($self, $flags, $prompt) = @_;
775    
776 root 1.143 #TODO, display dialog with relevant information
777 root 1.33 warn "<<<<QUERY:$flags:$prompt>>>\n";#d#
778     }
779    
780 root 1.99 sub conn::drawinfo {
781     my ($self, $color, $text) = @_;
782    
783     my @color = (
784     [1.00, 1.00, 1.00], #[0.00, 0.00, 0.00],
785     [1.00, 1.00, 1.00],
786 root 1.117 [0.50, 0.50, 1.00], #[0.00, 0.00, 0.55]
787 root 1.99 [1.00, 0.00, 0.00],
788     [1.00, 0.54, 0.00],
789     [0.11, 0.56, 1.00],
790     [0.93, 0.46, 0.00],
791     [0.18, 0.54, 0.34],
792     [0.56, 0.73, 0.56],
793     [0.80, 0.80, 0.80],
794     [0.55, 0.41, 0.13],
795     [0.99, 0.77, 0.26],
796     [0.74, 0.65, 0.41],
797     );
798    
799     $LOGVIEW->add_paragraph ($color[$color], $text);
800     }
801    
802 root 1.144 sub conn::spell_add {
803 root 1.143 my ($self, $spell) = @_;
804    
805 root 1.144 $MAPWIDGET->add_command ("invoke $spell->{name}", $spell->{message}, sub {
806     });
807     $MAPWIDGET->add_command ("cast $spell->{name}", $spell->{message}, sub {
808     });
809     }
810    
811     sub conn::spell_delete {
812     my ($self, $spell) = @_;
813     }
814    
815     sub conn::addme_success {
816     my ($self) = @_;
817    
818     for my $skill (values %{$self->{skill_info}}) {
819     $MAPWIDGET->add_command ("ready_skill $skill", "", sub {
820     });
821     $MAPWIDGET->add_command ("use_skill $skill", "", sub {
822     });
823     }
824 root 1.143 }
825    
826 root 1.87 %SDL_CB = (
827 root 1.145 CFClient::SDL_QUIT => sub {
828 root 1.87 Event::unloop -1;
829     },
830 root 1.145 CFClient::SDL_VIDEORESIZE => sub {
831 root 1.87 },
832 root 1.145 CFClient::SDL_VIDEOEXPOSE => sub {
833 root 1.87 refresh;
834     },
835 root 1.145 CFClient::SDL_KEYDOWN => sub {
836 root 1.147 if ($_[0]{mod} & CFClient::KMOD_ALT && $_[0]{sym} == 13) {
837 root 1.87 # alt-enter
838 root 1.134 video_shutdown;
839 root 1.99 $CFG->{fullscreen} = !$CFG->{fullscreen};
840 root 1.134 video_init;
841 root 1.87 } else {
842 root 1.147 CFClient::UI::feed_sdl_key_down_event ($_[0]);
843 elmex 1.23 }
844 root 1.87 },
845 root 1.145 CFClient::SDL_KEYUP => sub {
846 root 1.147 CFClient::UI::feed_sdl_key_up_event ($_[0]);
847 root 1.87 },
848 root 1.145 CFClient::SDL_MOUSEMOTION => sub {
849 root 1.147 CFClient::UI::feed_sdl_motion_event ($_[0]);
850 root 1.87 },
851 root 1.145 CFClient::SDL_MOUSEBUTTONDOWN => sub {
852 root 1.147 CFClient::UI::feed_sdl_button_down_event ($_[0]);
853 root 1.87 },
854 root 1.145 CFClient::SDL_MOUSEBUTTONUP => sub {
855 root 1.147 CFClient::UI::feed_sdl_button_up_event ($_[0]);
856 root 1.87 },
857 root 1.145 CFClient::SDL_ACTIVEEVENT => sub {
858 root 1.87 # printf "active %x %x\n", $SDL_EV->active_gain, $SDL_EV->active_state;#d#
859     },
860     );
861 elmex 1.23
862 root 1.1 #############################################################################
863    
864 root 1.131 $SIG{INT} = $SIG{TERM} = sub { exit };
865    
866 root 1.116 $TILECACHE = CFClient::db_table "tilecache";
867     $FACEMAP = CFClient::db_table "facemap";
868 root 1.114
869 root 1.67 CFClient::read_cfg "$Crossfire::VARDIR/pclientrc";
870 elmex 1.10
871 root 1.90 my %DEF_CFG = (
872 root 1.105 sdl_mode => 0,
873 root 1.90 width => 640,
874     height => 480,
875 root 1.105 fullscreen => 0,
876 root 1.90 fast => 0,
877 root 1.97 fow_enable => 1,
878 root 1.90 fow_intensity => 0.45,
879 root 1.92 fow_smooth => 0,
880 root 1.140 gui_fontsize => 1,
881 root 1.105 log_fontsize => 14,
882 root 1.90 mapsize => 100,
883     host => "crossfire.schmorp.de",
884 elmex 1.101 say_command => 'say',
885 root 1.139 audio_enable => 1,
886     bgm_enable => 1,
887 root 1.140 bgm_volume => 64,
888 root 1.90 );
889    
890     while (my ($k, $v) = each %DEF_CFG) {
891     $CFG->{$k} = $v unless exists $CFG->{$k};
892     }
893 elmex 1.12
894 root 1.89 sdl_init;
895 root 1.87
896 root 1.93 @SDL_MODES = reverse
897     grep $_->[0] >= 640 && $_->[1] >= 480,
898 root 1.145 CFClient::SDL_ListModes;
899 root 1.87
900 root 1.89 @SDL_MODES or CFClient::fatal "Unable to find a usable video mode\n(hardware accelerated opengl fullscreen)";
901    
902     $CFG->{sdl_mode} = 0 if $CFG->{sdl_mode} > @SDL_MODES;
903    
904 root 1.65 {
905 root 1.67 my @fonts = map CFClient::find_rcfile $_, qw(uifont.ttf uifontb.ttf uifonti.ttf uifontbi.ttf);
906 root 1.65
907 root 1.67 CFClient::add_font $_ for @fonts;
908     CFClient::set_font $fonts[0];
909 root 1.65 }
910 root 1.40
911 root 1.134 video_init;
912     audio_init;
913 root 1.122
914 root 1.87 Event::loop;
915 root 1.19
916 root 1.131 END { SDL::Quit }
917    
918 root 1.82