DEADSOFTWARE

8f9c13e2fadb4ba3f5a4866d96795c0c31e570de
[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 <SDL.h>
26 #include <SDL_mixer.h>
28 short mus_vol = 50;
30 Mix_Music * muslo;
32 char music_random = ON;
33 int music_time = 3;
34 int music_fade = 5;
36 Uint32 muscount;
38 int musdisabled = 1;
40 void S_initmusic(void)
41 {
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 {
67 if (SDL_WasInit(SDL_INIT_AUDIO)) {
68 F_freemus();
69 Mix_CloseAudio();
70 SDL_QuitSubSystem(SDL_INIT_AUDIO);
71 }
72 }
74 void S_startmusic(int time)
75 {
76 if (musdisabled) return;
77 Mix_PlayMusic(muslo, -1);
78 Mix_VolumeMusic(mus_vol);
79 muscount=time*60*1000/DELAY;
80 }
82 void S_stopmusic(void)
83 {
84 if (musdisabled) return;
85 Mix_HaltMusic();
86 muscount = 0;
87 }
89 void S_volumemusic(int v)
90 {
91 if (musdisabled) return;
92 mus_vol = v;
93 if (mus_vol>128) mus_vol=128;
94 if (mus_vol<0) mus_vol=0;
95 if (mus_vol==0 && Mix_PlayingMusic()) {
96 S_stopmusic();
97 }
98 else if (mus_vol>0 && !Mix_PlayingMusic()) {
99 S_startmusic(music_time);
101 else {
102 Mix_VolumeMusic(v);
106 struct {
107 Uint8 ascii;
108 Uint8 asciilc;
109 char *ch;
110 } atrans[] = {
111 {0x80, 0xA0, "A"},//А
112 {0x81, 0xA1, "B"},//Б
113 {0x82, 0xA2, "V"},//В
114 {0x83, 0xA3, "G"},//Г
115 {0x84, 0xA4, "D"},//Д
116 {0x85, 0xA5, "E"},//Е
117 {0x86, 0xA6, "ZH"},//Ж
118 {0x87, 0xA7, "Z"},//З
119 {0x88, 0xA8, "I"},//И
120 {0x89, 0xA9, "J"},//Й
121 {0x8A, 0xAA, "K"},//К
122 {0x8B, 0xAB, "L"},//Л
123 {0x8C, 0xAC, "M"},//М
124 {0x8D, 0xAD, "N"},//Н
125 {0x8E, 0xAE, "O"},//О
126 {0x8F, 0xAF, "P"},//П
127 {0x90, 0xE0, "R"},//Р
128 {0x91, 0xE1, "S"},//С
129 {0x92, 0xE2, "T"},//Т
130 {0x93, 0xE3, "U"},//У
131 {0x94, 0xE4, "F"},//Ф
132 {0x95, 0xE5, "H"},//Х
133 {0x96, 0xE6, "C"},//Ц
134 {0x97, 0xE7, "CH"},//Ч
135 {0x98, 0xE8, "SH"},//Ш
136 {0x99, 0xE9, "SCH"},//Щ
137 {0x9A, 0xEA, "X"},//Ъ
138 {0x9B, 0xEB, "Y"},//Ы
139 {0x9C, 0xEC, "J"},//Ь
140 {0x9D, 0xED, "E"},//Э
141 {0x9E, 0xEE, "JU"},//Ю
142 {0x9F, 0xEF, "JA"},//Я
143 {0}
144 };
146 char *get_trans_char (Uint8 c)
148 int i = 0;
149 while (atrans[i].ascii) {
151 if (atrans[i].ascii == c || atrans[i].asciilc == c) {
152 return atrans[i].ch;
154 i++;
156 return NULL;
159 void trans_ascii_str (char *dest, char *src)
161 char *p = dest;
162 int i;
163 for (i=0; i<strlen(src); i++) {
164 char *ch = get_trans_char(src[i]);
165 if (ch) {
166 strcpy(p,ch);
167 p+=strlen(ch);
169 else {
170 strncpy(p,&src[i],1);
171 p++;
174 *p='\0';
177 void F_loadmus(char n[8]) {
178 if (musdisabled) return;
179 char f[50];
180 char name[50];
181 #ifndef WIN32
182 strcpy(f, "/usr/share/doom2d-rembo/music/");
183 #else
184 strcpy(f, "music\\");
185 #endif
186 int l = strlen(f);
187 strncpy(&f[l], n, 8);
188 f[l+8]='\0';
189 trans_ascii_str(name, f);
190 muslo = Mix_LoadMUS(name);
191 if (muslo == NULL)
193 #ifndef WIN32
194 strcpy(f, "music/");
195 int l = strlen(f);
196 strncpy(&f[l], n, 8);
197 f[l+8]='\0';
198 trans_ascii_str(name, f);
199 muslo = Mix_LoadMUS(name);
200 #endif
202 if (!muslo) logo("Music not found '%s'\n", name);
206 void F_freemus(void) {
207 if (musdisabled) return;
208 if (muslo) {
209 Mix_HaltMusic();
210 Mix_FreeMusic(muslo);
212 muslo = NULL;
215 extern byte g_music[8];
217 static int volsetcount = 0;
219 void S_updatemusic()
221 if (musdisabled) return;
223 //періодично встановлюю гучність музикі, так як вона сама підвищується до максимуму через певний час
224 volsetcount++;
225 if (volsetcount%(5*1000/DELAY)==0) {
226 S_volumemusic(mus_vol);
229 if (muscount>0) {
230 if (muscount < music_fade*1100/DELAY) {
231 Mix_FadeOutMusic(music_fade*1000);
233 muscount--;
234 if (muscount==0) {
235 if (music_random) F_randmus(g_music);
236 else F_nextmus(g_music);
237 F_freemus();
238 F_loadmus(g_music);
239 S_startmusic(music_time);