Compare commits

...

8 Commits

Author SHA1 Message Date
AICDG
5db4252be4
Merge 5fea1e748d into 902ff3b0a9 2024-12-31 14:45:10 +08:00
Stowy
902ff3b0a9
Fixed compilation using clang on windows (#134)
Some checks failed
CMake on multiple platforms / build (Release, clang, clang++, ubuntu-latest) (push) Failing after 12s
CMake on multiple platforms / build (Release, gcc, g++, ubuntu-latest) (push) Failing after 10s
CMake on multiple platforms / build (Release, cl, cl, windows-latest) (push) Has been cancelled
2024-12-31 17:51:18 +13:00
SuperOpt
2938c00dc8
[Renderers/Cairo] Add FindCairo.cmake (#122) 2024-12-31 13:32:56 +13:00
SuperOpt
ba78b35604
C++ projects should use CXX flags (#136) 2024-12-31 13:29:49 +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
8 changed files with 288 additions and 8 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

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.27) cmake_minimum_required(VERSION 3.27)
project(clay) project(clay)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
add_subdirectory("examples/cpp-project-example") add_subdirectory("examples/cpp-project-example")
# Don't try to compile C99 projects using MSVC # Don't try to compile C99 projects using MSVC
@ -10,4 +12,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()

2
clay.h
View File

@ -134,7 +134,7 @@ CLAY__ALIGNMENT_STRUCT(bool);
CLAY__ALIGNMENT_STRUCT(uint8_t); CLAY__ALIGNMENT_STRUCT(uint8_t);
CLAY__ALIGNMENT_STRUCT(int32_t); CLAY__ALIGNMENT_STRUCT(int32_t);
#ifdef _MSC_VER #if defined(_MSC_VER) && !defined(__clang__)
#define CLAY_PACKED_ENUM __pragma(pack(push, 1)) enum __pragma(pack(pop)) #define CLAY_PACKED_ENUM __pragma(pack(push, 1)) enum __pragma(pack(pop))
#else #else
#define CLAY_PACKED_ENUM enum __attribute__((__packed__)) #define CLAY_PACKED_ENUM enum __attribute__((__packed__))

32
cmake/FindCairo.cmake Normal file
View File

@ -0,0 +1,32 @@
# Defines:
# CAIRO_FOUND - System has Cairo
# CAIRO_INCLUDE_DIRS - Cairo include directories
# CAIRO_LIBRARY - Cairo library
# Cairo::Cairo - Imported target
find_path(CAIRO_INCLUDE_DIRS
NAMES cairo/cairo.h
PATHS ${CAIRO_ROOT_DIR}
PATH_SUFFIXES include
)
find_library(CAIRO_LIBRARY
NAMES cairo
PATHS ${CAIRO_ROOT_DIR}
PATH_SUFFIXES lib lib64
)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Cairo
REQUIRED_VARS CAIRO_LIBRARY CAIRO_INCLUDE_DIRS
)
if(Cairo_FOUND AND NOT TARGET Cairo::Cairo)
add_library(Cairo::Cairo UNKNOWN IMPORTED)
set_target_properties(Cairo::Cairo PROPERTIES
IMPORTED_LOCATION "${CAIRO_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${CAIRO_INCLUDE_DIRS}"
)
endif()
mark_as_advanced(CAIRO_INCLUDE_DIRS CAIRO_LIBRARY)

View File

@ -2,13 +2,17 @@ cmake_minimum_required(VERSION 3.27)
project(clay_examples_cairo_pdf_rendering C) project(clay_examples_cairo_pdf_rendering C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake")
add_executable(clay_examples_cairo_pdf_rendering main.c) add_executable(clay_examples_cairo_pdf_rendering main.c)
find_package(Cairo REQUIRED)
target_compile_options(clay_examples_cairo_pdf_rendering PUBLIC) 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 . ${CAIRO_INCLUDE_DIRS})
target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC cairo)
target_link_libraries(clay_examples_cairo_pdf_rendering PUBLIC Cairo::Cairo)
set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces") set(CMAKE_C_FLAGS_DEBUG "-Wall -Werror -Wno-error=missing-braces")
set(CMAKE_C_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_C_FLAGS_DEBUG "-Werror -Wall -Wno-error=missing-braces") set(CMAKE_CXX_FLAGS_DEBUG "-Werror -Wall -Wno-error=missing-braces")
set(CMAKE_C_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_RELEASE "-O3")

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);
}
}
}
}