… | |
… | |
161 | our %SDL_CB; |
161 | our %SDL_CB; |
162 | |
162 | |
163 | our $SDL_MIXER; |
163 | our $SDL_MIXER; |
164 | our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; |
164 | our $MUSIC_DEFAULT = "in_a_heartbeat.ogg"; |
165 | our @MUSIC_WANT; |
165 | our @MUSIC_WANT; |
|
|
166 | our $MUSIC_START; |
166 | our $MUSIC_PLAYING; |
167 | our $MUSIC_PLAYING; |
167 | our $MUSIC_PLAYER; |
168 | our $MUSIC_PLAYER; |
|
|
169 | our $MUSIC_RESUME = 30; # resume music when players less than these many seconds before |
168 | our @SOUNDS; # event => file mapping |
170 | our @SOUNDS; # event => file mapping |
169 | our %AUDIO_CHUNKS; # audio files |
171 | our %AUDIO_CHUNKS; # audio files |
170 | |
172 | |
171 | our $ALT_ENTER_MESSAGE; |
173 | our $ALT_ENTER_MESSAGE; |
172 | our $STATUSBOX; |
174 | our $STATUSBOX; |
… | |
… | |
501 | $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER); |
503 | $SETUP_NOTEBOOK->set_current_page ($SETUP_SERVER); |
502 | $SETUP_DIALOG->show; |
504 | $SETUP_DIALOG->show; |
503 | $PL_WINDOW->hide; |
505 | $PL_WINDOW->hide; |
504 | $SPELL_LIST->clear_spells; |
506 | $SPELL_LIST->clear_spells; |
505 | $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); |
507 | $CFPlus::UI::ROOT->emit (stop_game => ! ! $CONN); |
|
|
508 | |
|
|
509 | &audio_music_set ([]); |
506 | |
510 | |
507 | return unless $CONN; |
511 | return unless $CONN; |
508 | |
512 | |
509 | status "connection closed"; |
513 | status "connection closed"; |
510 | |
514 | |
… | |
… | |
1276 | |
1280 | |
1277 | $window |
1281 | $window |
1278 | } |
1282 | } |
1279 | |
1283 | |
1280 | sub autopickup_setup { |
1284 | sub autopickup_setup { |
|
|
1285 | my $r = new CFPlus::UI::ScrolledWindow ( |
|
|
1286 | expand => 1, |
|
|
1287 | scroll_y => 1 |
|
|
1288 | ); |
1281 | my $table = new CFPlus::UI::Table; |
1289 | $r->add (my $table = new CFPlus::UI::Table |
|
|
1290 | row_expand => [0], |
|
|
1291 | col_expand => [0, 1, 0, 1], |
|
|
1292 | ); |
1282 | |
1293 | |
1283 | for ( |
1294 | for ( |
1284 | ["General", 0, 0, |
1295 | ["General", 0, 0, |
1285 | ["Enable autopickup" => PICKUP_NEWMODE, \$PICKUP_ENABLE], |
1296 | ["Enable autopickup" => PICKUP_NEWMODE, \$PICKUP_ENABLE], |
1286 | ["Inhibit autopickup" => PICKUP_INHIBIT], |
1297 | ["Inhibit autopickup" => PICKUP_INHIBIT], |
… | |
… | |
1370 | $::CONN->send_command ("pickup $::CFG->{pickup}") |
1381 | $::CONN->send_command ("pickup $::CFG->{pickup}") |
1371 | if defined $::CONN; |
1382 | if defined $::CONN; |
1372 | 0 |
1383 | 0 |
1373 | }); |
1384 | }); |
1374 | |
1385 | |
1375 | $table |
1386 | $r |
1376 | } |
1387 | } |
1377 | |
1388 | |
1378 | my %SORT_ORDER = ( |
1389 | my %SORT_ORDER = ( |
1379 | type => undef, |
1390 | type => undef, |
1380 | mtime => sub { |
1391 | mtime => sub { |
… | |
… | |
2015 | } |
2026 | } |
2016 | |
2027 | |
2017 | sub audio_music_set { |
2028 | sub audio_music_set { |
2018 | my ($songs) = @_; |
2029 | my ($songs) = @_; |
2019 | |
2030 | |
2020 | my $count = @$songs; |
2031 | my @want = |
2021 | my @meta; |
2032 | grep $_, |
|
|
2033 | map $CONN->{music_meta}{$_}, |
|
|
2034 | @$songs; |
2022 | |
2035 | |
2023 | for my $name (@$songs) { |
2036 | if (@want) { |
2024 | CFPlus::DB::get resmap => $name, sub { |
|
|
2025 | my ($data) = @_; |
|
|
2026 | |
|
|
2027 | if ($data) { |
|
|
2028 | $data = JSON::XS->new->utf8->decode ($data); |
|
|
2029 | $data->{path} = CFPlus::DB::path_of $name; |
|
|
2030 | push @meta, $data; |
|
|
2031 | } |
|
|
2032 | |
|
|
2033 | unless (--$count) { |
|
|
2034 | if (@meta) { |
|
|
2035 | @MUSIC_WANT = @meta; |
2037 | @MUSIC_WANT = @want; |
2036 | &audio_music_changed (); |
2038 | &audio_music_changed (); |
2037 | } |
2039 | } |
2038 | } |
2040 | } |
|
|
2041 | |
|
|
2042 | sub audio_music_start { |
|
|
2043 | my $path = $MUSIC_PLAYING->{path} |
|
|
2044 | or return; |
|
|
2045 | |
|
|
2046 | CFPlus::DB::prefetch_file $path, 1024_000, sub { |
|
|
2047 | # music might have changed... |
|
|
2048 | $path eq $MUSIC_PLAYING->{path} |
|
|
2049 | or return &audio_music_start (); |
|
|
2050 | |
|
|
2051 | $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $path; |
|
|
2052 | |
|
|
2053 | my $NOW = time; |
|
|
2054 | |
|
|
2055 | if ($MUSIC_PLAYING->{stop_time} > $NOW - $MUSIC_RESUME) { |
|
|
2056 | my $pos = $MUSIC_PLAYING->{stop_pos}; |
|
|
2057 | $MUSIC_PLAYER->fade_in_pos (0, 1000, $pos); |
|
|
2058 | $MUSIC_START = time - $pos; |
|
|
2059 | } else { |
|
|
2060 | $MUSIC_PLAYER->play (0); |
|
|
2061 | $MUSIC_START = time; |
2039 | }; |
2062 | } |
|
|
2063 | |
|
|
2064 | delete $MUSIC_PLAYING->{stop_time}; |
|
|
2065 | delete $MUSIC_PLAYING->{stop_pos}; |
2040 | } |
2066 | } |
2041 | } |
2067 | } |
2042 | |
2068 | |
2043 | sub audio_music_changed { |
2069 | sub audio_music_changed { |
2044 | return unless $CFG->{bgm_enable}; |
2070 | return unless $CFG->{bgm_enable}; |
2045 | |
2071 | |
2046 | # default MUSIC_WANT, undef/0 == MUSIC_DEFAULT |
2072 | # default MUSIC_WANT == MUSIC_DEFAULT |
2047 | @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; |
2073 | @MUSIC_WANT = { path => CFPlus::find_rcfile "music/$MUSIC_DEFAULT" } unless @MUSIC_WANT; |
2048 | |
2074 | |
2049 | # if the currently playing song is acceptable, let it continue |
2075 | # if the currently playing song is acceptable, let it continue |
|
|
2076 | return if $MUSIC_PLAYING |
2050 | return if grep $MUSIC_PLAYING eq $_->{path}, @MUSIC_WANT; |
2077 | && grep $MUSIC_PLAYING->{path} eq $_->{path}, @MUSIC_WANT; |
2051 | |
2078 | |
|
|
2079 | my $NOW = time; |
|
|
2080 | |
2052 | if (defined $MUSIC_PLAYING) { |
2081 | if ($MUSIC_PLAYING) { |
2053 | warn "fade out"; |
2082 | $MUSIC_PLAYING->{stop_time} = $NOW; |
|
|
2083 | $MUSIC_PLAYING->{stop_pos} = $NOW - $MUSIC_START; |
2054 | CFPlus::MixMusic::fade_out 2000; |
2084 | CFPlus::MixMusic::fade_out 1000; |
2055 | } else { |
2085 | } else { |
2056 | warn "new music\n"; |
2086 | # sort by stop time, oldest first |
|
|
2087 | @MUSIC_WANT = sort { $a->{stop_time} <=> $b->{stop_time} } @MUSIC_WANT; |
2057 | |
2088 | |
2058 | my $music = $MUSIC_WANT [rand @MUSIC_WANT]; |
2089 | # if the most recently-played piece played very recently, |
|
|
2090 | # resume it, else choose the oldest piece for rotation. |
|
|
2091 | $MUSIC_PLAYING = |
|
|
2092 | $MUSIC_WANT[-1]{stop_time} > $NOW - $MUSIC_RESUME |
|
|
2093 | ? $MUSIC_WANT[-1] |
|
|
2094 | : $MUSIC_WANT[0]; |
2059 | |
2095 | |
2060 | $MUSIC_PLAYING = $music->{path}; |
2096 | audio_music_start; |
2061 | |
|
|
2062 | $MUSIC_PLAYER = new_from_file CFPlus::MixMusic $music->{path}; |
|
|
2063 | $MUSIC_PLAYER->play (0); |
|
|
2064 | } |
2097 | } |
2065 | } |
2098 | } |
2066 | |
2099 | |
2067 | sub audio_music_finished { |
2100 | sub audio_music_finished { |
2068 | $MUSIC_PLAYING = undef; |
2101 | $MUSIC_PLAYING = undef; |
… | |
… | |
2136 | |
2169 | |
2137 | my $refresh_watcher = Event->timer (after => 0, hard => 0, interval => 1 / $MAX_FPS, cb => sub { |
2170 | my $refresh_watcher = Event->timer (after => 0, hard => 0, interval => 1 / $MAX_FPS, cb => sub { |
2138 | $NOW = time; |
2171 | $NOW = time; |
2139 | |
2172 | |
2140 | ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_) |
2173 | ($SDL_CB{$_->{type}} || sub { warn "unhandled event $_->{type}" })->($_) |
2141 | for CFPlus::SDL_PollEvent; |
2174 | for CFPlus::poll_events; |
2142 | |
2175 | |
2143 | if (%animate_object) { |
2176 | if (%animate_object) { |
2144 | $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; |
2177 | $_->animate ($LAST_REFRESH - $NOW) for values %animate_object; |
2145 | $WANT_REFRESH++; |
2178 | $WANT_REFRESH++; |
2146 | } |
2179 | } |
… | |
… | |
2180 | CFPlus::SDL_VIDEOEXPOSE => sub { |
2213 | CFPlus::SDL_VIDEOEXPOSE => sub { |
2181 | CFPlus::UI::full_refresh; |
2214 | CFPlus::UI::full_refresh; |
2182 | }, |
2215 | }, |
2183 | CFPlus::SDL_ACTIVEEVENT => sub { |
2216 | CFPlus::SDL_ACTIVEEVENT => sub { |
2184 | # not useful, as APPACTIVE include sonly iconified state, not unmapped |
2217 | # not useful, as APPACTIVE include sonly iconified state, not unmapped |
2185 | # printf "active %x %x\n", $_[0]{gain}, $_[0]{state};#d# |
2218 | # printf "active %x %x %x\n", $_[0]{gain}, $_[0]{state}, CFPlus::SDL_GetAppState;#d# |
|
|
2219 | # printf "a %x\n", CFPlus::SDL_GetAppState & CFPlus::SDL_APPACTIVE;#d# |
2186 | # printf "A\n" if $_[0]{state} & CFPlus::SDL_APPACTIVE; |
2220 | # printf "A\n" if $_[0]{state} & CFPlus::SDL_APPACTIVE; |
2187 | # printf "K\n" if $_[0]{state} & CFPlus::SDL_APPINPUTFOCUS; |
2221 | # printf "K\n" if $_[0]{state} & CFPlus::SDL_APPINPUTFOCUS; |
2188 | # printf "M\n" if $_[0]{state} & CFPlus::SDL_APPMOUSEFOCUS; |
2222 | # printf "M\n" if $_[0]{state} & CFPlus::SDL_APPMOUSEFOCUS; |
2189 | }, |
2223 | }, |
2190 | CFPlus::SDL_KEYDOWN => sub { |
2224 | CFPlus::SDL_KEYDOWN => sub { |