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.192 by root, Mon Jul 30 02:16:16 2007 UTC vs.
Revision 1.209 by root, Sat Aug 11 14:41:38 2007 UTC

113our $CONN; 113our $CONN;
114our $PROFILE; # current profile 114our $PROFILE; # current profile
115our $FAST; # fast, low-quality mode, possibly useful for software-rendering 115our $FAST; # fast, low-quality mode, possibly useful for software-rendering
116 116
117our $WANT_REFRESH; 117our $WANT_REFRESH;
118our $CAN_REFRESH;
119 118
120our @SDL_MODES; 119our @SDL_MODES;
121our $WIDTH; 120our $WIDTH;
122our $HEIGHT; 121our $HEIGHT;
123our $FULLSCREEN; 122our $FULLSCREEN;
144our $SETUP_KEYBOARD; 143our $SETUP_KEYBOARD;
145 144
146our $PL_NOTEBOOK; 145our $PL_NOTEBOOK;
147our $PL_WINDOW; 146our $PL_WINDOW;
148 147
148our $MUSIC_PLAYING_WIDGET;
149our $LICENSE_WIDGET;
150
149our $INVENTORY_PAGE; 151our $INVENTORY_PAGE;
150our $STATS_PAGE; 152our $STATS_PAGE;
151our $SKILL_PAGE; 153our $SKILL_PAGE;
152our $SPELL_PAGE; 154our $SPELL_PAGE;
153our $SPELL_LIST; 155our $SPELL_LIST;
159our $STATWIDS; 161our $STATWIDS;
160 162
161our $SDL_ACTIVE; 163our $SDL_ACTIVE;
162our %SDL_CB; 164our %SDL_CB;
163 165
166our $ALT_ENTER_MESSAGE;
167our $STATUSBOX;
168our $DEBUG_STATUS;
169
170our $INV;
171our $INVR;
172our $INV_RIGHT_HB;
173
174our $PICKUP_CFG;
175
176#############################################################################
177
178sub status {
179 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
180}
181
182sub debug {
183 $DEBUG_STATUS->set_text ($_[0]);
184}
185
186sub message {
187 my ($para) = @_;
188 $MESSAGE_WINDOW->message ($para);
189}
190
191#############################################################################
192#TODO: maybe move into own audio module...
193
164our $SDL_MIXER; 194our $SDL_MIXER;
195
165our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; 196our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
166our @MUSIC_WANT; 197our $MUSIC_WANT; # arryref of ambient music we want to play
198our @MUSIC_HAVE; # ambient music we have on disk
167our $MUSIC_START; 199our $MUSIC_START;
200our @MUSIC_JINGLE; # which jingles to play next
168our $MUSIC_PLAYING_DATA; 201our $MUSIC_PLAYING_DATA;
169our $MUSIC_PLAYING_META; 202our $MUSIC_PLAYING_META;
170our $MUSIC_PLAYER; 203our $MUSIC_PLAYER;
171our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before 204our $MUSIC_RESUME = 30; # resume music when played less than these many seconds before
172our @SOUNDS; # event => file mapping 205
173our %AUDIO_CHUNKS; # audio files 206our %AUDIO_CHUNK; # audio "files"
207our %AUDIO_PLAY; # which audio faces should be played
174 208
175our $ALT_ENTER_MESSAGE; 209sub audio_channel_finished {
176our $STATUSBOX; 210 my ($channel) = @_;
177our $DEBUG_STATUS;
178 211
179our $INV; 212# warn "channel $channel finished\n";#d#
180our $INVR;
181our $INV_RIGHT_HB;
182
183our $PICKUP_CFG;
184
185sub status {
186 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
187} 213}
188 214
189sub debug { 215sub audio_sound_push($) {
190 $DEBUG_STATUS->set_text ($_[0]);
191}
192
193sub message {
194 my ($para) = @_; 216 my ($face) = @_;
195 $MESSAGE_WINDOW->message ($para); 217
218 $CFG->{effects_enable}
219 or return;
220
221 $AUDIO_PLAY{$face}
222 or return;
223
224 if (my $chunk = $AUDIO_CHUNK{$face}) {
225 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
226 my (undef, $dx, $dy, $vol) = @$_;
227
228 my $channel = CFPlus::Channel::find;
229 $channel->volume ($vol * $CFG->{effects_volume} * 128 / 255);
230 $channel->set_position_r ($dx, $dy, 20);
231 $chunk->play ($channel);
232 }
233 } else {
234 # sound_meta not set means data is in flight either way
235 my $meta = $CONN->{sound_meta}{$face}
236 or return;
237
238 # if its a jingle, play it as ambient music
239 if ($meta->{meta}{jingle}) {
240 if (delete $AUDIO_PLAY{$face}) { # take the jingle out of the sound queue
241 push @MUSIC_JINGLE, $meta; # push it oto the music/jingle queue
242 &audio_music_push ($face);
243 }
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 or Carp::confess "sound face " . (JSON::XS::to_json $meta) . " unloadable: " . CFPlus::Mix_GetError;
250 $chunk->volume (($meta->{meta}{volume} || 1) * 128);
251 $AUDIO_CHUNK{$face} = $chunk;
252
253 audio_sound_push ($face);
254 };
255 }
256 }
196} 257}
258
259sub audio_sound_play {
260 my ($face, $dx, $dy, $vol) = @_;
261
262 $SDL_MIXER
263 or return;
264 $CFG->{effects_enable}
265 or return;
266
267 my $queue = $AUDIO_PLAY{$face} ||= [];
268 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
269 audio_sound_push $face
270 unless @$queue > 1;
271}
272
273sub audio_music_set_meta {
274 my ($meta) = @_;
275
276 $MUSIC_PLAYING_META = $meta;
277 $MUSIC_PLAYING_WIDGET->set_markup (
278 "<b>Name</b>: " . (CFPlus::asxml $meta->{meta}{name}) . "\n"
279 . "<b>Author</b>: " . (CFPlus::asxml $meta->{meta}{author}) . "\n"
280 . "<b>Source</b>: " . (CFPlus::asxml $meta->{meta}{source}) . "\n"
281 . "<b>License</b>: " . (CFPlus::asxml $meta->{meta}{license})
282 );
283}
284
285sub audio_music_update_volume {
286 return unless $MUSIC_PLAYING_META;
287 my $volume = $MUSIC_PLAYING_META->{meta}{volume} || 1;
288 my $base = $MUSIC_PLAYING_META->{meta}{jingle} ? 1 : $CFG->{bgm_volume};
289 CFPlus::MixMusic::volume $base * $volume * 128;
290}
291
292sub audio_music_start {
293 my $meta = $MUSIC_PLAYING_META;
294
295 CFPlus::DB::get res_data => $meta->{name}, sub {
296 return unless $SDL_MIXER;
297
298 # music might have changed...
299 $meta eq $MUSIC_PLAYING_META
300 or return &audio_music_start ();
301
302 audio_music_update_volume;
303
304 $MUSIC_PLAYING_DATA = \$_[0];
305
306 my $rwops = $meta->{path}
307 ? new_from_file CFPlus::RW $meta->{path}
308 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
309
310 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
311 or Carp::confess "music face $meta->{face} unloadable: " . CFPlus::Mix_GetError;
312
313 my $NOW = time;
314
315 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
316 my $pos = $MUSIC_PLAYING_META->{stop_pos};
317 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
318 $MUSIC_START = time - $pos;
319 } else {
320 $MUSIC_PLAYER->play (0);
321 $MUSIC_START = time;
322 }
323
324 delete $meta->{stop_time};
325 delete $meta->{stop_pos};
326 }
327}
328
329sub audio_music_push {
330 return unless $SDL_MIXER;
331
332 my $fade_out;
333
334 if (@MUSIC_JINGLE) {
335 @MUSIC_HAVE = $MUSIC_JINGLE[0];
336 $fade_out = 333;
337 } else {
338 return unless $CFG->{bgm_enable};
339
340 my @have =
341 grep $_,
342 map $CONN->{music_meta}{$_},
343 @$MUSIC_WANT;
344
345 @MUSIC_HAVE = @have
346 if @have;
347
348 # default MUSIC_HAVE == MUSIC_DEFAULT
349 @MUSIC_HAVE = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_HAVE;
350 $fade_out = 1000;
351 }
352
353 # if the currently playing song is acceptable, let it continue
354 return if grep $MUSIC_PLAYING_META == $_, @MUSIC_HAVE;
355
356 my $NOW = time;
357
358 if ($MUSIC_PLAYING_META) {
359 $MUSIC_PLAYING_META->{stop_time} = $NOW;
360 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
361 CFPlus::MixMusic::fade_out $fade_out;
362 } else {
363 # sort by stop time, oldest first
364 @MUSIC_HAVE = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_HAVE;
365
366 # if the most recently-played piece played very recently,
367 # resume it, else choose the oldest piece for rotation.
368 audio_music_set_meta
369 $MUSIC_HAVE[-1]{stop_time} > $NOW - $MUSIC_RESUME
370 ? $MUSIC_HAVE[-1]
371 : $MUSIC_HAVE[0];
372
373 audio_music_start;
374 }
375}
376
377sub audio_music_set_ambient {
378 my ($songs) = @_;
379
380 $MUSIC_WANT = $songs;
381 audio_music_push;
382}
383
384sub audio_music_finished {
385 # we compress multiple jingles of the same type
386 shift @MUSIC_JINGLE
387 while @MUSIC_JINGLE && $MUSIC_PLAYING_META == $MUSIC_JINGLE[0];
388
389 $MUSIC_PLAYING_WIDGET->clear;
390
391 undef $MUSIC_PLAYER;
392 undef $MUSIC_PLAYING_META;
393 undef $MUSIC_PLAYING_DATA;
394
395 audio_music_push;
396}
397
398sub audio_init {
399 if ($CFG->{audio_enable}) {
400 $ENV{MIX_EFFECTSMAXSPEED} = 1;
401 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
402
403 unless ($SDL_MIXER) {
404 status "Unable to open sound device: there will be no sound";
405 return;
406 }
407
408 CFPlus::Mix_AllocateChannels 16;
409
410 audio_music_finished;
411 } else {
412 undef $SDL_MIXER;
413 }
414}
415
416sub audio_shutdown {
417 undef $MUSIC_PLAYER;
418 undef $MUSIC_PLAYING_META;
419 undef $MUSIC_PLAYING_DATA;
420
421 $MUSIC_WANT = [];
422 @MUSIC_JINGLE = ();
423 %AUDIO_PLAY = ();
424 %AUDIO_CHUNK = ();
425
426 CFPlus::Mix_CloseAudio if $SDL_MIXER;
427 undef $SDL_MIXER;
428}
429
430#############################################################################
197 431
198sub destroy_query_dialog { 432sub destroy_query_dialog {
199 (delete $_[0]{query_dialog})->destroy 433 (delete $_[0]{query_dialog})->destroy
200 if $_[0]{query_dialog}; 434 if $_[0]{query_dialog};
201} 435}
481 $SETUP_DIALOG->show; 715 $SETUP_DIALOG->show;
482 $PL_WINDOW->hide; 716 $PL_WINDOW->hide;
483 $SPELL_LIST->clear_spells; 717 $SPELL_LIST->clear_spells;
484 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); 718 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN);
485 719
486 &audio_music_set ([]); 720 &audio_music_set_ambient ([]);
487 721
488 return unless $CONN; 722 return unless $CONN;
489 723
490 status "connection closed"; 724 status "connection closed";
491 725
570 804
571 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Smoothing"); 805 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Smoothing");
572 $table->add_at (1, $row++, new CFPlus::UI::CheckBox 806 $table->add_at (1, $row++, new CFPlus::UI::CheckBox
573 state => $CFG->{map_smoothing}, 807 state => $CFG->{map_smoothing},
574 tooltip => "<b>Map Smoothing</b> tries to make tile borders less square. " 808 tooltip => "<b>Map Smoothing</b> tries to make tile borders less square. "
575 . "This increases load on the graphics subsystem and works only with 2.x servers. " 809 . "This increases load on the graphics subsystem and works only with TRT servers. "
576 . "Changes take effect at next connection only.", 810 . "Changes take effect at next connection only.",
577 on_changed => sub { my ($self, $value) = @_; $CFG->{map_smoothing} = $value; 0 } 811 on_changed => sub { my ($self, $value) = @_; $CFG->{map_smoothing} = $value; 0 }
578 ); 812 );
579 813
580 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Fog of War"); 814 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Fog of War");
624} 858}
625 859
626sub audio_setup { 860sub audio_setup {
627 my $vbox = new CFPlus::UI::VBox; 861 my $vbox = new CFPlus::UI::VBox;
628 862
629 $vbox->add (my $table = new CFPlus::UI::Table expand => 1, col_expand => [0, 1]); 863 $vbox->add (my $table = new CFPlus::UI::Table expand => 1, col_expand => [0, 0, 1]);
630 864
631 my $row = 0; 865 my $row = 0;
632 866
633 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Audio Enable"); 867 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Audio Enable");
634 $table->add_at (1, $row++, new CFPlus::UI::CheckBox 868 $table->add_at (1, $row++, new CFPlus::UI::CheckBox
638 ); 872 );
639# $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Effects Volume"); 873# $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Effects Volume");
640# $table->add_at (1, 8, new CFPlus::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], on_changed => sub { 874# $table->add_at (1, 8, new CFPlus::UI::Slider range => [$CFG->{effects_volume}, 0, 128, 1], on_changed => sub {
641# $CFG->{effects_volume} = $_[1]; 875# $CFG->{effects_volume} = $_[1];
642# }); 876# });
877
878 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Sound Effects");
879 $table->add_at (1, $row, new CFPlus::UI::CheckBox
880 expand => 1, state => $CFG->{effects_enable},
881 tooltip => "If enabled, sound effects are enabled. If disabled, no sound effects will be played.",
882 on_changed => sub {
883 $CFG->{effects_enable} = $_[1];
884 $CONN->update_fx_want if $CONN;
885 0
886 }
887 );
888 $table->add_at (2, $row++, new CFPlus::UI::Slider
889 expand => 1, range => [$CFG->{effects_volume}, 0, 1, 0, 1/128],
890 tooltip => "The relative volume of sound effects. Best audio quality is achieved if this "
891 . "is set highest (rightmost) and you use your operating system volume setting. Changes are instant.",
892 on_changed => sub { $CFG->{effects_volume} = $_[1]; 0 }
893 );
894
643 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Background Music"); 895 $table->add_at (0, $row, new CFPlus::UI::Label valign => 0, align => 1, text => "Background Music");
644 $table->add_at (1, $row++, my $hbox = new CFPlus::UI::HBox); 896 $table->add_at (1, $row, new CFPlus::UI::CheckBox
645 $hbox->add (new CFPlus::UI::CheckBox
646 expand => 1, state => $CFG->{bgm_enable}, 897 expand => 1, state => $CFG->{bgm_enable},
647 tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played.", 898 tooltip => "If enabled, playing of background music is enabled. If disabled, no background music will be played.",
648 on_changed => sub { $CFG->{bgm_enable} = $_[1]; 0 } 899 on_changed => sub {
900 $CFG->{bgm_enable} = $_[1];
901 $CONN->update_fx_want if $CONN;
902 0
903 }
649 ); 904 );
650 $hbox->add (new CFPlus::UI::Slider 905 $table->add_at (2, $row++, new CFPlus::UI::Slider
651 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128], 906 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128],
652 tooltip => "The volume of the background music. Changes are instant.", 907 tooltip => "The volume of the background music. Changes are instant.",
653 on_changed => sub { $CFG->{bgm_volume} = $_[1]; CFPlus::MixMusic::volume $_[1] * 128; 0 } 908 on_changed => sub { $CFG->{bgm_volume} = $_[1]; audio_music_update_volume; 0 }
654 ); 909 );
655 910
656 $table->add_at (1, $row++, new CFPlus::UI::Button 911 $table->add_at (1, $row++, new CFPlus::UI::Button
657 expand => 1, align => 0, text => "Apply", 912 c_colspan => 2, expand => 1, align => 0, text => "Apply",
658 tooltip => "Apply the audio settings", 913 tooltip => "Apply the audio settings",
659 on_activate => sub { 914 on_activate => sub {
660 audio_shutdown (); 915 audio_shutdown ();
661 audio_init (); 916 audio_init ();
662 0 917 0
733 $table->add_at (0, 4, new CFPlus::UI::Button text => "die on click(tm)", on_activate => sub { &CFPlus::debug() } ); 988 $table->add_at (0, 4, new CFPlus::UI::Button text => "die on click(tm)", on_activate => sub { &CFPlus::debug() } );
734 989
735 $table->add_at (0, 5, new CFPlus::UI::TextEdit text => "line1\0152\0153");#d# 990 $table->add_at (0, 5, new CFPlus::UI::TextEdit text => "line1\0152\0153");#d#
736 991
737 $table->add_at (7,7, my $t = new CFPlus::UI::Table expand => 0); 992 $table->add_at (7,7, my $t = new CFPlus::UI::Table expand => 0);
738 $t->add_at (0,0, new CFPlus::UI::Label text => "a a a a", rowspan => 1, colspan => 2); 993 $t->add_at (0,0, new CFPlus::UI::Label text => "a a a a", c_rowspan => 1, c_colspan => 2);
739 $t->add_at (2,0, new CFPlus::UI::Label text => "b\nb", rowspan => 2, colspan => 1); 994 $t->add_at (2,0, new CFPlus::UI::Label text => "b\nb", c_rowspan => 2, c_colspan => 1);
740 $t->add_at (1,2, new CFPlus::UI::Label text => "c c c c", rowspan => 1, colspan => 2); 995 $t->add_at (1,2, new CFPlus::UI::Label text => "c c c c", c_rowspan => 1, c_colspan => 2);
741 $t->add_at (0,1, new CFPlus::UI::Label text => "d\nd", rowspan => 2, colspan => 1); 996 $t->add_at (0,1, new CFPlus::UI::Label text => "d\nd", c_rowspan => 2, c_colspan => 1);
742 $t->add_at (1,1, new CFPlus::UI::Label text => "e"); 997 $t->add_at (1,1, new CFPlus::UI::Label text => "e");
743 998
744 $table->add_at (7, 6, my $c = new CFPlus::UI::Canvas); 999 $table->add_at (7, 6, my $c = new CFPlus::UI::Canvas);
745 1000
746 $c->add_items ({ 1001 $c->add_items ({
1353 CFPlus::Protocol::set_opencont ($::CONN, 0, "Floor"); 1608 CFPlus::Protocol::set_opencont ($::CONN, 0, "Floor");
1354 1609
1355 $hb 1610 $hb
1356} 1611}
1357 1612
1613sub media_window {
1614 my $vb = new CFPlus::UI::VBox;
1615
1616 $vb->add (new CFPlus::UI::FancyFrame
1617 label => "Currently playing music",
1618 child => new CFPlus::UI::ScrolledWindow scroll_x => 1, scroll_y => 0,
1619 child => ($MUSIC_PLAYING_WIDGET = new CFPlus::UI::Label ellipsise => 0, fontsize => 0.8),
1620 );
1621
1622 $vb->add (new CFPlus::UI::FancyFrame
1623 label => "Other media used in this session",
1624 expand => 1,
1625 child => ($LICENSE_WIDGET = new CFPlus::UI::TextScroller
1626 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4),
1627 );
1628
1629 $vb
1630}
1631
1632sub add_license {
1633 my ($meta) = @_;
1634
1635 $meta = $meta->{meta}
1636 or return;
1637
1638 $meta->{license} || $meta->{author} || $meta->{source}
1639 or return;
1640
1641 $LICENSE_WIDGET->add_paragraph ({
1642 fg => [1, 1, 1, 1],
1643 markup => "<small>"
1644 . "<b>Name:</b> " . (CFPlus::asxml $meta->{name}) . "\n"
1645 . "<b>Author:</b> " . (CFPlus::asxml $meta->{author}) . "\n"
1646 . "<b>Source:</b> " . (CFPlus::asxml $meta->{source}) . "\n"
1647 . "<b>License:</b> " . (CFPlus::asxml $meta->{license}) . "\n"
1648 . "</small>",
1649 });
1650 $LICENSE_WIDGET->scroll_to_bottom;
1651}
1652
1358sub toggle_player_page { 1653sub toggle_player_page {
1359 my ($widget) = @_; 1654 my ($widget) = @_;
1360 1655
1361 if ($PL_WINDOW->{visible} && $PL_NOTEBOOK->get_current_page == $widget) { 1656 if ($PL_WINDOW->{visible} && $PL_NOTEBOOK->get_current_page == $widget) {
1362 $PL_WINDOW->hide; 1657 $PL_WINDOW->hide;
1399 $ntb->add_tab ( 1694 $ntb->add_tab (
1400 "Inventory (F5)" => $INVENTORY_PAGE = inventory_widget, 1695 "Inventory (F5)" => $INVENTORY_PAGE = inventory_widget,
1401 "Toggles the inventory window, where you can manage your loot (or treasures :). " 1696 "Toggles the inventory window, where you can manage your loot (or treasures :). "
1402 . "You can also hit the <b>Tab</b>-key to show/hide the Inventory." 1697 . "You can also hit the <b>Tab</b>-key to show/hide the Inventory."
1403 ); 1698 );
1404 $ntb->add_tab (Pickup => autopickup_setup, 1699 $ntb->add_tab (Pickup => autopickup_setup,
1405 "Configure autopickup settings, i.e. which items you will pick up automatically when walking (or running) over them."); 1700 "Configure autopickup settings, i.e. which items you will pick up automatically when walking (or running) over them.");
1701
1702 $ntb->add_tab (Media => media_window,
1703 "License, Author and Source info for media sent by the server.");
1406 1704
1407 $ntb->set_current_page ($INVENTORY_PAGE); 1705 $ntb->set_current_page ($INVENTORY_PAGE);
1408 1706
1409 $plwin->add ($ntb); 1707 $plwin->add ($ntb);
1410 $plwin 1708 $plwin
1787 CFPlus::OpenGL::shutdown; 2085 CFPlus::OpenGL::shutdown;
1788 2086
1789 undef $SDL_ACTIVE; 2087 undef $SDL_ACTIVE;
1790} 2088}
1791 2089
1792sub audio_channel_finished {
1793 my ($channel) = @_;
1794
1795# warn "channel $channel finished\n";#d#
1796}
1797
1798our %AUDIO_PLAY;
1799our %AUDIO_CHUNK;
1800
1801sub audio_sound_push($) {
1802 my ($face) = @_;
1803
1804 if (my $chunk = $AUDIO_CHUNK{$face}) {
1805 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
1806 my (undef, $dx, $dy, $vol) = @$_;
1807
1808 my $channel = $chunk->play
1809 or return;
1810
1811 $channel->volume (128 + $vol);
1812 $dx = $dx / 10 * 255;
1813 $channel->set_panning ((min 255, 255 - $dx), (min 255, 255 + $dx));
1814# my $angle = $dx ? : $dx < 0 ?
1815# my $distance = -$vol;
1816# $channel->set_position ($angle, $distance);
1817 }
1818 } else {
1819 # sound_meta not set means data is in flight either way
1820 my $meta = $CONN->{sound_meta}{$face}
1821 or return;
1822
1823 # fetch from database
1824 CFPlus::DB::get res_data => $meta->{name}, sub {
1825 my $vol = $meta->{meta}{volume} || 100;
1826 my $rwops = new CFPlus::RW $_[0];
1827 my $chunk = new CFPlus::MixChunk $rwops;
1828 $chunk->volume ($vol * 128 / 100);
1829 $AUDIO_CHUNK{$face} = $chunk;
1830
1831 audio_sound_push ($face);
1832 };
1833 }
1834}
1835
1836sub audio_sound_play {
1837 my ($face, $dx, $dy, $vol) = @_;
1838
1839 $SDL_MIXER
1840 or return;
1841
1842 my $queue = $AUDIO_PLAY{$face} ||= [];
1843 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
1844 audio_sound_push $face
1845 unless @$queue > 1;
1846}
1847
1848sub audio_music_set {
1849 my ($songs) = @_;
1850
1851 my @want =
1852 grep $_,
1853 map $CONN->{music_meta}{$_},
1854 @$songs;
1855
1856 if (@want) {
1857 @MUSIC_WANT = @want;
1858 &audio_music_changed ();
1859 }
1860}
1861
1862sub audio_music_start {
1863 my $meta = $MUSIC_PLAYING_META;
1864
1865 CFPlus::DB::get res_data => $meta->{name}, sub {
1866 return unless $SDL_MIXER;
1867
1868 # music might have changed...
1869 $meta eq $MUSIC_PLAYING_META
1870 or return &audio_music_start ();
1871
1872 $MUSIC_PLAYING_DATA = \$_[0];
1873
1874 my $rwops = $meta->{path}
1875 ? new_from_file CFPlus::RW $meta->{path}
1876 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
1877
1878 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
1879 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
1880
1881 my $NOW = time;
1882
1883 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
1884 my $pos = $MUSIC_PLAYING_META->{stop_pos};
1885 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
1886 $MUSIC_START = time - $pos;
1887 } else {
1888 $MUSIC_PLAYER->play (0);
1889 $MUSIC_START = time;
1890 }
1891
1892 delete $MUSIC_PLAYING_META->{stop_time};
1893 delete $MUSIC_PLAYING_META->{stop_pos};
1894 }
1895}
1896
1897sub audio_music_changed {
1898 return unless $CFG->{bgm_enable};
1899 return unless $SDL_MIXER;
1900
1901 # default MUSIC_WANT == MUSIC_DEFAULT
1902 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT;
1903
1904 # if the currently playing song is acceptable, let it continue
1905 return if $MUSIC_PLAYING_META
1906 && grep $MUSIC_PLAYING_META == $_, @MUSIC_WANT;
1907
1908 my $NOW = time;
1909
1910 if ($MUSIC_PLAYING_META) {
1911 $MUSIC_PLAYING_META->{stop_time} = $NOW;
1912 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
1913 CFPlus::MixMusic::fade_out 1000;
1914 } else {
1915 # sort by stop time, oldest first
1916 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
1917
1918 # if the most recently-played piece played very recently,
1919 # resume it, else choose the oldest piece for rotation.
1920 $MUSIC_PLAYING_META =
1921 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
1922 ? $MUSIC_WANT[-1]
1923 : $MUSIC_WANT[0];
1924
1925 audio_music_start;
1926 }
1927}
1928
1929sub audio_music_finished {
1930 undef $MUSIC_PLAYER;
1931 undef $MUSIC_PLAYING_META;
1932 undef $MUSIC_PLAYING_DATA;
1933
1934 audio_music_changed;
1935}
1936
1937sub audio_init {
1938 if ($CFG->{audio_enable}) {
1939 $ENV{MIX_EFFECTSMAXSPEED} = 1;
1940 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
1941
1942 unless ($SDL_MIXER) {
1943 status "Unable to open sound device: there will be no sound";
1944 return;
1945 }
1946
1947 CFPlus::Mix_AllocateChannels 16;
1948 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
1949
1950 audio_music_finished;
1951 } else {
1952 undef $SDL_MIXER;
1953 }
1954}
1955
1956sub audio_shutdown {
1957 CFPlus::Mix_CloseAudio if $SDL_MIXER;
1958 undef $SDL_MIXER;
1959 @SOUNDS = ();
1960 %AUDIO_CHUNKS = ();
1961}
1962
1963my %animate_object; 2090my %animate_object;
1964my $animate_timer; 2091my $animate_timer;
1965 2092
1966my $fps = 9; 2093my $fps = 9;
1967 2094
1968my %demo;#d#
1969
1970sub force_refresh { 2095sub force_refresh {
2096 if ($ENV{CFPLUS_DEBUG} & 4) {
1971 $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05; 2097 $fps = $fps * 0.98 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.02;
1972 debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4; 2098 debug sprintf "%3.2f", $fps;
2099 }
1973 2100
1974 $CFPlus::UI::ROOT->draw; 2101 $CFPlus::UI::ROOT->draw;
1975 2102 CFPlus::SDL_GL_SwapBuffers;
1976 $WANT_REFRESH = 0;
1977 $CAN_REFRESH = 0;
1978 $LAST_REFRESH = $NOW; 2103 $LAST_REFRESH = $NOW;
1979 2104 $WANT_REFRESH->stop;
1980 CFPlus::SDL_GL_SwapBuffers;
1981} 2105}
1982 2106
2107$WANT_REFRESH = Event->idle (min => 0.001, max => 0.06, parked => 1, cb => \&force_refresh);
2108
1983my $refresh_watcher = Event->timer (after => 0, hard => 0, interval => 1 / $MAX_FPS, cb => sub { 2109my $input = Event->timer (after => 0, hard => 0, interval => 1 / 50, cb => sub {
1984 $NOW = time; 2110 $NOW = time;
1985 2111
1986 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_) 2112 ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_)
1987 for CFPlus::poll_events; 2113 for CFPlus::poll_events;
1988 2114
1989 if (%animate_object) { 2115 if (%animate_object) {
1990 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; 2116 $_->animate ($LAST_REFRESH - $NOW) for values %animate_object;
1991 ++$WANT_REFRESH; 2117 $WANT_REFRESH->start;
1992 }
1993
1994 if ($WANT_REFRESH) {
1995 force_refresh;
1996 } else {
1997 $CAN_REFRESH = 1;
1998 } 2118 }
1999}); 2119});
2000 2120
2001sub animation_start { 2121sub animation_start {
2002 my ($widget) = @_; 2122 my ($widget) = @_;
2058 2178
2059 CFPlus::UI::set_layout ($::CFG->{layout}); 2179 CFPlus::UI::set_layout ($::CFG->{layout});
2060 2180
2061 my %DEF_CFG = ( 2181 my %DEF_CFG = (
2062 sdl_mode => 0, 2182 sdl_mode => 0,
2063 width => 640,
2064 height => 480,
2065 fullscreen => 0, 2183 fullscreen => 0,
2066 fast => 0, 2184 fast => 0,
2067 map_scale => 1, 2185 map_scale => 1,
2068 fow_enable => 1, 2186 fow_enable => 1,
2069 fow_intensity => 0, 2187 fow_intensity => 0,
2073 gauge_fontsize => 1, 2191 gauge_fontsize => 1,
2074 gauge_size => 0.35, 2192 gauge_size => 0.35,
2075 stat_fontsize => 0.7, 2193 stat_fontsize => 0.7,
2076 mapsize => 100, 2194 mapsize => 100,
2077 audio_enable => 1, 2195 audio_enable => 1,
2196 effects_enable => 1,
2197 effects_volume => 1,
2078 bgm_enable => 1, 2198 bgm_enable => 1,
2079 bgm_volume => 0.25, 2199 bgm_volume => 0.5,
2080 output_sync => 1, 2200 output_sync => 1,
2081 output_count => 1, 2201 output_count => 1,
2082 output_rate => "", 2202 output_rate => "",
2083 pickup => 0, 2203 pickup => 0,
2084 inv_sort => "mtime", 2204 inv_sort => "mtime",
2162Event::loop; 2282Event::loop;
2163#CFPlus::SDL_Quit; 2283#CFPlus::SDL_Quit;
2164#CFPlus::_exit 0; 2284#CFPlus::_exit 0;
2165 2285
2166END { 2286END {
2287 video_shutdown;
2288 audio_shutdown;
2167 CFPlus::SDL_Quit; 2289 CFPlus::SDL_Quit;
2168 CFPlus::DB::Server::stop; 2290 CFPlus::DB::Server::stop;
2169} 2291}
2170 2292
2171=head1 NAME 2293=head1 NAME
2172 2294
2173cfplus - A Crossfire+ and Crossfire game client 2295cfplus - A Crossfire TRT and Crossfire game client
2174 2296
2175=head1 SYNOPSIS 2297=head1 SYNOPSIS
2176 2298
2177Just run it - no commandline arguments are supported. 2299Just run it - no commandline arguments are supported.
2178 2300
2179=head1 USAGE 2301=head1 USAGE
2180 2302
2181cfplus utilises OpenGL for all UI elements and the game. It is supposed to be used 2303cfplus utilises OpenGL for all UI elements and the game. It is supposed to
2182fullscreen and interactively. 2304be used in fullscreen mode and interactively.
2183 2305
2184=head1 DEBUGGING 2306=head1 DEBUGGING
2185 2307
2186 2308
2187CFPLUS_DEBUG - environment variable 2309CFPLUS_DEBUG - environment variable

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines