Compare commits

...

9 Commits

Author SHA1 Message Date
AICDG
346f3b546a
Merge 5fea1e748d into c9e1a63378 2024-12-30 09:53:30 +08:00
SuperOpt
c9e1a63378
[Compilers] C projects should use C flags rather than CXX flags (#123)
Some checks failed
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Waiting to run
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 58s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 9s
2024-12-30 13:28:24 +13:00
Nic Barker
20543bdc74 Fix a typof of #if and #ifdef" 2024-12-30 13:11:32 +13:00
FintasticMan
c13eef1c1e
[Core] Fix more C99 compliance issues (#118) 2024-12-30 13:09:14 +13:00
Junior Rantila
c24a41b9e4
Add Clay_IsDebugModeEnabled() (#130) 2024-12-30 12:04:48 +13:00
AICDG
5fea1e748d
Merge branch 'main' into main 2024-12-28 19:14:06 +08:00
AICDG
13402c62ab
Merge branch 'main' into main 2024-12-27 11:00:41 +08:00
THISISAGOODNAME
9b8fd94170 update imgui demo 2024-12-27 10:59:17 +08:00
THISISAGOODNAME
b2e7eb2ee6 Create minimal ImDrawList example 2024-12-24 16:53:12 +08:00
14 changed files with 722 additions and 474 deletions

3
.gitignore vendored
View File

@ -1,5 +1,4 @@
cmake-build-debug/ cmake-build-*/
cmake-build-release/
.DS_Store .DS_Store
.idea/ .idea/
build/ build/

View File

@ -10,4 +10,5 @@ if(NOT MSVC)
add_subdirectory("examples/clay-official-website") add_subdirectory("examples/clay-official-website")
add_subdirectory("examples/introducing-clay-video-demo") add_subdirectory("examples/introducing-clay-video-demo")
add_subdirectory("examples/SDL2-video-demo") add_subdirectory("examples/SDL2-video-demo")
add_subdirectory("examples/minimal-imgui")
endif() endif()

863
clay.h

File diff suppressed because it is too large Load Diff

View File

@ -33,8 +33,9 @@ target_link_libraries(SDL2_video_demo PUBLIC
SDL2::SDL2-static SDL2::SDL2-static
SDL2_ttf::SDL2_ttf-static SDL2_ttf::SDL2_ttf-static
) )
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces -DCLAY_DEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O3")
add_custom_command( add_custom_command(
TARGET SDL2_video_demo POST_BUILD TARGET SDL2_video_demo POST_BUILD

View File

@ -8,8 +8,9 @@ target_compile_options(clay_examples_cairo_pdf_rendering PUBLIC)
target_include_directories(clay_examples_cairo_pdf_rendering PUBLIC .) target_include_directories(clay_examples_cairo_pdf_rendering PUBLIC .)
target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC cairo) target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC cairo)
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror")
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces")
set(CMAKE_C_FLAGS_RELEASE "-O3")
add_custom_command( add_custom_command(
TARGET clay_examples_cairo_pdf_rendering POST_BUILD TARGET clay_examples_cairo_pdf_rendering POST_BUILD

View File

@ -5,7 +5,7 @@ set(CMAKE_C_STANDARD 99)
add_executable(clay_official_website main.c) add_executable(clay_official_website main.c)
target_compile_options(clay_official_website PUBLIC -Wall -Werror -Wno-unknown-pragmas) target_compile_options(clay_official_website PUBLIC -Wall -Werror -Wno-unknown-pragmas -Wno-error=missing-braces)
target_include_directories(clay_official_website PUBLIC .) target_include_directories(clay_official_website PUBLIC .)
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_RELEASE "-O3")

View File

@ -8,5 +8,5 @@ add_executable(clay_examples_cpp_project_example main.cpp)
target_include_directories(clay_examples_cpp_project_example PUBLIC .) target_include_directories(clay_examples_cpp_project_example PUBLIC .)
set(CMAKE_CXX_FLAGS_DEBUG "-Werror -Wall") set(CMAKE_C_FLAGS_DEBUG "-Werror -Wall -Wno-error=missing-braces")
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_RELEASE "-O3")

View File

@ -24,8 +24,9 @@ target_compile_options(clay_examples_introducing_clay_video_demo PUBLIC)
target_include_directories(clay_examples_introducing_clay_video_demo PUBLIC .) target_include_directories(clay_examples_introducing_clay_video_demo PUBLIC .)
target_link_libraries(clay_examples_introducing_clay_video_demo PUBLIC raylib) target_link_libraries(clay_examples_introducing_clay_video_demo PUBLIC raylib)
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces -DCLAY_DEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O3")
add_custom_command( add_custom_command(
TARGET clay_examples_introducing_clay_video_demo POST_BUILD TARGET clay_examples_introducing_clay_video_demo POST_BUILD

View File

@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.27)
project(minimal_imgui)
set(CMAKE_C_STANDARD 99)
set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE)
#add_subdirectory(3rd_party)
include(FetchContent)
FetchContent_Declare(
sokol
GIT_REPOSITORY "https://github.com/floooh/sokol.git"
GIT_COMMIT "789d97071d17cbab4e3835a0b0b8b379e98c114f"
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
)
FetchContent_Declare(
imgui
GIT_REPOSITORY "https://github.com/ocornut/imgui.git"
GIT_TAG "v1.91.6"
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(sokol imgui)
add_executable(minimal_imgui
main.cpp
${imgui_SOURCE_DIR}/imgui.cpp
${imgui_SOURCE_DIR}/imgui_demo.cpp
${imgui_SOURCE_DIR}/imgui_draw.cpp
${imgui_SOURCE_DIR}/imgui_tables.cpp
${imgui_SOURCE_DIR}/imgui_widgets.cpp
)
target_compile_options(minimal_imgui PUBLIC)
target_include_directories(minimal_imgui PUBLIC .)
#set(CMAKE_CXX_FLAGS_DEBUG "-DCLAY_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
target_include_directories(minimal_imgui PUBLIC ${sokol_SOURCE_DIR} ${imgui_SOURCE_DIR})
if(WIN32)
target_link_libraries(minimal_imgui PUBLIC kernel32 user32 shell32 gdi32)
endif()

View File

@ -0,0 +1,196 @@
#define CLAY_IMPLEMENTATION
#include "../../clay.h"
#define SOKOL_IMPL
#define SOKOL_NO_ENTRY
#define SOKOL_GLCORE
#include "sokol_app.h"
#include "sokol_gfx.h"
#include "sokol_glue.h"
#include "sokol_log.h"
#define IMGUI_DEFINE_MATH_OPERATORS
#include "imgui.h"
#define SOKOL_IMGUI_IMPL
#include "util/sokol_imgui.h"
#include <stdio.h>
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
sg_pass_action pass_action = {};
static const uint32_t FONT_ID_BODY_24 = 0;
static const Clay_Color COLOR_ORANGE = (Clay_Color) {225, 138, 50, 255};
static const Clay_Color COLOR_BLUE = (Clay_Color) {111, 173, 162, 255};
static const Clay_Color COLOR_LIGHT = (Clay_Color) {224, 215, 210, 255};
void init();
void frame();
void cleanup();
void input(const sapp_event* event);
static void Label(Clay_String text) {
CLAY(CLAY_LAYOUT({ .padding = {16, 8} }),
CLAY_RECTANGLE({ .color = Clay_Hovered() ? COLOR_BLUE : COLOR_ORANGE })
) {
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
.textColor = { 255, 255, 255, 255 },
.fontId = FONT_ID_BODY_24,
.fontSize = 24,
}));
}
}
static Clay_RenderCommandArray CreateLayout() {
Clay_BeginLayout();
CLAY(CLAY_ID("MainContent"),
CLAY_LAYOUT({
.sizing = {
.width = CLAY_SIZING_GROW(),
.height = CLAY_SIZING_GROW(),
},
.childAlignment = {
.x = CLAY_ALIGN_X_CENTER,
.y = CLAY_ALIGN_Y_CENTER,
}
}),
CLAY_RECTANGLE({
.color = COLOR_LIGHT,
})
) {
Label(CLAY_STRING("Hello, World!"));
}
return Clay_EndLayout();
}
Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config);
void render(Clay_RenderCommandArray renderCommands);
int main(int argc, char** args) {
//-----------------------------------------------------------------------
// Setup Clay
//-----------------------------------------------------------------------
uint64_t totalMemorySize = Clay_MinMemorySize();
Clay_Arena clayMemory = (Clay_Arena) {
.label = CLAY_STRING("Clay Memory Arena"),
.capacity = totalMemorySize,
.memory = (char*)malloc(totalMemorySize),
};
Clay_SetMeasureTextFunction(measureText);
Clay_Initialize(clayMemory, (Clay_Dimensions) { SCREEN_WIDTH, SCREEN_HEIGHT });
//-----------------------------------------------------------------------
// Setup Sokol
//-----------------------------------------------------------------------
sapp_desc desc = {0};
desc.init_cb = init;
desc.frame_cb = frame;
desc.cleanup_cb = cleanup,
desc.event_cb = input,
desc.width = SCREEN_WIDTH,
desc.height = SCREEN_HEIGHT,
desc.window_title = "sokol + puredoom",
desc.icon.sokol_default = true,
desc.logger.func = slog_func;
sapp_run(&desc);
return 0;
}
void init() {
sg_desc desc = {0};
desc.environment = sglue_environment();
desc.logger.func = slog_func;
sg_setup(&desc);
pass_action.colors[0] = (sg_color_attachment_action){ .load_action=SG_LOADACTION_CLEAR, .clear_value={0.2f, 0.1f, 0.3f, 1.0f} };
simgui_desc_t simgui_desc = {0};
simgui_setup(&simgui_desc);
}
void frame() {
// const double dt = sapp_frame_duration();
const int width = sapp_width();
const int height = sapp_height();
simgui_new_frame({ width, height, sapp_frame_duration(), sapp_dpi_scale() });
// imgui
{
// ImGui::ShowDemoWindow();
ImGui::SetNextWindowSize(ImVec2{SCREEN_WIDTH, SCREEN_HEIGHT});
ImGui::SetNextWindowPos(ImVec2{0, 0});
ImGui::Begin("Text rendering", NULL, ImGuiWindowFlags_NoBackground|ImGuiWindowFlags_NoTitleBar|ImGuiWindowFlags_NoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoScrollbar|ImGuiWindowFlags_NoSavedSettings);
Clay_RenderCommandArray renderCommands = CreateLayout();
render(renderCommands);
ImGui::End();
}
sg_begin_pass({ .action = pass_action, .swapchain = sglue_swapchain() });
simgui_render();
sg_end_pass();
sg_commit();
}
void cleanup() {
simgui_shutdown();
sg_shutdown();
}
void input(const sapp_event* event) {
simgui_handle_event(event);
}
Clay_Dimensions measureText(Clay_String *text, Clay_TextElementConfig *config)
{
ImVec2 size = ImGui::CalcTextSize(text->chars, nullptr);
return (Clay_Dimensions) {
.width = size.x,
.height = size.y,
};
}
void render(Clay_RenderCommandArray renderCommands)
{
ImDrawList* draw_list = ImGui::GetWindowDrawList();
ImVec2 p = ImGui::GetCursorScreenPos();
for (uint32_t i = 0; i < renderCommands.length; i++)
{
Clay_RenderCommand *renderCommand = Clay_RenderCommandArray_Get(&renderCommands, i);
Clay_BoundingBox boundingBox = renderCommand->boundingBox;
switch (renderCommand->commandType)
{
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE: {
Clay_RectangleElementConfig *config = renderCommand->config.rectangleElementConfig;
Clay_Color color = config->color;
draw_list->AddRectFilled(p+ImVec2(boundingBox.x, boundingBox.y), p+ImVec2(boundingBox.x+boundingBox.width, boundingBox.y+boundingBox.height), ImColor(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f));
break;
}
case CLAY_RENDER_COMMAND_TYPE_TEXT: {
Clay_TextElementConfig *config = renderCommand->config.textElementConfig;
draw_list->AddText(p+ImVec2(boundingBox.x, boundingBox.y), ImColor(config->textColor.r/255.0f, config->textColor.g/255.0f, config->textColor.b/255.0f, config->textColor.a/255.0f), renderCommand->text.chars);
break;
}
default: {
fprintf(stderr, "Error: unhandled render command: %d\n", renderCommand->commandType);
#ifdef CLAY_OVERFLOW_TRAP
raise(SIGTRAP);
#endif
exit(1);
}
}
}
}

View File

@ -24,8 +24,9 @@ target_compile_options(clay_examples_raylib_sidebar_scrolling_container PUBLIC)
target_include_directories(clay_examples_raylib_sidebar_scrolling_container PUBLIC .) target_include_directories(clay_examples_raylib_sidebar_scrolling_container PUBLIC .)
target_link_libraries(clay_examples_raylib_sidebar_scrolling_container PUBLIC raylib) target_link_libraries(clay_examples_raylib_sidebar_scrolling_container PUBLIC raylib)
set(CMAKE_CXX_FLAGS_DEBUG "-Wall -Werror -DCLAY_DEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces -DCLAY_DEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O3")
add_custom_command( add_custom_command(
TARGET clay_examples_raylib_sidebar_scrolling_container POST_BUILD TARGET clay_examples_raylib_sidebar_scrolling_container POST_BUILD

View File

@ -0,0 +1,3 @@
$NAME$ $NAME$_Allocate_Arena(uint32_t capacity, Clay_Arena *arena) {
return CLAY__INIT($NAME$){.capacity = capacity, .length = 0, .internalArray = ($TYPE$ *)Clay__Array_Allocate_Arena(capacity, sizeof($TYPE$), CLAY__POINTER_ALIGNMENT, arena)};
}

View File

@ -1,6 +1,6 @@
typedef struct CLAY__TYPEDEF($NAME$, struct
{ {
uint32_t capacity; uint32_t capacity;
uint32_t length; uint32_t length;
$TYPE$ *internalArray; $TYPE$ *internalArray;
} $NAME$; });

View File

@ -1,5 +1,5 @@
typedef struct CLAY__TYPEDEF($NAME$Slice, struct
{ {
uint32_t length; uint32_t length;
$TYPE$ *internalArray; $TYPE$ *internalArray;
} $NAME$Slice; });