Skip to content

Commit

Permalink
More audio class refactoring, both for the spec and specblock. Some h…
Browse files Browse the repository at this point in the history
…as bled into the other major video/homedir classes, with retrospective updates to all related files. Updates to SimpleFileIO functions, particularly the write, so as to give more options and better argument deduction. Add using tags to Map2D.
  • Loading branch information
coornio committed Nov 17, 2024
1 parent 7d7ac51 commit 18dd083
Show file tree
Hide file tree
Showing 20 changed files with 227 additions and 219 deletions.
74 changes: 46 additions & 28 deletions src/Assistants/BasicAudioSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,79 +10,97 @@
#pragma region BasicAudioSpec Singleton Class

BasicAudioSpec::BasicAudioSpec() noexcept {
if (!SDL_InitSubSystem(SDL_INIT_AUDIO)) {
showErrorBox("Failed to init SDL audio!");
return;
mSuccessful = SDL_InitSubSystem(SDL_INIT_AUDIO);
if (!mSuccessful) {
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, "Failed to init SDL audio!",
SDL_GetError(), nullptr
);
}
}

BasicAudioSpec::~BasicAudioSpec() noexcept {
SDL_QuitSubSystem(SDL_INIT_AUDIO);
}

void BasicAudioSpec::showErrorBox(const char* const title) noexcept {
setErrorState(true);
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, title,
SDL_GetError(), nullptr
);
void BasicAudioSpec::setGlobalGain(const f32 gain) noexcept {
mGlobalGain = std::clamp(gain, 0.0f, 1.0f);
}

void BasicAudioSpec::setGlobalVolume(s32 value) noexcept {
auto& volume{ globalVolume() };
volume = std::clamp(value, 0, 255);
void BasicAudioSpec::addGlobalGain(const f32 gain) noexcept {
mGlobalGain = std::clamp(mGlobalGain + gain, 0.0f, 1.0f);
}

void BasicAudioSpec::changeGlobalVolume(s32 delta) noexcept {
auto& volume{ globalVolume() };
volume = std::clamp(volume + delta, 0, 255);
void BasicAudioSpec::addGlobalGain(const s32 gain) noexcept {
static constexpr f32 minF{ 1.0f / 255.0f };
mGlobalGain = std::clamp(mGlobalGain + gain * minF, 0.0f, 1.0f);
}

#pragma endregion
/*VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV*/

s32 AudioSpecBlock::getFrequency() const noexcept {
return mAudioSpec.freq;
}

s32 AudioSpecBlock::getStreamCount() const noexcept {
return static_cast<s32>(mAudioStreams.size());
}
/*==================================================================*/
#pragma region BasicAudioSpec Singleton Class

bool AudioSpecBlock::isPaused(const u32 index) const noexcept {
bool AudioSpecBlock::isStreamPaused(const u32 index) const noexcept {
assert(index < mAudioStreams.size() && "isStreamPaused() index out-of-bounds");
const auto deviceID{ SDL_GetAudioStreamDevice(mAudioStreams[index]) };
return deviceID ? SDL_AudioDevicePaused(deviceID) : true;
}

f32 AudioSpecBlock::getSampleRate(const f32 framerate) const noexcept {
return framerate > Epsilon::f32 ? mAudioSpec.freq / framerate : 0.0f;
return framerate > 1.0f ? mAudioSpec.freq / framerate : 0.0f;
}

f32 AudioSpecBlock::getGain(const u32 index) const noexcept {
assert(index < mAudioStreams.size() && "getGain() index out-of-bounds");
return SDL_GetAudioStreamGain(mAudioStreams[index]);
}

s32 AudioSpecBlock::getGainByte(const u32 index) const noexcept {
s32 AudioSpecBlock::getGainByte(const u32 index) const noexcept {
assert(index < mAudioStreams.size() && "getGainByte() index out-of-bounds");
return static_cast<s32>(getGain(index) * 255.0f);
}

/*==================================================================*/

void AudioSpecBlock::pauseStream(const u32 index) noexcept {
assert(index < mAudioStreams.size() && "pauseStream() index out-of-bounds");
SDL_PauseAudioStreamDevice(mAudioStreams[index]);
}

void AudioSpecBlock::pauseStreams() noexcept {
for (const auto& stream : mAudioStreams) {
SDL_PauseAudioStreamDevice(stream);
}
}

void AudioSpecBlock::resumeStream(const u32 index) noexcept {
assert(index < mAudioStreams.size() && "resumeStream() index out-of-bounds");
SDL_ResumeAudioStreamDevice(mAudioStreams[index]);
}

void AudioSpecBlock::resumeStreams() noexcept {
for (const auto& stream : mAudioStreams) {
SDL_ResumeAudioStreamDevice(stream);
}
}

void AudioSpecBlock::setGain(const u32 index, const f32 gain) noexcept {
assert(index < mAudioStreams.size() && "setGain() index out-of-bounds");
SDL_SetAudioStreamGain(mAudioStreams[index], gain);
}

void AudioSpecBlock::addGain(const u32 index, const f32 gain) noexcept {
assert(index < mAudioStreams.size() && "addGain() index out-of-bounds");
setGain(index, std::clamp(getGain(index) + gain, 0.0f, 1.0f));
}

void AudioSpecBlock::addGain(const u32 index, const s32 gain) noexcept {
assert(index < mAudioStreams.size() && "addGain() index out-of-bounds");
static constexpr f32 minF{ 1.0f / 255.0f };
setGain(index, std::clamp(getGain(index) + gain * minF, 0.0f, 1.0f));
}

void AudioSpecBlock::addGain(const u32 index, const f32 gain) noexcept {
setGain(index, std::clamp(getGain(index) + gain, 0.0f, 1.0f));
}
#pragma endregion
/*VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV*/
55 changes: 26 additions & 29 deletions src/Assistants/BasicAudioSpec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <span>
#include <cmath>
#include <vector>
#include <cassert>
#include <utility>
#include <algorithm>
#include <execution>
Expand All @@ -27,32 +28,23 @@ class BasicAudioSpec final {
BasicAudioSpec(const BasicAudioSpec&) = delete;
BasicAudioSpec& operator=(const BasicAudioSpec&) = delete;

static bool& errorState() noexcept {
static bool errorEncountered{};
return errorEncountered;
}

static s32& globalVolume() noexcept {
static s32 globalVolume{ 15 * 11 };
return globalVolume;
}
static inline f32 mGlobalGain{ 11 * (1.0f / 255.0f * 15) };
static inline bool mSuccessful{ true };

public:
static auto* create() noexcept {
static BasicAudioSpec self;
return errorState() ? nullptr : &self;
return mSuccessful ? &self : nullptr;
}

static bool getErrorState() noexcept { return errorState(); }
static void setErrorState(const bool state) noexcept { errorState() = state; }

static void showErrorBox(const char* const title) noexcept;
static bool isSuccessful() noexcept { return mSuccessful; }

static auto getGlobalVolume() noexcept { return globalVolume(); }
static auto getGlobalVolumeNorm() noexcept { return globalVolume() / 255.0f; }
static auto getGlobalGain() noexcept { return mGlobalGain; }
static auto getGlobalGainByte() noexcept { return static_cast<s32>(mGlobalGain * 255.0f); }

static void setGlobalVolume(s32 value) noexcept;
static void changeGlobalVolume(s32 delta) noexcept;
static void setGlobalGain(const f32 gain) noexcept;
static void addGlobalGain(const f32 gain) noexcept;
static void addGlobalGain(const s32 gain) noexcept;
};

#pragma endregion
Expand All @@ -76,26 +68,31 @@ class AudioSpecBlock {
audioStream = SDL_OpenAudioDeviceStream(device, &mAudioSpec, nullptr, nullptr);
}
}
~AudioSpecBlock() = default;

AudioSpecBlock(const AudioSpecBlock&) = delete;
AudioSpecBlock& operator=(const AudioSpecBlock&) = delete;

s32 getFrequency() const noexcept;
s32 getStreamCount() const noexcept;
/*==================================================================*/

bool isPaused(const u32 index) const noexcept;
auto getFrequency() const noexcept { return mAudioSpec.freq; }
auto getStreamCount() const noexcept { return static_cast<s32>(mAudioStreams.size()); }

bool isStreamPaused(const u32 index) const noexcept;
f32 getSampleRate(const f32 framerate) const noexcept;
f32 getGain(const u32 index) const noexcept;
s32 getGainByte(const u32 index) const noexcept;
f32 getGain(const u32 index) const noexcept;
s32 getGainByte(const u32 index) const noexcept;

/*==================================================================*/

void pauseStream(const u32 index) noexcept;
void pauseStreams() noexcept;

void resumeStream(const u32 index) noexcept;
void resumeStreams() noexcept;

void setGain(const u32 index, const f32 gain) noexcept;
void addGain(const u32 index, const s32 gain) noexcept;
void addGain(const u32 index, const f32 gain) noexcept;
void addGain(const u32 index, const s32 gain) noexcept;

/**
* @brief Pushes buffer of audio samples to SDL, accepts any span
Expand All @@ -104,17 +101,17 @@ class AudioSpecBlock {
*/
template <typename T>
void pushAudioData(const u32 index, const std::span<T> samplesBuffer) const {
if (isPaused(index)) { return; }
if (isStreamPaused(index) || !samplesBuffer.size()) { return; }

const auto globalVolume{ BasicAudioSpec::getGlobalVolumeNorm() };
const auto globalGain{ BasicAudioSpec::getGlobalGain() };

std::transform(
std::execution::unseq,
samplesBuffer.begin(),
samplesBuffer.end(),
samplesBuffer.data(),
[globalVolume](const T sample) noexcept {
return static_cast<T>(sample * globalVolume);
[globalGain](const T sample) noexcept {
return static_cast<T>(sample * globalGain);
}
);

Expand Down
34 changes: 17 additions & 17 deletions src/Assistants/BasicVideoSpec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,24 @@
BasicVideoSpec::BasicVideoSpec() noexcept
: enableBuzzGlow{ true }
{
if (!SDL_InitSubSystem(SDL_INIT_VIDEO)) {
mSuccessful = SDL_InitSubSystem(SDL_INIT_VIDEO);
if (!mSuccessful) {
showErrorBox("Failed to init SDL video!");
return;
}

mMainWindow = SDL_CreateWindow(sAppName, 0, 0, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
if (!mMainWindow) {
showErrorBox("Failed to create Main window!");
mSuccessful = mMainWindow = SDL_CreateWindow(sAppName, 0, 0, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
if (!mSuccessful) {
showErrorBox("Failed to create main window!");
return;
}
#if defined(SDL_PLATFORM_WIN32) && !defined(OLD_WINDOWS_SDK)
else {
const auto windowHandle{
SDL_GetPointerProperty(
SDL_GetWindowProperties(mMainWindow),
SDL_PROP_WINDOW_WIN32_HWND_POINTER,
nullptr
)
};
const auto windowHandle{ SDL_GetPointerProperty(
SDL_GetWindowProperties(mMainWindow),
SDL_PROP_WINDOW_WIN32_HWND_POINTER,
nullptr
) };

if (windowHandle) {
const auto windowRound{ DWMWCP_DONOTROUND };
Expand All @@ -64,9 +63,10 @@ BasicVideoSpec::BasicVideoSpec() noexcept
}
#endif

mMainRenderer = SDL_CreateRenderer(mMainWindow, nullptr);
if (!mMainRenderer) {
mSuccessful = mMainRenderer = SDL_CreateRenderer(mMainWindow, nullptr);
if (!mSuccessful) {
showErrorBox("Failed to create Main renderer!");
return;
}

IMGUI_CHECKVERSION();
Expand Down Expand Up @@ -103,7 +103,6 @@ void BasicVideoSpec::setWindowTitle(SDL_Window* window, const Str& name) {
}

void BasicVideoSpec::showErrorBox(const char* const title) noexcept {
setErrorState(true);
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, title,
SDL_GetError(), nullptr
Expand Down Expand Up @@ -164,14 +163,14 @@ void BasicVideoSpec::setViewportOpacity(const u32 alpha) {
bool BasicVideoSpec::setViewportDimensions(
const s32 texture_W, const s32 texture_H
) {
mMainTexture = SDL_CreateTexture(
mSuccessful = mMainTexture = SDL_CreateTexture(
mMainRenderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
texture_W, texture_H
);

if (!mMainTexture) {
if (!mSuccessful) {
showErrorBox("Failed to create Viewport texture!");
} else {
SDL_SetTextureScaleMode(mMainTexture, SDL_SCALEMODE_NEAREST);
Expand Down Expand Up @@ -278,7 +277,8 @@ void BasicVideoSpec::renderPresent() {
static_cast<s32>(mOuterFrame.h)
) };

if (!viewportTexture) {
mSuccessful = viewportTexture;
if (!mSuccessful) {
showErrorBox("Failed to create GUI texture!");
return;
} else {
Expand Down
11 changes: 3 additions & 8 deletions src/Assistants/BasicVideoSpec.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class BasicVideoSpec final {
SDL_Unique<SDL_Texture> mMainTexture{};

static inline const char* sAppName{};
static inline bool mSuccessful{ true };

SDL_FRect mOuterFrame{};
SDL_FRect mInnerFrame{};
Expand All @@ -40,20 +41,14 @@ class BasicVideoSpec final {
bool enableBuzzGlow{};
bool enableScanLine{};

static bool& errorState() noexcept {
static bool errorEncountered{};
return errorEncountered;
}

public:
static auto* create(const char *appName) noexcept {
sAppName = appName;
static BasicVideoSpec self;
return errorState() ? nullptr : &self;
return mSuccessful ? &self : nullptr;
}

static void setErrorState(const bool state) noexcept { errorState() = state; }
static bool getErrorState() noexcept { return errorState(); }
static bool isSuccessful() noexcept { return mSuccessful; }

static void showErrorBox(const char* const title) noexcept;

Expand Down
22 changes: 11 additions & 11 deletions src/Assistants/HomeDirManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,17 @@
#pragma region HomeDirManager Class

HomeDirManager::HomeDirManager(const char* const org, const char* const app) noexcept {
if (!getHomePath(org, app)) {
showErrorBox("Filesystem Error", "Unable to get home directory!");
mSuccessful = getHomePath(org, app);
if (!mSuccessful) {
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, "Filesystem Error",
"Unable to get home directory!", nullptr
);
} else {
blog.initLogFile("program.log", getHomePath());
}
}

void HomeDirManager::showErrorBox(const char* const title, const char* const message) noexcept {
setErrorState(true);
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, title,
message, nullptr
);
}

Path* HomeDirManager::addSystemDir(const Path& sub, const Path& sys) noexcept {
if (sub.empty()) { return nullptr; }

Expand All @@ -54,7 +50,11 @@ Path* HomeDirManager::addSystemDir(const Path& sub, const Path& sys) noexcept {

std::filesystem::create_directories(newDir, error);
if (!std::filesystem::exists(newDir, error) || error) {
showErrorBox("Filesystem Error", (newDir.string() + "\nUnable to create subdirectories!").c_str());
mSuccessful = false;
SDL_ShowSimpleMessageBox(
SDL_MESSAGEBOX_ERROR, "Filesystem Error",
(newDir.string() + "\nUnable to create subdirectories!").c_str(), nullptr
);
return nullptr;
}

Expand Down
Loading

0 comments on commit 18dd083

Please sign in to comment.