DEADSOFTWARE

62c8b7e6e3901c0ce7aad43e66c196a7918dd1cb
[flatwaifu.git] / music.c
1 #include "glob.h"
2 #include "sound.h"
3 #include <SDL.h>
4 #include <SDL_mixer.h>
6 short mus_vol = 50;
8 Mix_Music * muslo;
10 char music_random = ON;
11 int music_time = 3;
12 int music_fade = 5;
14 Uint32 muscount;
16 int musdisabled = 1;
18 void S_initmusic(void)
19 {
20 if (!SDL_WasInit(SDL_INIT_AUDIO)) {
21 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
22 fprintf(stderr, "\nUnable to initialize audio: %s\n", SDL_GetError());
23 musdisabled=1;
24 return;
25 }
27 if (Mix_OpenAudio(22050, AUDIO_S16, 1, 1000) < 0) {
28 fprintf(stderr, "Error initializing SDL_mixer: %s\n", Mix_GetError());
29 musdisabled=1;
30 return;
31 }
32 }
34 muslo=NULL;
36 muscount=0;
38 musdisabled = (mus_vol==0);
40 S_volumemusic(mus_vol);
41 }
43 void S_donemusic(void)
44 {
45 F_freemus();
46 Mix_CloseAudio();
47 SDL_QuitSubSystem(SDL_INIT_AUDIO);
48 }
50 void S_startmusic(int time)
51 {
52 if (musdisabled) return;
53 Mix_PlayMusic(muslo, -1);
54 Mix_VolumeMusic(mus_vol);
55 muscount=time*60*1000/DELAY;
56 }
58 void S_stopmusic(void)
59 {
60 if (musdisabled) return;
61 Mix_HaltMusic();
62 muscount = 0;
63 }
65 void S_volumemusic(int v)
66 {
67 if (musdisabled) return;
68 mus_vol = v;
69 if (mus_vol>128) mus_vol=128;
70 if (mus_vol<0) mus_vol=0;
71 if (mus_vol==0 && Mix_PlayingMusic()) {
72 S_stopmusic();
73 }
74 else if (mus_vol>0 && !Mix_PlayingMusic()) {
75 S_startmusic(music_time);
76 }
77 else {
78 Mix_VolumeMusic(v);
79 }
80 }
82 struct {
83 Uint8 ascii;
84 Uint8 asciilc;
85 char *ch;
86 } atrans[] = {
87 {0x80, 0xA0, "A"},//А
88 {0x81, 0xA1, "B"},//Б
89 {0x82, 0xA2, "V"},//В
90 {0x83, 0xA3, "G"},//Г
91 {0x84, 0xA4, "D"},//Д
92 {0x85, 0xA5, "E"},//Е
93 {0x86, 0xA6, "ZH"},//Ж
94 {0x87, 0xA7, "Z"},//З
95 {0x88, 0xA8, "I"},//И
96 {0x89, 0xA9, "J"},//Й
97 {0x8A, 0xAA, "K"},//К
98 {0x8B, 0xAB, "L"},//Л
99 {0x8C, 0xAC, "M"},//М
100 {0x8D, 0xAD, "N"},//Н
101 {0x8E, 0xAE, "O"},//О
102 {0x8F, 0xAF, "P"},//П
103 {0x90, 0xE0, "R"},//Р
104 {0x91, 0xE1, "S"},//С
105 {0x92, 0xE2, "T"},//Т
106 {0x93, 0xE3, "U"},//У
107 {0x94, 0xE4, "F"},//Ф
108 {0x95, 0xE5, "H"},//Х
109 {0x96, 0xE6, "C"},//Ц
110 {0x97, 0xE7, "CH"},//Ч
111 {0x98, 0xE8, "SH"},//Ш
112 {0x99, 0xE9, "SCH"},//Щ
113 {0x9A, 0xEA, "X"},//Ъ
114 {0x9B, 0xEB, "Y"},//Ы
115 {0x9C, 0xEC, "J"},//Ь
116 {0x9D, 0xED, "E"},//Э
117 {0x9E, 0xEE, "JU"},//Ю
118 {0x9F, 0xEF, "JA"},//Я
119 {0}
120 };
122 char *get_trans_char (Uint8 c)
124 int i = 0;
125 while (atrans[i].ascii) {
127 if (atrans[i].ascii == c || atrans[i].asciilc == c) {
128 return atrans[i].ch;
130 i++;
132 return NULL;
135 void trans_ascii_str (char *dest, char *src)
137 char *p = dest;
138 int i;
139 for (i=0; i<strlen(src); i++) {
140 char *ch = get_trans_char(src[i]);
141 if (ch) {
142 strcpy(p,ch);
143 p+=strlen(ch);
145 else {
146 strncpy(p,&src[i],1);
147 p++;
150 *p='\0';
153 void F_loadmus(char n[8]) {
154 if (musdisabled) return;
155 char f[50];
156 #ifndef WIN32
157 strcpy(f,"music/");
158 #else
159 strcpy(f,"music\\");
160 #endif
161 strncpy(&f[6], n, 8);
162 f[6+8]='\0';
163 muslo = Mix_LoadMUS(f);
164 if (muslo == NULL) {
165 char name[50];
166 trans_ascii_str(name, f);
167 muslo = Mix_LoadMUS(name);
168 if (!muslo) logo("Music not found '%s'\n", name);
173 void F_freemus(void) {
174 if (musdisabled) return;
175 if (muslo) {
176 Mix_HaltMusic();
177 Mix_FreeMusic(muslo);
179 muslo = NULL;
182 extern byte g_music[8];
184 static int volsetcount = 0;
186 void S_updatemusic()
188 if (musdisabled) return;
190 //періодично встановлюю гучність музикі, так як вона сама підвищується до максимуму через певний час
191 volsetcount++;
192 if (volsetcount%(5*1000/DELAY)==0) {
193 S_volumemusic(mus_vol);
196 if (muscount>0) {
197 if (muscount < music_fade*1100/DELAY) {
198 Mix_FadeOutMusic(music_fade*1000);
200 muscount--;
201 if (muscount==0) {
202 if (music_random) F_randmus(g_music);
203 else F_nextmus(g_music);
204 F_freemus();
205 F_loadmus(g_music);
206 S_startmusic(music_time);