OSDN Git Service

Project: Starfighter 1.4 オリジナル
[starfighter-jp/starfighter-jp.git] / src / audio.cpp
1 /*
2 Copyright (C) 2003 Parallel Realities
3 Copyright (C) 2011, 2012 Guus Sliepen
4 Copyright (C) 2015 Julian Marchant
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 as published by the Free Software Foundation; either version 3
9 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "Starfighter.h"
21
22 static Mix_Chunk *sound[SFX_MAX];
23 static Mix_Music *music = NULL;
24
25 void audio_loadSounds()
26 {
27         sound[SFX_EXPLOSION] = Mix_LoadWAV("sound/explode.ogg");
28         sound[SFX_HIT] = Mix_LoadWAV("sound/explode2.ogg");
29         sound[SFX_DEATH] = Mix_LoadWAV("sound/maledeath.ogg");
30         sound[SFX_MISSILE] = Mix_LoadWAV("sound/missile.ogg");
31         sound[SFX_PLASMA] = Mix_LoadWAV("sound/plasma.ogg");
32         sound[SFX_CLOCK] = Mix_LoadWAV("sound/clock.ogg");
33         sound[SFX_FLY] = Mix_LoadWAV("sound/flyby.ogg");
34         sound[SFX_ENERGYRAY] = Mix_LoadWAV("sound/beamLaser.ogg");
35         sound[SFX_PICKUP] = Mix_LoadWAV("sound/item.ogg");
36         sound[SFX_SHIELDUP] = Mix_LoadWAV("sound/shield.ogg");
37         sound[SFX_CLOAK] = Mix_LoadWAV("sound/cloak.ogg");
38         sound[SFX_DEBRIS] = Mix_LoadWAV("sound/explode3.ogg");
39         sound[SFX_DEBRIS2] = Mix_LoadWAV("sound/explode4.ogg");
40         sound[SFX_LASER] = Mix_LoadWAV("sound/laser.ogg");
41         sound[SFX_PLASMA2] = Mix_LoadWAV("sound/plasma2.ogg");
42         sound[SFX_PLASMA3] = Mix_LoadWAV("sound/plasma3.ogg");
43 }
44
45 void audio_playSound(int sid, float x)
46 {
47         if ((!engine.useSound) || (!engine.useAudio))
48                 return;
49
50         int channel = -1;
51         static int freechannel = 4;
52
53         switch(sid)
54         {
55                 case SFX_DEATH:
56                 case SFX_CLOCK:
57                 case SFX_FLY:
58                 case SFX_SHIELDUP:
59                 case SFX_PICKUP:
60                 case SFX_CLOAK:
61                 case SFX_PLASMA2:
62                 case SFX_PLASMA3:
63                         channel = -1;   
64                         break;
65                 case SFX_PLASMA:
66                 case SFX_LASER:
67                         channel = 0;
68                         break;
69                 case SFX_ENERGYRAY:
70                 case SFX_MISSILE:
71                         channel = 1;
72                         break;
73                 case SFX_HIT:
74                         channel = 2;
75                         break;
76                 case SFX_EXPLOSION:
77                 case SFX_DEBRIS:
78                 case SFX_DEBRIS2:
79                         channel = 3;
80                         break;
81         }
82
83         if(channel == -1) {
84                 channel = freechannel++;
85                 if(freechannel >= 8)
86                         freechannel = 4;
87         }
88
89         int angle = atanf((x - (screen->w / 2)) / (screen->w / 2)) * 180 / M_PI;
90         int attenuation = fabsf(x - (screen->w / 2)) / 40;
91
92         if (angle < 0)
93                 angle += 360;
94
95         if (attenuation > 255)
96                 attenuation = 255;
97
98         Mix_SetPosition(channel, angle, attenuation);
99         Mix_PlayChannel(channel, sound[sid], 0);
100 }
101
102 void audio_haltMusic()
103 {
104         if (Mix_PlayingMusic())
105         {
106                 Mix_ResumeMusic();
107                 Mix_HaltMusic();
108         }
109
110         if (music != NULL)
111         {
112                 Mix_FreeMusic(music);
113                 music = NULL;
114         }
115 }
116
117 void audio_pauseMusic()
118 {
119         if (Mix_PlayingMusic() && !Mix_PausedMusic())
120                 Mix_PauseMusic();
121 }
122
123 void audio_resumeMusic()
124 {
125         Mix_ResumeMusic();
126 }
127
128 void audio_setMusicVolume(int volume)
129 {
130         if (engine.useMusic && engine.useAudio)
131                 Mix_VolumeMusic(volume);
132 }
133
134 void audio_playMusic(const char *filename, int loops)
135 {
136         if (engine.useMusic && engine.useAudio)
137         {
138                 audio_haltMusic();
139                 music = Mix_LoadMUS(filename);
140                 audio_setMusicVolume(100);
141                 Mix_PlayMusic(music, loops);
142         }
143 }
144
145 void audio_playRandomTrack()
146 {
147         if ((!engine.useMusic) || (!engine.useAudio))
148                 return;
149
150         int tracks = 4;
151         char track[][64] = {
152                 "music/railjet_short.ogg", "music/space_dimensions.ogg",
153                 "music/frozen_jam.ogg", "music/sound_and_silence.ogg"
154         };
155
156         switch(game.area)
157         {
158                 case MISN_START:
159                         audio_playMusic("music/railjet_short.ogg", -1);
160                         break;
161                 case MISN_MOEBO:
162                 case MISN_ELAMALE:
163                 case MISN_ELLESH:
164                 case MISN_EARTH:
165                         audio_playMusic("music/orbital_colossus.ogg", -1);
166                         break;
167                 case MISN_VENUS:
168                         audio_playMusic("music/RE.ogg", -1);
169                         break;
170                 default:
171                         audio_playMusic(track[rand() % tracks], -1);
172         }
173 }
174
175 void audio_free()
176 {
177         for (int i = 0 ; i < SFX_MAX ; i++)
178         {
179                 if (sound[i] != NULL)
180                 {
181                         Mix_FreeChunk(sound[i]);
182                         sound[i] = NULL;
183                 }
184         }
185
186         if (music != NULL)
187         {
188                 Mix_FreeMusic(music);
189                 music = NULL;
190         }
191 }