Skip to content

Commit

Permalink
Updated SDL packages and all changed function calls required for this…
Browse files Browse the repository at this point in the history
… update.

Added refs to font/sound/texture assets and adjust Assethandler accordingly. Because of this we now have a proper Assethandler::Delete(asset) method.
Fixed defaultFont so it uses the proper asset.
  • Loading branch information
bXi committed May 2, 2024
1 parent 58e50c5 commit b21e091
Show file tree
Hide file tree
Showing 14 changed files with 155 additions and 68 deletions.
31 changes: 18 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ add_library("luminoveau" STATIC

audio/audiohandler.cpp
audio/audiohandler.h

configuration/configuration.h

text/texthandler.cpp
Expand All @@ -26,8 +27,10 @@ add_library("luminoveau" STATIC

log/loghandler.cpp
log/loghandler.h

render2d/render2dhandler.cpp
render2d/render2dhandler.h

settings/mini.h
settings/settingshandler.cpp
settings/settingshandler.h
Expand All @@ -36,9 +39,10 @@ add_library("luminoveau" STATIC
state/state.h
state/state.cpp

assethandler/assethandler.cpp
assethandler/assethandler.h
assethandler/DroidSansMono.cpp
assethandler/assethandler.cpp
assethandler/assethandler.h
assethandler/DroidSansMono.cpp

utils/camera.h
utils/colors.h
utils/constants.h
Expand All @@ -52,16 +56,17 @@ add_library("luminoveau" STATIC
utils/rectangles.h
utils/vectors.cpp
utils/vectors.h

window/windowhandler.cpp
window/windowhandler.h

render2d/SDL2_gfxPrimitives.c
render2d/SDL2_gfxPrimitives.h
render2d/SDL2_gfxPrimitives_font.h
render2d/SDL2_rotozoom.h
render2d/SDL2_rotozoom.c
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIGURATION>)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$<CONFIGURATION>)


if (FORCE_ALL_WARNINGS)
target_compile_options("luminoveau" PRIVATE -Wall)
Expand All @@ -78,10 +83,10 @@ if(CMAKE_BUILD_TYPE STREQUAL "Release")
endif()


target_compile_features("luminoveau" PRIVATE cxx_std_23)
target_compile_features("luminoveau" PRIVATE cxx_std_20)

target_include_directories("luminoveau" PUBLIC
"${PROJECT_SOURCE_DIR}"
"${PROJECT_SOURCE_DIR}"
)

file(
Expand All @@ -100,7 +105,7 @@ set(INTERFACE_SDL3_SHARED OFF)
set(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS ON)
CPMAddPackage(
NAME SDL3
GIT_TAG ed62d6e
GIT_TAG e3395a7
GITHUB_REPOSITORY libsdl-org/SDL
)

Expand All @@ -112,7 +117,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)

CPMAddPackage(
NAME SDL3_image
GIT_TAG 34e2279
GIT_TAG 9add12c
GITHUB_REPOSITORY libsdl-org/SDL_image
)

Expand All @@ -125,7 +130,7 @@ if(NOT MSVC)
NAME freetype
GIT_REPOSITORY https://github.com/aseprite/freetype2.git
GIT_TAG e8ebfe9
)
)

if (freetype_ADDED)
add_library(Freetype::Freetype ALIAS freetype)
Expand All @@ -135,7 +140,7 @@ set(SDL3IMAGE_BUILD_SHARED_LIBS OFF)

CPMAddPackage(
NAME SDL3_ttf
GIT_TAG 303c280
GIT_TAG 6e260a2
GITHUB_REPOSITORY libsdl-org/SDL_ttf
)

Expand All @@ -152,7 +157,7 @@ target_link_libraries("luminoveau" PUBLIC
if (ADD_IMGUI)
CPMAddPackage(
NAME Imgui
GIT_TAG 5288687
GIT_TAG 4f9ba19
GITHUB_REPOSITORY ocornut/imgui
)

Expand All @@ -179,4 +184,4 @@ if (ADD_IMGUI)
)
endif()
endif()
endif()
endif()
20 changes: 9 additions & 11 deletions assethandler/assethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

