-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio.c
97 lines (76 loc) · 1.92 KB
/
audio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include "audio.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#define RESERVE_CHANNELS 16
static int channels;
static int curr_channel;
static int audio_enabled;
static int curr_music;
Mix_Chunk *sounds[5];
Mix_Music *music[7];
void audio_init()
{
audio_enabled = 0;
curr_channel = 0;
curr_music = -1;
if (SDL_Init(SDL_INIT_AUDIO) < 0)
{
return;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{
return;
}
channels = Mix_ReserveChannels(RESERVE_CHANNELS);
sounds[SOUND_BTN_UP] = Mix_LoadWAV("sounds/btn-up.wav");
sounds[SOUND_BTN_DOWN] = Mix_LoadWAV("sounds/btn-down.wav");
sounds[SOUND_MSG] = Mix_LoadWAV("sounds/alert.wav");
sounds[SOUND_ENCOUNTER] = Mix_LoadWAV("sounds/encounter.wav");
sounds[SOUND_SELECT] = Mix_LoadWAV("sounds/select.wav");
music[MUSIC_LOSS] = Mix_LoadMUS("sounds/loss.wav");
music[MUSIC_WIN] = Mix_LoadMUS("sounds/win.wav");
music[MUSIC_MENU] = Mix_LoadMUS("sounds/menu.wav");
music[MUSIC_MENU_BROKEN] = Mix_LoadMUS("sounds/menu-broken.wav");
music[MUSIC_BG] = Mix_LoadMUS("sounds/bg.wav");
music[MUSIC_BG_BROKEN] = Mix_LoadMUS("sounds/bg-broken.wav");
music[MUSIC_BG2] = Mix_LoadMUS("sounds/bg2.wav");
//Mix_LoadWAV("sounds/alert-long.wav");
//Mix_Music *music = Mix_LoadMUS("test.mp3");
//Mix_PlayMusic(music, 1);
audio_enabled = 1;
}
void audio_sound_play(int id)
{
if (!audio_enabled)
{
return;
}
if (curr_channel >= channels)
{
curr_channel = 0;
}
Mix_PlayChannel(curr_channel, sounds[id], 0);
curr_channel++;
}
void audio_music_play(int id)
{
if (!audio_enabled)
{
return;
}
Mix_PlayMusic(music[id], -1);
curr_music = id;
}
void audio_music_stop()
{
if (!audio_enabled)
{
return;
}
Mix_HaltMusic();
curr_music = -1;
}
int audio_music_current()
{
return curr_music;
}