Skip to content

Commit f9f00dc

Browse files
committed
Add SDL3
1 parent 369d038 commit f9f00dc

File tree

5 files changed

+469
-0
lines changed

5 files changed

+469
-0
lines changed

demo/sdl3_renderer/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
SDL
2+
build

demo/sdl3_renderer/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.16)
2+
3+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
4+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/$<CONFIGURATION>")
5+
set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}" CACHE INTERNAL "")
6+
7+
# CMAKE Modules
8+
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
9+
10+
project(nuklear_sdl3_renderer)
11+
12+
set(EXECUTABLE_NAME ${PROJECT_NAME})
13+
14+
# The SDL java code is hardcoded to load libmain.so on android, so we need to change EXECUTABLE_NAME
15+
if (ANDROID)
16+
set(EXECUTABLE_NAME main)
17+
add_library(${EXECUTABLE_NAME} SHARED)
18+
else()
19+
add_executable(${EXECUTABLE_NAME})
20+
endif()
21+
22+
# Add your sources to the target
23+
target_sources(${EXECUTABLE_NAME} PRIVATE main.c)
24+
25+
find_package(SDL3 REQUIRED)
26+
27+
target_link_libraries(${EXECUTABLE_NAME} PUBLIC SDL3::SDL3)
28+
29+
# Nuklear Include Directory
30+
target_include_directories(${EXECUTABLE_NAME} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/../..)
31+
32+
# on Visual Studio, set our app as the default project
33+
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "${EXECUTABLE_NAME}")
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
include(FetchContent)
2+
FetchContent_Declare(
3+
SDL3
4+
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
5+
GIT_TAG eb3fc06
6+
#GIT_SHALLOW 1
7+
)
8+
FetchContent_MakeAvailable(SDL3)

demo/sdl3_renderer/main.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#define SDL_MAIN_USE_CALLBACKS
2+
#include <SDL3/SDL.h>
3+
#include <SDL3/SDL_main.h>
4+
5+
#define NK_SDL_RENDERER_IMPLEMENTATION
6+
#include "nuklear_sdl3_renderer.h"
7+
8+
typedef struct AppContext {
9+
SDL_Window* window;
10+
SDL_Renderer* renderer;
11+
} AppContext;
12+
13+
SDL_AppResult SDL_Fail(){
14+
SDL_LogError(SDL_LOG_CATEGORY_CUSTOM, "Error %s", SDL_GetError());
15+
return SDL_APP_FAILURE;
16+
}
17+
18+
SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
19+
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS)) {
20+
return SDL_Fail();
21+
}
22+
23+
AppContext* appContext = (AppContext*)SDL_malloc(sizeof(AppContext));
24+
if (appContext == NULL) {
25+
return SDL_Fail();
26+
}
27+
28+
if (!SDL_CreateWindowAndRenderer("Nuklear: SDL3 Renderer", 1200, 800, SDL_WINDOW_RESIZABLE, &appContext->window, &appContext->renderer)) {
29+
SDL_free(appContext);
30+
return SDL_Fail();
31+
}
32+
33+
*appstate = appContext;
34+
35+
return SDL_APP_CONTINUE;
36+
}
37+
38+
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event* event) {
39+
AppContext* app = (AppContext*)appstate;
40+
41+
switch (event->type) {
42+
case SDL_EVENT_QUIT:
43+
return SDL_APP_SUCCESS;
44+
}
45+
46+
nk_sdl_handle_event(event);
47+
48+
return SDL_APP_CONTINUE;
49+
}
50+
51+
SDL_AppResult SDL_AppIterate(void *appstate) {
52+
AppContext* app = (AppContext*)appstate;
53+
54+
SDL_SetRenderDrawColor(app->renderer, 255, 0, 255, SDL_ALPHA_OPAQUE);
55+
SDL_RenderClear(app->renderer);
56+
SDL_RenderPresent(app->renderer);
57+
58+
return SDL_APP_CONTINUE;
59+
}
60+
61+
void SDL_AppQuit(void* appstate, SDL_AppResult result) {
62+
AppContext* app = (AppContext*)appstate;
63+
64+
if (app) {
65+
SDL_DestroyRenderer(app->renderer);
66+
SDL_DestroyWindow(app->window);
67+
SDL_free(app);
68+
}
69+
70+
SDL_Quit();
71+
}

0 commit comments

Comments
 (0)