-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_list.c
92 lines (78 loc) · 1.54 KB
/
sound_list.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#ifdef __APPLE__
#include <OpenAL/al.h>
#include <OpenAL/alc.h>
#else
#include <AL/al.h>
#include <AL/alc.h>
#endif
#include "vector2.h"
#include "sound_list.h"
typedef struct s_node {
ALuint src;
struct s_node * next;
} s_node;
typedef struct s_type {
s_node * head;
ALuint buf;
} s_type;
s_list s_init(ALuint buffer){
s_list sl;
sl = (s_type*)malloc(sizeof(s_type));
if(!sl){return NULL;}
sl->head = NULL;
sl->buf = buffer;
return sl;
}
void s_add_snd(s_list sl, vector2 p){
s_node * new;
new = (s_node*)malloc(sizeof(s_node));
if(!new){return;}
alGenSources(1, &new->src);
alSourcei(new->src, AL_BUFFER, sl->buf);
alSource3f(new->src, AL_POSITION, p.x, p.y, 0);
alSourcePlay(new->src);
new->next = NULL;
if(sl->head == NULL){
sl->head = new;
return;
}
new->next = sl->head;
sl->head = new;
return;
}
void s_update(s_list sl){
s_node * cycle = sl->head;
s_node * prev = NULL;
while(cycle != NULL){
int state;
alGetSourcei(cycle->src, AL_SOURCE_STATE, &state);
if(state == AL_STOPPED){
if(prev == NULL){
s_node * tmp = cycle->next;
sl->head = tmp;
alDeleteSources(1, &cycle->src);
free(cycle);
cycle = tmp;
}
else{
prev->next = cycle->next;
alDeleteSources(1, &cycle->src);
free(cycle);
cycle = prev->next;
}
}
else{cycle = cycle->next;}
}
}
void s_free(s_list sl){
s_node * cycle = sl->head;
while(cycle != NULL){
s_node * tmp = cycle->next;
alDeleteSources(1, &cycle->src);
free(cycle);
cycle = tmp;
}
}