summaryrefslogtreecommitdiff
path: root/audio.c
blob: 40d5ac2db9b74974fe4a601c169e6af6165695cc (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
#include "audio.h"

struct sound *
find_sound(struct prge_context ctx, struct string name)
{
    for (i32 i = 0; i < cache.count; i++) {
        struct sound *sound = sounds + i;
        if (strings_equal(sound->name, name))
            return sound;
    }
    return 0;
}

struct sound *
load_sound(struct sound *sounds, struct sound new_sound)
{
    assert(sounds);

    i32 i;
    struct sound *sound;
    for (i = 0; i < MAX_SOUNDS_LOADED; ++i) {
        sound = sounds+i;
        if (!sound->data)
            break;
    }

    if (i == MAX_SOUNDS_LOADED) {
        printf("warning: max sounds loaded\n");
        return -1;
    }

    *sound = new_sound;

    return i;
}

i32 enqueue_sound(struct sound_queue *queue, sound_queue_node_t *nodes, struct sound *sound)
{
    if (queue->count == MAX_SOUNDS_PLAYING)
        return 0;

    sound_queue_node_t *node;
    for (i32 i = 0; i < MAX_SOUNDS_PLAYING; ++i) {
        node = nodes+i;
        if (!node->sound)
            break;
    }

    node->sound = sound;

    dllpushfront(queue->first, queue->last, node);

    queue->count++;

    return 1;
}

struct sound *dequeue_sound(struct sound_queue *queue)
{
    if (queue->count == 0)
        return 0;

    struct sound *sound = queue->first->sound;
    queue->first->sound = 0;
    dllremove(queue->first, queue->last, queue->first);
    queue->count--;

    return sound;
}

#endif