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 "file.h"
20 #include "joystick.h"
21 #include "keyboard.h"
22 #include "loudness.h"
23 #include "musmast.h"
24 #include "nortsong.h"
25 #include "opentyr.h"
26 #include "params.h"
27 #include "sndmast.h"
28 #include "vga256d.h"
29
30 #include "SDL.h"
31
32 Uint32 target, target2;
33
34 JE_boolean notYetLoadedSound = true;
35
36 JE_word frameCount, frameCount2, frameCountMax;
37
38 JE_byte *digiFx[SAMPLE_COUNT] = { NULL }; /* [1..soundnum + 9] */
39 JE_word fxSize[SAMPLE_COUNT]; /* [1..soundnum + 9] */
40
41 JE_word tyrMusicVolume, fxVolume;
42 JE_word fxPlayVol;
43 JE_word tempVolume;
44
45 JE_word speed; /* JE: holds timer speed for 70Hz */
46
47 float jasondelay = 1000.0f / (1193180.0f / 0x4300);
48
setdelay(JE_byte delay)49 void setdelay( JE_byte delay )
50 {
51 target = (delay * 16) + SDL_GetTicks();
52 }
53
setjasondelay(int delay)54 void setjasondelay( int delay )
55 {
56 target = SDL_GetTicks() + delay * jasondelay;
57 }
58
setjasondelay2(int delay)59 void setjasondelay2( int delay )
60 {
61 target2 = SDL_GetTicks() + delay * jasondelay;
62 }
63
delaycount(void)64 int delaycount( void )
65 {
66 return (SDL_GetTicks() < target ? target - SDL_GetTicks() : 0);
67 }
68
delaycount2(void)69 int delaycount2( void )
70 {
71 return (SDL_GetTicks() < target2 ? target2 - SDL_GetTicks() : 0);
72 }
73
wait_delay(void)74 void wait_delay( void )
75 {
76 Sint32 delay = target - SDL_GetTicks();
77 if (delay > 0)
78 SDL_Delay(delay);
79 }
80
service_wait_delay(void)81 void service_wait_delay( void )
82 {
83 while (SDL_GetTicks() < target)
84 {
85 SDL_Delay(SDL_GetTicks() - target > SDL_POLL_INTERVAL ? SDL_POLL_INTERVAL : SDL_GetTicks() - target);
86 service_SDL_events(false);
87 }
88 }
89
wait_delayorinput(JE_boolean keyboard,JE_boolean mouse,JE_boolean joystick)90 void wait_delayorinput( JE_boolean keyboard, JE_boolean mouse, JE_boolean joystick )
91 {
92 service_SDL_events(true);
93 while (SDL_GetTicks() < target && !((keyboard && keydown) || (mouse && mousedown) || (joystick && joydown)))
94 {
95 SDL_Delay(SDL_GetTicks() - target > SDL_POLL_INTERVAL ? SDL_POLL_INTERVAL : SDL_GetTicks() - target);
96 push_joysticks_as_keyboard();
97 service_SDL_events(false);
98 }
99 }
100
JE_loadSndFile(const char * effects_sndfile,const char * voices_sndfile)101 void JE_loadSndFile( const char *effects_sndfile, const char *voices_sndfile )
102 {
103 JE_byte y, z;
104 JE_word x;
105 JE_longint templ;
106 JE_longint sndPos[2][SAMPLE_COUNT + 1];
107 JE_word sndNum;
108
109 FILE *fi;
110
111 /* SYN: Loading offsets into TYRIAN.SND */
112 fi = dir_fopen_die(data_dir(), effects_sndfile, "rb");
113 efread(&sndNum, sizeof(sndNum), 1, fi);
114
115 for (x = 0; x < sndNum; x++)
116 {
117 efread(&sndPos[0][x], sizeof(sndPos[0][x]), 1, fi);
118 }
119 fseek(fi, 0, SEEK_END);
120 sndPos[0][sndNum] = ftell(fi); /* Store file size */
121
122 for (z = 0; z < sndNum; z++)
123 {
124 fseek(fi, sndPos[0][z], SEEK_SET);
125 fxSize[z] = (sndPos[0][z+1] - sndPos[0][z]); /* Store sample sizes */
126 free(digiFx[z]);
127 digiFx[z] = malloc(fxSize[z]);
128 efread(digiFx[z], 1, fxSize[z], fi); /* JE: Load sample to buffer */
129 }
130
131 fclose(fi);
132
133 /* SYN: Loading offsets into VOICES.SND */
134 fi = dir_fopen_die(data_dir(), voices_sndfile, "rb");
135
136 efread(&sndNum, sizeof(sndNum), 1, fi);
137
138 for (x = 0; x < sndNum; x++)
139 {
140 efread(&sndPos[1][x], sizeof(sndPos[1][x]), 1, fi);
141 }
142 fseek(fi, 0, SEEK_END);
143 sndPos[1][sndNum] = ftell(fi); /* Store file size */
144
145 z = SAMPLE_COUNT - 9;
146
147 for (y = 0; y < sndNum; y++)
148 {
149 fseek(fi, sndPos[1][y], SEEK_SET);
150
151 templ = (sndPos[1][y+1] - sndPos[1][y]) - 100; /* SYN: I'm not entirely sure what's going on here. */
152 if (templ < 1) templ = 1;
153 fxSize[z + y] = templ; /* Store sample sizes */
154 digiFx[z + y] = malloc(fxSize[z + y]);
155 efread(digiFx[z + y], 1, fxSize[z + y], fi); /* JE: Load sample to buffer */
156 }
157
158 fclose(fi);
159
160 notYetLoadedSound = false;
161
162 }
163
JE_playSampleNum(JE_byte samplenum)164 void JE_playSampleNum( JE_byte samplenum )
165 {
166 JE_multiSamplePlay(digiFx[samplenum-1], fxSize[samplenum-1], 0, fxPlayVol);
167 }
168
JE_calcFXVol(void)169 void JE_calcFXVol( void ) // TODO: not sure *exactly* what this does
170 {
171 fxPlayVol = (fxVolume - 1) >> 5;
172 }
173
JE_setTimerInt(void)174 void JE_setTimerInt( void )
175 {
176 jasondelay = 1000.0f / (1193180.0f / speed);
177 }
178
JE_resetTimerInt(void)179 void JE_resetTimerInt( void )
180 {
181 jasondelay = 1000.0f / (1193180.0f / 0x4300);
182 }
183
JE_changeVolume(JE_word * music,int music_delta,JE_word * sample,int sample_delta)184 void JE_changeVolume( JE_word *music, int music_delta, JE_word *sample, int sample_delta )
185 {
186 int music_temp = *music + music_delta,
187 sample_temp = *sample + sample_delta;
188
189 if (music_delta)
190 {
191 if (music_temp > 255)
192 {
193 music_temp = 255;
194 JE_playSampleNum(S_CLINK);
195 }
196 else if (music_temp < 0)
197 {
198 music_temp = 0;
199 JE_playSampleNum(S_CLINK);
200 }
201 }
202
203 if (sample_delta)
204 {
205 if (sample_temp > 255)
206 {
207 sample_temp = 255;
208 JE_playSampleNum(S_CLINK);
209 }
210 else if (sample_temp < 0)
211 {
212 sample_temp = 0;
213 JE_playSampleNum(S_CLINK);
214 }
215 }
216
217 *music = music_temp;
218 *sample = sample_temp;
219
220 JE_calcFXVol();
221
222 set_volume(*music, *sample);
223 }
224
225