diff --git a/CMakeLists.txt b/CMakeLists.txt index cc48582..3cfbcbe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,7 @@ add_library("luminoveau" STATIC audio/audiohandler.cpp audio/audiohandler.h + configuration/configuration.h text/texthandler.cpp @@ -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 @@ -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 @@ -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}/$) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/$) + if (FORCE_ALL_WARNINGS) target_compile_options("luminoveau" PRIVATE -Wall) @@ -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( @@ -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 ) @@ -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 ) @@ -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) @@ -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 ) @@ -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 ) @@ -179,4 +184,4 @@ if (ADD_IMGUI) ) endif() endif() -endif() \ No newline at end of file +endif() diff --git a/assethandler/assethandler.cpp b/assethandler/assethandler.cpp index 458bda6..b9ecc7c 100644 --- a/assethandler/assethandler.cpp +++ b/assethandler/assethandler.cpp @@ -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]; @@ -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()); @@ -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; @@ -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); } @@ -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); @@ -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) { diff --git a/assethandler/assethandler.h b/assethandler/assethandler.h index 9b78644..c422701 100644 --- a/assethandler/assethandler.h +++ b/assethandler/assethandler.h @@ -64,7 +64,7 @@ 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. @@ -72,14 +72,14 @@ class AssetHandler { * @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 GetTextures() { return get()._textures; } + static std::unordered_map GetTextures() { return get()._textures; } /** * @brief Retrieves a font asset with the specified filename and font size. @@ -122,6 +122,14 @@ class AssetHandler { } + template + static void Delete(T& asset) { + get()._delete(asset); + } + + + + static std::unordered_map &GetLoadedMusics() { return get()._musics; @@ -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); @@ -155,10 +163,10 @@ class AssetHandler { //Containers - std::unordered_map _fonts; + std::unordered_map _fonts; std::unordered_map _musics; - std::unordered_map _sounds; - std::unordered_map _textures; + std::unordered_map _sounds; + std::unordered_map _textures; ScaleMode defaultMode = ScaleMode::NEAREST; @@ -166,7 +174,77 @@ class AssetHandler { static const unsigned char DroidSansMono_ttf[]; unsigned int DroidSansMono_ttf_len = 119380; - Font defaultFont; + FontAsset defaultFont; + + + template + void _delete(T& asset) { + + int a =1; + + + + + if constexpr (std::is_same_v) { + 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(asset).font); + _fonts.erase(it); + } else { + throw std::runtime_error("Font not found in the map"); + } + } else if constexpr (std::is_same_v) { + 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(asset).music); + _musics.erase(it); + } else { + throw std::runtime_error("MusicAsset not found in the map"); + } + } else if constexpr (std::is_same_v) { + 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(asset).sound); + _sounds.erase(it); + } else { + throw std::runtime_error("Sound not found in the map"); + } + } else if constexpr (std::is_same_v) { + 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(asset).surface) { + SDL_DestroySurface(static_cast(asset).surface); + } + if (static_cast(asset).texture) { + SDL_DestroyTexture(static_cast(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; @@ -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(); @@ -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."); @@ -199,4 +278,4 @@ class AssetHandler { }; }; -//*/ \ No newline at end of file +//*/ diff --git a/assettypes/font.h b/assettypes/font.h index 598542f..b840818 100644 --- a/assettypes/font.h +++ b/assettypes/font.h @@ -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. */ -}; \ No newline at end of file +}; + +using Font = FontAsset&; diff --git a/assettypes/sound.h b/assettypes/sound.h index 7d1242b..8393d6e 100644 --- a/assettypes/sound.h +++ b/assettypes/sound.h @@ -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&; diff --git a/assettypes/texture.h b/assettypes/texture.h index ea20cdb..50019d7 100644 --- a/assettypes/texture.h +++ b/assettypes/texture.h @@ -7,11 +7,14 @@ /** * @brief Represents a texture asset for rendering images using SDL. */ -struct Texture { - int width; /**< Width of the texture. */ - int height; /**< Height of the texture. */ +struct TextureAsset { + int width = -1; /**< Width of the texture. */ + int height = -1; /**< Height of the texture. */ std::string filename; /**< Filename of the texture image file. */ SDL_Surface *surface = nullptr; /**< Pointer to the SDL surface representing the texture. */ SDL_Texture *texture = nullptr; /**< Pointer to the SDL texture. */ -}; \ No newline at end of file +}; + +using Texture = TextureAsset&; + diff --git a/input/inputhandler.cpp b/input/inputhandler.cpp index fd06a21..9830304 100644 --- a/input/inputhandler.cpp +++ b/input/inputhandler.cpp @@ -3,14 +3,11 @@ #include "window/windowhandler.h" void Input::_init() { - - SDL_SetHint(SDL_HINT_ACCELEROMETER_AS_JOYSTICK, "0"); SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS4_RUMBLE, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_PS5_RUMBLE, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_HIDAPI_STEAM, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ROG_CHAKRAM, "1"); SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1"); - SDL_SetHint(SDL_HINT_LINUX_JOYSTICK_DEADZONES, "1"); SDL_Init(SDL_INIT_JOYSTICK | SDL_INIT_GAMEPAD); diff --git a/render2d/SDL2_rotozoom.c b/render2d/SDL2_rotozoom.c index 8d7f0f7..a3648f0 100644 --- a/render2d/SDL2_rotozoom.c +++ b/render2d/SDL2_rotozoom.c @@ -816,7 +816,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) return NULL; } - if ((src->format->BitsPerPixel % 8) != 0) { + if ((src->format->bits_per_pixel % 8) != 0) { SDL_SetError("Invalid source surface bit depth"); return NULL; } @@ -836,7 +836,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) newHeight = src->h; } - dst = SDL_CreateSurface(newWidth, newHeight, src->format->BitsPerPixel); + dst = SDL_CreateSurface(newWidth, newHeight, src->format->bits_per_pixel); if(!dst) { SDL_SetError("Could not create destination surface"); return NULL; @@ -850,7 +850,7 @@ SDL_Surface* rotateSurface90Degrees(SDL_Surface* src, int numClockwiseTurns) } /* Calculate byte-per-pixel */ - bpp = src->format->BitsPerPixel / 8; + bpp = src->format->bits_per_pixel / 8; switch(normalizedClockwiseTurns) { case 0: /* Make a copy of the surface */ @@ -1070,8 +1070,8 @@ SDL_Surface *rotozoomSurfaceXY(SDL_Surface * src, double angle, double zoomx, do /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->BitsPerPixel == 32); - if ((is32bit) || (src->format->BitsPerPixel == 8)) { + is32bit = (src->format->bits_per_pixel == 32); + if ((is32bit) || (src->format->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ @@ -1362,8 +1362,8 @@ SDL_Surface *zoomSurface(SDL_Surface * src, double zoomx, double zoomy, int smoo /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->BitsPerPixel == 32); - if ((is32bit) || (src->format->BitsPerPixel == 8)) { + is32bit = (src->format->bits_per_pixel == 32); + if ((is32bit) || (src->format->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ @@ -1507,8 +1507,8 @@ SDL_Surface *shrinkSurface(SDL_Surface *src, int factorx, int factory) /* * Determine if source surface is 32bit or 8bit */ - is32bit = (src->format->BitsPerPixel == 32); - if ((is32bit) || (src->format->BitsPerPixel == 8)) { + is32bit = (src->format->bits_per_pixel == 32); + if ((is32bit) || (src->format->bits_per_pixel == 8)) { /* * Use source surface 'as is' */ diff --git a/render2d/render2dhandler.cpp b/render2d/render2dhandler.cpp index 56bcf81..b2a63a7 100644 --- a/render2d/render2dhandler.cpp +++ b/render2d/render2dhandler.cpp @@ -53,7 +53,7 @@ void Render2D::_drawTexturePart(Texture texture, vf2d pos, vf2d size, rectf src, if (src.width < 0.f) { flipFlags |= SDL_FLIP_HORIZONTAL; } if (src.height < 0.f) { flipFlags |= SDL_FLIP_VERTICAL; } - SDL_RenderTextureRotated(renderer, texture.texture, &srcRect, &dstRect, 0.0, nullptr, (SDL_RendererFlip) flipFlags); + SDL_RenderTextureRotated(renderer, texture.texture, &srcRect, &dstRect, 0.0, nullptr, (SDL_FlipMode) flipFlags); } void Render2D::_beginScissorMode(rectf area) { @@ -272,7 +272,7 @@ void Render2D::_drawTextureMode7(Texture texture, vf2d pos, vf2d size, Mode7Para // ImGui::End(); - SDL_RenderTextureRotated(Window::GetRenderer(), texture.texture, &srcRect, &destRect, 0.0, ¢er, (SDL_RendererFlip) flipFlags); + SDL_RenderTextureRotated(Window::GetRenderer(), texture.texture, &srcRect, &destRect, 0.0, ¢er, (SDL_FlipMode) flipFlags); } diff --git a/text/texthandler.cpp b/text/texthandler.cpp index c1a88e4..943af4a 100644 --- a/text/texthandler.cpp +++ b/text/texthandler.cpp @@ -33,14 +33,14 @@ int Text::_measureText(Font font, std::string textToDraw) { return width; } -Texture Text::_drawTextToTexture(Font font, std::string textToDraw, Color color) { +TextureAsset Text::_drawTextToTexture(Font font, std::string textToDraw, Color color) { if (textToDraw.empty()) { // In this case we return space so we still render something for when the user uses // the height of the returned texture to position multiple lines of text. textToDraw = " "; }; - Texture tex; + TextureAsset tex; tex.surface = TTF_RenderUTF8_Blended(font.font, textToDraw.c_str(), color); tex.width = tex.surface->w; diff --git a/text/texthandler.h b/text/texthandler.h index bbc5daa..c1d842d 100644 --- a/text/texthandler.h +++ b/text/texthandler.h @@ -48,7 +48,7 @@ class Text { * @param color The color of the rendered text (default is WHITE). * @return A Texture object representing the rendered text, or an empty texture if rendering fails. */ - static Texture DrawTextToTexture(Font font, std::string textToDraw, Color color) { + static TextureAsset DrawTextToTexture(Font font, std::string textToDraw, Color color) { return get()._drawTextToTexture(font, textToDraw, color); } @@ -58,7 +58,7 @@ class Text { int _measureText(Font font, std::string text); - Texture _drawTextToTexture(Font font, std::string textToDraw, Color color); + TextureAsset _drawTextToTexture(Font font, std::string textToDraw, Color color); public: Text(const Text &) = delete; @@ -69,5 +69,5 @@ class Text { } private: - Text() {}; + Text() = default; }; \ No newline at end of file diff --git a/utils/helpers.cpp b/utils/helpers.cpp index d811729..743eadd 100644 --- a/utils/helpers.cpp +++ b/utils/helpers.cpp @@ -213,7 +213,7 @@ void Helpers::DrawMainMenu() { ImGui::Text("size = %d x %d", my_image_width, my_image_height); SDL_PixelFormat *pixelFormat = texture.second.surface->format; - Uint32 pixelFormatEnum = pixelFormat->format; + SDL_PixelFormatEnum pixelFormatEnum = pixelFormat->format; const char *surfacePixelFormatName = SDL_GetPixelFormatName(pixelFormatEnum); ImGui::Text("pixel format = %s", surfacePixelFormatName); @@ -391,8 +391,9 @@ void Helpers::DrawMainMenu() { ImGui::Checkbox("Guide", &gamepaddata.Guide); if (SDL_GetGamepadType(gamepad) != SDL_GAMEPAD_TYPE_PS5) { + SDL_PropertiesID props = SDL_GetGamepadProperties(gamepad); - if (SDL_GamepadHasRumble(gamepad)) { + if (SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_RUMBLE_BOOLEAN, SDL_FALSE)) { ImGui::SetCursorPos(ImVec2(offSetX + 550, offSetY - 40)); ImGui::VSliderFloat(" ", {30, 150}, &gamepaddata.rumbleLeft, 0.0f, 1.0f, "", ImGuiSliderFlags_AlwaysClamp); @@ -407,7 +408,7 @@ void Helpers::DrawMainMenu() { SDL_RumbleGamepad(gamepad, (Uint16) rumbleLeft, (Uint16) rumbleRight, 100); } - if (SDL_GamepadHasRumbleTriggers(gamepad)) { + if (SDL_GetBooleanProperty(props, SDL_PROP_GAMEPAD_CAP_TRIGGER_RUMBLE_BOOLEAN, SDL_FALSE)) { ImGui::SetCursorPos(ImVec2(offSetX + 550, offSetY + 200)); ImGui::VSliderFloat(" ", {30, 150}, &gamepaddata.rumbleTriggerLeft, 0.0f, 1.0f, "", ImGuiSliderFlags_AlwaysClamp); diff --git a/window/windowhandler.cpp b/window/windowhandler.cpp index e6e96e2..56945fb 100644 --- a/window/windowhandler.cpp +++ b/window/windowhandler.cpp @@ -24,7 +24,7 @@ void Window::_initWindow(const std::string &title, int width, int height, int sc #ifdef __EMSCRIPTEN__ auto renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); #else - auto renderer = SDL_CreateRenderer(window, "opengl", SDL_RENDERER_ACCELERATED | flags); + auto renderer = SDL_CreateRenderer(window, "opengl", flags); #endif if (!renderer) { diff --git a/window/windowhandler.h b/window/windowhandler.h index 77cf605..dd8c36c 100644 --- a/window/windowhandler.h +++ b/window/windowhandler.h @@ -226,7 +226,7 @@ class Window { void _toggleDebugMenu(); - Texture _screenBuffer; + TextureAsset _screenBuffer; int _scaleFactor = 1;