Skip to content

Commit

Permalink
SDL 3 + curl thread download manager
Browse files Browse the repository at this point in the history
  • Loading branch information
arabine committed Apr 1, 2024
1 parent 001034d commit 42c3d9d
Show file tree
Hide file tree
Showing 11 changed files with 311 additions and 143 deletions.
7 changes: 7 additions & 0 deletions software/library/library_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,16 @@ LibraryManager::LibraryManager() {}
void LibraryManager::Initialize(const std::string &library_path)
{
m_library_path = library_path;
CheckDirectories();
Scan();
}

void LibraryManager::CheckDirectories()
{
std::filesystem::path dlDir = std::filesystem::path(m_library_path) / "store";
std::filesystem::create_directories(dlDir);
}

void LibraryManager::Scan()
{
std::filesystem::path directoryPath(m_library_path);
Expand Down
5 changes: 5 additions & 0 deletions software/library/library_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ class LibraryManager
void Scan();

std::shared_ptr<StoryProject> NewProject();
void CheckDirectories();

std::shared_ptr<StoryProject> GetStory(const std::string &uuid);

void SetStoreUrl(const std::string &store_url) { m_storeUrl = store_url; }
std::string GetStoreUrl() const { return m_storeUrl; }

private:
std::string m_library_path;
std::vector<std::shared_ptr<StoryProject>> m_projectsList;
std::string m_storeUrl;
};

#endif // LIBRARYMANAGER_H
41 changes: 27 additions & 14 deletions story-editor/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,36 @@ add_compile_definitions(IMGUI_INCLUDE="imgui.h")
add_subdirectory(libs/ImGuiFileDialog)

# =========================================================================================================================
# SDL
# SDL2
# =========================================================================================================================
Set(FETCHCONTENT_QUIET FALSE)

# FetchContent_Declare(
# sdl2
# GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
# GIT_TAG origin/SDL2
# GIT_SHALLOW TRUE
# GIT_PROGRESS TRUE
# )

# set(BUILD_SHARED_LIBS TRUE)
# set(SDL_STATIC TRUE)
# FetchContent_MakeAvailable(sdl2)

# =========================================================================================================================
# SDL3
# =========================================================================================================================
FetchContent_Declare(
sdl2
sdl3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG origin/SDL2
GIT_TAG prerelease-3.1.0
GIT_SHALLOW TRUE
GIT_PROGRESS TRUE
)

set(BUILD_SHARED_LIBS TRUE)
set(SDL_STATIC TRUE)
FetchContent_MakeAvailable(sdl2)

# add_subdirectory(libs/SDL)
# include_directories(libs/SDL/include)
FetchContent_MakeAvailable(sdl3)
include_directories(${sdl3_SOURCE_DIR}/include)

