From 79f946a4a5119a162b565a0a0e100a50e1e1e5cb Mon Sep 17 00:00:00 2001 From: Anthony Rabine Date: Mon, 1 Apr 2024 21:24:36 +0200 Subject: [PATCH] Use SDL3_Image + multiple bug fixes --- .gitmodules | 3 +++ story-editor/CMakeLists.txt | 6 ++++- story-editor/libs/SDL_image | 1 + story-editor/src/gui.cpp | 39 ++++++++++++++++++++++++++++++--- story-editor/src/media_node.cpp | 2 +- 5 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 .gitmodules create mode 160000 story-editor/libs/SDL_image diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..08c168a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "story-editor/libs/SDL_image"] + path = story-editor/libs/SDL_image + url = git@github.com:libsdl-org/SDL_image.git diff --git a/story-editor/CMakeLists.txt b/story-editor/CMakeLists.txt index ee12900..41329f8 100644 --- a/story-editor/CMakeLists.txt +++ b/story-editor/CMakeLists.txt @@ -107,6 +107,9 @@ include_directories(${sdl3_SOURCE_DIR}/include) # set(BUILD_SHARED_LIBS FALSE) # # END ADDITION +add_subdirectory(libs/SDL_image) +include_directories(libs/SDL_image/include) + # FetchContent_MakeAvailable(SDL2_image) set(SRCS @@ -255,7 +258,7 @@ target_compile_definitions(${STORY_EDITOR_PROJECT} PUBLIC cimg_display=0) target_compile_definitions(${STORY_EDITOR_PROJECT} PUBLIC "$<$:DEBUG>") -target_link_directories(${STORY_EDITOR_PROJECT} PUBLIC ${sdl3_BINARY_DIR} ${curl_BINARY_DIR}) +target_link_directories(${STORY_EDITOR_PROJECT} PUBLIC ${sdl3_BINARY_DIR} ${curl_BINARY_DIR} ${CMAKE_BINARY_DIR}/libs/SDL_image) set(SDL2_BIN_DIR ${sdl3_BINARY_DIR}) @@ -266,6 +269,7 @@ if(UNIX) OpenGL::GL dl SDL3 + SDL3_image libcurl_static OpenSSL::SSL OpenSSL::Crypto ) diff --git a/story-editor/libs/SDL_image b/story-editor/libs/SDL_image new file mode 160000 index 0000000..25e816f --- /dev/null +++ b/story-editor/libs/SDL_image @@ -0,0 +1 @@ +Subproject commit 25e816f614dbe4aa9fdaf89c248ae283f20038f9 diff --git a/story-editor/src/gui.cpp b/story-editor/src/gui.cpp index c91bec0..e120c60 100644 --- a/story-editor/src/gui.cpp +++ b/story-editor/src/gui.cpp @@ -32,7 +32,7 @@ your use of the corresponding standard functions. #else #include #endif - +#include #include "IconsMaterialDesignIcons.h" #include "IconsFontAwesome5_c.h" @@ -61,6 +61,36 @@ static std::string GetFileExtension(const std::string &fileName) // Simple helper function to load an image into a OpenGL texture with common settings bool LoadTextureFromFile(const char* filename, Gui::Image &img) { + + SDL_Surface *surface, *temp; + + surface = IMG_Load(filename); + if (!surface) { + SDL_Log("Couldn't load %s: %s\n", filename, SDL_GetError()); + return false; + } + + /* Use the tonemap operator to convert to SDR output */ + const char *tonemap = NULL; + SDL_SetStringProperty(SDL_GetSurfaceProperties(surface), SDL_PROP_SURFACE_TONEMAP_OPERATOR_STRING, tonemap); + temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA32); + SDL_DestroySurface(surface); + if (!temp) { + SDL_Log("Couldn't convert surface: %s\n", SDL_GetError()); + return false; + } + + img.texture = SDL_CreateTextureFromSurface(renderer, temp); + SDL_DestroySurface(temp); + if (!img.texture) { + SDL_Log("Couldn't create texture: %s\n", SDL_GetError()); + return false; + } + + SDL_QueryTexture(static_cast(img.texture), NULL, NULL, &img.w, &img.h); + +/* + std::string ext = GetFileExtension(filename); SDL_Surface* surface = nullptr; @@ -120,7 +150,7 @@ bool LoadTextureFromFile(const char* filename, Gui::Image &img) SDL_DestroySurface(surface); // SDL3 // SDL_FreeSurface(surface); // SDL2 - +*/ return true; } @@ -334,7 +364,10 @@ bool Gui::LoadRawImage(const std::string &filename, Image &image) { bool success = true; - LoadTextureFromFile(filename.c_str(), image); + if (std::filesystem::is_regular_file(filename)) + { + LoadTextureFromFile(filename.c_str(), image); + } return success; } diff --git a/story-editor/src/media_node.cpp b/story-editor/src/media_node.cpp index 188f977..a9c6a94 100644 --- a/story-editor/src/media_node.cpp +++ b/story-editor/src/media_node.cpp @@ -10,7 +10,7 @@ MediaNode::MediaNode(const std::string &title, IStoryManager &proj) : BaseNode(title, proj) , m_story(proj) { - Gui::LoadRawImage("fairy.png", m_image); + // Gui::LoadRawImage("fairy.png", m_image); // Create defaut one input and one output AddInput();