mirror of
https://github.com/nicbarker/clay.git
synced 2026-02-06 12:48:49 +00:00
[Renderers/Sokol] Sokol renderer & examples (#373)
This commit is contained in:
parent
87efc49f52
commit
eb46688b82
8 changed files with 747 additions and 0 deletions
10
examples/sokol-corner-radius/CMakeLists.txt
Normal file
10
examples/sokol-corner-radius/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
cmake_minimum_required(VERSION 3.27)
|
||||
project(sokol_corner_radius C)
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
|
||||
add_executable(sokol_corner_radius WIN32 main.c)
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT sokol_corner_radius)
|
||||
else()
|
||||
add_executable(sokol_corner_radius main.c)
|
||||
endif()
|
||||
target_link_libraries(sokol_corner_radius PUBLIC sokol)
|
||||
110
examples/sokol-corner-radius/main.c
Normal file
110
examples/sokol-corner-radius/main.c
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
#include "sokol_app.h"
|
||||
#include "sokol_gfx.h"
|
||||
#include "sokol_glue.h"
|
||||
#include "sokol_log.h"
|
||||
|
||||
#define CLAY_IMPLEMENTATION
|
||||
#include "../../clay.h"
|
||||
|
||||
#include "util/sokol_gl.h"
|
||||
#include "fontstash.h"
|
||||
#include "util/sokol_fontstash.h"
|
||||
#define SOKOL_CLAY_IMPL
|
||||
#include "../../renderers/sokol/sokol_clay.h"
|
||||
|
||||
static void init() {
|
||||
sg_setup(&(sg_desc){
|
||||
.environment = sglue_environment(),
|
||||
.logger.func = slog_func,
|
||||
});
|
||||
sgl_setup(&(sgl_desc_t){
|
||||
.logger.func = slog_func,
|
||||
});
|
||||
sclay_setup();
|
||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||
Clay_Initialize(clayMemory, (Clay_Dimensions){ (float)sapp_width(), (float)sapp_height() }, (Clay_ErrorHandler){0});
|
||||
Clay_SetMeasureTextFunction(sclay_measure_text, NULL);
|
||||
}
|
||||
|
||||
Clay_RenderCommandArray CornerRadiusTest(){
|
||||
Clay_BeginLayout();
|
||||
Clay_Sizing layoutExpand = {
|
||||
.width = CLAY_SIZING_GROW(0),
|
||||
.height = CLAY_SIZING_GROW(0)
|
||||
};
|
||||
CLAY({ .id = CLAY_ID("OuterContainer"),
|
||||
.backgroundColor = {43, 41, 51, 255},
|
||||
.layout = {
|
||||
.layoutDirection = CLAY_TOP_TO_BOTTOM,
|
||||
.sizing = layoutExpand,
|
||||
.padding = {0, 0, 20, 20},
|
||||
.childGap = 20
|
||||
}
|
||||
}) {
|
||||
for(int i = 0; i < 6; ++i){
|
||||
CLAY({ .id = CLAY_IDI("Row", i),
|
||||
.layout = {
|
||||
.layoutDirection = CLAY_LEFT_TO_RIGHT,
|
||||
.sizing = layoutExpand,
|
||||
.padding = {20, 20, 0, 0},
|
||||
.childGap = 20
|
||||
}
|
||||
}) {
|
||||
for(int j = 0; j < 6; ++j){
|
||||
CLAY({ .id = CLAY_IDI("Tile", i*6+j),
|
||||
.backgroundColor = {120, 140, 255, 128},
|
||||
.cornerRadius = {(i%3)*15, (j%3)*15, (i/2)*15, (j/2)*15},
|
||||
.border = {
|
||||
.color = {120, 140, 255, 255},
|
||||
.width = {3, 9, 6, 12, 0},
|
||||
},
|
||||
.layout = { .sizing = layoutExpand }
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return Clay_EndLayout();
|
||||
}
|
||||
|
||||
static void frame() {
|
||||
sclay_new_frame();
|
||||
Clay_RenderCommandArray renderCommands = CornerRadiusTest();
|
||||
|
||||
sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() });
|
||||
sgl_matrix_mode_modelview();
|
||||
sgl_load_identity();
|
||||
sclay_render(renderCommands, NULL);
|
||||
sgl_draw();
|
||||
sg_end_pass();
|
||||
sg_commit();
|
||||
}
|
||||
|
||||
static void event(const sapp_event *ev) {
|
||||
if(ev->type == SAPP_EVENTTYPE_KEY_DOWN && ev->key_code == SAPP_KEYCODE_D){
|
||||
Clay_SetDebugModeEnabled(true);
|
||||
} else {
|
||||
sclay_handle_event(ev);
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup() {
|
||||
sclay_shutdown();
|
||||
sgl_shutdown();
|
||||
sg_shutdown();
|
||||
}
|
||||
|
||||
sapp_desc sokol_main(int argc, char **argv) {
|
||||
return (sapp_desc){
|
||||
.init_cb = init,
|
||||
.frame_cb = frame,
|
||||
.event_cb = event,
|
||||
.cleanup_cb = cleanup,
|
||||
.window_title = "Clay - Corner Radius Test",
|
||||
.width = 800,
|
||||
.height = 600,
|
||||
.icon.sokol_default = true,
|
||||
.logger.func = slog_func,
|
||||
};
|
||||
}
|
||||
71
examples/sokol-video-demo/CMakeLists.txt
Normal file
71
examples/sokol-video-demo/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
cmake_minimum_required(VERSION 3.27)
|
||||
project(sokol_video_demo C)
|
||||
|
||||
include(FetchContent)
|
||||
set(FETCHCONTENT_QUIET FALSE)
|
||||
|
||||
# Linux -pthread shenanigans
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
endif()
|
||||
|
||||
FetchContent_Declare(
|
||||
fontstash
|
||||
GIT_REPOSITORY "https://github.com/memononen/fontstash.git"
|
||||
GIT_TAG "b5ddc9741061343740d85d636d782ed3e07cf7be"
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(fontstash)
|
||||
|
||||
FetchContent_Declare(
|
||||
sokol
|
||||
GIT_REPOSITORY "https://github.com/floooh/sokol.git"
|
||||
GIT_TAG "da9de496f938b7575eff7f01ab774d77469bd390"
|
||||
GIT_PROGRESS TRUE
|
||||
GIT_SHALLOW TRUE
|
||||
)
|
||||
FetchContent_MakeAvailable(sokol)
|
||||
set(sokol_HEADERS
|
||||
${sokol_SOURCE_DIR}/sokol_app.h
|
||||
${sokol_SOURCE_DIR}/sokol_gfx.h
|
||||
${sokol_SOURCE_DIR}/sokol_glue.h
|
||||
${sokol_SOURCE_DIR}/sokol_log.h
|
||||
${sokol_SOURCE_DIR}/util/sokol_gl.h
|
||||
${fontstash_SOURCE_DIR}/src/fontstash.h
|
||||
${sokol_SOURCE_DIR}/util/sokol_fontstash.h)
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
|
||||
add_library(sokol STATIC sokol.c ${sokol_HEADERS})
|
||||
target_compile_options(sokol PRIVATE -x objective-c)
|
||||
target_link_libraries(sokol PUBLIC
|
||||
"-framework QuartzCore"
|
||||
"-framework Cocoa"
|
||||
"-framework MetalKit"
|
||||
"-framework Metal")
|
||||
else()
|
||||
add_library(sokol STATIC sokol.c ${sokol_HEADERS})
|
||||
if (CMAKE_SYSTEM_NAME STREQUAL Linux)
|
||||
target_compile_definitions(sokol PRIVATE SOKOL_GLCORE=1)
|
||||
target_link_libraries(sokol INTERFACE X11 Xi Xcursor GL dl m)
|
||||
target_link_libraries(sokol PUBLIC Threads::Threads)
|
||||
endif()
|
||||
endif()
|
||||
target_include_directories(sokol INTERFACE ${sokol_SOURCE_DIR} ${fontstash_SOURCE_DIR}/src
|
||||
PRIVATE ${sokol_SOURCE_DIR} ${fontstash_SOURCE_DIR}/src)
|
||||
|
||||
|
||||
|
||||
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
|
||||
add_executable(sokol_video_demo WIN32 main.c)
|
||||
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT sokol_video_demo)
|
||||
else()
|
||||
add_executable(sokol_video_demo main.c)
|
||||
endif()
|
||||
target_link_libraries(sokol_video_demo PUBLIC sokol)
|
||||
|
||||
add_custom_command(
|
||||
TARGET sokol_video_demo POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/resources
|
||||
${CMAKE_CURRENT_BINARY_DIR}/resources)
|
||||
77
examples/sokol-video-demo/main.c
Normal file
77
examples/sokol-video-demo/main.c
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
#include "sokol_app.h"
|
||||
#include "sokol_gfx.h"
|
||||
#include "sokol_glue.h"
|
||||
#include "sokol_log.h"
|
||||
|
||||
#define CLAY_IMPLEMENTATION
|
||||
#include "../../clay.h"
|
||||
|
||||
#include "util/sokol_gl.h"
|
||||
#include "fontstash.h"
|
||||
#include "util/sokol_fontstash.h"
|
||||
#define SOKOL_CLAY_IMPL
|
||||
#include "../../renderers/sokol/sokol_clay.h"
|
||||
|
||||
#include "../shared-layouts/clay-video-demo.c"
|
||||
|
||||
static ClayVideoDemo_Data demoData;
|
||||
static sclay_font_t fonts[1];
|
||||
|
||||
static void init() {
|
||||
sg_setup(&(sg_desc){
|
||||
.environment = sglue_environment(),
|
||||
.logger.func = slog_func,
|
||||
});
|
||||
sgl_setup(&(sgl_desc_t){
|
||||
.logger.func = slog_func,
|
||||
});
|
||||
sclay_setup();
|
||||
uint64_t totalMemorySize = Clay_MinMemorySize();
|
||||
Clay_Arena clayMemory = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
|
||||
Clay_Initialize(clayMemory, (Clay_Dimensions){ (float)sapp_width(), (float)sapp_height() }, (Clay_ErrorHandler){0});
|
||||
fonts[FONT_ID_BODY_16] = sclay_add_font("resources/Roboto-Regular.ttf");
|
||||
Clay_SetMeasureTextFunction(sclay_measure_text, &fonts);
|
||||
demoData = ClayVideoDemo_Initialize();
|
||||
}
|
||||
|
||||
static void frame() {
|
||||
sclay_new_frame();
|
||||
Clay_RenderCommandArray renderCommands = ClayVideoDemo_CreateLayout(&demoData);
|
||||
|
||||
sg_begin_pass(&(sg_pass){ .swapchain = sglue_swapchain() });
|
||||
sgl_matrix_mode_modelview();
|
||||
sgl_load_identity();
|
||||
sclay_render(renderCommands, fonts);
|
||||
sgl_draw();
|
||||
sg_end_pass();
|
||||
sg_commit();
|
||||
}
|
||||
|
||||
static void event(const sapp_event *ev) {
|
||||
if(ev->type == SAPP_EVENTTYPE_KEY_DOWN && ev->key_code == SAPP_KEYCODE_D){
|
||||
Clay_SetDebugModeEnabled(true);
|
||||
} else {
|
||||
sclay_handle_event(ev);
|
||||
}
|
||||
}
|
||||
|
||||
static void cleanup() {
|
||||
sclay_shutdown();
|
||||
sgl_shutdown();
|
||||
sg_shutdown();
|
||||
}
|
||||
|
||||
sapp_desc sokol_main(int argc, char **argv) {
|
||||
return (sapp_desc){
|
||||
.init_cb = init,
|
||||
.frame_cb = frame,
|
||||
.event_cb = event,
|
||||
.cleanup_cb = cleanup,
|
||||
.window_title = "Clay - Sokol Renderer Example",
|
||||
.width = 800,
|
||||
.height = 600,
|
||||
.high_dpi = true,
|
||||
.icon.sokol_default = true,
|
||||
.logger.func = slog_func,
|
||||
};
|
||||
}
|
||||
BIN
examples/sokol-video-demo/resources/Roboto-Regular.ttf
Normal file
BIN
examples/sokol-video-demo/resources/Roboto-Regular.ttf
Normal file
Binary file not shown.
24
examples/sokol-video-demo/sokol.c
Normal file
24
examples/sokol-video-demo/sokol.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#define SOKOL_IMPL
|
||||
#if defined(_WIN32)
|
||||
#define SOKOL_D3D11
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
#define SOKOL_GLES2
|
||||
#elif defined(__APPLE__)
|
||||
#define SOKOL_METAL
|
||||
#else
|
||||
#define SOKOL_GLCORE33
|
||||
#endif
|
||||
#include "sokol_app.h"
|
||||
#include "sokol_gfx.h"
|
||||
#include "sokol_time.h"
|
||||
#include "sokol_fetch.h"
|
||||
#include "sokol_glue.h"
|
||||
#include "sokol_log.h"
|
||||
|
||||
#include "util/sokol_gl.h"
|
||||
#include <stdio.h> // fontstash requires this
|
||||
#include <stdlib.h> // fontstash requires this
|
||||
#define FONTSTASH_IMPLEMENTATION
|
||||
#include "fontstash.h"
|
||||
#define SOKOL_FONTSTASH_IMPL
|
||||
#include "util/sokol_fontstash.h"
|
||||
Loading…
Add table
Add a link
Reference in a new issue