~ubuntu-branches/debian/stretch/opentyrian/stretch

« back to all changes in this revision

Viewing changes to src/jukebox.c

  • Committer: Package Import Robot
  • Author(s): Etienne Millon
  • Date: 2015-03-31 08:48:54 UTC
  • Revision ID: package-import@ubuntu.com-20150331084854-f5a4uoz7uv3vopk6
Tags: upstream-2.1.20130907+dfsg
ImportĀ upstreamĀ versionĀ 2.1.20130907+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 "font.h"
 
20
#include "joystick.h"
 
21
#include "jukebox.h"
 
22
#include "keyboard.h"
 
23
#include "lds_play.h"
 
24
#include "loudness.h"
 
25
#include "mtrand.h"
 
26
#include "nortsong.h"
 
27
#include "opentyr.h"
 
28
#include "palette.h"
 
29
#include "sprite.h"
 
30
#include "starlib.h"
 
31
#include "vga_palette.h"
 
32
#include "video.h"
 
33
 
 
34
void jukebox( void )
 
35
{
 
36
        bool trigger_quit = false,  // true when user wants to quit
 
37
             quitting = false;
 
38
        
 
39
        bool hide_text = false;
 
40
 
 
41
        bool fade_looped_songs = true, fading_song = false;
 
42
        bool stopped = false;
 
43
 
 
44
        bool fx = false;
 
45
        int fx_num = 0;
 
46
 
 
47
        int palette_fade_steps = 15;
 
48
 
 
49
        int diff[256][3];
 
50
        init_step_fade_palette(diff, vga_palette, 0, 255);
 
51
 
 
52
        JE_starlib_init();
 
53
 
 
54
        int fade_volume = tyrMusicVolume;
 
55
        
 
56
        for (; ; )
 
57
        {
 
58
                if (!stopped && !audio_disabled)
 
59
                {
 
60
                        if (songlooped && fade_looped_songs)
 
61
                                fading_song = true;
 
62
 
 
63
                        if (fading_song)
 
64
                        {
 
65
                                if (fade_volume > 5)
 
66
                                {
 
67
                                        fade_volume -= 2;
 
68
                                }
 
69
                                else
 
70
                                {
 
71
                                        fade_volume = tyrMusicVolume;
 
72
 
 
73
                                        fading_song = false;
 
74
                                }
 
75
 
 
76
                                set_volume(fade_volume, fxVolume);
 
77
                        }
 
78
 
 
79
                        if (!playing || (songlooped && fade_looped_songs && !fading_song))
 
80
                                play_song(mt_rand() % MUSIC_NUM);
 
81
                }
 
82
 
 
83
                setdelay(1);
 
84
 
 
85
                SDL_FillRect(VGAScreenSeg, NULL, 0);
 
86
 
 
87
                // starlib input needs to be rewritten
 
88
                JE_starlib_main();
 
89
 
 
90
                push_joysticks_as_keyboard();
 
91
                service_SDL_events(true);
 
92
 
 
93
                if (!hide_text)
 
94
                {
 
95
                        char buffer[60];
 
96
                        
 
97
                        if (fx)
 
98
                                snprintf(buffer, sizeof(buffer), "%d %s", fx_num + 1, soundTitle[fx_num]);
 
99
                        else
 
100
                                snprintf(buffer, sizeof(buffer), "%d %s", song_playing + 1, musicTitle[song_playing]);
 
101
                        
 
102
                        const int x = VGAScreen->w / 2;
 
103
                        
 
104
                        draw_font_hv(VGAScreen, x, 170, "Press ESC to quit the jukebox.",           small_font, centered, 1, 0);
 
105
                        draw_font_hv(VGAScreen, x, 180, "Arrow keys change the song being played.", small_font, centered, 1, 0);
 
106
                        draw_font_hv(VGAScreen, x, 190, buffer,                                     small_font, centered, 1, 4);
 
107
                }
 
108
 
 
109
                if (palette_fade_steps > 0)
 
110
                        step_fade_palette(diff, palette_fade_steps--, 0, 255);
 
111
                
 
112
                JE_showVGA();
 
113
 
 
114
                wait_delay();
 
115
 
 
116
                // quit on mouse click
 
117
                Uint16 x, y;
 
118
                if (JE_mousePosition(&x, &y) > 0)
 
119
                        trigger_quit = true;
 
120
 
 
121
                if (newkey)
 
122
                {
 
123
                        switch (lastkey_sym)
 
124
                        {
 
125
                        case SDLK_ESCAPE: // quit jukebox
 
126
                        case SDLK_q:
 
127
                                trigger_quit = true;
 
128
                                break;
 
129
 
 
130
                        case SDLK_SPACE:
 
131
                                hide_text = !hide_text;
 
132
                                break;
 
133
 
 
134
                        case SDLK_f:
 
135
                                fading_song = !fading_song;
 
136
                                break;
 
137
                        case SDLK_n:
 
138
                                fade_looped_songs = !fade_looped_songs;
 
139
                                break;
 
140
 
 
141
                        case SDLK_SLASH: // switch to sfx mode
 
142
                                fx = !fx;
 
143
                                break;
 
144
                        case SDLK_COMMA:
 
145
                                if (fx && --fx_num < 0)
 
146
                                        fx_num = SAMPLE_COUNT - 1;
 
147
                                break;
 
148
                        case SDLK_PERIOD:
 
149
                                if (fx && ++fx_num >= SAMPLE_COUNT)
 
150
                                        fx_num = 0;
 
151
                                break;
 
152
                        case SDLK_SEMICOLON:
 
153
                                if (fx)
 
154
                                        JE_playSampleNum(fx_num + 1);
 
155
                                break;
 
156
 
 
157
                        case SDLK_LEFT:
 
158
                        case SDLK_UP:
 
159
                                play_song((song_playing > 0 ? song_playing : MUSIC_NUM) - 1);
 
160
                                stopped = false;
 
161
                                break;
 
162
                        case SDLK_RETURN:
 
163
                        case SDLK_RIGHT:
 
164
                        case SDLK_DOWN:
 
165
                                play_song((song_playing + 1) % MUSIC_NUM);
 
166
                                stopped = false;
 
167
                                break;
 
168
                        case SDLK_s: // stop song
 
169
                                stop_song();
 
170
                                stopped = true;
 
171
                                break;
 
172
                        case SDLK_r: // restart song
 
173
                                restart_song();
 
174
                                stopped = false;
 
175
                                break;
 
176
 
 
177
                        default:
 
178
                                break;
 
179
                        }
 
180
                }
 
181
                
 
182
                // user wants to quit, start fade-out
 
183
                if (trigger_quit && !quitting)
 
184
                {
 
185
                        palette_fade_steps = 15;
 
186
                        
 
187
                        SDL_Color black = { 0, 0, 0 };
 
188
                        init_step_fade_solid(diff, black, 0, 255);
 
189
                        
 
190
                        quitting = true;
 
191
                }
 
192
                
 
193
                // if fade-out finished, we can finally quit
 
194
                if (quitting && palette_fade_steps == 0)
 
195
                        break;
 
196
        }
 
197
 
 
198
        set_volume(tyrMusicVolume, fxVolume);
 
199
}
 
200