Audio Abstrakcija - WIP

This commit is contained in:
2026-03-13 14:28:08 +01:00
parent 8ae713ba8a
commit 52df67da09
13 changed files with 324 additions and 43 deletions

21
include/audio/audio.hpp Normal file
View File

@@ -0,0 +1,21 @@
#pragma once
#include <SDL3/SDL.h>
#include <utils.hpp>
namespace Game::Audio {
class Audio {
public:
Audio() = default;
DISABLE_COPY_AND_MOVE(Audio)
~Audio();
static Audio& getInstance();
bool init();
SDL_AudioDeviceID getAudioDevice() const { return mDevice; }
private:
SDL_AudioSpec mAudioSpec;
SDL_AudioDeviceID mDevice;
};
};

View File

@@ -3,6 +3,7 @@
#include <object/entity.hpp>
#include <renderer/texture.hpp>
#include <renderer/font.hpp>
#include <object/sound.hpp>
namespace Game::AGame {
class Player : public Object::Entity {
@@ -12,5 +13,8 @@ namespace Game::AGame {
~Player() override = default;
void start() override;
void update(float deltaTime) override;
private:
Object::Sound mSound;
};
}

37
include/object/sound.hpp Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include <SDL3/SDL.h>
#include <SDL3_mixer/SDL_mixer.h>
#include <utils.hpp>
#include <audio/audio.hpp>
namespace Game::Object {
enum class Format {
WAV,
OGG,
MP3,
UNKNOWN
};
class Sound {
public:
Sound() = default;
Sound(std::string path, Format format, int volume = 128);
Sound(const Sound&);
Sound& operator=(const Sound&);
Sound(Sound&&);
Sound& operator=(Sound&&);
~Sound();
void setVolume(int volume) { mVolume = volume; }
int getVolume() const { return mVolume; }
void play();
private:
Uint8* mAudioBuffer = nullptr;
Uint32 mAudioLength = 0;
SDL_AudioStream* mAudioStream = nullptr;
int mVolume;
};
}

View File

@@ -17,6 +17,9 @@ namespace Game::Renderer {
SDL_Texture* getSDLTexture();
std::string getId();
float getWidth();
float getHeight();
protected:
SDL_Texture* mTex;
std::string mId;

View File

@@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <SDL3/SDL.h>
#define DISABLE_COPY(Class) \
Class(const Class&) = delete; \
@@ -29,3 +30,8 @@
#define END_GAME_ENTITY() \
};
#define RUNNING_TIME() \
SDL_GetTicks() / 1000.f
#define PI 3.14159265358979323846f

View File

@@ -9,6 +9,7 @@
#include <renderer/renderer.hpp>
#include <state/gamestate.hpp>
#include <game/gamemanager.hpp>
#include <audio/audio.hpp>
namespace Game::Window {
class Window {