summaryrefslogtreecommitdiffhomepage
path: root/Stars45/RadioVox.cpp
blob: 59340a46722c3340956c669b4570166bcb3a6045 (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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*  Starshatter: The Open Source Project
    Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors
    Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
    Copyright (c) 1997-2006, Destroyer Studios LLC.

    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    View class for Radio Communications HUD Overlay
*/

#include "MemDebug.h"
#include "RadioVox.h"
#include "RadioView.h"
#include "AudioConfig.h"

#include "DataLoader.h"
#include "Game.h"
#include "Sound.h"
#include "ThreadSync.h"

// +====================================================================+
//
// RADIO VOX CONTROLLER:
//

DWORD WINAPI VoxUpdateProc(LPVOID link);

class RadioVoxController
{
public:
    enum { MAX_QUEUE = 5 };

    RadioVoxController();
    ~RadioVoxController();

    bool  Add(RadioVox* vox);
    void  Update();
    DWORD UpdateThread();

    bool           shutdown;
    HANDLE         hthread;
    List<RadioVox> queue;
    ThreadSync     sync;
};

static RadioVoxController* controller = 0;

// +--------------------------------------------------------------------+

RadioVoxController::RadioVoxController()
: hthread(0), shutdown(false)
{
    DWORD thread_id = 0;
    hthread = CreateThread(0, 4096, VoxUpdateProc,
    (LPVOID) this, 0, &thread_id);
}

// +--------------------------------------------------------------------+

RadioVoxController::~RadioVoxController()
{
    shutdown = true;

    WaitForSingleObject(hthread, 500);
    CloseHandle(hthread);
    hthread = 0;

    queue.destroy();
}

// +--------------------------------------------------------------------+

DWORD WINAPI VoxUpdateProc(LPVOID link)
{
    RadioVoxController* controller = (RadioVoxController*) link;

    if (controller)
    return controller->UpdateThread();

    return (DWORD) E_POINTER;
}

// +--------------------------------------------------------------------+

DWORD
RadioVoxController::UpdateThread()
{
    while (!shutdown) {
        Update();
        Sleep(50);
    }

    return 0;
}

// +--------------------------------------------------------------------+

void
RadioVoxController::Update()
{
    AutoThreadSync a(sync);

    if (queue.size()) {
        RadioVox* vox = queue.first();

        if (!vox->Update())
        delete queue.removeIndex(0);
    }
}

bool
RadioVoxController::Add(RadioVox* vox)
{
    if (!vox || vox->sounds.isEmpty())
    return false;

    AutoThreadSync a(sync);

    if (queue.size() < MAX_QUEUE) {
        queue.append(vox);
        return true;
    }

    return false;
}

// +====================================================================+
//
// RADIO VOX MESSAGE:
//

void
RadioVox::Initialize()
{
    if (!controller) {
        controller = new(__FILE__,__LINE__) RadioVoxController;
    }
}

void
RadioVox::Close()
{
    delete controller;
    controller = 0;
}

// +--------------------------------------------------------------------+

RadioVox::RadioVox(int n, const char* p, const char* m)
: path(p), message(m), index(0), channel(n)
{
}

RadioVox::~RadioVox()
{
    sounds.destroy();
}

// +--------------------------------------------------------------------+

bool
RadioVox::AddPhrase(const char* key)
{
    if (AudioConfig::VoxVolume() <= AudioConfig::Silence())
    return false;

    DataLoader* loader = DataLoader::GetLoader();
    if (!loader)
    return false;

    if (key && *key) {
        char datapath[256];
        char filename[256];

        sprintf_s(datapath, "Vox/%s/", path.data());
        sprintf_s(filename, "%s.wav",   key);

        bool        use_fs = loader->IsFileSystemEnabled();
        Sound*      sound  = 0;

        loader->UseFileSystem(true);
        loader->SetDataPath(datapath);
        loader->LoadSound(filename, sound, Sound::LOCALIZED, true); // optional sound
        loader->SetDataPath(0);
        loader->UseFileSystem(use_fs);

        if (sound) {
            sound->SetVolume(AudioConfig::VoxVolume());
            sound->SetFlags(Sound::LOCALIZED | Sound::LOCKED);
            sound->SetFilename(filename);
            sounds.append(sound);

            return true;
        }
    }

    return false;
}

// +--------------------------------------------------------------------+

bool
RadioVox::Start()
{
    if (controller)
    return controller->Add(this);

    return false;
}

bool
RadioVox::Update()
{
    if (message.length()) {
        RadioView::Message(message);
        message = "";
    }

    bool active = false;

    while (!active && index < sounds.size()) {
        Sound* s = sounds[index];

        if (s->IsReady()) {
            if (channel & 1)
            s->SetPan(channel * -3000);
            else
            s->SetPan(channel *  3000);

            s->Play();
            active = true;
        }

        else if (s->IsPlaying()) {
            s->Update();
            active = true;
        }

        else {
            index++;
        }
    }

    return active;
}