Skip to content

Commit 98b11cf

Browse files
committed
learn clay lib
1 parent e68f6c7 commit 98b11cf

File tree

4 files changed

+109
-4
lines changed

4 files changed

+109
-4
lines changed

.gitmodules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,8 @@
8585
[submodule "vendor/lpeg"]
8686
path = vendor/lpeg
8787
url = https://github.com/roberto-ieru/LPeg.git
88+
shallow = true
89+
[submodule "vendor/clay"]
90+
path = vendor/clay
91+
url = https://github.com/nicbarker/clay
92+
shallow = true

cmake/sokol.cmake

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ if(BUILD_SOKOL)
112112
target_include_directories(tic80 PRIVATE
113113
${CMAKE_SOURCE_DIR}/include
114114
${CMAKE_SOURCE_DIR}/src
115-
${THIRDPARTY_DIR}/sokol)
115+
${THIRDPARTY_DIR}/sokol
116+
${THIRDPARTY_DIR}/clay)
116117

117118
target_link_libraries(tic80 PRIVATE tic80studio sokol)
118119

src/system/sokol/main.c

Lines changed: 101 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929

3030
#include "sokol.h"
3131

32+
#define CLAY_IMPLEMENTATION
33+
#include "clay.h"
34+
3235
#include "studio/studio.h"
3336
#include "crt.h"
3437
#include "thread.h"
@@ -40,6 +43,12 @@ typedef struct
4043
Studio* studio;
4144
tic80_input input;
4245

46+
struct
47+
{
48+
float x, y;
49+
bool down;
50+
} pointer;
51+
4352
struct
4453
{
4554
s32 x, y, dx, dy;
@@ -447,6 +456,11 @@ static thread_ret_t loop(void* userdata)
447456
return 0;
448457
}
449458

459+
static void HandleClayErrors(Clay_ErrorData errorData)
460+
{
461+
printf("%s\n", errorData.errorText.chars);
462+
}
463+
450464
static void init(void *userdata)
451465
{
452466
App *app = userdata;
@@ -460,8 +474,8 @@ static void init(void *userdata)
460474
});
461475

462476
sgl_setup(&(sgl_desc_t){
463-
.max_vertices = 6,
464-
.max_commands = 1,
477+
// .max_vertices = 6,
478+
// .max_commands = 1,
465479
.sample_count = 1,
466480
#if !defined(NDEBUG)
467481
.logger = {.func = slog_func},
@@ -532,6 +546,11 @@ static void init(void *userdata)
532546
#endif
533547

534548
sgamepad_init();
549+
550+
u64 totalMemorySize = Clay_MinMemorySize();
551+
Clay_Arena arena = Clay_CreateArenaWithCapacityAndMemory(totalMemorySize, malloc(totalMemorySize));
552+
553+
Clay_Initialize(arena, (Clay_Dimensions) { sapp_widthf(), sapp_heightf() }, (Clay_ErrorHandler) { HandleClayErrors });
535554
}
536555

537556
typedef struct
@@ -610,6 +629,78 @@ static void checkrate(App* app)
610629
else threadedMode(app);
611630
}
612631

632+
static void clayupd(App *app)
633+
{
634+
const Clay_Color COLOR_LIGHT = (Clay_Color) {224, 215, 210, 255};
635+
const Clay_Color COLOR_RED = (Clay_Color) {168, 66, 28, 255};
636+
const Clay_Color COLOR_ORANGE = (Clay_Color) {225, 138, 50, 255};
637+
638+
Clay_SetLayoutDimensions((Clay_Dimensions) { sapp_widthf(), sapp_heightf() });
639+
Clay_SetPointerState((Clay_Vector2) { app->pointer.x, app->pointer.y }, app->pointer.down);
640+
641+
Clay_BeginLayout();
642+
643+
CLAY(CLAY_ID("OuterContainer"),
644+
{
645+
.layout =
646+
{
647+
.sizing = {CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0)},
648+
.padding = CLAY_PADDING_ALL(16),
649+
},
650+
.backgroundColor = {}
651+
})
652+
{
653+
CLAY(CLAY_ID("SideBar"),
654+
{
655+
.layout =
656+
{
657+
.sizing = {CLAY_SIZING_FIXED(500), CLAY_SIZING_GROW(0)},
658+
},
659+
.backgroundColor = COLOR_ORANGE
660+
}){}
661+
662+
CLAY(CLAY_ID("Content"),
663+
{
664+
.layout =
665+
{
666+
.sizing = {CLAY_SIZING_GROW(0), CLAY_SIZING_GROW(0)},
667+
},
668+
.backgroundColor = COLOR_RED
669+
}){}
670+
}
671+
672+
Clay_RenderCommandArray renderCommands = Clay_EndLayout();
673+
674+
for (s32 i = 0; i < renderCommands.length; i++)
675+
{
676+
Clay_RenderCommand *renderCommand = &renderCommands.internalArray[i];
677+
678+
switch (renderCommand->commandType)
679+
{
680+
case CLAY_RENDER_COMMAND_TYPE_RECTANGLE:
681+
{
682+
// Clay_RectangleRenderData *config = &renderCommand->renderData.rectangle;
683+
684+
Clay_BoundingBox b = renderCommand->boundingBox;
685+
Clay_Color c = renderCommand->renderData.rectangle.backgroundColor;
686+
687+
sgl_begin_quads();
688+
sgl_v2f_c4b(b.x + 0, b.y + 0, c.r, c.g, c.b, c.a);
689+
sgl_v2f_c4b(b.x + b.width, b.y + 0, c.r, c.g, c.b, c.a);
690+
sgl_v2f_c4b(b.x + b.width, b.y + b.height, c.r, c.g, c.b, c.a);
691+
sgl_v2f_c4b(b.x + 0, b.y + b.height, c.r, c.g, c.b, c.a);
692+
sgl_end();
693+
694+
}
695+
696+
default:
697+
{
698+
break;
699+
}
700+
}
701+
}
702+
}
703+
613704
static void frame(void *userdata)
614705
{
615706
App *app = userdata;
@@ -695,6 +786,8 @@ static void frame(void *userdata)
695786
drawImage(viewport(app), app->image, app->nearest);
696787
}
697788

789+
clayupd(app);
790+
698791
// draw screen
699792
sg_begin_pass(&(sg_pass)
700793
{
@@ -906,16 +999,21 @@ static void event(const sapp_event* event, void *userdata)
906999
{
9071000
Rect r = viewport(app);
9081001

1002+
app->pointer.x = event->mouse_x;
1003+
app->pointer.y = event->mouse_y;
1004+
9091005
app->mouse.x = (event->mouse_x - r.x) * TIC80_FULLWIDTH / r.w;
9101006
app->mouse.y = (event->mouse_y - r.y) * TIC80_FULLHEIGHT / r.h;
9111007
app->mouse.dx = event->mouse_dx;
9121008
app->mouse.dy = event->mouse_dy;
9131009
}
9141010
break;
915-
case SAPP_EVENTTYPE_MOUSE_DOWN:
1011+
case SAPP_EVENTTYPE_MOUSE_DOWN:
1012+
app->pointer.down = true;
9161013
processMouse(app, event->mouse_button, 1);
9171014
break;
9181015
case SAPP_EVENTTYPE_MOUSE_UP:
1016+
app->pointer.down = false;
9191017
processMouse(app, event->mouse_button, 0);
9201018
break;
9211019
case SAPP_EVENTTYPE_MOUSE_SCROLL:

vendor/clay

Submodule clay added at fd97d81

0 commit comments

Comments
 (0)