ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/bin/cfplus
(Generate patch)

Comparing deliantra/Deliantra-Client/bin/cfplus (file contents):
Revision 1.190 by root, Sun Jul 29 18:56:03 2007 UTC vs.
Revision 1.199 by root, Wed Aug 1 14:27:21 2007 UTC

71unshift @INC, $ENV{PAR_TEMP} 71unshift @INC, $ENV{PAR_TEMP}
72 if %PAR::LibCache; 72 if %PAR::LibCache;
73 73
74use Time::HiRes 'time'; 74use Time::HiRes 'time';
75use Event; 75use Event;
76use List::Util qw(max min);
76 77
77use Crossfire; 78use Crossfire;
78use Crossfire::Protocol::Constants; 79use Crossfire::Protocol::Constants;
79 80
80use Compress::LZF; 81use Compress::LZF;
158our $STATWIDS; 159our $STATWIDS;
159 160
160our $SDL_ACTIVE; 161our $SDL_ACTIVE;
161our %SDL_CB; 162our %SDL_CB;
162 163
164our $ALT_ENTER_MESSAGE;
165our $STATUSBOX;
166our $DEBUG_STATUS;
167
168our $INV;
169our $INVR;
170our $INV_RIGHT_HB;
171
172our $PICKUP_CFG;
173
174#############################################################################
175
176sub status {
177 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
178}
179
180sub debug {
181 $DEBUG_STATUS->set_text ($_[0]);
182}
183
184sub message {
185 my ($para) = @_;
186 $MESSAGE_WINDOW->message ($para);
187}
188
189#############################################################################
190#TODO: maybe move into own audio module...
191
163our $SDL_MIXER; 192our $SDL_MIXER;
193
164our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; 194our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
165our @MUSIC_WANT; 195our $MUSIC_WANT; # arryref of ambient music we want to play
196our @MUSIC_HAVE; # ambient music we have on disk
166our $MUSIC_START; 197our $MUSIC_START;
198our @MUSIC_JINGLE; # which jingles to play next
167our $MUSIC_PLAYING_DATA; 199our $MUSIC_PLAYING_DATA;
168our $MUSIC_PLAYING_META; 200our $MUSIC_PLAYING_META;
169our $MUSIC_PLAYER; 201our $MUSIC_PLAYER;
170our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before 202our $MUSIC_RESUME = 30; # resume music when played less than these many seconds before
171our @SOUNDS; # event => file mapping 203
172our %AUDIO_CHUNKS; # audio files 204our %AUDIO_CHUNK; # audio "files"
205our %AUDIO_PLAY; # which audio faces should be played
173 206
174our $ALT_ENTER_MESSAGE; 207sub audio_channel_finished {
175our $STATUSBOX; 208 my ($channel) = @_;
176our $DEBUG_STATUS;
177 209
178our $INV; 210# warn "channel $channel finished\n";#d#
179our $INVR;
180our $INV_RIGHT_HB;
181
182our $PICKUP_CFG;
183
184sub status {
185 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
186} 211}
187 212
188sub debug { 213sub audio_sound_push($) {
189 $DEBUG_STATUS->set_text ($_[0]);
190}
191
192sub message {
193 my ($para) = @_; 214 my ($face) = @_;
194 $MESSAGE_WINDOW->message ($para); 215
216 $CFG->{effects_enable}
217 or return;
218
219 if (my $chunk = $AUDIO_CHUNK{$face}) {
220 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
221 my (undef, $dx, $dy, $vol) = @$_;
222
223 my $channel = CFPlus::Channel::find;
224 $channel->volume ($vol * $CFG->{effects_volume} * 128 / 255);
225 $dx = $dx / 10 * 255;
226 $channel->set_panning (255 - $dx, 255 + $dx);
227
228# my $angle = $dx ? : $dx < 0 ?
229# my $distance = -$vol;
230# $channel->set_position ($angle, $distance);
231
232 $chunk->play ($channel);
233 }
234 } else {
235 # sound_meta not set means data is in flight either way
236 my $meta = $CONN->{sound_meta}{$face}
237 or return;
238
239 # if its a jingle, play it as ambient music
240 if ($meta->{meta}{jingle}) {
241 delete $AUDIO_PLAY{$face}; # take the jingle out of the sound queue
242 push @MUSIC_JINGLE, $meta; # push it oto the music/jingle queue
243 &audio_music_push ($face);
244 } else {
245 # fetch from database
246 CFPlus::DB::get res_data => $meta->{name}, sub {
247 my $rwops = new CFPlus::RW $_[0];
248 my $chunk = new CFPlus::MixChunk $rwops;
249 $chunk->volume (($meta->{meta}{volume} || 1) * 128);
250 $AUDIO_CHUNK{$face} = $chunk;
251
252 audio_sound_push ($face);
253 };
254 }
255 }
195} 256}
257
258sub audio_sound_play {
259 my ($face, $dx, $dy, $vol) = @_;
260
261 $SDL_MIXER
262 or return;
263 $CFG->{effects_enable}
264 or return;
265
266 my $queue = $AUDIO_PLAY{$face} ||= [];
267 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
268 audio_sound_push $face
269 unless @$queue > 1;
270}
271
272sub audio_music_update_volume {
273 return unless $MUSIC_PLAYING_META;
274 my $volume = $MUSIC_PLAYING_META->{meta}{volume} || 1;
275 my $base = $MUSIC_PLAYING_META->{meta}{jingle} ? 1 : $CFG->{bgm_volume};
276 CFPlus::MixMusic::volume $base * $volume * 128;
277}
278
279sub audio_music_start {
280 my $meta = $MUSIC_PLAYING_META;
281
282 CFPlus::DB::get res_data => $meta->{name}, sub {
283 return unless $SDL_MIXER;
284
285 # music might have changed...
286 $meta eq $MUSIC_PLAYING_META
287 or return &audio_music_start ();
288
289 audio_music_update_volume;
290
291 $MUSIC_PLAYING_DATA = \$_[0];
292
293 my $rwops = $meta->{path}
294 ? new_from_file CFPlus::RW $meta->{path}
295 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
296
297 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
298 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
299
300 my $NOW = time;
301
302 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
303 my $pos = $MUSIC_PLAYING_META->{stop_pos};
304 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
305 $MUSIC_START = time - $pos;
306 } else {
307 $MUSIC_PLAYER->play (0);
308 $MUSIC_START = time;
309 }
310
311 delete $MUSIC_PLAYING_META->{stop_time};
312 delete $MUSIC_PLAYING_META->{stop_pos};
313 }
314}
315
316sub audio_music_push {
317 return unless $SDL_MIXER;
318
319 my $fade_out;
320
321 if (@MUSIC_JINGLE) {
322 @MUSIC_HAVE = $MUSIC_JINGLE[0];
323 $fade_out = 333;
324 } else {
325 return unless $CFG->{bgm_enable};
326
327 my @have =
328 grep $_,
329 map $CONN->{music_meta}{$_},
330 @$MUSIC_WANT;
331
332 @MUSIC_HAVE = @have
333 if @have;
334
335 # default MUSIC_HAVE == MUSIC_DEFAULT
336 @MUSIC_HAVE = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_HAVE;
337 $fade_out = 1000;
338 }
339
340 # if the currently playing song is acceptable, let it continue
341 return if $MUSIC_PLAYING_META
342 && grep $MUSIC_PLAYING_META == $_, @MUSIC_HAVE;
343
344 my $NOW = time;
345
346 if ($MUSIC_PLAYING_META) {
347 $MUSIC_PLAYING_META->{stop_time} = $NOW;
348 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
349 CFPlus::MixMusic::fade_out $fade_out;
350 } else {
351 # sort by stop time, oldest first
352 @MUSIC_HAVE = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_HAVE;
353
354 # if the most recently-played piece played very recently,
355 # resume it, else choose the oldest piece for rotation.
356 $MUSIC_PLAYING_META =
357 $MUSIC_HAVE[-1]{stop_time} > $NOW - $MUSIC_RESUME
358 ? $MUSIC_HAVE[-1]
359 : $MUSIC_HAVE[0];
360
361 audio_music_start;
362 }
363}
364
365sub audio_music_set_ambient {
366 my ($songs) = @_;
367
368 $MUSIC_WANT = $songs;
369 audio_music_push;
370}
371
372sub audio_music_finished {
373 # we compress multiple jingles of the same type
374 shift @MUSIC_JINGLE
375 while @MUSIC_JINGLE && $MUSIC_PLAYING_META == $MUSIC_JINGLE[0];
376
377 undef $MUSIC_PLAYER;
378 undef $MUSIC_PLAYING_META;
379 undef $MUSIC_PLAYING_DATA;
380
381 audio_music_push;
382}
383
384sub audio_init {
385 if ($CFG->{audio_enable}) {
386 $ENV{MIX_EFFECTSMAXSPEED} = 1;
387 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
388
389 unless ($SDL_MIXER) {
390 status "Unable to open sound device: there will be no sound";
391 return;
392 }
393
394 CFPlus::Mix_AllocateChannels 16;
395
396 audio_music_finished;
397 } else {
398 undef $SDL_MIXER;
399 }
400}
401
402sub audio_shutdown {
403 undef $MUSIC_PLAYER;
404 undef $MUSIC_PLAYING_META;
405 undef $MUSIC_PLAYING_DATA;
406
407 $MUSIC_WANT = [];
408 @MUSIC_JINGLE = ();
409 %AUDIO_PLAY = ();
410 %AUDIO_CHUNK = ();
411
412 CFPlus::Mix_CloseAudio if $SDL_MIXER;
413 undef $SDL_MIXER;
414}
415
416#############################################################################
196 417
197sub destroy_query_dialog { 418sub destroy_query_dialog {
198 (delete $_[0]{query_dialog})->destroy 419 (delete $_[0]{query_dialog})->destroy
199 if $_[0]{query_dialog}; 420 if $_[0]{query_dialog};
200} 421}
460 query => \&server_query, 681 query => \&server_query,
461 682
462 setup_req => { 683 setup_req => {
463 smoothing => $CFG->{map_smoothing}*1, 684 smoothing => $CFG->{map_smoothing}*1,
464 }, 685 },
465
466 sound_play => sub {
467 my ($x, $y, $soundnum, $type) = @_;
468
469 $SDL_MIXER
470 or return;
471
472 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
473 or return;
474
475 $chunk->play;
476 },
477 }; 686 };
478 687
479 if ($CONN) { 688 if ($CONN) {
480 CFPlus::lowdelay fileno $CONN->{fh}; 689 CFPlus::lowdelay fileno $CONN->{fh};
481 690
492 $SETUP_DIALOG->show; 701 $SETUP_DIALOG->show;
493 $PL_WINDOW->hide; 702 $PL_WINDOW->hide;
494 $SPELL_LIST->clear_spells; 703 $SPELL_LIST->clear_spells;
495 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); 704 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN);
496 705
497 &audio_music_set ([]); 706 &audio_music_set_ambient ([]);
498 707
499 return unless $CONN; 708 return unless $CONN;
500 709
501 status "connection closed"; 710 status "connection closed";
502 711
635} 844}
636 845
637sub audio_setup { 846sub audio_setup {
638 my $vbox = new CFPlus::UI::VBox; 847 my $vbox = new CFPlus::UI::VBox;
639 848
640 $vbox->add (my $table = new CFPlus::UI::Table expand => 1, col_expand => [0, 1]); 849 $vbox->add (my $table = new CFPlus::UI::Table expand => 1, col_expand => [0, 0, 1]);
641 850
642 my $row = 0; 851 my $row = 0;
643 852
644 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Audio Enable"); 853 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Audio Enable");
645 $table->add_at (1, $row++, new CFPlus::UI::CheckBox 854 $table->add_at (1, $row++, new CFPlus::UI::CheckBox
649 ); 858 );
650# $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Effects Volume"); 859# $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Effects Volume");
651# $table->add_at (1, 8, new CFPlus::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], on_changed => sub { 860# $table->add_at (1, 8, new CFPlus::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], on_changed => sub {
652# $CFG->{effects_volume} = $_[1]; 861# $CFG->{effects_volume} = $_[1];
653# }); 862# });
863
864 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Effects Volume");
865 $table->add_at (1, $row, new CFPlus::UI::CheckBox
866 expand => 1, state => $CFG->{effects_enable},
867 tooltip => "If enabled, sound effects are enabled. If disabled, no sound effects will be played.",
868 on_changed => sub { $CFG->{effects_enable} = $_[1]; 0 }
869 );
870 $table->add_at (2, $row++, new CFPlus::UI::Slider
871 expand => 1, range => [$CFG->{effects_volume}, 0, 1, 0, 1/128],
872 tooltip => "The relative volume of sound effects. Best audio quality is achieved if this "
873 . "is set highest and you use your operating system volume setting. Changes are instant.",
874 on_changed => sub { $CFG->{effects_volume} = $_[1]; 0 }
875 );
876
654 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Background Music"); 877 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Background Music");
655 $table->add_at (1, $row++, my $hbox = new CFPlus::UI::HBox); 878 $table->add_at (1, $row, new CFPlus::UI::CheckBox
656 $hbox->add (new CFPlus::UI::CheckBox
657 expand => 1, state => $CFG->{bgm_enable}, 879 expand => 1, state => $CFG->{bgm_enable},
658 tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played.", 880 tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played.",
659 on_changed => sub { $CFG->{bgm_enable} = $_[1]; 0 } 881 on_changed => sub { $CFG->{bgm_enable} = $_[1]; 0 }
660 ); 882 );
661 $hbox->add (new CFPlus::UI::Slider 883 $table->add_at (2, $row++, new CFPlus::UI::Slider
662 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128], 884 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128],
663 tooltip => "The volume of the background music. Changes are instant.", 885 tooltip => "The volume of the background music. Changes are instant.",
664 on_changed => sub { $CFG->{bgm_volume} = $_[1]; CFPlus::MixMusic::volume $_[1] * 128; 0 } 886 on_changed => sub { $CFG->{bgm_volume} = $_[1]; audio_music_update_volume; 0 }
665 ); 887 );
666 888
667 $table->add_at (1, $row++, new CFPlus::UI::Button 889 $table->add_at (1, $row++, new CFPlus::UI::Button
668 expand => 1, align => 0, text => "Apply", 890 c_colspan => 2, expand => 1, align => 0, text => "Apply",
669 tooltip => "Apply the audio settings", 891 tooltip => "Apply the audio settings",
670 on_activate => sub { 892 on_activate => sub {
671 audio_shutdown (); 893 audio_shutdown ();
672 audio_init (); 894 audio_init ();
673 0 895 0
744 $table->add_at (0, 4, new CFPlus::UI::Button text => "die on click(tm)", on_activate => sub { &CFPlus::debug() } ); 966 $table->add_at (0, 4, new CFPlus::UI::Button text => "die on click(tm)", on_activate => sub { &CFPlus::debug() } );
745 967
746 $table->add_at (0, 5, new CFPlus::UI::TextEdit text => "line1\0152\0153");#d# 968 $table->add_at (0, 5, new CFPlus::UI::TextEdit text => "line1\0152\0153");#d#
747 969
748 $table->add_at (7,7, my $t = new CFPlus::UI::Table expand => 0); 970 $table->add_at (7,7, my $t = new CFPlus::UI::Table expand => 0);
749 $t->add_at (0,0, new CFPlus::UI::Label text => "a a a a", rowspan => 1, colspan => 2); 971 $t->add_at (0,0, new CFPlus::UI::Label text => "a a a a", c_rowspan => 1, c_colspan => 2);
750 $t->add_at (2,0, new CFPlus::UI::Label text => "b\nb", rowspan => 2, colspan => 1); 972 $t->add_at (2,0, new CFPlus::UI::Label text => "b\nb", c_rowspan => 2, c_colspan => 1);
751 $t->add_at (1,2, new CFPlus::UI::Label text => "c c c c", rowspan => 1, colspan => 2); 973 $t->add_at (1,2, new CFPlus::UI::Label text => "c c c c", c_rowspan => 1, c_colspan => 2);
752 $t->add_at (0,1, new CFPlus::UI::Label text => "d\nd", rowspan => 2, colspan => 1); 974 $t->add_at (0,1, new CFPlus::UI::Label text => "d\nd", c_rowspan => 2, c_colspan => 1);
753 $t->add_at (1,1, new CFPlus::UI::Label text => "e"); 975 $t->add_at (1,1, new CFPlus::UI::Label text => "e");
754 976
755 $table->add_at (7, 6, my $c = new CFPlus::UI::Canvas); 977 $table->add_at (7, 6, my $c = new CFPlus::UI::Canvas);
756 978
757 $c->add_items ({ 979 $c->add_items ({
1082 text => $CFG->{profile}{default}{host}, 1304 text => $CFG->{profile}{default}{host},
1083 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to", 1305 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to",
1084 on_changed => sub { 1306 on_changed => sub {
1085 my ($self, $value) = @_; 1307 my ($self, $value) = @_;
1086 $CFG->{profile}{default}{host} = $value; 1308 $CFG->{profile}{default}{host} = $value;
1087 0 1309 1
1088 } 1310 }
1089 ); 1311 );
1090 1312
1091 $vbox->add (new CFPlus::UI::Button 1313 $vbox->add (new CFPlus::UI::Button
1092 expand => 1, 1314 expand => 1,
1093 text => "Server List", 1315 text => "Server List",
1094 other => $METASERVER, 1316 other => $METASERVER,
1095 tooltip => "Show a list of available crossfire servers", 1317 tooltip => "Show a list of available crossfire servers",
1096 on_activate => sub { $METASERVER->toggle_visibility; 0 }, 1318 on_activate => sub { $METASERVER->toggle_visibility; 0 },
1097 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 0 }, 1319 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 },
1098 ); 1320 );
1099 } 1321 }
1100 1322
1101 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username"); 1323 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username");
1102 $table->add_at (1, 4, new CFPlus::UI::Entry 1324 $table->add_at (1, 4, new CFPlus::UI::Entry
1103 text => $CFG->{profile}{default}{user}, 1325 text => $CFG->{profile}{default}{user},
1104 tooltip => "The name of your character on the server", 1326 tooltip => "The name of your character on the server",
1105 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value } 1327 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value; 1 }
1106 ); 1328 );
1107 1329
1108 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password"); 1330 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password");
1109 $table->add_at (1, 5, new CFPlus::UI::Entry 1331 $table->add_at (1, 5, new CFPlus::UI::Entry
1110 text => $CFG->{profile}{default}{password}, 1332 text => $CFG->{profile}{default}{password},
1111 hidden => 1, 1333 hidden => 1,
1112 tooltip => "The password for your character", 1334 tooltip => "The password for your character",
1113 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value } 1335 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value; 1 }
1114 ); 1336 );
1115 1337
1116 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size"); 1338 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size");
1117 $table->add_at (1, 7, new CFPlus::UI::Slider 1339 $table->add_at (1, 7, new CFPlus::UI::Slider
1118 force_w => 100, 1340 force_w => 100,
1119 range => [$CFG->{mapsize}, 10, 100, 0, 1], 1341 range => [$CFG->{mapsize}, 10, 100, 0, 1],
1120 tooltip => "This is the size of the portion of the map update the server sends you. " 1342 tooltip => "This is the size of the portion of the map update the server sends you. "
1121 . "If you set this to a high value you will be able to see further, " 1343 . "If you set this to a high value you will be able to see further, "
1122 . "but you also increase bandwidth requirements and latency. " 1344 . "but you also increase bandwidth requirements and latency. "
1123 . "This option is only used once at log-in.", 1345 . "This option is only used once at log-in.",
1124 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 0 }, 1346 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 },
1125 ); 1347 );
1126 1348
1127 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate"); 1349 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate");
1128 $table->add_at (1, 8, new CFPlus::UI::Entry 1350 $table->add_at (1, 8, new CFPlus::UI::Entry
1129 text => $CFG->{output_rate}, 1351 text => $CFG->{output_rate},
1130 tooltip => "The approximate bandwidth in bytes per second that the server should not exceed " 1352 tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1131 . "when sending images, to ensure interactiveness. When 0 or unset, the server " 1353 . "when sending data. When 0 or unset, the server "
1132 . "default will be used, which is usually around 100kb/s.", 1354 . "default will be used, which is usually around 100kb/s. Most servers will "
1355 . "dynamically find an optimal rate, so adjust this only when necessary.",
1133 on_changed => sub { $CFG->{output_rate} = $_[1]; 0 }, 1356 on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1134 ); 1357 );
1135 1358
1136 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count"); 1359 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count");
1137 $table->add_at (1, 9, new CFPlus::UI::Entry 1360 $table->add_at (1, 9, new CFPlus::UI::Entry
1138 text => $CFG->{output_count}, 1361 text => $CFG->{output_count},
1139 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1362 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1140 on_changed => sub { $CFG->{output_count} = $_[1]; 0 }, 1363 on_changed => sub { $CFG->{output_count} = $_[1]; 1 },
1141 ); 1364 );
1142 1365
1143 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); 1366 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync");
1144 $table->add_at (1, 10, new CFPlus::UI::Entry 1367 $table->add_at (1, 10, new CFPlus::UI::Entry
1145 text => $CFG->{output_sync}, 1368 text => $CFG->{output_sync},
1146 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1369 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1147 on_changed => sub { $CFG->{output_sync} = $_[1]; 0 }, 1370 on_changed => sub { $CFG->{output_sync} = $_[1]; 1 },
1148 ); 1371 );
1149 1372
1150 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button 1373 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button
1151 expand => 1, 1374 expand => 1,
1152 align => 0, 1375 align => 0,
1153 text => "Login", 1376 text => "Login",
1154 on_activate => sub { 1377 on_activate => sub {
1155 $CONN ? stop_game 1378 $CONN ? stop_game
1156 : start_game; 1379 : start_game;
1157 0 1380 1
1158 }, 1381 },
1159 ); 1382 );
1160 1383
1161 $vbox->add (new CFPlus::UI::FancyFrame 1384 $vbox->add (new CFPlus::UI::FancyFrame
1162 label => "Server Info", 1385 label => "Server Info",
1797 CFPlus::OpenGL::shutdown; 2020 CFPlus::OpenGL::shutdown;
1798 2021
1799 undef $SDL_ACTIVE; 2022 undef $SDL_ACTIVE;
1800} 2023}
1801 2024
1802sub audio_channel_finished {
1803 my ($channel) = @_;
1804
1805 #warn "channel $channel finished\n";#d#
1806}
1807
1808sub audio_music_set {
1809 my ($songs) = @_;
1810
1811 my @want =
1812 grep $_,
1813 map $CONN->{music_meta}{$_},
1814 @$songs;
1815
1816 if (@want) {
1817 @MUSIC_WANT = @want;
1818 &audio_music_changed ();
1819 }
1820}
1821
1822sub audio_music_start {
1823 my $meta = $MUSIC_PLAYING_META;
1824
1825 CFPlus::DB::get res_data => $meta->{name}, sub {
1826 return unless $SDL_MIXER;
1827
1828 # music might have changed...
1829 $meta eq $MUSIC_PLAYING_META
1830 or return &audio_music_start ();
1831
1832 $MUSIC_PLAYING_DATA = \$_[0];
1833
1834 my $rwops = $meta->{path}
1835 ? new_from_file CFPlus::RW $meta->{path}
1836 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
1837
1838 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
1839 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
1840
1841 my $NOW = time;
1842
1843 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
1844 my $pos = $MUSIC_PLAYING_META->{stop_pos};
1845 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
1846 $MUSIC_START = time - $pos;
1847 } else {
1848 $MUSIC_PLAYER->play (0);
1849 $MUSIC_START = time;
1850 }
1851
1852 delete $MUSIC_PLAYING_META->{stop_time};
1853 delete $MUSIC_PLAYING_META->{stop_pos};
1854 }
1855}
1856
1857sub audio_music_changed {
1858 return unless $CFG->{bgm_enable};
1859 return unless $SDL_MIXER;
1860
1861 # default MUSIC_WANT == MUSIC_DEFAULT
1862 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT;
1863
1864 # if the currently playing song is acceptable, let it continue
1865 return if $MUSIC_PLAYING_META
1866 && grep $MUSIC_PLAYING_META == $_, @MUSIC_WANT;
1867
1868 my $NOW = time;
1869
1870 if ($MUSIC_PLAYING_META) {
1871 $MUSIC_PLAYING_META->{stop_time} = $NOW;
1872 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
1873 CFPlus::MixMusic::fade_out 1000;
1874 } else {
1875 # sort by stop time, oldest first
1876 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
1877
1878 # if the most recently-played piece played very recently,
1879 # resume it, else choose the oldest piece for rotation.
1880 $MUSIC_PLAYING_META =
1881 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
1882 ? $MUSIC_WANT[-1]
1883 : $MUSIC_WANT[0];
1884
1885 audio_music_start;
1886 }
1887}
1888
1889sub audio_music_finished {
1890 undef $MUSIC_PLAYER;
1891 undef $MUSIC_PLAYING_META;
1892 undef $MUSIC_PLAYING_DATA;
1893
1894 audio_music_changed;
1895}
1896
1897sub audio_init {
1898 if ($CFG->{audio_enable}) {
1899 if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") {
1900 $ENV{MIX_EFFECTSMAXSPEED} = 1;
1901 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
1902
1903 unless ($SDL_MIXER) {
1904 status "Unable to open sound device: there will be no sound";
1905 return;
1906 }
1907
1908 CFPlus::Mix_AllocateChannels 8;
1909 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1910
1911 audio_music_finished;
1912
1913 local $_;
1914 while (<$fh>) {
1915 next if /^\s*#/;
1916 next if /^\s*$/;
1917
1918 my ($file, $volume, $event) = split /\s+/, $_, 3;
1919
1920 push @SOUNDS, "$volume,$file";
1921
1922 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
1923 my $rwops = new_from_file CFPlus::RW CFPlus::find_rcfile "sounds/$file";
1924 my $chunk = new CFPlus::MixChunk $rwops;
1925 $chunk->volume ($volume * 128 / 100);
1926 $chunk
1927 };
1928 }
1929 } else {
1930 status "unable to open sound config: $!";
1931 }
1932 } else {
1933 undef $SDL_MIXER;
1934 }
1935}
1936
1937sub audio_shutdown {
1938 CFPlus::Mix_CloseAudio if $SDL_MIXER;
1939 undef $SDL_MIXER;
1940 @SOUNDS = ();
1941 %AUDIO_CHUNKS = ();
1942}
1943
1944my %animate_object; 2025my %animate_object;
1945my $animate_timer; 2026my $animate_timer;
1946 2027
1947my $fps = 9; 2028my $fps = 9;
1948
1949my %demo;#d#
1950 2029
1951sub force_refresh { 2030sub force_refresh {
1952 $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05; 2031 $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05;
1953 debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4; 2032 debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4;
1954 2033
2039 2118
2040 CFPlus::UI::set_layout ($::CFG->{layout}); 2119 CFPlus::UI::set_layout ($::CFG->{layout});
2041 2120
2042 my %DEF_CFG = ( 2121 my %DEF_CFG = (
2043 sdl_mode => 0, 2122 sdl_mode => 0,
2044 width => 640,
2045 height => 480,
2046 fullscreen => 0, 2123 fullscreen => 0,
2047 fast => 0, 2124 fast => 0,
2048 map_scale => 1, 2125 map_scale => 1,
2049 fow_enable => 1, 2126 fow_enable => 1,
2050 fow_intensity => 0, 2127 fow_intensity => 0,
2054 gauge_fontsize => 1, 2131 gauge_fontsize => 1,
2055 gauge_size => 0.35, 2132 gauge_size => 0.35,
2056 stat_fontsize => 0.7, 2133 stat_fontsize => 0.7,
2057 mapsize => 100, 2134 mapsize => 100,
2058 audio_enable => 1, 2135 audio_enable => 1,
2136 effects_enable => 1,
2137 effects_volume => 1,
2059 bgm_enable => 1, 2138 bgm_enable => 1,
2060 bgm_volume => 0.25, 2139 bgm_volume => 0.5,
2061 output_sync => 1, 2140 output_sync => 1,
2062 output_count => 1, 2141 output_count => 1,
2063 output_rate => "", 2142 output_rate => "",
2064 pickup => 0, 2143 pickup => 0,
2065 inv_sort => "mtime", 2144 inv_sort => "mtime",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines