// UNICA TU del proyecto que define MINIAUDIO_IMPLEMENTATION.
#define MINIAUDIO_IMPLEMENTATION
#include "../../vendor/miniaudio/miniaudio.h"

#include "audio_engine.h"

#include <cstdlib>

namespace fn::audio {

Engine engine_init() {
    Engine e{nullptr, false};
    ma_engine* eng = static_cast<ma_engine*>(std::malloc(sizeof(ma_engine)));
    if (!eng) return e;
    if (ma_engine_init(NULL, eng) != MA_SUCCESS) {
        std::free(eng);
        return e;
    }
    e.impl = eng;
    e.ok   = true;
    return e;
}

void engine_shutdown(Engine& e) {
    if (!e.ok || !e.impl) return;
    ma_engine* eng = static_cast<ma_engine*>(e.impl);
    ma_engine_uninit(eng);
    std::free(eng);
    e.impl = nullptr;
    e.ok   = false;
}

void engine_set_volume(Engine& e, float v) {
    if (!e.ok || !e.impl) return;
    ma_engine_set_volume(static_cast<ma_engine*>(e.impl), v);
}

}  // namespace fn::audio
