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.185 by elmex, Tue Jul 24 18:39:39 2007 UTC vs.
Revision 1.196 by root, Tue Jul 31 02:03:13 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;
160our $SDL_ACTIVE; 161our $SDL_ACTIVE;
161our %SDL_CB; 162our %SDL_CB;
162 163
163our $SDL_MIXER; 164our $SDL_MIXER;
164our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; 165our $MUSIC_DEFAULT = "in_a_heartbeat.ogg";
165our @MUSIC_WANT; 166our $MUSIC_WANT; # arryref of ambient music
167our @MUSIC_HAVE; # ambient music we want now
166our $MUSIC_START; 168our $MUSIC_START;
167our $MUSIC_PLAYING; 169our $MUSIC_PLAYING_DATA;
170our $MUSIC_PLAYING_META;
168our $MUSIC_PLAYER; 171our $MUSIC_PLAYER;
169our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before 172our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before
170our @SOUNDS; # event => file mapping 173our @SOUNDS; # event => file mapping
171our %AUDIO_CHUNKS; # audio files 174our %AUDIO_CHUNK; # audio files
172 175
173our $ALT_ENTER_MESSAGE; 176our $ALT_ENTER_MESSAGE;
174our $STATUSBOX; 177our $STATUSBOX;
175our $DEBUG_STATUS; 178our $DEBUG_STATUS;
176 179
178our $INVR; 181our $INVR;
179our $INV_RIGHT_HB; 182our $INV_RIGHT_HB;
180 183
181our $PICKUP_CFG; 184our $PICKUP_CFG;
182 185
183our $IN_BUILD_MODE; 186#############################################################################
184our $BUILD_BUTTON; 187#TODO: maybe move into own audio module...
188
189sub audio_channel_finished {
190 my ($channel) = @_;
191
192# warn "channel $channel finished\n";#d#
193}
194
195our %AUDIO_PLAY;
196our %AUDIO_CHUNK;
197
198sub audio_sound_push($) {
199 my ($face) = @_;
200
201 if (my $chunk = $AUDIO_CHUNK{$face}) {
202 for (grep $_->[0] >= Event::time, @{(delete $AUDIO_PLAY{$face}) || []}) {
203 my (undef, $dx, $dy, $vol) = @$_;
204
205 my $channel = CFPlus::Channel::find;
206 $channel->volume ($vol * 128 / 255);
207 $dx = $dx / 10 * 255;
208 $channel->set_panning (255 - $dx, 255 + $dx);
209
210# my $angle = $dx ? : $dx < 0 ?
211# my $distance = -$vol;
212# $channel->set_position ($angle, $distance);
213
214 $chunk->play ($channel);
215 }
216 } else {
217 # sound_meta not set means data is in flight either way
218 my $meta = $CONN->{sound_meta}{$face}
219 or return;
220
221 # fetch from database
222 CFPlus::DB::get res_data => $meta->{name}, sub {
223 my $rwops = new CFPlus::RW $_[0];
224 my $chunk = new CFPlus::MixChunk $rwops;
225 $chunk->volume (($meta->{meta}{volume} || 1) * 128);
226 $AUDIO_CHUNK{$face} = $chunk;
227
228 audio_sound_push ($face);
229 };
230 }
231}
232
233sub audio_sound_play {
234 my ($face, $dx, $dy, $vol) = @_;
235
236 $SDL_MIXER
237 or return;
238
239 my $queue = $AUDIO_PLAY{$face} ||= [];
240 push @$queue, [Event::time + 0.2, $dx, $dy, $vol]; # delay sound by max. 0.2s
241 audio_sound_push $face
242 unless @$queue > 1;
243}
244
245sub audio_music_update_volume {
246 return unless $MUSIC_PLAYING_META;
247 my $volume = $MUSIC_PLAYING_META->{meta}{volume} || 1;
248 my $base = $CFG->{bgm_volume};
249 CFPlus::MixMusic::volume $base * $volume * 128;
250}
251
252sub audio_music_push {
253 my @have =
254 grep $_,
255 map $CONN->{music_meta}{$_},
256 @$MUSIC_WANT;
257
258 if (@have) {
259 @MUSIC_HAVE = @have;
260 &audio_music_changed ();
261 }
262}
263
264sub audio_music_set_ambient {
265 my ($songs) = @_;
266
267 $MUSIC_WANT = $songs;
268
269 audio_music_push;
270}
271
272sub audio_music_start {
273 my $meta = $MUSIC_PLAYING_META;
274
275 CFPlus::DB::get res_data => $meta->{name}, sub {
276 return unless $SDL_MIXER;
277
278 # music might have changed...
279 $meta eq $MUSIC_PLAYING_META
280 or return &audio_music_start ();
281
282 audio_music_update_volume;
283
284 $MUSIC_PLAYING_DATA = \$_[0];
285
286 my $rwops = $meta->{path}
287 ? new_from_file CFPlus::RW $meta->{path}
288 : new CFPlus::RW $$MUSIC_PLAYING_DATA;
289
290 $MUSIC_PLAYER = new CFPlus::MixMusic $rwops
291 or ((warn CFPlus::Mix_GetError), return); # pretty fatal error
292
293 my $NOW = time;
294
295 if ($MUSIC_PLAYING_META->{stop_time} > $NOW - $MUSIC_RESUME) {
296 my $pos = $MUSIC_PLAYING_META->{stop_pos};
297 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
298 $MUSIC_START = time - $pos;
299 } else {
300 $MUSIC_PLAYER->play (0);
301 $MUSIC_START = time;
302 }
303
304 delete $MUSIC_PLAYING_META->{stop_time};
305 delete $MUSIC_PLAYING_META->{stop_pos};
306 }
307}
308
309sub audio_music_changed {
310 return unless $CFG->{bgm_enable};
311 return unless $SDL_MIXER;
312
313 # default MUSIC_HAVE == MUSIC_DEFAULT
314 @MUSIC_HAVE = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_HAVE;
315
316 # if the currently playing song is acceptable, let it continue
317 return if $MUSIC_PLAYING_META
318 && grep $MUSIC_PLAYING_META == $_, @MUSIC_HAVE;
319
320 my $NOW = time;
321
322 if ($MUSIC_PLAYING_META) {
323 $MUSIC_PLAYING_META->{stop_time} = $NOW;
324 $MUSIC_PLAYING_META->{stop_pos} = $NOW - $MUSIC_START;
325 CFPlus::MixMusic::fade_out 1000;
326 } else {
327 # sort by stop time, oldest first
328 @MUSIC_HAVE = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_HAVE;
329
330 # if the most recently-played piece played very recently,
331 # resume it, else choose the oldest piece for rotation.
332 $MUSIC_PLAYING_META =
333 $MUSIC_HAVE[-1]{stop_time} > $NOW - $MUSIC_RESUME
334 ? $MUSIC_HAVE[-1]
335 : $MUSIC_HAVE[0];
336
337 audio_music_start;
338 }
339}
340
341sub audio_music_finished {
342 undef $MUSIC_PLAYER;
343 undef $MUSIC_PLAYING_META;
344 undef $MUSIC_PLAYING_DATA;
345
346 audio_music_changed;
347}
348
349#############################################################################
185 350
186sub status { 351sub status {
187 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]); 352 $STATUSBOX->add (CFPlus::asxml $_[0], pri => -10, group => "status", timeout => 10, fg => [1, 1, 0, 1]);
188} 353}
189 354
462 query => \&server_query, 627 query => \&server_query,
463 628
464 setup_req => { 629 setup_req => {
465 smoothing => $CFG->{map_smoothing}*1, 630 smoothing => $CFG->{map_smoothing}*1,
466 }, 631 },
467
468 sound_play => sub {
469 my ($x, $y, $soundnum, $type) = @_;
470
471 $SDL_MIXER
472 or return;
473
474 my $chunk = $AUDIO_CHUNKS{$SOUNDS[$soundnum]}
475 or return;
476
477 $chunk->play;
478 },
479 }; 632 };
480 633
481 if ($CONN) { 634 if ($CONN) {
482 CFPlus::lowdelay fileno $CONN->{fh}; 635 CFPlus::lowdelay fileno $CONN->{fh};
483 636
492 $LOGIN_BUTTON->set_text ("Login"); 645 $LOGIN_BUTTON->set_text ("Login");
493 $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER); 646 $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER);
494 $SETUP_DIALOG->show; 647 $SETUP_DIALOG->show;
495 $PL_WINDOW->hide; 648 $PL_WINDOW->hide;
496 $SPELL_LIST->clear_spells; 649 $SPELL_LIST->clear_spells;
497 $MESSAGE_WINDOW->clear_channels;
498 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); 650 $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN);
499 651
500 &audio_music_set ([]); 652 &audio_music_set_ambient ([]);
501 653
502 return unless $CONN; 654 return unless $CONN;
503 655
504 status "connection closed"; 656 status "connection closed";
505 657
662 on_changed => sub { $CFG->{bgm_enable} = $_[1]; 0 } 814 on_changed => sub { $CFG->{bgm_enable} = $_[1]; 0 }
663 ); 815 );
664 $hbox->add (new CFPlus::UI::Slider 816 $hbox->add (new CFPlus::UI::Slider
665 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128], 817 expand => 1, range => [$CFG->{bgm_volume}, 0, 1, 0, 1/128],
666 tooltip => "The volume of the background music. Changes are instant.", 818 tooltip => "The volume of the background music. Changes are instant.",
667 on_changed => sub { $CFG->{bgm_volume} = $_[1]; CFPlus::MixMusic::volume $_[1] * 128; 0 } 819 on_changed => sub { $CFG->{bgm_volume} = $_[1]; audio_music_update_volume; 0 }
668 ); 820 );
669 821
670 $table->add_at (1, $row++, new CFPlus::UI::Button 822 $table->add_at (1, $row++, new CFPlus::UI::Button
671 expand => 1, align => 0, text => "Apply", 823 expand => 1, align => 0, text => "Apply",
672 tooltip => "Apply the audio settings", 824 tooltip => "Apply the audio settings",
1085 text => $CFG->{profile}{default}{host}, 1237 text => $CFG->{profile}{default}{host},
1086 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to", 1238 tooltip => "The hostname or ip address of the Crossfire(+) server to connect to",
1087 on_changed => sub { 1239 on_changed => sub {
1088 my ($self, $value) = @_; 1240 my ($self, $value) = @_;
1089 $CFG->{profile}{default}{host} = $value; 1241 $CFG->{profile}{default}{host} = $value;
1090 0 1242 1
1091 } 1243 }
1092 ); 1244 );
1093 1245
1094 $vbox->add (new CFPlus::UI::Button 1246 $vbox->add (new CFPlus::UI::Button
1095 expand => 1, 1247 expand => 1,
1096 text => "Server List", 1248 text => "Server List",
1097 other => $METASERVER, 1249 other => $METASERVER,
1098 tooltip => "Show a list of available crossfire servers", 1250 tooltip => "Show a list of available crossfire servers",
1099 on_activate => sub { $METASERVER->toggle_visibility; 0 }, 1251 on_activate => sub { $METASERVER->toggle_visibility; 0 },
1100 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 0 }, 1252 on_visibility_change => sub { $METASERVER->hide unless $_[1]; 1 },
1101 ); 1253 );
1102 } 1254 }
1103 1255
1104 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username"); 1256 $table->add_at (0, 4, new CFPlus::UI::Label valign => 0, align => 1, text => "Username");
1105 $table->add_at (1, 4, new CFPlus::UI::Entry 1257 $table->add_at (1, 4, new CFPlus::UI::Entry
1106 text => $CFG->{profile}{default}{user}, 1258 text => $CFG->{profile}{default}{user},
1107 tooltip => "The name of your character on the server", 1259 tooltip => "The name of your character on the server",
1108 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value } 1260 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{user} = $value; 1 }
1109 ); 1261 );
1110 1262
1111 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password"); 1263 $table->add_at (0, 5, new CFPlus::UI::Label valign => 0, align => 1, text => "Password");
1112 $table->add_at (1, 5, new CFPlus::UI::Entry 1264 $table->add_at (1, 5, new CFPlus::UI::Entry
1113 text => $CFG->{profile}{default}{password}, 1265 text => $CFG->{profile}{default}{password},
1114 hidden => 1, 1266 hidden => 1,
1115 tooltip => "The password for your character", 1267 tooltip => "The password for your character",
1116 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value } 1268 on_changed => sub { my ($self, $value) = @_; $CFG->{profile}{default}{password} = $value; 1 }
1117 ); 1269 );
1118 1270
1119 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size"); 1271 $table->add_at (0, 7, new CFPlus::UI::Label valign => 0, align => 1, text => "Map Size");
1120 $table->add_at (1, 7, new CFPlus::UI::Slider 1272 $table->add_at (1, 7, new CFPlus::UI::Slider
1121 force_w => 100, 1273 force_w => 100,
1122 range => [$CFG->{mapsize}, 10, 100, 0, 1], 1274 range => [$CFG->{mapsize}, 10, 100, 0, 1],
1123 tooltip => "This is the size of the portion of the map update the server sends you. " 1275 tooltip => "This is the size of the portion of the map update the server sends you. "
1124 . "If you set this to a high value you will be able to see further, " 1276 . "If you set this to a high value you will be able to see further, "
1125 . "but you also increase bandwidth requirements and latency. " 1277 . "but you also increase bandwidth requirements and latency. "
1126 . "This option is only used once at log-in.", 1278 . "This option is only used once at log-in.",
1127 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 0 }, 1279 on_changed => sub { my ($self, $value) = @_; $CFG->{mapsize} = $self->{range}[0] = $value = int $value; 1 },
1128 ); 1280 );
1129 1281
1130 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Face Prefetch");
1131 $table->add_at (1, 8, new CFPlus::UI::CheckBox
1132 state => $CFG->{face_prefetch},
1133 tooltip => "<b>Background Image Prefetch</b>\n\n"
1134 . "If enabled, the client automatically pre-fetches images from the server. "
1135 . "This might increase or create lag, but increases the chances "
1136 . "of faces being ready for display when you encounter them. "
1137 . "It also uses up server bandwidth on every connect, "
1138 . "so only set it if you really need to prefetch images. "
1139 . "This option can be set and unset any time.",
1140 on_changed => sub { $CFG->{face_prefetch} = $_[1]; 0 },
1141 );
1142
1143 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate"); 1282 $table->add_at (0, 8, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Rate");
1283 $table->add_at (1, 8, new CFPlus::UI::Entry
1284 text => $CFG->{output_rate},
1285 tooltip => "The maximum bandwidth in bytes per second that the server should not exceed "
1286 . "when sending data. When 0 or unset, the server "
1287 . "default will be used, which is usually around 100kb/s. Most servers will "
1288 . "dynamically find an optimal rate, so adjust this only when necessary.",
1289 on_changed => sub { $CFG->{output_rate} = $_[1]; 1 },
1290 );
1291
1292 $table->add_at (0, 9, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count");
1144 $table->add_at (1, 9, new CFPlus::UI::Entry 1293 $table->add_at (1, 9, new CFPlus::UI::Entry
1145 text => $CFG->{output_rate},
1146 tooltip => "The approximate bandwidth in bytes per second that the server should not exceed "
1147 . "when sending images, to ensure interactiveness. When 0 or unset, the server "
1148 . "default will be used, which is usually around 100kb/s.",
1149 on_changed => sub { $CFG->{output_rate} = $_[1]; 0 },
1150 );
1151
1152 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Count");
1153 $table->add_at (1, 10, new CFPlus::UI::Entry
1154 text => $CFG->{output_count}, 1294 text => $CFG->{output_count},
1155 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1295 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1156 on_changed => sub { $CFG->{output_count} = $_[1]; 0 }, 1296 on_changed => sub { $CFG->{output_count} = $_[1]; 1 },
1157 ); 1297 );
1158 1298
1159 $table->add_at (0, 11, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync"); 1299 $table->add_at (0, 10, new CFPlus::UI::Label valign => 0, align => 1, text => "Output-Sync");
1160 $table->add_at (1, 11, new CFPlus::UI::Entry 1300 $table->add_at (1, 10, new CFPlus::UI::Entry
1161 text => $CFG->{output_sync}, 1301 text => $CFG->{output_sync},
1162 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.", 1302 tooltip => "Should be set to 1 unless you know what you are doing. This option is only used once at log-in.",
1163 on_changed => sub { $CFG->{output_sync} = $_[1]; 0 }, 1303 on_changed => sub { $CFG->{output_sync} = $_[1]; 1 },
1164 ); 1304 );
1165 1305
1166 $table->add_at (1, 12, $LOGIN_BUTTON = new CFPlus::UI::Button 1306 $table->add_at (1, 11, $LOGIN_BUTTON = new CFPlus::UI::Button
1167 expand => 1, 1307 expand => 1,
1168 align => 0, 1308 align => 0,
1169 text => "Login", 1309 text => "Login",
1170 on_activate => sub { 1310 on_activate => sub {
1171 $CONN ? stop_game 1311 $CONN ? stop_game
1172 : start_game; 1312 : start_game;
1173 0 1313 1
1174 }, 1314 },
1175 ); 1315 );
1176 1316
1177 $vbox->add (new CFPlus::UI::FancyFrame 1317 $vbox->add (new CFPlus::UI::FancyFrame
1178 label => "Server Info", 1318 label => "Server Info",
1807 } 1947 }
1808 1948
1809 $STATUSBOX->add ("Set video mode $WIDTH×$HEIGHT", timeout => 10, fg => [1, 1, 1, 0.5]); 1949 $STATUSBOX->add ("Set video mode $WIDTH×$HEIGHT", timeout => 10, fg => [1, 1, 1, 0.5]);
1810} 1950}
1811 1951
1812sub setup_build_button {
1813 my ($enabled) = @_;
1814 if ($enabled) {
1815 $BUILD_BUTTON->hide if $BUILD_BUTTON;
1816 $BUILD_BUTTON ||= new CFPlus::UI::Button
1817 text => "Build",
1818 tooltip => "Opens the ingame builder",
1819 on_activate => sub {
1820 if ($CONN) {
1821 $CONN->send_ext_req (builder_player_items => sub {
1822 open_ingame_editor ($_[0]) if exists $_[0]->{items};
1823 });
1824 }
1825 0
1826 };
1827 $BUTTONBAR->add ($BUILD_BUTTON);
1828 } else {
1829 $BUILD_BUTTON->hide if $BUILD_BUTTON;
1830 }
1831}
1832
1833sub open_ingame_editor {
1834 my ($msg) = @_;
1835
1836 my $win = new CFPlus::UI::Toplevel
1837 x => 0,
1838 y => 'center',
1839 z => 4,
1840 name => 'builder_window',
1841 force_w => int $WIDTH * 1/4,
1842 force_h => int $HEIGHT * 3/4,
1843 title => "In game builder",
1844 has_close_button => 1;
1845
1846 my $r = new CFPlus::UI::ScrolledWindow (
1847 expand => 1,
1848 scroll_y => 1
1849 );
1850 $r->add (my $vb = new CFPlus::UI::VBox);
1851 $win->add ($r);
1852
1853
1854 $vb->add (
1855 new CFPlus::UI::Button
1856 text => "Disable build mode",
1857 on_activate => sub { $::IN_BUILD_MODE = undef }
1858 );
1859 $vb->add (
1860 new CFPlus::UI::Button
1861 text => "ERASE",
1862 on_activate => sub { $::IN_BUILD_MODE = { do_erase => 1 } }
1863 );
1864
1865 for my $itemarchname (
1866 sort {
1867 $msg->{items}->{$a}->{build_arch_name}
1868 cmp $msg->{items}->{$b}->{build_arch_name}
1869 } keys %{$msg->{items}}
1870 ) {
1871 my $info = $msg->{items}->{$itemarchname};
1872 $vb->add (
1873 new CFPlus::UI::Button text => $info->{build_arch_name},
1874 on_activate => sub {
1875 $::IN_BUILD_MODE = { item => $itemarchname, info => $info };
1876
1877 if (grep { $msg->{items}->{$itemarchname}->{$_} } qw/has_connection has_name has_text/) {
1878 build_mode_query_arch_info ();
1879 }
1880 }
1881 );
1882 }
1883
1884 $win->show;
1885}
1886
1887sub build_mode_query_arch_info {
1888 my ($iteminfo) = $::IN_BUILD_MODE;
1889 my $itemarchname = $iteminfo->{item};
1890 my $info = $iteminfo->{info};
1891
1892 my $dialog = new CFPlus::UI::Toplevel
1893 x => "center",
1894 y => "center",
1895 z => 50,
1896 force_w => int $WIDTH * 1/2,
1897 title => "Enter information for placement of '$itemarchname'",
1898 has_close_button => 1;
1899
1900 $dialog->add (my $vb = new CFPlus::UI::VBox expand => 1);
1901
1902 $vb->add (my $table = new CFPlus::UI::Table expand => 1);
1903 my $row = 0;
1904 if ($info->{has_name}) {
1905 $table->add_at (0, $row, new CFPlus::UI::Label text => "Name:");
1906 $table->add_at (1, $row++, new CFPlus::UI::Entry expand => 1, on_changed => sub { $::IN_BUILD_MODE->{name} = $_[1]; 0 });
1907 }
1908 if ($info->{has_text}) {
1909 $table->add_at (0, $row, new CFPlus::UI::Label text => "Text:");
1910 $table->add_at (1, $row++, new CFPlus::UI::Entry expand => 1, on_changed => sub { $::IN_BUILD_MODE->{text} = $_[1]; 0 });
1911 }
1912 if ($info->{has_connection}) {
1913 $table->add_at (0, $row, new CFPlus::UI::Label text => "Connection ID:");
1914 $table->add_at (1, $row++,
1915 new CFPlus::UI::Entry
1916 expand => 1,
1917 on_changed => sub { $::IN_BUILD_MODE->{connection} = $_[1]; 0 },
1918 tooltip => "Enter the connection ID here. The connection ID connects actors like a lever to a gate or a magic ear to a gate"
1919 );
1920 }
1921
1922 $vb->add (my $hb = new CFPlus::UI::HBox expand => 1);
1923 $hb->add (new CFPlus::UI::Button
1924 text => "Close",
1925 expand => 1,
1926 on_activate => sub { $dialog->hide; 0 },
1927 );
1928 $dialog->show;
1929}
1930
1931sub video_shutdown { 1952sub video_shutdown {
1932 CFPlus::OpenGL::shutdown; 1953 CFPlus::OpenGL::shutdown;
1933 1954
1934 undef $SDL_ACTIVE; 1955 undef $SDL_ACTIVE;
1935} 1956}
1936 1957
1937sub audio_channel_finished {
1938 my ($channel) = @_;
1939
1940 #warn "channel $channel finished\n";#d#
1941}
1942
1943sub audio_music_set {
1944 my ($songs) = @_;
1945
1946 my @want =
1947 grep $_,
1948 map $CONN->{music_meta}{$_},
1949 @$songs;
1950
1951 if (@want) {
1952 @MUSIC_WANT = @want;
1953 &audio_music_changed ();
1954 }
1955}
1956
1957sub audio_music_start {
1958 my $path = $MUSIC_PLAYING->{path}
1959 or return;
1960
1961 CFPlus::DB::prefetch_file $path, 1024_000, sub {
1962 return unless $SDL_MIXER;
1963
1964 # music might have changed...
1965 $path eq $MUSIC_PLAYING->{path}
1966 or return &audio_music_start ();
1967
1968 $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $path;
1969
1970 my $NOW = time;
1971
1972 if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) {
1973 my $pos = $MUSIC_PLAYING->{stop_pos};
1974 $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos);
1975 $MUSIC_START = time - $pos;
1976 } else {
1977 $MUSIC_PLAYER->play (0);
1978 $MUSIC_START = time;
1979 }
1980
1981 delete $MUSIC_PLAYING->{stop_time};
1982 delete $MUSIC_PLAYING->{stop_pos};
1983 }
1984}
1985
1986sub audio_music_changed {
1987 return unless $CFG->{bgm_enable};
1988 return unless $SDL_MIXER;
1989
1990 # default MUSIC_WANT == MUSIC_DEFAULT
1991 @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT;
1992
1993 # if the currently playing song is acceptable, let it continue
1994 return if $MUSIC_PLAYING
1995 && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT;
1996
1997 my $NOW = time;
1998
1999 if ($MUSIC_PLAYING) {
2000 $MUSIC_PLAYING->{stop_time} = $NOW;
2001 $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START;
2002 CFPlus::MixMusic::fade_out 1000;
2003 } else {
2004 # sort by stop time, oldest first
2005 @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT;
2006
2007 # if the most recently-played piece played very recently,
2008 # resume it, else choose the oldest piece for rotation.
2009 $MUSIC_PLAYING =
2010 $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME
2011 ? $MUSIC_WANT[-1]
2012 : $MUSIC_WANT[0];
2013
2014 audio_music_start;
2015 }
2016}
2017
2018sub audio_music_finished {
2019 $MUSIC_PLAYING = undef;
2020 undef $MUSIC_PLAYER;
2021
2022 audio_music_changed;
2023}
2024
2025sub audio_init { 1958sub audio_init {
2026 if ($CFG->{audio_enable}) { 1959 if ($CFG->{audio_enable}) {
2027 if (open my $fh, "<", CFPlus::find_rcfile "sounds/config") { 1960 $ENV{MIX_EFFECTSMAXSPEED} = 1;
2028 $SDL_MIXER = !CFPlus::Mix_OpenAudio; 1961 $SDL_MIXER = !CFPlus::Mix_OpenAudio;
2029 1962
2030 unless ($SDL_MIXER) { 1963 unless ($SDL_MIXER) {
2031 status "Unable to open sound device: there will be no sound"; 1964 status "Unable to open sound device: there will be no sound";
2032 return; 1965 return;
2033 }
2034
2035 CFPlus::Mix_AllocateChannels 8;
2036 CFPlus::MixMusic::volume $CFG->{bgm_volume} * 128;
2037
2038 audio_music_finished;
2039
2040 local $_;
2041 while (<$fh>) {
2042 next if /^\s*#/;
2043 next if /^\s*$/;
2044
2045 my ($file, $volume, $event) = split /\s+/, $_, 3;
2046
2047 push @SOUNDS, "$volume,$file";
2048
2049 $AUDIO_CHUNKS{"$volume,$file"} ||= do {
2050 my $chunk = new_from_file CFPlus::MixChunk CFPlus::find_rcfile "sounds/$file";
2051 $chunk->volume ($volume * 128 / 100);
2052 $chunk
2053 };
2054 }
2055 } else {
2056 status "unable to open sound config: $!";
2057 } 1966 }
1967
1968 CFPlus::Mix_AllocateChannels 16;
1969
1970 audio_music_finished;
2058 } else { 1971 } else {
2059 undef $SDL_MIXER; 1972 undef $SDL_MIXER;
2060 } 1973 }
2061} 1974}
2062 1975
2063sub audio_shutdown { 1976sub audio_shutdown {
2064 CFPlus::Mix_CloseAudio if $SDL_MIXER; 1977 CFPlus::Mix_CloseAudio if $SDL_MIXER;
2065 undef $SDL_MIXER; 1978 undef $SDL_MIXER;
2066 @SOUNDS = (); 1979 @SOUNDS = ();
2067 %AUDIO_CHUNKS = (); 1980 %AUDIO_CHUNK = ();
2068} 1981}
2069 1982
2070my %animate_object; 1983my %animate_object;
2071my $animate_timer; 1984my $animate_timer;
2072 1985
2073my $fps = 9; 1986my $fps = 9;
2074
2075my %demo;#d#
2076 1987
2077sub force_refresh { 1988sub force_refresh {
2078 $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05; 1989 $fps = $fps * 0.95 + 1 / (($NOW - $LAST_REFRESH) || 0.1) * 0.05;
2079 debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4; 1990 debug sprintf "%3.2f", $fps if $ENV{CFPLUS_DEBUG} & 4;
2080 1991
2112 2023
2113sub animation_stop { 2024sub animation_stop {
2114 my ($widget) = @_; 2025 my ($widget) = @_;
2115 delete $animate_object{$widget}; 2026 delete $animate_object{$widget};
2116} 2027}
2117
2118# check once/second for faces that need to be prefetched
2119# this should, of course, only run on demand, but
2120# SDL forces worse things on us....
2121
2122Event->timer (after => 1, interval => 0.25, cb => sub {
2123 $CONN->face_prefetch
2124 if $CONN;
2125});
2126 2028
2127%SDL_CB = ( 2029%SDL_CB = (
2128 CFPlus::SDL_QUIT => sub { 2030 CFPlus::SDL_QUIT => sub {
2129 exit; 2031 exit;
2130 }, 2032 },
2174 2076
2175 CFPlus::UI::set_layout ($::CFG->{layout}); 2077 CFPlus::UI::set_layout ($::CFG->{layout});
2176 2078
2177 my %DEF_CFG = ( 2079 my %DEF_CFG = (
2178 sdl_mode => 0, 2080 sdl_mode => 0,
2179 width => 640,
2180 height => 480,
2181 fullscreen => 0, 2081 fullscreen => 0,
2182 fast => 0, 2082 fast => 0,
2183 map_scale => 1, 2083 map_scale => 1,
2184 fow_enable => 1, 2084 fow_enable => 1,
2185 fow_intensity => 0, 2085 fow_intensity => 0,
2191 stat_fontsize => 0.7, 2091 stat_fontsize => 0.7,
2192 mapsize => 100, 2092 mapsize => 100,
2193 audio_enable => 1, 2093 audio_enable => 1,
2194 bgm_enable => 1, 2094 bgm_enable => 1,
2195 bgm_volume => 0.25, 2095 bgm_volume => 0.25,
2196 face_prefetch => 0,
2197 output_sync => 1, 2096 output_sync => 1,
2198 output_count => 1, 2097 output_count => 1,
2199 output_rate => "", 2098 output_rate => "",
2200 pickup => 0, 2099 pickup => 0,
2201 inv_sort => "mtime", 2100 inv_sort => "mtime",

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines