Skip to content

Commit e01ab15

Browse files
committed
convert classes to stucts
1 parent e2addaf commit e01ab15

File tree

12 files changed

+760
-779
lines changed

12 files changed

+760
-779
lines changed

shared/Audio.cc

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
#include "Audio.h"
22
#include <stdio.h>
33

4-
Audio::Audio() {
5-
wave_position = 0;
6-
wave_increment = ((double) TONE * (2.0 * M_PI)) / (double) FREQUENCY;
4+
/* Global audio instance */
5+
struct audio audio;
6+
7+
void audio_create(void) {
8+
audio.wave_position = 0;
9+
audio.wave_increment = ((double) TONE * (2.0 * M_PI)) / (double) FREQUENCY;
710
}
811

9-
Audio::~Audio() {
12+
void audio_destroy(void) {
1013
/* pause & close the audio */
11-
SDL_PauseAudioDevice(device, 1);
12-
if (device) SDL_CloseAudioDevice(device);
13-
free(audio_buffer);
14+
SDL_PauseAudioDevice(audio.device, 1);
15+
if (audio.device) SDL_CloseAudioDevice(audio.device);
16+
free(audio.audio_buffer);
1417
}
1518

16-
int Audio::Initialize() {
17-
audiospec.freq = FREQUENCY;
18-
audiospec.format = AUDIO_U8; /* unsigned 8-bit data stream */
19-
audiospec.channels = 1; /* mono */
20-
audiospec.samples = 2048; /* must be a power of 2 */
21-
audiospec.callback = NULL;
22-
audiospec.userdata = NULL;
19+
int audio_initialize(void) {
20+
audio.audiospec.freq = FREQUENCY;
21+
audio.audiospec.format = AUDIO_U8; /* unsigned 8-bit data stream */
22+
audio.audiospec.channels = 1; /* mono */
23+
audio.audiospec.samples = 2048; /* must be a power of 2 */
24+
audio.audiospec.callback = NULL;
25+
audio.audiospec.userdata = NULL;
2326

2427
/* open default audio device (allow audio changes) */
25-
device = SDL_OpenAudioDevice(NULL, 0, &audiospec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
28+
audio.device = SDL_OpenAudioDevice(NULL, 0, &audio.audiospec, NULL, SDL_AUDIO_ALLOW_ANY_CHANGE);
2629

27-
if (!device) {
30+
if (!audio.device) {
2831
fprintf(stderr, "Error: %s\n", SDL_GetError());
2932
return 1;
3033
}
3134

3235
/* ~.5 seconds worth of audio (probably overkill) */
33-
audio_buffer = (unsigned char *)malloc(SAMPLES_PER_FRAME * 30);
34-
if (!audio_buffer) {
36+
audio.audio_buffer = (unsigned char *)malloc(SAMPLES_PER_FRAME * 30);
37+
if (!audio.audio_buffer) {
3538
fprintf(stderr, "Unable to allocate memory for audio buffer.\n");
3639
return 1;
3740
}
3841

3942
/* start playing audio */
40-
SDL_PauseAudioDevice(device, 0);
43+
SDL_PauseAudioDevice(audio.device, 0);
4144

4245
return 0;
4346
}
4447

45-
void Audio::SineWave(int length) {
48+
static void audio_sine_wave(int length) {
4649
for (int i = 0; i < length; i++) {
4750
/* sine wave varies from 120 - 134 */
48-
audio_buffer[i] = (unsigned char) ((AMPLITUDE * sin(wave_position)) + BIAS);
49-
wave_position += wave_increment;
51+
audio.audio_buffer[i] = (unsigned char) ((AMPLITUDE * sin(audio.wave_position)) + BIAS);
52+
audio.wave_position += audio.wave_increment;
5053
}
5154
}
5255

53-
void Audio::Beep(int length) {
54-
if (SDL_GetQueuedAudioSize(device) < (SAMPLES_PER_FRAME * 2)) {
55-
SineWave(length);
56-
SDL_QueueAudio(device, audio_buffer, length);
56+
void audio_beep(int length) {
57+
if (SDL_GetQueuedAudioSize(audio.device) < (SAMPLES_PER_FRAME * 2)) {
58+
audio_sine_wave(length);
59+
SDL_QueueAudio(audio.device, audio.audio_buffer, length);
5760
}
5861
}

shared/Audio.h

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,22 @@
99
#define BIAS 127
1010
#define SAMPLES_PER_FRAME ((FREQUENCY / 60) * 3)
1111

12-
class Audio {
13-
private:
14-
SDL_AudioSpec audiospec;
15-
SDL_AudioDeviceID device;
16-
unsigned char *audio_buffer;
12+
struct audio {
13+
SDL_AudioSpec audiospec;
14+
SDL_AudioDeviceID device;
15+
unsigned char *audio_buffer;
1716

18-
double wave_position;
19-
double wave_increment;
20-
21-
void SineWave(int length);
17+
double wave_position;
18+
double wave_increment;
19+
};
2220

23-
public:
21+
/* Global audio instance */
22+
extern struct audio audio;
2423

25-
Audio();
26-
~Audio();
27-
int Initialize();
28-
void Beep(int length);
29-
};
24+
/* Audio functions */
25+
void audio_create(void);
26+
void audio_destroy(void);
27+
int audio_initialize(void);
28+
void audio_beep(int length);
3029

3130
#endif

0 commit comments

Comments
 (0)