aboutsummaryrefslogtreecommitdiff
path: root/src/gunner/audio_sys.c
blob: 54aba0e4d974b683a9f3f74031d88c646daa13ab (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Defines

#define MAX_SOUND 8

static Sound sounds[MAX_SOUND] = {0};
static s8 sound_paths[MAX_SOUND] = {0};

static Music track = {0};
static s8 music_path = {0};

// Audio things

i32 load_audio(const char* path)
{
	i32 current_idx = BLANK_DEFAULT;
	
	if(path == NULL)
		return current_idx;
	
	s8 path_str = cstr_to_s8(path);
	
	for(int i = 0; i < MAX_SOUND; ++i){
		if(equal_s8(path_str, sound_paths[current_idx])){
			free_s8(&path_str);
			return i;
		}
		
		if(sounds[i].stream.buffer == NULL){
			current_idx = i;
			sounds[i] = LoadSound(path);
			sound_paths[i] = path_str;
			break;
		}
	}
	
	return current_idx;
}

void play_audio(i32 idx)
{
	assert(idx >= 0 && idx < MAX_SOUND);
	PlaySound(sounds[idx]);
}

void pause_audio(i32 idx)
{
	assert(idx >= 0 && idx < MAX_SOUND);
	PauseSound(sounds[idx]);
}

void resume_audio(i32 idx)
{
	assert(idx >= 0 && idx < MAX_SOUND);
	ResumeSound(sounds[idx]);
}

// Music things //

void load_music(const char* path)
{
	if(path == NULL)
		return;
	
	s8 path_str = cstr_to_s8(path);
	if(equal_s8(path_str, music_path)){
		free_s8(&path_str);
		return;
	}
	
	track = LoadMusicStream(path);
}

void play_music(void)
{
	PlayMusicStream(track);
}

void set_music_loop(b32 loop)
{
	track.looping = loop;
}

void set_music_loop_point(f32 point)
{
	track.loopPoint = point;
}

void pause_music(void)
{
	PauseMusicStream(track);
}

void resume_music(void)
{
	ResumeMusicStream(track);
}

// Main funcs

void set_main_vol(f32 vol)
{
	assert(vol >= 0.f);
	SetMasterVolume(vol);
}

// Update stuff

void update_audio(void)
{
	UpdateMusicStream(track);
}

void unload_audio(i32 idx)
{
	StopSound(sounds[idx]);
	UnloadSound(sounds[idx]);
	free_s8(&sound_paths[idx]);
}

void unload_active_audio(void)
{
	for(int i = 0; i < MAX_SOUND; ++i){
		if(sounds[i].stream.buffer != NULL){
			unload_audio(i);
		}
	}
}

void unload_current_music(void)
{
	StopMusicStream(track);
	UnloadMusicStream(track);
	free_s8(&music_path);
}