-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfx.cpp
165 lines (130 loc) · 2.77 KB
/
sfx.cpp
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
#include "sfx.hpp"
#include "reader.hpp"
#include "console.hpp"
#include "common.hpp"
#include <vector>
#include <cassert>
#if !DISABLE_SOUND
#include <SDL/SDL.h>
#endif
//#include <iostream>
//#include <cmath> // TEMP
Sfx sfx;
extern "C" void SDLCALL Sfx_callback(void *userdata, Uint8 *stream, int len)
{
uint32 frame_count = len / 2;
sfx_mixer_mix((sfx_mixer*)userdata, stream, frame_count);
}
void Sfx::init()
{
#if !DISABLE_SOUND
if(initialized)
return;
SDL_InitSubSystem(SDL_INIT_AUDIO);
mixer = sfx_mixer_create();
SDL_AudioSpec spec;
memset(&spec, 0, sizeof(spec));
spec.channels = 1;
spec.freq = 44100;
spec.format = AUDIO_S16SYS;
spec.size = 4*512;
spec.callback = Sfx_callback;
spec.userdata = mixer;
int ret = SDL_OpenAudio(&spec, NULL);
if(ret == 0)
{
initialized = true;
//Mix_AllocateChannels(8);
//Mix_Volume(-1, 128);
SDL_PauseAudio(0);
}
else
{
Console::writeWarning(std::string("SDL_OpenAudio returned error: ") + SDL_GetError());
}
#endif
}
void Sfx::deinit()
{
#if !DISABLE_SOUND
if(!initialized)
return;
initialized = false;
//Mix_CloseAudio();
SDL_CloseAudio();
SDL_QuitSubSystem(SDL_INIT_AUDIO);
#endif
}
/*
void Sfx::loadFromSND()
{
#if !DISABLE_SOUND
ReaderFile& snd = openLieroSND();
int count = readUint16(snd);
sounds.resize(count);
long oldPos = snd.tellg();
for(int i = 0; i < count; ++i)
{
snd.seekg(oldPos + 8);
int offset = readUint32(snd);
int length = readUint32(snd);
oldPos = snd.tellg();
int byteLength = length * 4;
sounds[i] = sfx_new_sound(byteLength / 2);
int16_t* ptr = reinterpret_cast<int16_t*>(sfx_sound_data(sounds[i]));
std::vector<uint8_t> temp(length);
if(length > 0)
{
snd.seekg(offset);
snd.get(&temp[0], length);
}
int prev = ((int8_t)temp[0]) * 30;
*ptr++ = prev;
for(int j = 1; j < length; ++j)
{
int cur = (int8_t)temp[j] * 30;
*ptr++ = (prev + cur) / 2;
*ptr++ = cur;
prev = cur;
}
*ptr++ = prev;
}
#endif
}*/
void Sfx::play(Common& common, int sound, void* id, int loops)
{
#if !DISABLE_SOUND
if(!initialized)
return;
sfx_mixer_add(mixer, common.sounds[sound], sfx_mixer_now(mixer), id, loops ? SFX_SOUND_LOOP : SFX_SOUND_NORMAL);
#endif
}
void Sfx::stop(void* id)
{
#if !DISABLE_SOUND
if(!initialized)
return;
sfx_mixer_stop(mixer, id);
#endif
}
bool Sfx::isPlaying(void* id)
{
#if !DISABLE_SOUND
if(!initialized)
return false;
return sfx_is_playing(mixer, id) != 0;
#else
return false;
#endif
}
Sfx::~Sfx()
{
deinit();
#if !DISABLE_SOUND
/*
for(std::size_t i = 0; i < sounds.size(); ++i)
{
sfx_free_sound(sounds[i]);
}*/
#endif
}