# =========================================================================================================================
# SDL3-Image
Expand Down Expand Up @@ -155,8 +167,8 @@ set(SRCS
libs/imgui-node-editor/crude_json.cpp
libs/ImGuiFileDialog/ImGuiFileDialog.cpp

${imgui_SOURCE_DIR}/backends/imgui_impl_sdl2.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdlrenderer2.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdl3.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_sdlrenderer3.cpp
${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
Expand Down Expand Up @@ -243,16 +255,17 @@ 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 ${sdl2_BINARY_DIR} ${curl_BINARY_DIR})
message(${sdl2_BINARY_DIR})
set(SDL2_BIN_DIR ${sdl2_BINARY_DIR})
target_link_directories(${STORY_EDITOR_PROJECT} PUBLIC ${sdl3_BINARY_DIR} ${curl_BINARY_DIR})


set(SDL2_BIN_DIR ${sdl3_BINARY_DIR})

if(UNIX)
target_link_libraries(${STORY_EDITOR_PROJECT}
pthread
OpenGL::GL
dl
SDL2
SDL3
libcurl_static
OpenSSL::SSL OpenSSL::Crypto
)
Expand Down
107 changes: 61 additions & 46 deletions story-editor/src/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,34 @@ your use of the corresponding standard functions.
#include <iostream>
#include <filesystem>

#include "imgui_impl_sdl2.h"
#include "imgui_impl_sdlrenderer2.h"
#include <stdio.h>
#include <SDL2/SDL.h>

#include "imgui_internal.h"
// SDL2 ---------------------------------
// #include "imgui_impl_sdl2.h"
// #include "imgui_impl_sdlrenderer2.h"
// #include <SDL2/SDL.h>
// #if defined(IMGUI_IMPL_OPENGL_ES2)
// #include <SDL2/SDL_opengles2.h>
// #else
// #include <SDL2/SDL_opengl.h>
//#endif

// SDL3 ---------------------------------
#include "imgui_impl_sdl3.h"
#include "imgui_impl_sdlrenderer3.h"
#include <SDL3/SDL.h>
#if defined(IMGUI_IMPL_OPENGL_ES2)
#include <SDL2/SDL_opengles2.h>
#include <SDL3/SDL_opengles2.h>
#else
#include <SDL2/SDL_opengl.h>
#include <SDL3/SDL_opengl.h>
#endif


#include "IconsMaterialDesignIcons.h"
#include "IconsFontAwesome5_c.h"
#include "qoi.h"


#include "platform_folders.h"

static void glfw_error_callback(int error, const char* description)
{
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
Expand All @@ -50,8 +61,6 @@ 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)
{


std::string ext = GetFileExtension(filename);

SDL_Surface* surface = nullptr;
Expand All @@ -64,8 +73,12 @@ bool LoadTextureFromFile(const char* filename, Gui::Image &img)
img.w = desc.width;
img.h = desc.height;

surface = SDL_CreateRGBSurfaceFrom((void*)pixels, img.w, img.h, channels * 8, channels * img.w,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
// SDL3
surface = SDL_CreateSurfaceFrom((void*)pixels, img.w, img.h, 4 * img.w, SDL_PIXELFORMAT_RGBA8888);

// SDL2
// surface = SDL_CreateRGBSurfaceFrom((void*)pixels, img.w, img.h, channels * 8, channels * img.w,
// 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);

if (pixels != NULL)
{
Expand All @@ -84,12 +97,12 @@ bool LoadTextureFromFile(const char* filename, Gui::Image &img)
}


// SDL3
// SDL_Surface* surface = SDL_CreateSurfaceFrom((void*)data, img.w, img.h, 4 * img.w, SDL_PIXELFORMAT_RGBA8888);
//SDL3
surface = SDL_CreateSurfaceFrom((void*)data, img.w, img.h, 4 * img.w, SDL_PIXELFORMAT_RGBA32);

// SDL2
surface = SDL_CreateRGBSurfaceFrom((void*)data, img.w, img.h, channels * 8, channels * img.w,
0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
// surface = SDL_CreateRGBSurfaceFrom((void*)data, img.w, img.h, channels * 8, channels * img.w,
// 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
stbi_image_free(data);
}

Expand All @@ -105,8 +118,8 @@ bool LoadTextureFromFile(const char* filename, Gui::Image &img)
fprintf(stderr, "Failed to create SDL texture: %s\n", SDL_GetError());
}

// SDL_DestroySurface(surface); // SDL3
SDL_FreeSurface(surface); // SDL2
SDL_DestroySurface(surface); // SDL3
// SDL_FreeSurface(surface); // SDL2


return true;
Expand All @@ -129,8 +142,11 @@ Gui::Gui()

bool Gui::Initialize()
{
// Setup SDL
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
// Setup SDL2
// if (SDL_Init(SDL_INIT_EVERYTHING) != 0)

// Setup SDL3
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMEPAD) != 0)
{
printf("Error: SDL_Init(): %s\n", SDL_GetError());
return -1;
Expand All @@ -143,17 +159,17 @@ bool Gui::Initialize()
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN);

// SDL3
//window = SDL_CreateWindow("Dear ImGui SDL3+SDL_Renderer example", 1280, 720, window_flags);
window = SDL_CreateWindow("Story Editor", 1280, 720, window_flags);

// SDL2
window = SDL_CreateWindow("Story Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
// window = SDL_CreateWindow("Story Editor", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);

if (window == nullptr)
{
printf("Error: SDL_CreateWindow(): %s\n", SDL_GetError());
return -1;
}
renderer = SDL_CreateRenderer(window, 0, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
renderer = SDL_CreateRenderer(window, nullptr, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
if (renderer == nullptr)
{
SDL_Log("Error: SDL_CreateRenderer(): %s\n", SDL_GetError());
Expand All @@ -170,6 +186,7 @@ bool Gui::Initialize()
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable;
io.ConfigFlags |= ImGuiConfigFlags_ViewportsEnable;


io.Fonts->AddFontFromFileTTF( std::string(m_executablePath + "/fonts/roboto.ttf").c_str(), 20);
Expand Down Expand Up @@ -201,13 +218,12 @@ bool Gui::Initialize()
// Setup Platform/Renderer backends

// SDL3
// ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
// ImGui_ImplSDLRenderer3_Init(renderer);

ImGui_ImplSDL3_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer3_Init(renderer);

// SDL2
ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
ImGui_ImplSDLRenderer2_Init(renderer);
// ImGui_ImplSDL2_InitForSDLRenderer(window, renderer);
// ImGui_ImplSDLRenderer2_Init(renderer);

// Load Fonts
// - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
Expand Down Expand Up @@ -237,20 +253,19 @@ bool Gui::PollEvent()
while (SDL_PollEvent(&event))
{
// SDL3
/*

ImGui_ImplSDL3_ProcessEvent(&event);
if (event.type == SDL_EVENT_QUIT)
done = true;
if (event.type == SDL_EVENT_WINDOW_CLOSE_REQUESTED && event.window.windowID == SDL_GetWindowID(window))
done = true;
*/

// SLD2
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
done = true;
// ImGui_ImplSDL2_ProcessEvent(&event);
// if (event.type == SDL_QUIT)
// done = true;
// if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
// done = true;

}
return done;
Expand All @@ -262,12 +277,12 @@ void Gui::StartFrame()
// Start the Dear ImGui frame

// SDL3
// ImGui_ImplSDLRenderer3_NewFrame();
// ImGui_ImplSDL3_NewFrame();
ImGui_ImplSDLRenderer3_NewFrame();
ImGui_ImplSDL3_NewFrame();

// SDL2
ImGui_ImplSDLRenderer2_NewFrame();
ImGui_ImplSDL2_NewFrame();
// ImGui_ImplSDLRenderer2_NewFrame();
// ImGui_ImplSDL2_NewFrame();

ImGui::NewFrame();
}
Expand All @@ -284,8 +299,8 @@ void Gui::EndFrame()

ImGui::Render();

//ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData()); // SDL3
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData()); // SDL2
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData()); // SDL3
// ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData()); // SDL2

SDL_RenderPresent(renderer);
}
Expand All @@ -295,12 +310,12 @@ void Gui::Destroy()
// Cleanup

// SDL3
// ImGui_ImplSDLRenderer3_Shutdown();
// ImGui_ImplSDL3_Shutdown();
ImGui_ImplSDLRenderer3_Shutdown();
ImGui_ImplSDL3_Shutdown();

// SDL2
ImGui_ImplSDLRenderer2_Shutdown();
ImGui_ImplSDL2_Shutdown();
// ImGui_ImplSDLRenderer2_Shutdown();
// ImGui_ImplSDL2_Shutdown();


ImGui::DestroyContext();
Expand Down Expand Up @@ -495,7 +510,7 @@ void Gui::ApplyTheme()



#include "imgui_internal.h"

namespace ImGui {
void LoadingIndicatorCircle(const char* label, const float indicator_radius,
const ImVec4& main_color, const ImVec4& backdrop_color,
Expand Down
10 changes: 5 additions & 5 deletions story-editor/src/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class Gui
std::string m_executablePath;
};

namespace ImGui {
void LoadingIndicatorCircle(const char* label, const float indicator_radius,
const ImVec4& main_color, const ImVec4& backdrop_color,
const int circle_count, const float speed);
}
// namespace ImGui {
// void LoadingIndicatorCircle(const char* label, const float indicator_radius,
// const ImVec4& main_color, const ImVec4& backdrop_color,
// const int circle_count, const float speed);
// }

#endif // GUI_H
Loading

0 comments on commit 42c3d9d

Please sign in to comment.