DEADSOFTWARE

4dc87c295fbfc66a3569e70939577c83772c4dac
[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 "sound.h"
25 #include "files.h"
26 #include <SDL.h>
27 #include <SDL_mixer.h>
29 short mus_vol = 50;
31 Mix_Music * muslo;
33 char music_random = ON;
34 int music_time = 3;
35 int music_fade = 5;
37 Uint32 muscount;
39 int musdisabled = 1;
41 void S_initmusic(void)
42 {
43 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
44 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
45 fprintf(stderr, "\nUnable to initialize audio: %s\n", SDL_GetError());
46 musdisabled=1;
47 return;
48 }
50 if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1000) < 0) {
51 fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
52 musdisabled=1;
53 return;
54 }
55 }
57 muslo=NULL;
59 muscount=0;
61 musdisabled = (mus_vol==0);
63 S_volumemusic(mus_vol);
64 }
66 void S_donemusic(void)
67 {
68 if (SDL_WasInit(SDL_INIT_AUDIO)) {
69 F_freemus();
70 Mix_CloseAudio();
71 SDL_QuitSubSystem(SDL_INIT_AUDIO);
72 }
73 }
75 void S_startmusic(int time)
76 {
77 if (musdisabled) return;
78 Mix_PlayMusic(muslo, -1);
79 Mix_VolumeMusic(mus_vol);
80 muscount=time*60*1000/DELAY;
81 }
83 void S_stopmusic(void)
84 {
85 if (musdisabled) return;
86 Mix_HaltMusic();
87 muscount = 0;
88 }
90 void S_volumemusic(int v)
91 {
92 if (musdisabled) return;
93 mus_vol = v;
94 if (mus_vol>128) mus_vol=128;
95 if (mus_vol<0) mus_vol=0;
96 if (mus_vol==0 && Mix_PlayingMusic()) {
97 S_stopmusic();
98 }
99 else if (mus_vol>0 && !Mix_PlayingMusic()) {
100 S_startmusic(music_time);
102 else {
103 Mix_VolumeMusic(v);
107 struct {
108 Uint8 ascii;
109 Uint8 asciilc;
110 char *ch;
111 } atrans[] = {
112 {0x80, 0xA0, "A"},//А
113 {0x81, 0xA1, "B"},//Б
114 {0x82, 0xA2, "V"},//В
115 {0x83, 0xA3, "G"},//Г
116 {0x84, 0xA4, "D"},//Д
117 {0x85, 0xA5, "E"},//Е
118 {0x86, 0xA6, "ZH"},//Ж
119 {0x87, 0xA7, "Z"},//З
120 {0x88, 0xA8, "I"},//И
121 {0x89, 0xA9, "J"},//Й
122 {0x8A, 0xAA, "K"},//К
123 {0x8B, 0xAB, "L"},//Л
124 {0x8C, 0xAC, "M"},//М
125 {0x8D, 0xAD, "N"},//Н
126 {0x8E, 0xAE, "O"},//О
127 {0x8F, 0xAF, "P"},//П
128 {0x90, 0xE0, "R"},//Р
129 {0x91, 0xE1, "S"},//С
130 {0x92, 0xE2, "T"},//Т
131 {0x93, 0xE3, "U"},//У
132 {0x94, 0xE4, "F"},//Ф
133 {0x95, 0xE5, "H"},//Х
134 {0x96, 0xE6, "C"},//Ц
135 {0x97, 0xE7, "CH"},//Ч
136 {0x98, 0xE8, "SH"},//Ш
137 {0x99, 0xE9, "SCH"},//Щ
138 {0x9A, 0xEA, "X"},//Ъ
139 {0x9B, 0xEB, "Y"},//Ы
140 {0x9C, 0xEC, "J"},//Ь
141 {0x9D, 0xED, "E"},//Э
142 {0x9E, 0xEE, "JU"},//Ю
143 {0x9F, 0xEF, "JA"},//Я
144 {0}
145 };
147 char *get_trans_char (Uint8 c)
149 int i = 0;
150 while (atrans[i].ascii) {
152 if (atrans[i].ascii == c || atrans[i].asciilc == c) {
153 return atrans[i].ch;
155 i++;
157 return NULL;
160 void trans_ascii_str (char *dest, char *src)
162 char *p = dest;
163 int i;
164 for (i=0; i<strlen(src); i++) {
165 char *ch = get_trans_char(src[i]);
166 if (ch) {
167 strcpy(p,ch);
168 p+=strlen(ch);
170 else {
171 strncpy(p,&src[i],1);
172 p++;
175 *p='\0';
178 void F_loadmus(char n[8]) {
179 if (musdisabled) return;
180 char f[50];
181 char name[50];
182 #ifndef WIN32
183 strcpy(f, "/usr/share/doom2d-rembo/music/");
184 #else
185 strcpy(f, "music\\");
186 #endif
187 int l = strlen(f);
188 strncpy(&f[l], n, 8);
189 f[l+8]='\0';
190 trans_ascii_str(name, f);
191 muslo = Mix_LoadMUS(name);
192 if (muslo == NULL)
194 #ifndef WIN32
195 strcpy(f, "music/");
196 int l = strlen(f);
197 strncpy(&f[l], n, 8);
198 f[l+8]='\0';
199 trans_ascii_str(name, f);
200 muslo = Mix_LoadMUS(name);
201 #endif
203 if (!muslo) logo("Music not found '%s'\n", name);
207 void F_freemus(void) {
208 if (musdisabled) return;
209 if (muslo) {
210 Mix_HaltMusic();
211 Mix_FreeMusic(muslo);
213 muslo = NULL;
216 extern char g_music[8];
218 static int volsetcount = 0;
220 void S_updatemusic()
222 if (musdisabled) return;
224 //періодично встановлюю гучність музикі, так як вона сама підвищується до максимуму через певний час
225 volsetcount++;
226 if (volsetcount%(5*1000/DELAY)==0) {
227 S_volumemusic(mus_vol);
230 if (muscount>0) {
231 if (muscount < music_fade*1100/DELAY) {
232 Mix_FadeOutMusic(music_fade*1000);
234 muscount--;
235 if (muscount==0) {
236 if (music_random) F_randmus(g_music);
237 else F_nextmus(g_music);
238 F_freemus();
239 F_loadmus(g_music);
240 S_startmusic(music_time);