Skip to content

Commit

Permalink
Use SDL3_Image + multiple bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arabine committed Apr 1, 2024
1 parent 42c3d9d commit 79f946a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion story-editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -255,7 +258,7 @@ target_compile_definitions(${STORY_EDITOR_PROJECT} PUBLIC cimg_display=0)

target_compile_definitions(${STORY_EDITOR_PROJECT} PUBLIC "$<$<CONFIG:DEBUG>: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})
Expand All @@ -266,6 +269,7 @@ if(UNIX)
OpenGL::GL
dl
SDL3
SDL3_image
libcurl_static
OpenSSL::SSL OpenSSL::Crypto
)
Expand Down
1 change: 1 addition & 0 deletions story-editor/libs/SDL_image
Submodule SDL_image added at 25e816
39 changes: 36 additions & 3 deletions story-editor/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ your use of the corresponding standard functions.
#else
#include <SDL3/SDL_opengl.h>
#endif

#include <SDL3_image/SDL_image.h>

#include "IconsMaterialDesignIcons.h"
#include "IconsFontAwesome5_c.h"
Expand Down Expand Up @@ -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<SDL_Texture*>(img.texture), NULL, NULL, &img.w, &img.h);

/*
std::string ext = GetFileExtension(filename);
SDL_Surface* surface = nullptr;
Expand Down Expand Up @@ -120,7 +150,7 @@ bool LoadTextureFromFile(const char* filename, Gui::Image &img)
SDL_DestroySurface(surface); // SDL3
// SDL_FreeSurface(surface); // SDL2

*/

return true;
}
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion story-editor/src/media_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 79f946a

Please sign in to comment.