Skip to content

Commit

Permalink
Add SDL3
Browse files Browse the repository at this point in the history
  • Loading branch information
RobLoach committed Dec 15, 2024
1 parent 369d038 commit f9f00dc
Show file tree
Hide file tree
Showing 5 changed files with 469 additions and 0 deletions.
2 changes: 2 additions & 0 deletions demo/sdl3_renderer/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
SDL
build
33 changes: 33 additions & 0 deletions demo/sdl3_renderer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.16)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE INTERNAL "")

# CMAKE Modules
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

project(nuklear_sdl3_renderer)

set(EXECUTABLE_NAME ${PROJECT_NAME})

# The SDL java code is hardcoded to load libmain.so on android, so we need to change EXECUTABLE_NAME
if (ANDROID)
set(EXECUTABLE_NAME main)
add_library(${EXECUTABLE_NAME} SHARED)
else()
add_executable(${EXECUTABLE_NAME})
endif()

# Add your sources to the target
target_sources(${EXECUTABLE_NAME} PRIVATE main.c)

find_package(SDL3 REQUIRED)

target_link_libraries(${EXECUTABLE_NAME} PUBLIC SDL3::SDL3)

# Nuklear Include Directory
target_include_directories(${EXECUTABLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../..)

# on Visual Studio, set our app as the default project
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "${EXECUTABLE_NAME}")
8 changes: 8 additions & 0 deletions demo/sdl3_renderer/cmake/FindSDL3.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
include(FetchContent)
FetchContent_Declare(
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG eb3fc06
#GIT_SHALLOW 1
)
FetchContent_MakeAvailable(SDL3)
71 changes: 71 additions & 0 deletions demo/sdl3_renderer/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>

#define NK_SDL_RENDERER_IMPLEMENTATION
#include "nuklear_sdl3_renderer.h"

typedef struct AppContext {
SDL_Window* window;
SDL_Renderer* renderer;
} AppContext;

SDL_AppResult SDL_Fail(){
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
return SDL_APP_FAILURE;
}

SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
return SDL_Fail();
}

AppContext* appContext = (AppContext*)SDL_malloc(sizeof(AppContext));
if (appContext == NULL) {
return SDL_Fail();
}

if (!SDL_CreateWindowAndRenderer("Nuklear: SDL3 Renderer", 1200, 800, SDL_WINDOW_RESIZABLE, &appContext->window, &appContext->renderer)) {
SDL_free(appContext);
return SDL_Fail();
}

*appstate = appContext;

return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event* event) {
AppContext* app = (AppContext*)appstate;

switch (event->type) {
case SDL_EVENT_QUIT:
return SDL_APP_SUCCESS;
}

nk_sdl_handle_event(event);

return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppIterate(void *appstate) {
AppContext* app = (AppContext*)appstate;

SDL_SetRenderDrawColor(app->renderer, 255, 0, 255, SDL_ALPHA_OPAQUE);
SDL_RenderClear(app->renderer);
SDL_RenderPresent(app->renderer);

return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void* appstate, SDL_AppResult result) {
AppContext* app = (AppContext*)appstate;

if (app) {
SDL_DestroyRenderer(app->renderer);
SDL_DestroyWindow(app->window);
SDL_free(app);
}

SDL_Quit();
}
Loading

0 comments on commit f9f00dc

Please sign in to comment.