summaryrefslogtreecommitdiff
path: root/src/d/snd/d_snd_se_sound_pool.cpp
blob: 9c5cfc70d2e3b62287788a73fa2011a3dcffa5ce (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
#include "d/snd/d_snd_se_sound_pool.h"

#include "common.h"
#include "d/snd/d_snd_se_sound_1.h"
#include "d/snd/d_snd_util.h"
#include "nw4r/ut/ut_list.h"

SND_DISPOSER_DEFINE(dSndSeSoundPool_c);

dSndSeSoundPool_c::dSndSeSoundPool_c() {
    // TODO offsetof
    nw4r::ut::List_Init(&mFreeType1List, 0x0C);
    nw4r::ut::List_Init(&mActiveType1List, 0x0C);
    nw4r::ut::List_Init(&mFreeType2List, 0x0C);

    for (dSndSeSound1_c *sound = mSoundsType1; sound < &mSoundsType1[POOL_SIZE]; sound++) {
        releaseSoundType1(sound, true);
    }

    for (dSndSeSound2_c *sound = mSoundsType2; sound < &mSoundsType2[POOL_SIZE]; sound++) {
        releaseSoundType2(sound);
    }
}

void dSndSeSoundPool_c::calc() {
    dSndSeSound1_c *it, *next;
    for (it = static_cast<dSndSeSound1_c *>(nw4r::ut::List_GetFirst(&mActiveType1List)); it != nullptr; it = next) {
        next = static_cast<dSndSeSound1_c *>(nw4r::ut::List_GetNext(&mActiveType1List, it));
        if (!it->isAttachedSound()) {
            it->clear();
            nw4r::ut::List_Remove(&mActiveType1List, it);
            nw4r::ut::List_Append(&mFreeType1List, it);
        }
    }
}

dSndSeSound1_c *dSndSeSoundPool_c::acquireSoundType1(dSoundSource_c *source, u32 soundId) {
    if (soundId == -1) {
        return nullptr;
    }

    for (dSndSeSound1_c *it = static_cast<dSndSeSound1_c *>(nw4r::ut::List_GetFirst(&mFreeType1List)); it != nullptr;
         it = static_cast<dSndSeSound1_c *>(nw4r::ut::List_GetNext(&mFreeType1List, it))) {
        if (!it->isAttachedSound()) {
            it->setSource(source, soundId);
            removeSoundType1(it);
            return it;
        }
    }
    return nullptr;
}

dSndSeSound2_c *dSndSeSoundPool_c::acquireSoundType2(dSoundSource_c *source, u32 soundId) {
    if (soundId == -1) {
        return nullptr;
    }

    for (dSndSeSound2_c *it = static_cast<dSndSeSound2_c *>(nw4r::ut::List_GetFirst(&mFreeType2List)); it != nullptr;
         it = static_cast<dSndSeSound2_c *>(nw4r::ut::List_GetNext(&mFreeType2List, it))) {
        if (!it->isAttachedSound()) {
            it->setSource(source, soundId);
            removeSoundType2(it);
            return it;
        }
    }
    return nullptr;
}

void dSndSeSoundPool_c::releaseSoundType1(dSndSeSound1_c *sound, bool stop) {
    if (sound == nullptr) {
        return;
    }

    if (sound->isAttachedSound()) {
        if (stop) {
            sound->stop(0);
            sound->clear();
            nw4r::ut::List_Append(&mFreeType1List, sound);
        } else {
            nw4r::ut::List_Append(&mActiveType1List, sound);
        }
    } else {
        sound->clear();
        nw4r::ut::List_Append(&mFreeType1List, sound);
    }
}

void dSndSeSoundPool_c::releaseSoundType2(dSndSeSound2_c *sound) {
    if (sound == nullptr) {
        return;
    }

    sound->stop(0);
    sound->clear();
    nw4r::ut::List_Append(&mFreeType2List, sound);
}

void dSndSeSoundPool_c::removeSoundType1(dSndSeSound1_c *sound) {
    if (sound == nullptr) {
        return;
    }

    nw4r::ut::List_Remove(&mFreeType1List, sound);
}

void dSndSeSoundPool_c::removeSoundType2(dSndSeSound2_c *sound) {
    if (sound == nullptr) {
        return;
    }

    nw4r::ut::List_Remove(&mFreeType2List, sound);
}