feat: translated entire codebase to C++

This commit is contained in:
Sara 2025-09-20 00:52:47 +02:00
parent 00569f2ef1
commit 9acea87ab8
15 changed files with 188 additions and 114 deletions

View file

@ -1,2 +1,2 @@
CompileFlags:
Add: [ -Wall, --std=c23, -xc, -Ivendor/ ]
Add: [ -Wall, --std=c++23, -xc++, -Ivendor/ ]

View file

@ -1,6 +1,6 @@
build:
# BUILDING
make
bear -- make
run:
bin/DiceGui

View file

@ -4,15 +4,18 @@ workspace "DiceGui"
project "Dice"
kind "ConsoleApp"
language "C"
cdialect "c23"
language "C++"
cppdialect "c++23"
location "build/"
files { "src/**.c" }
files { "src/**.cpp", "src/**.c" }
includedirs { "include/" }
links { "m", "stdc++", "SDL3", "SDL3_ttf", "SDL3_image" }
buildoptions { "-Wall" }
targetdir "bin/"
postbuildcommands { "{RMDIR} %{cfg.targetdir}/assets", "{COPYDIR} %{wks.location}/assets/ %{cfg.targetdir}/assets/" }
postbuildcommands {
"{RMDIR} %{cfg.targetdir}/assets",
"{COPYDIR} %{wks.location}/assets/ %{cfg.targetdir}/assets/"
}
filter "configurations:debug"
defines { "DEBUG" }
symbols "On"

View file

