| 1 | /* |
| 2 | * OpenTyrian: A modern cross-platform port of Tyrian |
| 3 | * Copyright (C) 2007-2009 The OpenTyrian Development Team |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version 2 |
| 8 | * of the License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | #include "jukebox.h" |
| 20 | |
| 21 | #include "font.h" |
| 22 | #include "joystick.h" |
| 23 | #include "keyboard.h" |
| 24 | #include "lds_play.h" |
| 25 | #include "loudness.h" |
| 26 | #include "mtrand.h" |
| 27 | #include "nortsong.h" |
| 28 | #include "opentyr.h" |
| 29 | #include "palette.h" |
| 30 | #include "sprite.h" |
| 31 | #include "starlib.h" |
| 32 | #include "vga_palette.h" |
| 33 | #include "video.h" |
| 34 | |
| 35 | #include <stdio.h> |
| 36 | |
| 37 | void jukebox(void) // FKA Setup.jukeboxGo |
| 38 | { |
| 39 | bool trigger_quit = false, // true when user wants to quit |
| 40 | quitting = false; |
| 41 | |
| 42 | bool hide_text = false; |
| 43 | |
| 44 | bool fade_looped_songs = true, fading_song = false; |
| 45 | bool stopped = false; |
| 46 | |
| 47 | bool fx = false; |
| 48 | int fx_num = 0; |
| 49 | |
| 50 | int palette_fade_steps = 15; |
| 51 | |
| 52 | int diff[256][3]; |
| 53 | init_step_fade_palette(diff, vga_palette, 0, 255); |
| 54 | |
| 55 | JE_starlib_init(); |
| 56 | |
| 57 | int fade_volume = tyrMusicVolume; |
| 58 | |
| 59 | for (; ; ) |
| 60 | { |
| 61 | if (!stopped && !audio_disabled) |
| 62 | { |
| 63 | if (songlooped && fade_looped_songs) |
| 64 | fading_song = true; |
| 65 | |
| 66 | if (fading_song) |
| 67 | { |
| 68 | if (fade_volume > 5) |
| 69 | { |
| 70 | fade_volume -= 2; |
| 71 | } |
| 72 | else |
| 73 | { |
| 74 | fade_volume = tyrMusicVolume; |
| 75 | |
| 76 | fading_song = false; |
| 77 | } |
| 78 | |
| 79 | set_volume(fade_volume, fxVolume); |
| 80 | } |
| 81 | |
| 82 | if (!playing || (songlooped && fade_looped_songs && !fading_song)) |
| 83 | play_song(mt_rand() % MUSIC_NUM); |
| 84 | } |
| 85 | |
| 86 | setDelay(1); |
| 87 | |
| 88 | SDL_FillRect(VGAScreenSeg, NULL, 0); |
| 89 | |
| 90 | // starlib input needs to be rewritten |
| 91 | JE_starlib_main(); |
| 92 | |
| 93 | push_joysticks_as_keyboard(); |
| 94 | service_SDL_events(true); |
| 95 | |
| 96 | if (!hide_text) |
| 97 | { |
| 98 | char buffer[60]; |
| 99 | |
| 100 | if (fx) |
| 101 | snprintf(buffer, sizeof(buffer), "%d %s", fx_num + 1, soundTitle[fx_num]); |
| 102 | else |
| 103 | snprintf(buffer, sizeof(buffer), "%d %s", song_playing + 1, musicTitle[song_playing]); |
| 104 | |
| 105 | const int x = VGAScreen->w / 2; |
| 106 | |
| 107 | draw_font_hv(VGAScreen, x, 170, "Press ESC to quit the jukebox.", small_font, centered, 1, 0); |
| 108 | draw_font_hv(VGAScreen, x, 180, "Arrow keys change the song being played.", small_font, centered, 1, 0); |
| 109 | draw_font_hv(VGAScreen, x, 190, buffer, small_font, centered, 1, 4); |
| 110 | } |
| 111 | |
| 112 | if (palette_fade_steps > 0) |
| 113 | step_fade_palette(diff, palette_fade_steps--, 0, 255); |
| 114 | |
| 115 | JE_showVGA(); |
| 116 | |
| 117 | wait_delay(); |
| 118 | |
| 119 | // quit on mouse click |
| 120 | Uint16 x, y; |
| 121 | if (JE_mousePosition(&x, &y) > 0) |
| 122 | trigger_quit = true; |
| 123 | |
| 124 | if (newkey) |
| 125 | { |
| 126 | switch (lastkey_scan) |
| 127 | { |
| 128 | case SDL_SCANCODE_ESCAPE: // quit jukebox |
| 129 | case SDL_SCANCODE_Q: |
| 130 | trigger_quit = true; |
| 131 | break; |
| 132 | |
| 133 | case SDL_SCANCODE_SPACE: |
| 134 | hide_text = !hide_text; |
| 135 | break; |
| 136 | |
| 137 | case SDL_SCANCODE_F: |
| 138 | fading_song = !fading_song; |
| 139 | break; |
| 140 | case SDL_SCANCODE_N: |
| 141 | fade_looped_songs = !fade_looped_songs; |
| 142 | break; |
| 143 | |
| 144 | case SDL_SCANCODE_SLASH: // switch to sfx mode |
| 145 | fx = !fx; |
| 146 | break; |
| 147 | case SDL_SCANCODE_COMMA: |
| 148 | if (fx && --fx_num < 0) |
| 149 | fx_num = SOUND_COUNT - 1; |
| 150 | break; |
| 151 | case SDL_SCANCODE_PERIOD: |
| 152 | if (fx && ++fx_num >= SOUND_COUNT) |
| 153 | fx_num = 0; |
| 154 | break; |
| 155 | case SDL_SCANCODE_SEMICOLON: |
| 156 | if (fx) |
| 157 | JE_playSampleNum(fx_num + 1); |
| 158 | break; |
| 159 | |
| 160 | case SDL_SCANCODE_LEFT: |
| 161 | case SDL_SCANCODE_UP: |
| 162 | play_song((song_playing > 0 ? song_playing : MUSIC_NUM) - 1); |
| 163 | stopped = false; |
| 164 | break; |
| 165 | case SDL_SCANCODE_RETURN: |
| 166 | case SDL_SCANCODE_RIGHT: |
| 167 | case SDL_SCANCODE_DOWN: |
| 168 | play_song((song_playing + 1) % MUSIC_NUM); |
| 169 | stopped = false; |
| 170 | break; |
| 171 | case SDL_SCANCODE_S: // stop song |
| 172 | stop_song(); |
| 173 | stopped = true; |
| 174 | break; |
| 175 | case SDL_SCANCODE_R: // restart song |
| 176 | restart_song(); |
| 177 | stopped = false; |
| 178 | break; |
| 179 | |
| 180 | default: |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // user wants to quit, start fade-out |
| 186 | if (trigger_quit && !quitting) |
| 187 | { |
| 188 | palette_fade_steps = 15; |
| 189 | |
| 190 | SDL_Color black = { 0, 0, 0 }; |
| 191 | init_step_fade_solid(diff, black, 0, 255); |
| 192 | |
| 193 | quitting = true; |
| 194 | } |
| 195 | |
| 196 | // if fade-out finished, we can finally quit |
| 197 | if (quitting && palette_fade_steps == 0) |
| 198 | break; |
| 199 | } |
| 200 | |
| 201 | set_volume(tyrMusicVolume, fxVolume); |
| 202 | } |