-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
88 lines (80 loc) · 1.91 KB
/
sound.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
#include "sound.h"
struct Sound* loadplay_sound(char* filename, int loop)
{
struct Sound* sound = malloc(sizeof(struct Sound));
if(!(sound->data = al_load_sample(filename)))
{
free(sound);
return NULL;
}
if(!al_play_sample(sound->data, 1.0, 0.0, 1.0,
loop ? ALLEGRO_PLAYMODE_LOOP : ALLEGRO_PLAYMODE_ONCE, &(sound->id)))
{
al_destroy_sample(sound->data);
free(sound);
return NULL;
}
return sound;
}
void freestop_sound(struct Sound* sound)
{
al_stop_sample(&(sound->id));
al_destroy_sample(sound->data);
free(sound);
}
int sound_id_in_range(int id)
{
if(id < 0 && id >= SOUNDS)
{
sprintf(error, "sound id %d not in range [0,%d]", id, SOUNDS - 1);
return 0;
}
else
return 1;
}
struct Sound* sound_loaded(struct Sound** sounds, int id)
{
struct Sound* sound = sounds[id];
if(!sound)
sprintf(error, "sound %d is not loaded", id);
return sound;
}
void cmd_play_sound(struct Game* game, char* arg)
{
char* filename = cut_command(arg);
int id = eval(game->vars, arg);
path_compatibilize(filename);
if(sound_id_in_range(id))
{
if(game->display->sounds[id])
freestop_sound(game->display->sounds[id]);
if(!(game->display->sounds[id] = loadplay_sound(filename, 0)))
sprintf(error, "cannot load sound %d \"%s\"", id, filename);
}
}
void cmd_play_sound_loop(struct Game* game, char* arg)
{
char* filename = cut_command(arg);
int id = eval(game->vars, arg);
path_compatibilize(filename);
if(sound_id_in_range(id))
{
if(game->display->sounds[id])
freestop_sound(game->display->sounds[id]);
if(!(game->display->sounds[id] = loadplay_sound(filename, 1)))
sprintf(error, "cannot load sound %d \"%s\"", id, filename);
}
}
void cmd_stop_sound(struct Game* game, char* arg)
{
int id = eval(game->vars, arg);
struct Sound* sound;
if(sound_id_in_range(id))
{
if((sound = sound_loaded(game->display->sounds, id)))
{
freestop_sound(sound);
game->display->sounds[id] = NULL;
}
}
}