From 9dc64ba962200f17658b0c367a0a4a32f00946c4 Mon Sep 17 00:00:00 2001 From: Sara Date: Thu, 18 Sep 2025 14:57:05 +0200 Subject: [PATCH] feat: renamed Die namespace to Dice --- src/dice.c | 92 +++++++++++++++++++++++++++++--------------- src/dice.h | 22 ++++++----- src/dice_container.c | 54 +++++++++++++++++--------- src/elements.c | 1 + src/resources.c | 2 +- src/resources.h | 2 +- src/style.c | 4 +- src/style.h | 4 +- 8 files changed, 114 insertions(+), 67 deletions(-) diff --git a/src/dice.c b/src/dice.c index 77c4607..6674647 100644 --- a/src/dice.c +++ b/src/dice.c @@ -1,22 +1,27 @@ #include "dice.h" #include -int Die_Roll(enum Die_Dice die) { - int const max = die; - return (rand() % max) + 1; +int Dice_Roll(enum Dice_Die die) { + if (die == COIN) { + return (rand() % 2); + } else { + int const max = die; + return (rand() % max) + 1; + } } -static int current_active_count = 0; -static enum Die_Dice active_dice_set[MAX_ACTIVE_DICE]; +static int activeDiceCount = 0; +static enum Dice_Die activeDice[MAX_ACTIVE_DICE]; -static struct Die_ResultType roll_results[MAX_ACTIVE_DICE]; -static struct Die_ResultType roll_total = { - .roll = 0, .string_len = 0 +static struct Dice_ResultType rollResult[MAX_ACTIVE_DICE]; +static struct Dice_ResultType rollTotal = { + .string = "0", + .roll = 0, .string_len = 1, }; static -struct Die_ResultType Die_RollToResultType(int roll, enum Die_Dice die) { - struct Die_ResultType result = { }; +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"); @@ -31,43 +36,66 @@ struct Die_ResultType Die_RollToResultType(int roll, enum Die_Dice die) { return result; } -enum Die_Dice const *Die_GetActiveSet(size_t *out_length) { +enum Dice_Die const *Dice_GetActiveSet(size_t *out_length) { if (out_length != nullptr) { - *out_length = current_active_count; + *out_length = activeDiceCount; } - return active_dice_set; + return activeDice; } -size_t Die_AddToActiveSet(enum Die_Dice die) { - if (current_active_count >= MAX_ACTIVE_DICE) { +size_t Dice_AddToActiveSet(enum Dice_Die die) { + if (activeDiceCount >= MAX_ACTIVE_DICE) { return MAX_ACTIVE_DICE; } - active_dice_set[current_active_count] = die; - roll_results[current_active_count] = Die_RollToResultType(die, die); - return current_active_count++; + activeDice[activeDiceCount] = die; + rollResult[activeDiceCount] = Dice_RollToResultType(die, die); + rollTotal.roll += die; + rollTotal = Dice_RollToResultType(rollTotal.roll, 0); + return activeDiceCount++; } -void Die_RemoveFromActiveSet(size_t index) { - memcpy(active_dice_set + index, active_dice_set + index + 1, MAX_ACTIVE_DICE - index - 1); - --current_active_count; -} - -void Die_RollActiveSet() { - for (size_t i = 0; i < current_active_count; ++i) { - roll_results[i] = Die_RollToResultType(Die_Roll(active_dice_set[i]), active_dice_set[i]); - roll_total.roll += roll_results[i].roll; +void Dice_RemoveFromActiveSet(size_t index) { + if (index >= MAX_ACTIVE_DICE) { + return; } - roll_total.string_len = SDL_snprintf(roll_total.string, MAX_ROLL_STR_LEN, "%d", roll_total.roll); + rollTotal.roll -= rollResult[index].roll; + rollTotal = Dice_RollToResultType(rollTotal.roll, 0); + memcpy(activeDice + index, activeDice + index + 1, MAX_ACTIVE_DICE - index - 1); + --activeDiceCount; } -struct Die_ResultType *Die_GetLastResult(size_t *out_length) { +void Dice_ClearActiveSet() { + rollTotal.roll = 0; + rollTotal = Dice_RollToResultType(rollTotal.roll, 0); + activeDiceCount = 0; +} + +void Dice_RollActiveSet() { + int total = 0; + for (size_t i = 0; i < activeDiceCount; ++i) { + rollResult[i] = Dice_RollToResultType(Dice_Roll(activeDice[i]), activeDice[i]); + total += rollResult[i].roll; + } + rollTotal = Dice_RollToResultType(total, 0); +} + +struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) { if (out_length != nullptr) { - *out_length = current_active_count; + *out_length = activeDiceCount; } - return roll_results; + return rollResult; } -Clay_String Die_ToString(enum Die_Dice die) { +struct Dice_ResultType *Dice_GetLastResultTotal() { + rollTotal.clay_string = (Clay_String) { + .chars = rollTotal.string, + .length = rollTotal.string_len, + .isStaticallyAllocated = false + }; + return &rollTotal; +} + +Clay_String Dice_ToString(enum Dice_Die die) { switch (die) { case COIN: return CLAY_STRING("C"); case D4: return CLAY_STRING("4"); diff --git a/src/dice.h b/src/dice.h index fc2874f..dd01437 100644 --- a/src/dice.h +++ b/src/dice.h @@ -12,8 +12,8 @@ #define MAX_ROLL_STR_LEN 10 #endif -enum Die_Dice { - COIN = 2, +enum Dice_Die { + COIN = 1, D4 = 4, D6 = 6, D8 = 8, @@ -23,23 +23,25 @@ enum Die_Dice { D100 = 100 }; -struct Die_ResultType { +struct Dice_ResultType { int roll; size_t string_len; char string[MAX_ROLL_STR_LEN]; Clay_String clay_string; }; -extern int Die_Roll(enum Die_Dice die); +extern int Dice_Roll(enum Dice_Die die); -extern enum Die_Dice const *Die_GetActiveSet(size_t *out_length); -extern size_t Die_AddToActiveSet(enum Die_Dice die); -extern void Die_RemoveFromActiveSet(size_t index); +extern enum Dice_Die const *Dice_GetActiveSet(size_t *out_length); +extern size_t Dice_AddToActiveSet(enum Dice_Die die); +extern void Dice_RemoveFromActiveSet(size_t index); +extern void Dice_ClearActiveSet(); -extern struct Die_ResultType *Die_GetLastResult(size_t *out_length); +extern struct Dice_ResultType *Dice_GetLastResult(size_t *out_length); +extern struct Dice_ResultType *Dice_GetLastResultTotal(); -extern void Die_RollActiveSet(); +extern void Dice_RollActiveSet(); -extern Clay_String Die_ToString(enum Die_Dice die); +extern Clay_String Dice_ToString(enum Dice_Die die); #endif // !DICE_H diff --git a/src/dice_container.c b/src/dice_container.c index 64917ba..90e66b2 100644 --- a/src/dice_container.c +++ b/src/dice_container.c @@ -10,30 +10,26 @@ static void HandleRollSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) { if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { - Die_RollActiveSet(); + Dice_RollActiveSet(); } } -static inline -void RollSetButton() { - CLAY(CLAY_ID("RollSetButton"), { - .layout = { - .padding = { 30, 30, 10, 10 }, - } - }) { - TextButton(CLAY_STRING("Roll"), buttonBackground, &HandleRollSetButtonInteraction, 0); +static +void HandleClearSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) { + if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { + Dice_ClearActiveSet(); } } static void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) { if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { - Die_AddToActiveSet((enum Die_Dice)die); + Dice_AddToActiveSet((enum Dice_Die)die); } } static inline -void AddDieButton(enum Die_Dice die) { +void AddDieButton(enum Dice_Die die) { CLAY(CLAY_IDI("AddDieButton", die), { .layout = { .sizing = { CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100) }, @@ -42,9 +38,10 @@ void AddDieButton(enum Die_Dice die) { .image = { GetDiceImage(die) } }) { Clay_OnHover(&HandleAddDieButtonInteraction, die); - CLAY_TEXT(Die_ToString(die), CLAY_TEXT_CONFIG({ + CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG({ .textColor = TextColors(0), .H(2), + .textAlignment = CLAY_TEXT_ALIGN_CENTER })); } } @@ -67,7 +64,7 @@ void DiceSelectorContainer() { .padding = panelPadding, }, .clip = { - false, true, Clay_GetScrollOffset(), + true, true, Clay_GetScrollOffset(), }, }) { AddDieButton(COIN); @@ -85,12 +82,12 @@ void DiceSelectorContainer() { static void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) { if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { - Die_RemoveFromActiveSet(index); + Dice_RemoveFromActiveSet(index); } } static inline -void RemoveDieButton(enum Die_Dice die, int index) { +void RemoveDieButton(enum Dice_Die die, int index) { CLAY(CLAY_IDI("RemoveDieButton", index), { .layout = { .sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) }, @@ -99,7 +96,7 @@ void RemoveDieButton(enum Die_Dice die, int index) { .image = { GetDiceImage(die) }, }) { size_t result_length; - struct Die_ResultType const *result = Die_GetLastResult(&result_length); + struct Dice_ResultType const *result = Dice_GetLastResult(&result_length); Clay_String string = { .chars = result[index].string, .length = result[index].string_len, @@ -108,7 +105,8 @@ void RemoveDieButton(enum Die_Dice die, int index) { Clay_OnHover(&HandleRemoveDieButtonInteraction, index); CLAY_TEXT(string, CLAY_TEXT_CONFIG({ .H(1), - .textColor = TextColors(0) + .textColor = TextColors(0), + .textAlignment = CLAY_TEXT_ALIGN_CENTER, })); } } @@ -137,12 +135,30 @@ void ActiveDiceContainer() { }, }) { size_t dice_count = 0; - enum Die_Dice const *dice = Die_GetActiveSet(&dice_count); + enum Dice_Die const *dice = Dice_GetActiveSet(&dice_count); for (size_t i = 0; i < dice_count; ++i) { RemoveDieButton(dice[i], i); } } - RollSetButton(); + 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 }, + }, + }) { + CLAY_AUTO_ID({ + .layout.padding = { 10, 10, 10, 10 } + }) { + CLAY_TEXT(Dice_GetLastResultTotal()->clay_string, CLAY_TEXT_CONFIG({ + .BODY(), + .textColor = TextColors(0), + })); + } + TextButton(CLAY_STRING("Roll"), buttonBackground, &HandleRollSetButtonInteraction, 0); + TextButton(CLAY_STRING("Clear"), buttonBackground, &HandleClearSetButtonInteraction, 0); + } } } diff --git a/src/elements.c b/src/elements.c index 190b6fe..91819c1 100644 --- a/src/elements.c +++ b/src/elements.c @@ -13,6 +13,7 @@ void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intpt CLAY_TEXT(text, CLAY_TEXT_CONFIG({ .BODY(), .textColor = TextColors(0), + .textAlignment = CLAY_TEXT_ALIGN_CENTER, })); Clay_OnHover(onHovered, onHoveredData); } diff --git a/src/resources.c b/src/resources.c index 335e8e5..02d2249 100644 --- a/src/resources.c +++ b/src/resources.c @@ -67,7 +67,7 @@ void LoadResources() { LoadDiceImages(); } -SDL_Texture *GetDiceImage(enum Die_Dice die) { +SDL_Texture *GetDiceImage(enum Dice_Die die) { switch (die) { case COIN: return diceImages[COIN_IMAGE]; diff --git a/src/resources.h b/src/resources.h index 061e0c0..3935e39 100644 --- a/src/resources.h +++ b/src/resources.h @@ -29,6 +29,6 @@ extern SDL_Texture *diceImages[DICE_IMAGE_MAX]; extern void LoadResources(); -extern SDL_Texture *GetDiceImage(enum Die_Dice die); +extern SDL_Texture *GetDiceImage(enum Dice_Die die); #endif // !RESOURCES_H diff --git a/src/style.c b/src/style.c index f300e9b..f357c10 100644 --- a/src/style.c +++ b/src/style.c @@ -45,7 +45,7 @@ Clay_ElementDeclaration WindowStyle() { }; } -Clay_Color DieColor(enum Die_Dice die) { +Clay_Color DieColor(enum Dice_Die die) { switch(die) { case COIN: return dieColors[0]; case D4: return dieColors[1]; @@ -59,7 +59,7 @@ Clay_Color DieColor(enum Die_Dice die) { } } -Clay_Color DieButtonColor(enum Die_Dice die, bool selected) { +Clay_Color DieButtonColor(enum Dice_Die die, bool selected) { return selected ? ToHoveredColor(DieColor(die)) : DieColor(die); } diff --git a/src/style.h b/src/style.h index ee01d42..8d791d2 100644 --- a/src/style.h +++ b/src/style.h @@ -112,8 +112,8 @@ extern Clay_Color PanelBackground(size_t idx); extern Clay_Color TextColors(size_t idx); extern Clay_Color WindowBackground(); extern Clay_ElementDeclaration WindowStyle(); -extern Clay_Color DieColor(enum Die_Dice die); -extern Clay_Color DieButtonColor(enum Die_Dice die, bool selected); +extern Clay_Color DieColor(enum Dice_Die die); +extern Clay_Color DieButtonColor(enum Dice_Die die, bool selected); extern Clay_Color ToHoveredColor(Clay_Color color); #endif // !STYLE_H