ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/pclient
Revision: 1.146
Committed: Wed Apr 19 21:17:16 2006 UTC (18 years, 1 month ago) by root
Branch: MAIN
Changes since 1.145: +10 -17 lines
Log Message:
got rid of SDL::Sound SDL::Music and SDL::Mixer

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