diff --git a/src/dice.cpp b/src/dice.cpp index 4e81850..5d27380 100644 --- a/src/dice.cpp +++ b/src/dice.cpp @@ -5,9 +5,6 @@ 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 = { - .roll = 0, .die = NONE -}; int Dice_Roll(enum Dice_Die die) { if (die == COIN) { @@ -39,34 +36,26 @@ 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, NONE); return activeDiceCount++; } void Dice_RemoveFromActiveSet(size_t index) { if (index >= MAX_ACTIVE_DICE) { return; + } else for (size_t i = index; i < activeDiceCount; ++i) { + rollResult[i] = rollResult[i+1]; } - rollTotal.roll -= rollResult[index].roll; - 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, NONE); 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, NONE); } struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) { @@ -76,8 +65,12 @@ struct Dice_ResultType *Dice_GetLastResult(size_t *out_length) { return rollResult; } -struct Dice_ResultType Dice_GetLastResultTotal() { - return rollTotal; +int Dice_GetLastResultTotal() { + int result = 0; + for (size_t i = 0; i < activeDiceCount; ++i) { + result += rollResult[i].roll; + } + return result; } Clay_String Dice_ToString(enum Dice_Die die) { @@ -98,9 +91,7 @@ Clay_String Dice_ToString(enum Dice_Die die) { Clay_String Dice_ResultToString(Dice_ResultType const &result) { static char chars[MAX_ROLL_STR_LEN]; Clay_String string = { - .isStaticallyAllocated = false, - .length = 0, - .chars = chars, + false, 0, chars, }; if (result.die == COIN) { string.length = SDL_snprintf(chars, MAX_ROLL_STR_LEN, result.roll == 1 ? "H" : "T"); diff --git a/src/dice.h b/src/dice.h index ae6ba0b..7be7d51 100644 --- a/src/dice.h +++ b/src/dice.h @@ -37,7 +37,7 @@ 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 int Dice_GetLastResultTotal(); extern void Dice_RollActiveSet(); diff --git a/src/dice_container.cpp b/src/dice_container.cpp index 8ab96d7..2131f6b 100644 --- a/src/dice_container.cpp +++ b/src/dice_container.cpp @@ -79,28 +79,32 @@ void DiceSelectorContainer() { static void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) { if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) { - Dice_RemoveFromActiveSet(index); + Dice_RemoveFromActiveSet((size_t)index); } } static inline -void RemoveDieButton(enum Dice_Die die, int index) { +void RemoveDieButton(enum Dice_Die die, size_t index) { CLAY(CLAY_IDI("RemoveDieButton", index), { .layout = { .sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) }, .childAlignment = { CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER }, }, - .image = { GetDiceImage(die, Clay_Hovered()) }, + .image = { + GetDiceImage(die, Clay_Hovered()) + }, }) { + Clay_OnHover(&HandleRemoveDieButtonInteraction, (intptr_t)index); size_t result_length; struct Dice_ResultType const *result = Dice_GetLastResult(&result_length); - Clay_OnHover(&HandleRemoveDieButtonInteraction, index); CLAY_TEXT( UiData_StoreClayStr(Dice_ResultToString(result[index])), CLAY_TEXT_CONFIG(Header(1, { - .textColor = TextColors(0), - .textAlignment = CLAY_TEXT_ALIGN_CENTER, - }))); + .textColor = TextColors(0), + .textAlignment = CLAY_TEXT_ALIGN_CENTER, + } + )) + ); } } @@ -140,10 +144,12 @@ void ActiveDiceContainer() { }, }) { TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0); - Dice_ResultType result = Dice_GetLastResultTotal(); - CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString(result)), CLAY_TEXT_CONFIG(Header(1, { - .textColor = TextColors(0), - }))); + int result = Dice_GetLastResultTotal(); + CLAY_TEXT(UiData_StoreClayStr(Dice_ResultToString({ result, NONE })), + CLAY_TEXT_CONFIG(Header(1, { + .textColor = TextColors(0), + })) + ); TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0); } }