Texture AssetHandler::_getTexture(const std::string &fileName) {
if (_textures.find(fileName) == _textures.end()) {
Texture _tex = _loadTexture(fileName);
auto _tex = _loadTexture(fileName);
_textures[std::string(fileName)] = _tex;

return _textures[fileName];
Expand All @@ -16,11 +16,9 @@ Texture AssetHandler::_getTexture(const std::string &fileName) {
}
}

TextureAsset AssetHandler::_loadTexture(const std::string &fileName) {


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

Texture texture;
TextureAsset texture;

auto surface = IMG_Load(fileName.c_str());

Expand Down Expand Up @@ -59,8 +57,8 @@ Texture AssetHandler::_loadTexture(const std::string &fileName) {

}

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

texture.width = size.x;
texture.height = size.y;
Expand All @@ -80,13 +78,13 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {
if (!surface) {
int texWidth, texHeight;
SDL_QueryTexture(texture.texture, NULL, NULL, &texWidth, &texHeight);
surface = SDL_CreateSurface(texWidth, texHeight, 32);
surface = SDL_CreateSurface(texWidth, texHeight,SDL_PixelFormatEnum::SDL_PIXELFORMAT_RGBA32);

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

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

}

Expand All @@ -113,7 +111,7 @@ void AssetHandler::_saveTextureAsPNG(Texture texture, const char *fileName) {

Sound AssetHandler::_getSound(const std::string &fileName) {
if (_sounds.find(fileName) == _sounds.end()) {
Sound _sound;
SoundAsset _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);

Expand Down Expand Up @@ -162,7 +160,7 @@ Font AssetHandler::_getFont(const std::string &fileName, const int fontSize) {

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

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

if (_font.font == nullptr) {
Expand Down
105 changes: 92 additions & 13 deletions assethandler/assethandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ class AssetHandler {
* @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(std::move(texture), fileName); }
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(const vf2d& size) { return get()._createEmptyTexture(size); }
static TextureAsset CreateEmptyTexture(const 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; }
static std::unordered_map<std::string, TextureAsset> GetTextures() { return get()._textures; }

/**
* @brief Retrieves a font asset with the specified filename and font size.
Expand Down Expand Up @@ -122,6 +122,14 @@ class AssetHandler {
}


template<typename T>
static void Delete(T& asset) {
get()._delete(asset);
}





static std::unordered_map<std::string, MusicAsset> &GetLoadedMusics() {
return get()._musics;
Expand All @@ -133,13 +141,13 @@ class AssetHandler {

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

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

void _setDefaultTextureScaleMode(ScaleMode mode);

ScaleMode _getDefaultTextureScaleMode();

Texture _createEmptyTexture(const vf2d &size);
TextureAsset _createEmptyTexture(const vf2d &size);

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

Expand All @@ -155,18 +163,88 @@ class AssetHandler {

//Containers

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

ScaleMode defaultMode = ScaleMode::NEAREST;

// default font asset Droid Sans Mono.ttf
static const unsigned char DroidSansMono_ttf[];
unsigned int DroidSansMono_ttf_len = 119380;

Font defaultFont;
FontAsset defaultFont;


template<typename T>
void _delete(T& asset) {

int a =1;




if constexpr (std::is_same_v<T, FontAsset>) {
std::cout << "Deleting Font..." << std::endl;
auto it = std::find_if(_fonts.begin(), _fonts.end(), [&](const auto& pair) {
return &pair.second == &asset;
});

if (it != _fonts.end()) {
TTF_CloseFont(static_cast<FontAsset>(asset).font);
_fonts.erase(it);
} else {
throw std::runtime_error("Font not found in the map");
}
} else if constexpr (std::is_same_v<T, MusicAsset>) {
std::cout << "Deleting Music..." << std::endl;
auto it = std::find_if(_musics.begin(), _musics.end(), [&](const auto& pair) {
return &pair.second == &asset;
});

if (it != _musics.end()) {
ma_sound_uninit(static_cast<MusicAsset>(asset).music);
_musics.erase(it);
} else {
throw std::runtime_error("MusicAsset not found in the map");
}
} else if constexpr (std::is_same_v<T, SoundAsset>) {
std::cout << "Deleting Sound..." << std::endl;
auto it = std::find_if(_sounds.begin(), _sounds.end(), [&](const auto& pair) {
return &pair.second == &asset;
});

if (it != _sounds.end()) {
ma_sound_uninit(static_cast<SoundAsset>(asset).sound);
_sounds.erase(it);
} else {
throw std::runtime_error("Sound not found in the map");
}
} else if constexpr (std::is_same_v<T, TextureAsset>) {
std::cout << "Deleting Texture..." << std::endl;
auto it = std::find_if(_textures.begin(), _textures.end(), [&](const auto& pair) {
return &pair.second == &asset;
});

if (it != _textures.end()) {
if (static_cast<TextureAsset>(asset).surface) {
SDL_DestroySurface(static_cast<Texture>(asset).surface);
}
if (static_cast<Texture>(asset).texture) {
SDL_DestroyTexture(static_cast<Texture>(asset).texture);
}
_textures.erase(it);
} else {
throw std::runtime_error("Texture not found in the map");
}
} else {
throw std::runtime_error("Trying to delete invalid asset");
}


};


public:
AssetHandler(const AssetHandler &) = delete;
Expand All @@ -178,7 +256,8 @@ class AssetHandler {

private:
AssetHandler() {
SDL_RWops* rwops = SDL_RWFromConstMem(DroidSansMono_ttf, DroidSansMono_ttf_len);

SDL_IOStream* rwops = SDL_IOFromConstMem(DroidSansMono_ttf, DroidSansMono_ttf_len);
if (rwops == NULL) {
// Handle error
TTF_Quit();
Expand All @@ -187,10 +266,10 @@ class AssetHandler {
}

// Load the font from the memory stream
TTF_Font* font = TTF_OpenFontRW(rwops, 1, 16); // Replace "24" with your desired font size
TTF_Font* font = TTF_OpenFontIO(rwops, 1, 16); // Replace "24" with your desired font size
if (font == NULL) {
// Handle error
SDL_RWclose(rwops);
SDL_CloseIO(rwops);
TTF_Quit();
SDL_Quit();
throw std::exception("Could not load the default font.");
Expand All @@ -199,4 +278,4 @@ class AssetHandler {
};
};

//*/
//*/
6 changes: 4 additions & 2 deletions assettypes/font.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/**
* @brief Represents a font asset for rendering text using SDL_ttf.
*/
struct Font {
struct FontAsset {
TTF_Font *font = nullptr; /**< Pointer to the TrueType font loaded with SDL_ttf. */
};
};

using Font = FontAsset&;
4 changes: 3 additions & 1 deletion assettypes/sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/**
* @brief Represents a sound asset for playing short audio clips using miniaudio.
*/
struct Sound {
struct SoundAsset {
ma_sound *sound = nullptr; /**< Pointer to the audio data loaded with miniaudio. */
};

using Sound = SoundAsset&;
Loading

0 comments on commit b21e091

Please sign in to comment.