-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAudioManager.cpp
More file actions
47 lines (40 loc) · 1.08 KB
/
AudioManager.cpp
File metadata and controls
47 lines (40 loc) · 1.08 KB
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
//
// Created by jnetn on 13/02/2026.
//
#include "AudioManager.h"
#include <iostream>
AudioManager::AudioManager()
: wavBuffer(nullptr), wavLength(0), initialized(false) {}
AudioManager::~AudioManager() {
Cleanup();
}
bool AudioManager::LoadSound(const std::string& filepath) {
if (SDL_LoadWAV(filepath.c_str(), &wavSpec, &wavBuffer, &wavLength) == nullptr) {
std::cerr << "Failed to load WAV: " << SDL_GetError() << std::endl;
return false;
}
if (SDL_OpenAudio(&wavSpec, nullptr) < 0) {
std::cerr << "Failed to open audio: " << SDL_GetError() << std::endl;
SDL_FreeWAV(wavBuffer);
return false;
}
initialized = true;
return true;
}
void AudioManager::PlaySound() {
if (initialized && wavBuffer) {
SDL_ClearQueuedAudio(1);
SDL_QueueAudio(1, wavBuffer, wavLength);
SDL_PauseAudio(0);
}
}
void AudioManager::Cleanup() {
if (wavBuffer) {
SDL_FreeWAV(wavBuffer);
wavBuffer = nullptr;
}
if (initialized) {
SDL_CloseAudio();
initialized = false;
}
}