DEADSOFTWARE

opengl: fix building on non osx
[flatwaifu.git] / src / music.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 */
23 #include "glob.h"
24 #include "files.h"
25 #include "music.h"
26 #include "error.h"
27 #include "game.h"
28 #include <SDL.h>
29 #include <SDL_mixer.h>
31 short mus_vol = 50;
32 char music_random = ON;
33 int music_time = 3;
34 int music_fade = 5;
36 static Uint32 muscount;
37 static Mix_Music * muslo;
38 static int musdisabled = 1;
39 static int volsetcount = 0;
41 void S_initmusic (void) {
42 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
43 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
44 fprintf(stderr, "\nUnable to initialize audio: %s\n", SDL_GetError());
45 musdisabled=1;
46 return;
47 }
49 if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1000) < 0) {
50 fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
51 musdisabled=1;
52 return;
53 }
54 }
56 muslo=NULL;
58 muscount=0;
60 musdisabled = (mus_vol==0);
62 S_volumemusic(mus_vol);
63 }
65 void S_donemusic (void) {
66 if (SDL_WasInit(SDL_INIT_AUDIO)) {
67 F_freemus();
68 Mix_CloseAudio();
69 SDL_QuitSubSystem(SDL_INIT_AUDIO);
70 }
71 }
73 void S_startmusic (int time) {
74 if (musdisabled) return;
75 Mix_PlayMusic(muslo, -1);
76 Mix_VolumeMusic(mus_vol);
77 muscount=time*60*1000/DELAY;
78 }
80 void S_stopmusic (void) {
81 if (musdisabled) return;
82 Mix_HaltMusic();
83 muscount = 0;
84 }
86 void S_volumemusic (int v) {
87 if (musdisabled) return;
88 mus_vol = v;
89 if (mus_vol>128) mus_vol=128;
90 if (mus_vol<0) mus_vol=0;
91 if (mus_vol==0 && Mix_PlayingMusic()) {
92 S_stopmusic();
93 }
94 else if (mus_vol>0 && !Mix_PlayingMusic()) {
95 S_startmusic(music_time);
96 }
97 else {
98 Mix_VolumeMusic(v);
99 }
102 static struct {
103 Uint8 ascii;
104 Uint8 asciilc;
105 char *ch;
106 } atrans[] = {
107 {0x80, 0xA0, "A"},//А
108 {0x81, 0xA1, "B"},//Б
109 {0x82, 0xA2, "V"},//В
110 {0x83, 0xA3, "G"},//Г
111 {0x84, 0xA4, "D"},//Д
112 {0x85, 0xA5, "E"},//Е
113 {0x86, 0xA6, "ZH"},//Ж
114 {0x87, 0xA7, "Z"},//З
115 {0x88, 0xA8, "I"},//И
116 {0x89, 0xA9, "J"},//Й
117 {0x8A, 0xAA, "K"},//К
118 {0x8B, 0xAB, "L"},//Л
119 {0x8C, 0xAC, "M"},//М
120 {0x8D, 0xAD, "N"},//Н
121 {0x8E, 0xAE, "O"},//О
122 {0x8F, 0xAF, "P"},//П
123 {0x90, 0xE0, "R"},//Р
124 {0x91, 0xE1, "S"},//С
125 {0x92, 0xE2, "T"},//Т
126 {0x93, 0xE3, "U"},//У
127 {0x94, 0xE4, "F"},//Ф
128 {0x95, 0xE5, "H"},//Х
129 {0x96, 0xE6, "C"},//Ц
130 {0x97, 0xE7, "CH"},//Ч
131 {0x98, 0xE8, "SH"},//Ш
132 {0x99, 0xE9, "SCH"},//Щ
133 {0x9A, 0xEA, "X"},//Ъ
134 {0x9B, 0xEB, "Y"},//Ы
135 {0x9C, 0xEC, "J"},//Ь
136 {0x9D, 0xED, "E"},//Э
137 {0x9E, 0xEE, "JU"},//Ю
138 {0x9F, 0xEF, "JA"},//Я
139 {0}
140 };
142 static char *get_trans_char (Uint8 c)
144 int i = 0;
145 while (atrans[i].ascii) {
147 if (atrans[i].ascii == c || atrans[i].asciilc == c) {
148 return atrans[i].ch;
150 i++;
152 return NULL;
155 static void trans_ascii_str (char *dest, char *src)
157 char *p = dest;
158 int i;
159 for (i=0; i<strlen(src); i++) {
160 char *ch = get_trans_char(src[i]);
161 if (ch) {
162 strcpy(p,ch);
163 p+=strlen(ch);
165 else {
166 strncpy(p,&src[i],1);
167 p++;
170 *p='\0';
173 void F_loadmus (char n[8]) {
174 if (musdisabled) return;
175 char f[50];
176 char name[50];
177 #ifndef WIN32
178 strcpy(f, "/usr/share/doom2d-rembo/music/");
179 #else
180 strcpy(f, "music\\");
181 #endif
182 int l = strlen(f);
183 strncpy(&f[l], n, 8);
184 f[l+8]='\0';
185 trans_ascii_str(name, f);
186 muslo = Mix_LoadMUS(name);
187 if (muslo == NULL)
189 #ifndef WIN32
190 strcpy(f, "music/");
191 int l = strlen(f);
192 strncpy(&f[l], n, 8);
193 f[l+8]='\0';
194 trans_ascii_str(name, f);
195 muslo = Mix_LoadMUS(name);
196 #endif
198 if (!muslo) logo("Music not found '%s'\n", name);
202 void F_freemus (void) {
203 if (musdisabled) return;
204 if (muslo) {
205 Mix_HaltMusic();
206 Mix_FreeMusic(muslo);
208 muslo = NULL;
211 void S_updatemusic (void) {
212 if (musdisabled) return;
214 //періодично встановлюю гучність музикі, так як вона сама підвищується до максимуму через певний час
215 volsetcount++;
216 if (volsetcount%(5*1000/DELAY)==0) {
217 S_volumemusic(mus_vol);
220 if (muscount>0) {
221 if (muscount < music_fade*1100/DELAY) {
222 Mix_FadeOutMusic(music_fade*1000);
224 muscount--;
225 if (muscount==0) {
226 if (music_random) F_randmus(g_music);
227 else F_nextmus(g_music);
228 F_freemus();
229 F_loadmus(g_music);
230 S_startmusic(music_time);