feat: renamed Die namespace to Dice
This commit is contained in:
parent
420f4e5244
commit
9dc64ba962
86
src/dice.c
86
src/dice.c
|
|
@ -1,22 +1,27 @@
|
||||||
#include "dice.h"
|
#include "dice.h"
|
||||||
#include <memory.h>
|
#include <memory.h>
|
||||||
|
|
||||||
int Die_Roll(enum Die_Dice die) {
|
int Dice_Roll(enum Dice_Die die) {
|
||||||
|
if (die == COIN) {
|
||||||
|
return (rand() % 2);
|
||||||
|
} else {
|
||||||
int const max = die;
|
int const max = die;
|
||||||
return (rand() % max) + 1;
|
return (rand() % max) + 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static int current_active_count = 0;
|
static int activeDiceCount = 0;
|
||||||
static enum Die_Dice active_dice_set[MAX_ACTIVE_DICE];
|
static enum Dice_Die activeDice[MAX_ACTIVE_DICE];
|
||||||
|
|
||||||
static struct Die_ResultType roll_results[MAX_ACTIVE_DICE];
|
static struct Dice_ResultType rollResult[MAX_ACTIVE_DICE];
|
||||||
static struct Die_ResultType roll_total = {
|
static struct Dice_ResultType rollTotal = {
|
||||||
.roll = 0, .string_len = 0
|
.string = "0",
|
||||||
|
.roll = 0, .string_len = 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
static
|
static
|
||||||
struct Die_ResultType Die_RollToResultType(int roll, enum Die_Dice die) {
|
struct Dice_ResultType Dice_RollToResultType(int roll, enum Dice_Die die) {
|
||||||
struct Die_ResultType result = { };
|
struct Dice_ResultType result = { };
|
||||||
result.roll = roll;
|
result.roll = roll;
|
||||||
if (die == COIN) {
|
if (die == COIN) {
|
||||||
result.string_len = SDL_snprintf(result.string, MAX_ROLL_STR_LEN, roll == 1 ? "H" : "T");
|
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;
|
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) {
|
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) {
|
size_t Dice_AddToActiveSet(enum Dice_Die die) {
|
||||||
if (current_active_count >= MAX_ACTIVE_DICE) {
|
if (activeDiceCount >= MAX_ACTIVE_DICE) {
|
||||||
return MAX_ACTIVE_DICE;
|
return MAX_ACTIVE_DICE;
|
||||||
}
|
}
|
||||||
active_dice_set[current_active_count] = die;
|
activeDice[activeDiceCount] = die;
|
||||||
roll_results[current_active_count] = Die_RollToResultType(die, die);
|
rollResult[activeDiceCount] = Dice_RollToResultType(die, die);
|
||||||
return current_active_count++;
|
rollTotal.roll += die;
|
||||||
|
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
|
||||||
|
return activeDiceCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Die_RemoveFromActiveSet(size_t index) {
|
void Dice_RemoveFromActiveSet(size_t index) {
|
||||||
memcpy(active_dice_set + index, active_dice_set + index + 1, MAX_ACTIVE_DICE - index - 1);
|
if (index >= MAX_ACTIVE_DICE) {
|
||||||
--current_active_count;
|
return;
|
||||||
|
}
|
||||||
|
rollTotal.roll -= rollResult[index].roll;
|
||||||
|
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
|
||||||
|
memcpy(activeDice + index, activeDice + index + 1, MAX_ACTIVE_DICE - index - 1);
|
||||||
|
--activeDiceCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Die_RollActiveSet() {
|
void Dice_ClearActiveSet() {
|
||||||
for (size_t i = 0; i < current_active_count; ++i) {
|
rollTotal.roll = 0;
|
||||||
roll_results[i] = Die_RollToResultType(Die_Roll(active_dice_set[i]), active_dice_set[i]);
|
rollTotal = Dice_RollToResultType(rollTotal.roll, 0);
|
||||||
roll_total.roll += roll_results[i].roll;
|
activeDiceCount = 0;
|
||||||
}
|
|
||||||
roll_total.string_len = SDL_snprintf(roll_total.string, MAX_ROLL_STR_LEN, "%d", roll_total.roll);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Die_ResultType *Die_GetLastResult(size_t *out_length) {
|
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) {
|
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) {
|
switch (die) {
|
||||||
case COIN: return CLAY_STRING("C");
|
case COIN: return CLAY_STRING("C");
|
||||||
case D4: return CLAY_STRING("4");
|
case D4: return CLAY_STRING("4");
|
||||||
|
|
|
||||||
22
src/dice.h
22
src/dice.h
|
|
@ -12,8 +12,8 @@
|
||||||
#define MAX_ROLL_STR_LEN 10
|
#define MAX_ROLL_STR_LEN 10
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
enum Die_Dice {
|
enum Dice_Die {
|
||||||
COIN = 2,
|
COIN = 1,
|
||||||
D4 = 4,
|
D4 = 4,
|
||||||
D6 = 6,
|
D6 = 6,
|
||||||
D8 = 8,
|
D8 = 8,
|
||||||
|
|
@ -23,23 +23,25 @@ enum Die_Dice {
|
||||||
D100 = 100
|
D100 = 100
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Die_ResultType {
|
struct Dice_ResultType {
|
||||||
int roll;
|
int roll;
|
||||||
size_t string_len;
|
size_t string_len;
|
||||||
char string[MAX_ROLL_STR_LEN];
|
char string[MAX_ROLL_STR_LEN];
|
||||||
Clay_String clay_string;
|
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 enum Dice_Die const *Dice_GetActiveSet(size_t *out_length);
|
||||||
extern size_t Die_AddToActiveSet(enum Die_Dice die);
|
extern size_t Dice_AddToActiveSet(enum Dice_Die die);
|
||||||
extern void Die_RemoveFromActiveSet(size_t index);
|
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
|
#endif // !DICE_H
|
||||||
|
|
|
||||||
|
|
@ -10,30 +10,26 @@
|
||||||
static
|
static
|
||||||
void HandleRollSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
|
void HandleRollSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
|
||||||
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
||||||
Die_RollActiveSet();
|
Dice_RollActiveSet();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static
|
||||||
void RollSetButton() {
|
void HandleClearSetButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
|
||||||
CLAY(CLAY_ID("RollSetButton"), {
|
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
||||||
.layout = {
|
Dice_ClearActiveSet();
|
||||||
.padding = { 30, 30, 10, 10 },
|
|
||||||
}
|
|
||||||
}) {
|
|
||||||
TextButton(CLAY_STRING("Roll"), buttonBackground, &HandleRollSetButtonInteraction, 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) {
|
void HandleAddDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t die) {
|
||||||
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
||||||
Die_AddToActiveSet((enum Die_Dice)die);
|
Dice_AddToActiveSet((enum Dice_Die)die);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
void AddDieButton(enum Die_Dice die) {
|
void AddDieButton(enum Dice_Die die) {
|
||||||
CLAY(CLAY_IDI("AddDieButton", die), {
|
CLAY(CLAY_IDI("AddDieButton", die), {
|
||||||
.layout = {
|
.layout = {
|
||||||
.sizing = { CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100) },
|
.sizing = { CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100) },
|
||||||
|
|
@ -42,9 +38,10 @@ void AddDieButton(enum Die_Dice die) {
|
||||||
.image = { GetDiceImage(die) }
|
.image = { GetDiceImage(die) }
|
||||||
}) {
|
}) {
|
||||||
Clay_OnHover(&HandleAddDieButtonInteraction, die);
|
Clay_OnHover(&HandleAddDieButtonInteraction, die);
|
||||||
CLAY_TEXT(Die_ToString(die), CLAY_TEXT_CONFIG({
|
CLAY_TEXT(Dice_ToString(die), CLAY_TEXT_CONFIG({
|
||||||
.textColor = TextColors(0),
|
.textColor = TextColors(0),
|
||||||
.H(2),
|
.H(2),
|
||||||
|
.textAlignment = CLAY_TEXT_ALIGN_CENTER
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -67,7 +64,7 @@ void DiceSelectorContainer() {
|
||||||
.padding = panelPadding,
|
.padding = panelPadding,
|
||||||
},
|
},
|
||||||
.clip = {
|
.clip = {
|
||||||
false, true, Clay_GetScrollOffset(),
|
true, true, Clay_GetScrollOffset(),
|
||||||
},
|
},
|
||||||
}) {
|
}) {
|
||||||
AddDieButton(COIN);
|
AddDieButton(COIN);
|
||||||
|
|
@ -85,12 +82,12 @@ void DiceSelectorContainer() {
|
||||||
static
|
static
|
||||||
void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) {
|
void HandleRemoveDieButtonInteraction(Clay_ElementId element, Clay_PointerData pointer, intptr_t index) {
|
||||||
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
if (pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
|
||||||
Die_RemoveFromActiveSet(index);
|
Dice_RemoveFromActiveSet(index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline
|
static inline
|
||||||
void RemoveDieButton(enum Die_Dice die, int index) {
|
void RemoveDieButton(enum Dice_Die die, int index) {
|
||||||
CLAY(CLAY_IDI("RemoveDieButton", index), {
|
CLAY(CLAY_IDI("RemoveDieButton", index), {
|
||||||
.layout = {
|
.layout = {
|
||||||
.sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) },
|
.sizing = { CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200) },
|
||||||
|
|
@ -99,7 +96,7 @@ void RemoveDieButton(enum Die_Dice die, int index) {
|
||||||
.image = { GetDiceImage(die) },
|
.image = { GetDiceImage(die) },
|
||||||
}) {
|
}) {
|
||||||
size_t result_length;
|
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 = {
|
Clay_String string = {
|
||||||
.chars = result[index].string,
|
.chars = result[index].string,
|
||||||
.length = result[index].string_len,
|
.length = result[index].string_len,
|
||||||
|
|
@ -108,7 +105,8 @@ void RemoveDieButton(enum Die_Dice die, int index) {
|
||||||
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
|
Clay_OnHover(&HandleRemoveDieButtonInteraction, index);
|
||||||
CLAY_TEXT(string, CLAY_TEXT_CONFIG({
|
CLAY_TEXT(string, CLAY_TEXT_CONFIG({
|
||||||
.H(1),
|
.H(1),
|
||||||
.textColor = TextColors(0)
|
.textColor = TextColors(0),
|
||||||
|
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -137,12 +135,30 @@ void ActiveDiceContainer() {
|
||||||
},
|
},
|
||||||
}) {
|
}) {
|
||||||
size_t dice_count = 0;
|
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) {
|
for (size_t i = 0; i < dice_count; ++i) {
|
||||||
RemoveDieButton(dice[i], 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intpt
|
||||||
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
|
CLAY_TEXT(text, CLAY_TEXT_CONFIG({
|
||||||
.BODY(),
|
.BODY(),
|
||||||
.textColor = TextColors(0),
|
.textColor = TextColors(0),
|
||||||
|
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
|
||||||
}));
|
}));
|
||||||
Clay_OnHover(onHovered, onHoveredData);
|
Clay_OnHover(onHovered, onHoveredData);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -67,7 +67,7 @@ void LoadResources() {
|
||||||
LoadDiceImages();
|
LoadDiceImages();
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_Texture *GetDiceImage(enum Die_Dice die) {
|
SDL_Texture *GetDiceImage(enum Dice_Die die) {
|
||||||
switch (die) {
|
switch (die) {
|
||||||
case COIN:
|
case COIN:
|
||||||
return diceImages[COIN_IMAGE];
|
return diceImages[COIN_IMAGE];
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,6 @@ extern SDL_Texture *diceImages[DICE_IMAGE_MAX];
|
||||||
|
|
||||||
extern void LoadResources();
|
extern void LoadResources();
|
||||||
|
|
||||||
extern SDL_Texture *GetDiceImage(enum Die_Dice die);
|
extern SDL_Texture *GetDiceImage(enum Dice_Die die);
|
||||||
|
|
||||||
#endif // !RESOURCES_H
|
#endif // !RESOURCES_H
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ Clay_ElementDeclaration WindowStyle() {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Clay_Color DieColor(enum Die_Dice die) {
|
Clay_Color DieColor(enum Dice_Die die) {
|
||||||
switch(die) {
|
switch(die) {
|
||||||
case COIN: return dieColors[0];
|
case COIN: return dieColors[0];
|
||||||
case D4: return dieColors[1];
|
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);
|
return selected ? ToHoveredColor(DieColor(die)) : DieColor(die);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,8 @@ extern Clay_Color PanelBackground(size_t idx);
|
||||||
extern Clay_Color TextColors(size_t idx);
|
extern Clay_Color TextColors(size_t idx);
|
||||||
extern Clay_Color WindowBackground();
|
extern Clay_Color WindowBackground();
|
||||||
extern Clay_ElementDeclaration WindowStyle();
|
extern Clay_ElementDeclaration WindowStyle();
|
||||||
extern Clay_Color DieColor(enum Die_Dice die);
|
extern Clay_Color DieColor(enum Dice_Die die);
|
||||||
extern Clay_Color DieButtonColor(enum Die_Dice die, bool selected);
|
extern Clay_Color DieButtonColor(enum Dice_Die die, bool selected);
|
||||||
extern Clay_Color ToHoveredColor(Clay_Color color);
|
extern Clay_Color ToHoveredColor(Clay_Color color);
|
||||||
|
|
||||||
#endif // !STYLE_H
|
#endif // !STYLE_H
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue