Skip to content

Commit

Permalink
Changed Textures to AssetHandler. Moved the tile/rectangle methods to…
Browse files Browse the repository at this point in the history
… a future spritesheet util.

Moved loading of fonts/sounds/musics into the AssetHandler.
Added GetAudioEngine() so the AssetHandler can call this to load sounds with.
Added GetLoadedMusics() to AssetHandler so the AudioHandler can check all the musics that need to play.
  • Loading branch information
bXi committed Mar 29, 2024
1 parent 61ab404 commit 66763b2
Show file tree
Hide file tree
Showing 15 changed files with 271 additions and 341 deletions.
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ add_library("luminoveau" STATIC
state/state.h
state/state.cpp

texture/texturehandler.cpp
texture/texturehandler.h
assethandler/assethandler.cpp
assethandler/assethandler.h
utils/camera.h
utils/colors.h
utils/constants.h
Expand Down
123 changes: 81 additions & 42 deletions texture/texturehandler.cpp → assethandler/assethandler.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "texturehandler.h"
#include "assethandler.h"
#include "window/windowhandler.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION

#include "stb_image_write.h"

Texture Textures::_getTexture(const std::string &fileName) {
Texture AssetHandler::_getTexture(const std::string &fileName) {
if (_textures.find(fileName) == _textures.end()) {
Texture _tex = _loadTexture(fileName);
_textures[std::string(fileName)] = _tex;
Expand All @@ -16,46 +16,9 @@ Texture Textures::_getTexture(const std::string &fileName) {
}
}

rectf Textures::_getRectangle(int x, int y) {

const rectf rect = {(float) (x * Configuration::tileWidth), (float) (y * Configuration::tileHeight),
(float) (Configuration::tileWidth),
(float) (Configuration::tileHeight)};
return rect;
};

rectf Textures::_getRectangle(int x, int y, int spriteWidth, int spriteHeight) {

const rectf rect = {(float) (x * spriteWidth), (float) (y * spriteHeight),
(float) (spriteWidth),
(float) (spriteHeight)};
return rect;
};

rectf Textures::_getTile(int tileId) {
return GetRectangle(tileId % 16, (int) tileId / 16);
};

rectf Textures::_getTile(int tileId, bool doubleHeight) {
rectf r = GetRectangle(tileId % 16, (int) tileId / 16);
if (doubleHeight) {
r.y -= r.height;
r.height *= 2;
}
return r;
}

rectf Textures::_getTile(int tileId, bool doubleHeight, int spriteWidth, int spriteHeight) {
rectf r = GetRectangle(tileId % 16, (int) tileId / 16, spriteWidth, spriteHeight);
if (doubleHeight) {
r.y -= r.height;
r.height *= 2;
}
return r;
}


Texture Textures::_loadTexture(const std::string &fileName) {
Texture AssetHandler::_loadTexture(const std::string &fileName) {

Texture texture;

Expand Down Expand Up @@ -93,7 +56,7 @@ Texture Textures::_loadTexture(const std::string &fileName) {

}

Texture Textures::_createEmptyTexture(const vf2d &size) {
Texture AssetHandler::_createEmptyTexture(const vf2d &size) {
Texture texture;

texture.width = size.x;
Expand All @@ -107,15 +70,21 @@ Texture Textures::_createEmptyTexture(const vf2d &size) {
return texture;
}

void Textures::_saveTextureAsPNG(Texture texture, const char *fileName) {
void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {

SDL_Surface *surface = texture.surface;

if (!surface) {
int texWidth, texHeight;
SDL_QueryTexture(texture.texture, NULL, NULL, &texWidth, &texHeight);
surface = SDL_CreateSurface(texWidth, texHeight, 32);

if (!surface) {
SDL_Log("%s", SDL_GetError());
}

SDL_RenderReadPixels(Window::GetRenderer(), NULL, SDL_PIXELFORMAT_RGBA32, surface->pixels, surface->pitch);

}

SDL_Surface *rgbaSurface = surface;
Expand All @@ -138,3 +107,73 @@ void Textures::_saveTextureAsPNG(Texture texture, const char *fileName) {
SDL_DestroySurface(rgbaSurface);
}
}

Sound AssetHandler::_getSound(const std::string &fileName) {
if (_sounds.find(fileName) == _sounds.end()) {
Sound _sound;
_sound.sound = new ma_sound();
ma_result result = ma_sound_init_from_file(Audio::GetAudioEngine(), fileName.c_str(), MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC, nullptr, nullptr, _sound.sound);

if (result != MA_SUCCESS) {
std::string error = Helpers::TextFormat("GetSound failed: %s", fileName.c_str());

SDL_Log("%s", error.c_str());
throw std::runtime_error(error.c_str());
}

_sounds[fileName] = _sound;

return _sounds[fileName];
} else {
return _sounds[fileName];
}
}

Music AssetHandler::_getMusic(const std::string &fileName) {
if (_musics.find(fileName) == _musics.end()) {
MusicAsset _music;

_music.music = new ma_sound();
ma_result result = ma_sound_init_from_file(Audio::GetAudioEngine(), fileName.c_str(), MA_SOUND_FLAG_DECODE | MA_SOUND_FLAG_ASYNC, nullptr, nullptr, _music.music);

if (result != MA_SUCCESS) {
std::string error = Helpers::TextFormat("GetMusic failed: %s", fileName.c_str());

SDL_Log("%s", error.c_str());
throw std::runtime_error(error.c_str());
}

_musics[fileName] = _music;

return _musics[fileName];
} else {
return _musics[fileName];
}
}


Font AssetHandler::_getFont(const std::string &fileName, const int fontSize) {
std::string index = std::string(Helpers::TextFormat("%s%d", fileName.c_str(), fontSize));

auto it = _fonts.find(index);

if (it == _fonts.end()) {

Font _font;
_font.font = TTF_OpenFont(fileName.c_str(), fontSize);

if (_font.font == nullptr) {
std::string error = Helpers::TextFormat("Couldn't load %d pt font from %s: %s\n",
fontSize, fileName.c_str(), SDL_GetError());

SDL_Log("%s", error.c_str());
throw std::runtime_error(error.c_str());
}

_fonts[index] = _font;

return _fonts[index];
} else {
return _fonts[index];
}
}
139 changes: 139 additions & 0 deletions assethandler/assethandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#pragma once

#include <unordered_map>
#include <string>

#include "SDL3/SDL.h"
#include "SDL3_image/SDL_image.h"

#include "utils/vectors.h"
#include "utils/rectangles.h"

#include "audio/audiohandler.h"

#include "assettypes/font.h"
#include "assettypes/music.h"
#include "assettypes/sound.h"
#include "assettypes/texture.h"

/**
* @brief Manages assets and provides utility functions for working with assets.
*/
class AssetHandler {
public:
/**
* @brief Retrieves a texture by its file name.
*
* @param fileName The name of the texture file.
* @return The texture corresponding to the file name.
*/
static Texture GetTexture(const char *fileName) { return get()._getTexture(fileName); }

/**
* @brief Loads a texture from the specified file.
*
* @param fileName The name of the texture file to load.
*/
static void LoadTexture(const char *fileName) { get()._loadTexture(fileName); }

/**
* @brief Saves the given texture as a PNG file.
*
* @param texture The texture to save.
* @param fileName The name of the PNG file to save.
*/
static void SaveTextureAsPNG(Texture texture, const char *fileName) { get()._saveTextureAsPNG(texture, fileName); }

/**
* @brief Creates an empty texture with the specified size.
*
* @param size The size of the empty texture.
* @return The created empty texture.
*/
static Texture CreateEmptyTexture(vf2d size) { return get()._createEmptyTexture(size); }

/**
* @brief Retrieves a map of all loaded textures.
*
* @return The map of loaded textures.
*/
static std::unordered_map<std::string, Texture> GetTextures() { return get()._textures; }

/**
* @brief Retrieves a font asset with the specified filename and font size.
*
* @param fileName The filename of the font asset.
* @param fontSize The size of the font.
* @return The font asset.
*/
static Font GetFont(const char *fileName, const int fontSize) {
return get()._getFont(fileName, fontSize);
}

/**
* @brief Retrieves a music asset.
*
* @param fileName The filename of the music asset.
* @return The music asset.
*/
static Music& GetMusic(const char *fileName) {
return get()._getMusic(fileName);
}

/**
* @brief Retrieves a sound asset.
*
* @param fileName The filename of the sound asset.
* @return The sound asset.
*/
static Sound GetSound(const char *fileName) {
return get()._getSound(fileName);
}


static std::unordered_map<std::string, MusicAsset> &GetLoadedMusics() {
return get()._musics;
}


private:
// Textures

Texture _getTexture(const std::string &fileName);

Texture _loadTexture(const std::string &fileName);

Texture _createEmptyTexture(const vf2d &size);

void _saveTextureAsPNG(Texture texture, const char *fileName);

// Fonts

Font _getFont(const std::string &fileName, int fontSize);

// Audio

Sound _getSound(const std::string &fileName);

Music _getMusic(const std::string &fileName);

//Containers

std::unordered_map<std::string, Font> _fonts;
std::unordered_map<std::string, MusicAsset> _musics;
std::unordered_map<std::string, Sound> _sounds;
std::unordered_map<std::string, Texture> _textures;

public:
AssetHandler(const AssetHandler &) = delete;

static AssetHandler &get() {
static AssetHandler instance;
return instance;
}

private:
AssetHandler() = default;
};

//*/
File renamed without changes.
9 changes: 7 additions & 2 deletions assettypes/music.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

/**
* @brief Represents a music asset for playing audio using miniaudio.
* @typedef Music Music
*/
struct Music {
struct MusicAsset {
ma_sound *music = nullptr; /**< Pointer to the audio data loaded with miniaudio. */

bool shouldPlay = false; /**< Flag indicating whether the music should play. */
bool started = false; /**< Flag indicating whether the music playback has started. */
};

bool isSame = false;
};

using Music = MusicAsset&;
Loading

0 comments on commit 66763b2

Please sign in to comment.