DEADSOFTWARE

Поменял регистр на нижний конфиг файла
[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 F_freemus();
68 Mix_CloseAudio();
69 SDL_QuitSubSystem(SDL_INIT_AUDIO);
70 }
72 void S_startmusic(int time)
73 {
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 {
82 if (musdisabled) return;
83 Mix_HaltMusic();
84 muscount = 0;
85 }
87 void S_volumemusic(int v)
88 {
89 if (musdisabled) return;
90 mus_vol = v;
91 if (mus_vol>128) mus_vol=128;
92 if (mus_vol<0) mus_vol=0;
93 if (mus_vol==0 && Mix_PlayingMusic()) {
94 S_stopmusic();
95 }
96 else if (mus_vol>0 && !Mix_PlayingMusic()) {
97 S_startmusic(music_time);
98 }
99 else {
100 Mix_VolumeMusic(v);
104 struct {
105 Uint8 ascii;
106 Uint8 asciilc;
107 char *ch;
108 } atrans[] = {
109 {0x80, 0xA0, "A"},//А
110 {0x81, 0xA1, "B"},//Б
111 {0x82, 0xA2, "V"},//В
112 {0x83, 0xA3, "G"},//Г
113 {0x84, 0xA4, "D"},//Д
114 {0x85, 0xA5, "E"},//Е
115 {0x86, 0xA6, "ZH"},//Ж
116 {0x87, 0xA7, "Z"},//З
117 {0x88, 0xA8, "I"},//И
118 {0x89, 0xA9, "J"},//Й
119 {0x8A, 0xAA, "K"},//К
120 {0x8B, 0xAB, "L"},//Л
121 {0x8C, 0xAC, "M"},//М
122 {0x8D, 0xAD, "N"},//Н
123 {0x8E, 0xAE, "O"},//О
124 {0x8F, 0xAF, "P"},//П
125 {0x90, 0xE0, "R"},//Р
126 {0x91, 0xE1, "S"},//С
127 {0x92, 0xE2, "T"},//Т
128 {0x93, 0xE3, "U"},//У
129 {0x94, 0xE4, "F"},//Ф
130 {0x95, 0xE5, "H"},//Х
131 {0x96, 0xE6, "C"},//Ц
132 {0x97, 0xE7, "CH"},//Ч
133 {0x98, 0xE8, "SH"},//Ш
134 {0x99, 0xE9, "SCH"},//Щ
135 {0x9A, 0xEA, "X"},//Ъ
136 {0x9B, 0xEB, "Y"},//Ы
137 {0x9C, 0xEC, "J"},//Ь
138 {0x9D, 0xED, "E"},//Э
139 {0x9E, 0xEE, "JU"},//Ю
140 {0x9F, 0xEF, "JA"},//Я
141 {0}
142 };
144 char *get_trans_char (Uint8 c)
146 int i = 0;
147 while (atrans[i].ascii) {
149 if (atrans[i].ascii == c || atrans[i].asciilc == c) {
150 return atrans[i].ch;
152 i++;
154 return NULL;
157 void trans_ascii_str (char *dest, char *src)
159 char *p = dest;
160 int i;
161 for (i=0; i<strlen(src); i++) {
162 char *ch = get_trans_char(src[i]);
163 if (ch) {
164 strcpy(p,ch);
165 p+=strlen(ch);
167 else {
168 strncpy(p,&src[i],1);
169 p++;
172 *p='\0';
175 void F_loadmus(char n[8]) {
176 if (musdisabled) return;
177 char f[50];
178 #ifndef WIN32
179 strcpy(f,"music/");
180 #else
181 strcpy(f,"music\\");
182 #endif
183 strncpy(&f[6], n, 8);
184 f[6+8]='\0';
185 muslo = Mix_LoadMUS(f);
186 if (muslo == NULL) {
187 char name[50];
188 trans_ascii_str(name, f);
189 muslo = Mix_LoadMUS(name);
190 if (!muslo) logo("Music not found '%s'\n", name);
195 void F_freemus(void) {
196 if (musdisabled) return;
197 if (muslo) {
198 Mix_HaltMusic();
199 Mix_FreeMusic(muslo);
201 muslo = NULL;
204 extern byte g_music[8];
206 static int volsetcount = 0;
208 void S_updatemusic()
210 if (musdisabled) return;
212 //періодично встановлюю гучність музикі, так як вона сама підвищується до максимуму через певний час
213 volsetcount++;
214 if (volsetcount%(5*1000/DELAY)==0) {
215 S_volumemusic(mus_vol);
218 if (muscount>0) {
219 if (muscount < music_fade*1100/DELAY) {
220 Mix_FadeOutMusic(music_fade*1000);
222 muscount--;
223 if (muscount==0) {
224 if (music_random) F_randmus(g_music);
225 else F_nextmus(g_music);
226 F_freemus();
227 F_loadmus(g_music);
228 S_startmusic(music_time);