@ -6,24 +6,22 @@
static inline
void DiceLogContainer() {
CLAY(CLAY_ID("LogContainer"), {
CLAY(CLAY_ID("LogContainer"), PanelContainer(0, (Clay_ElementDeclaration) {
.layout = {
.sizing = layoutExpand,
.padding = CLAY_PADDING_ALL(16),
},
.PANEL(0),
}) {}
})) {}
}
static inline
void InitiativeListContainer() {
CLAY(CLAY_ID("InitiativeListContainer"), {
CLAY(CLAY_ID("InitiativeListContainer"), PanelContainer(0, (Clay_ElementDeclaration) {
.layout = {
.sizing = layoutExpand,
.padding = CLAY_PADDING_ALL(16),
},
.PANEL(0)
}) {}
})) {}
}
Clay_RenderCommandArray RenderApplication() {

View file

@ -1,13 +1,12 @@
#include "dice.h"
#include <memory.h>
static int activeDiceCount = 0;
static size_t activeDiceCount = 0;
static enum Dice_Die activeDice[MAX_ACTIVE_DICE];
static struct Dice_ResultType rollResult[MAX_ACTIVE_DICE];
static struct Dice_ResultType rollTotal = {
.string = "0",
.roll = 0, .string_len = 1,
.roll = 0, .die = NONE
};
int Dice_Roll(enum Dice_Die die) {
@ -23,16 +22,7 @@ static
struct Dice_ResultType Dice_RollToResultType(int roll, enum Dice_Die die) {
struct Dice_ResultType result = { };
result.roll = roll;
if (die == COIN) {
result.string_len = SDL_snprintf(result.string, MAX_ROLL_STR_LEN, roll == 1 ? "H" : "T");
} else {
result.string_len = SDL_snprintf(result.string, MAX_ROLL_STR_LEN, "%d", roll);
}
result.clay_string = (Clay_String) {
.chars = result.string,
.length = result.string_len,
.isStaticallyAllocated = false
};
result.die = die;
return result;
}
@ -50,7 +40,7 @@ size_t Dice_AddToActiveSet(enum Dice_Die die) {
activeDice[activeDiceCount] = die;
rollResult[activeDiceCount] = Dice_RollToResultType(die, die);
rollTotal.roll += die;
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
return activeDiceCount++;
}
@ -59,14 +49,14 @@ void Dice_RemoveFromActiveSet(size_t index) {
return;
}
rollTotal.roll -= rollResult[index].roll;
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
memcpy(activeDice + index, activeDice + index + 1, MAX_ACTIVE_DICE - index - 1);
--activeDiceCount;
}
void Dice_ClearActiveSet() {
rollTotal.roll = 0;
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
rollTotal = Dice_RollToResultType(rollTotal.roll, NONE);
activeDiceCount = 0;
}
@ -76,7 +66,7 @@ void Dice_RollActiveSet() {
rollResult[i] = Dice_RollToResultType(Dice_Roll(activeDice[i]), activeDice[i]);
total += rollResult[i].roll;
}
rollTotal = Dice_RollToResultType(total, 0);
rollTotal = Dice_RollToResultType(total, NONE);
}
struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) {
@ -86,17 +76,13 @@ struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) {
return rollResult;
}
struct Dice_ResultType *Dice_GetLastResultTotal() {
rollTotal.clay_string = (Clay_String) {
.chars = rollTotal.string,
.length = rollTotal.string_len,
.isStaticallyAllocated = false
};
return &rollTotal;
struct Dice_ResultType Dice_GetLastResultTotal() {
return rollTotal;
}
Clay_String Dice_ToString(enum Dice_Die die) {
switch (die) {
case NONE: return CLAY_STRING("N/A");
case COIN: return CLAY_STRING("C");
case D4: return CLAY_STRING("4");
case D6: return CLAY_STRING("6");
@ -106,4 +92,21 @@ Clay_String Dice_ToString(enum Dice_Die die) {
case D20: return CLAY_STRING("20");
case D100: return CLAY_STRING("100");
}
return CLAY_STRING("INVALID");
}
Clay_String Dice_ResultToString(Dice_ResultType const &result) {
static char chars[MAX_ROLL_STR_LEN];
Clay_String string = {
.isStaticallyAllocated = false,
.length = 0,
.chars = chars,
};
if (result.die == COIN) {
string.length = SDL_snprintf(chars, MAX_ROLL_STR_LEN, result.roll == 1 ? "H" : "T");
} else {
string.length = SDL_snprintf(chars, MAX_ROLL_STR_LEN, "%d", result.roll);
}
string.chars = chars;
return string;
}

View file

@ -5,6 +5,7 @@
#include <stdlib.h>
enum Dice_Die {
NONE,
COIN = 1,
D4 = 4,
D6 = 6,
@ -20,9 +21,7 @@ enum Dice_Die {
#endif
struct Dice_ResultType {
int roll;
size_t string_len;
char string[MAX_ROLL_STR_LEN];
Clay_String clay_string;
enum Dice_Die die;
};
#ifndef MAX_ACTIVE_DICE
@ -32,15 +31,17 @@ struct Dice_ResultType {
extern int Dice_Roll(enum Dice_Die die);
extern enum Dice_Die const *Dice_GetActiveSet(size_t *out_length);
extern size_t Dice_AddToActiveSet(enum Dice_Die die);
extern size_t Dice_AddToActiveSet(Dice_Die die);
extern void Dice_RemoveFromActiveSet(size_t index);
extern void Dice_ClearActiveSet();
extern Clay_String Dice_RollToString(Dice_ResultType &type);
extern struct Dice_ResultType *Dice_GetLastResult(size_t *out_length);
extern struct Dice_ResultType *Dice_GetLastResultTotal();
extern struct Dice_ResultType Dice_GetLastResultTotal();
extern void Dice_RollActiveSet();
extern Clay_String Dice_ToString(enum Dice_Die die);
extern Clay_String Dice_ResultToString(Dice_ResultType const &result);
#endif // !DICE_H

View file

@ -6,6 +6,7 @@
#include "elements.h"
#include "style.h"
#include "dice.h"
#include "ui_data.h"
static
void HandleRollSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
@ -38,34 +39,31 @@ void AddDieButton(enum Dice_Die die) {
.image = { GetDiceImage(die, Clay_Hovered()) },
}) {
Clay_OnHover(&HandleAddDieButtonInteraction, die);
CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG({
.H(2),
CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG(Header(2, {
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
}));
})));
}
}
static inline
void DiceSelectorContainer() {
CLAY(CLAY_ID("DiceSelector"), {
CLAY(CLAY_ID("DiceSelector"), PanelContainer(0, (Clay_ElementDeclaration) {
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
},
.PANEL(0),
}) {
CLAY_AUTO_ID({
}
})) {
CLAY_AUTO_ID(ListContainer(0, {
.layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.padding = { 2, 2, 5, 5 },
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_TOP },
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
.clip = {
false, true, Clay_GetScrollOffset(),
},
.LISTCONTAINER(0),
}) {
})) {
AddDieButton(D4);
AddDieButton(D6);
AddDieButton(D8);
@ -96,37 +94,32 @@ void RemoveDieButton(enum Dice_Die die, int index) {
}) {
size_t result_length;
struct Dice_ResultType const *result = Dice_GetLastResult(&result_length);
Clay_String string = {
.chars = result[index].string,
.length = result[index].string_len,
.isStaticallyAllocated = true
};
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
CLAY_TEXT(string, CLAY_TEXT_CONFIG({
.H(1),
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
}));
CLAY_TEXT(
UiData_StoreClayStr(Dice_ResultToString(result[index])),
CLAY_TEXT_CONFIG(Header(1, {
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
})));
}
}
static inline
void ActiveDiceContainer() {
CLAY(CLAY_ID("ActiveDice"), {
CLAY(CLAY_ID("ActiveDice"), PanelContainer(0, {
.layout = {
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
.PANEL(0),
}) {
})) {
CLAY(CLAY_ID("ActiveDiceInner"), {
.layout = {
.sizing = { CLAY_SIZING_FIT(), CLAY_SIZING_GROW() },
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.childAlignment = { CLAY_ALIGN_X_LEFT, CLAY_ALIGN_Y_CENTER },
.childGap = 16,
.padding = { 100, 100, 0, 0 },
.childGap = 16,
.childAlignment = { CLAY_ALIGN_X_LEFT, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
.clip = {
true, true, { Clay_GetScrollOffset().x, 0 }
@ -140,17 +133,17 @@ void ActiveDiceContainer() {
}
CLAY(CLAY_ID("ActiveDiceControls"), {
.layout = {
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.childGap = 20,
.padding = { 0, 0, 0, 10 },
.childGap = 20,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
}) {
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
CLAY_TEXT(Dice_GetLastResultTotal()->clay_string, CLAY_TEXT_CONFIG({
.H(3),
Dice_ResultType result = Dice_GetLastResultTotal();
CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString(result)), CLAY_TEXT_CONFIG(Header(1, {
.textColor = TextColors(0),
}));
})));
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
}
}
@ -159,9 +152,9 @@ void ActiveDiceContainer() {
void DiceContainer() {
CLAY(CLAY_ID("DiceContainer"), {
.layout = {
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_PERCENT(0.4) },
.childGap = containerGap
.childGap = containerGap,
.layoutDirection = CLAY_LEFT_TO_RIGHT,
},
}) {
DiceSelectorContainer();

View file

@ -5,18 +5,17 @@ void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intpt
Clay_Color hovered = ToHoveredColor(color);
CLAY_AUTO_ID({
.layout = {
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
.padding = buttonPadding,
.childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER },
},
.cornerRadius = buttonRadii,
.backgroundColor = Clay_Hovered() ? hovered : color,
.border = { ToHoveredColor(Clay_Hovered() ? hovered : color), CLAY_BORDER_ALL(1) }
.cornerRadius = buttonRadii,
.border = { ToHoveredColor(Clay_Hovered() ? hovered : color), CLAY_BORDER_ALL(1) },
}) {
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
.BODY(),
CLAY_TEXT(text, CLAY_TEXT_CONFIG(BodyText({
.textColor = TextColors(0),
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
}));
})));
Clay_OnHover(onHovered, onHoveredData);
}
}

View file

@ -7,6 +7,7 @@
#include "application.h"
#include "defs.h"
#include "style.h"
#include "ui_data.h"
#include "resources.h"
#define SDL_MAIN_HANDLED
@ -36,9 +37,7 @@ uint64_t clayMemorySize = 0;
Clay_Arena clayPrimaryArena;
Clay_SDL3RendererData backendData = {
.renderer = nullptr,
.fonts = nullptr,
.textEngine = nullptr
nullptr, nullptr, nullptr
};
static inline
@ -50,14 +49,14 @@ void LogOutputResolution() {
static
Clay_Dimensions MeasureText(Clay_StringSlice text, Clay_TextElementConfig *config, void *userData) {
TTF_Font **fonts = userData;
TTF_Font **fonts = (TTF_Font**)userData;
TTF_Font *font = fonts[config->fontId];
int width, height;
TTF_SetFontSize(font, config->fontSize);
if (!TTF_GetStringSize(font, text.chars, text.length, &width, &height)) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "MeasureText failed to measure text %s", SDL_GetError());
}
return (Clay_Dimensions) { width, height };
return (Clay_Dimensions) { (float)width, (float)height };
}
static
@ -94,9 +93,9 @@ static
void InitClay() {
clayMemorySize = Clay_MinMemorySize();
clayPrimaryArena = Clay_CreateArenaWithCapacityAndMemory(clayMemorySize, SDL_malloc(clayMemorySize));
Clay_Initialize(clayPrimaryArena, (Clay_Dimensions) { screenWidth, screenHeight }, (Clay_ErrorHandler) { HandleClayErrors });
Clay_Initialize(clayPrimaryArena, { (float)screenWidth, (float)screenHeight }, { HandleClayErrors });
Clay_SetMeasureTextFunction(MeasureText, fonts);
Clay_SetLayoutDimensions((Clay_Dimensions) { screenWidth, screenHeight });
Clay_SetLayoutDimensions({ (float)screenWidth, (float)screenHeight });
float x, y;
SDL_GetMouseState(&x, &y);
Clay_SetPointerState((Clay_Vector2) { x, y }, false);
@ -110,11 +109,7 @@ int main(int argc, char *argv[]) {
LoadResources();
LogOutputResolution();
InitClay();
backendData = (Clay_SDL3RendererData) {
.renderer = renderer,
.fonts = fonts,
.textEngine = textEngine,
};
backendData = (Clay_SDL3RendererData) { renderer, textEngine, fonts };
SDL_Event event;
uint64_t startFrameTime = SDL_GetTicksNS();
double deltaTime = 0.0;
@ -124,6 +119,7 @@ int main(int argc, char *argv[]) {
deltaTime = SDL_GetTicksNS() - startFrameTime;
startFrameTime = SDL_GetTicksNS();
Clay_Vector2 scrollMotion = { 0, 0 };
UiData_Clear();
while (SDL_PollEvent(&event)) {
HandleEvent(event);
switch (event.type) {
@ -131,9 +127,9 @@ int main(int argc, char *argv[]) {
running = false;
break;
case SDL_EVENT_WINDOW_RESIZED:
Clay_SetLayoutDimensions((Clay_Dimensions){
event.window.data1,
event.window.data2
Clay_SetLayoutDimensions({
(float)event.window.data1,
(float)event.window.data2
});
LogOutputResolution();
break;

View file

@ -1,6 +1,9 @@
#ifndef CLAY_RENDERER_SDL3_H
#define CLAY_RENDERER_SDL3_H
#ifdef __cplusplus
extern "C" {
#endif
#include <clay/clay.h>
#include <SDL3_ttf/SDL_ttf.h>
#include <SDL3/SDL_render.h>
@ -12,5 +15,8 @@ typedef struct {
} Clay_SDL3RendererData;
extern void SDL_Clay_RenderClayCommands(Clay_SDL3RendererData *rendererData, Clay_RenderCommandArray *rcommands);
#ifdef __cplusplus
}
#endif
#endif // !CLAY_RENDERER_SDL3_H

View file

@ -79,6 +79,7 @@ void LoadResources() {
SDL_Texture *GetDiceImage(enum Dice_Die die, bool selected) {
switch (die) {
default:
case COIN:
return selected ? diceImagesSelected[COIN_IMAGE] : diceImages[COIN_IMAGE];
case D4:

View file

@ -1,6 +1,26 @@
#include "style.h"
#include "defs.h"
#include "dice.h"
#include <clay/clay.h>
Clay_ElementDeclaration ListContainer(size_t depth, Clay_ElementDeclaration source) {
source.border = (Clay_BorderElementConfig) {
PanelBorder(depth),
CLAY_BORDER_ALL(2)
};
source.cornerRadius = defaultRadiusAll;
return source;
}
Clay_ElementDeclaration PanelContainer(size_t depth, Clay_ElementDeclaration source) {
source.backgroundColor = PanelBackground(depth);
source.border = (Clay_BorderElementConfig) {
PanelBorder(depth),
CLAY_BORDER_OUTSIDE(2)
};
source.cornerRadius = defaultRadiusAll;
return source;
}
Clay_Color PanelBackground(size_t idx) {
return (Clay_Color) {
@ -11,6 +31,18 @@ Clay_Color PanelBackground(size_t idx) {
};
}
Clay_TextElementConfig BodyText(Clay_TextElementConfig base) {
base.fontId = FONT_DEFAULT;
base.fontSize = baseFontSize;
return base;
}
Clay_TextElementConfig Header(size_t header, Clay_TextElementConfig base) {
base.fontId = FONT_BOLD;
base.fontSize = headerSizes[(header)-1];
return base;
}
Clay_Color PanelBorder(size_t idx) {
return (Clay_Color) {
255*panelBorder[idx],
@ -41,10 +73,10 @@ Clay_Color WindowBackground() {
Clay_ElementDeclaration WindowStyle() {
return (Clay_ElementDeclaration) {
.layout = {
.layoutDirection = CLAY_TOP_TO_BOTTOM,
.sizing = layoutExpand,
.padding = CLAY_PADDING_ALL(windowPadding),
.childGap = containerGap,
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
.backgroundColor = WindowBackground()
};

View file

@ -36,14 +36,8 @@ constexpr Clay_Padding panelPadding = {
24, 24,
};
#define LISTCONTAINER(depth_)\
border = { PanelBorder(depth_), CLAY_BORDER_ALL(2) },\
.cornerRadius = defaultRadiusAll
#define PANEL(depth_)\
backgroundColor = PanelBackground(depth_),\
.border = { PanelBorder(depth_), CLAY_BORDER_OUTSIDE(2) },\
.cornerRadius = defaultRadiusAll
extern Clay_ElementDeclaration ListContainer(size_t depth, Clay_ElementDeclaration source);
extern Clay_ElementDeclaration PanelContainer(size_t depth, Clay_ElementDeclaration source);
////////////////////////////////////
// TEXT STYLE
@ -62,13 +56,8 @@ constexpr uint16_t headerSizes[] = {
28, 16
};
#define BODY()\
fontId = FONT_DEFAULT,\
.fontSize = baseFontSize
#define H(level_)\
fontId = FONT_BOLD,\
.fontSize = headerSizes[(level_)-1]
extern Clay_TextElementConfig BodyText(Clay_TextElementConfig base);
extern Clay_TextElementConfig Header(size_t header, Clay_TextElementConfig base);
////////////////////////////////////
// BUTTONS

30
src/ui_data.c Normal file
View file

@ -0,0 +1,30 @@
#include "ui_data.h"
#include <stdint.h>
#include <string.h>
constexpr size_t uiDataLength = UI_DATA_LENGTH;
static size_t utilized = 9;
static char uiDataArena[uiDataLength];
static char *uiDataWriter = uiDataArena;
Clay_String UiData_StoreString(char const *data, size_t amount) {
if (utilized + amount > uiDataLength) {
return CLAY_STRING("out of arena memory");
}
memcpy(uiDataWriter, data, amount);
Clay_String result = {
false, (int32_t)amount, uiDataWriter
};
uiDataWriter += amount;
utilized += amount;
return result;
}
Clay_String UiData_StoreClayStr(Clay_String string) {
return UiData_StoreString(string.chars, string.length);
}
void UiData_Clear() {
utilized = 0;
uiDataWriter = uiDataArena;
}

23
src/ui_data.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef UI_DATA_H
#define UI_DATA_H
#include <clay/clay.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifndef UI_DATA_LENGTH
#define UI_DATA_LENGTH 1024
#endif
extern Clay_String UiData_StoreString(char const *data, size_t amount);
extern Clay_String UiData_StoreClayStr(Clay_String str);
extern void UiData_Clear();
#ifdef __cplusplus
}
#endif
#endif // !UI_DATA_