Compare commits

...

9 commits

16 changed files with 153 additions and 103 deletions

View file

@ -12,6 +12,9 @@
xml:space="preserve"
inkscape:export-batch-path="../assets/icons"
inkscape:export-batch-name=""
inkscape:export-filename="dice_icons.svg"
inkscape:export-xdpi="96"
inkscape:export-ydpi="96"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
@ -25,9 +28,9 @@
inkscape:pagecheckerboard="true"
inkscape:deskcolor="#d1d1d1"
inkscape:document-units="mm"
inkscape:zoom="0.089092089"
inkscape:cx="2575.9863"
inkscape:cy="-836.21342"
inkscape:zoom="0.28641836"
inkscape:cx="2646.4784"
inkscape:cy="677.33087"
inkscape:current-layer="layer1"><inkscape:page
x="0"
y="0"
@ -102,9 +105,9 @@
sodipodi:arg2="-1.0471975"
inkscape:rounded="0"
inkscape:randomized="0"
d="m 72.788858,-0.84280396 58.211902,33.60865896 0,67.217316 -58.211905,33.608659 -58.211902,-33.608662 1e-6,-67.217315 z"
d="m 72.788858,-0.84280396 58.211902,33.60865896 0,67.217316 -58.211905,33.608659 -58.211902,-33.608662 1e-6,-67.217316 z"
inkscape:transform-center-y="-10.356297"
transform="matrix(0.72805306,0,0,0.72805306,887.53914,23.277513)"
transform="matrix(0.72805306,0,0,0.72805306,887.53916,19.409163)"
inkscape:transform-center-x="4.5682712e-06" /><path
sodipodi:type="star"
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
@ -121,12 +124,12 @@
inkscape:randomized="0"
d="M 72.788858,-0.84280396 140.00617,66.374514 72.788855,133.59183 5.5715408,66.37451 Z"
inkscape:transform-center-y="-11.404593"
transform="matrix(0.80174873,0,0,0.80174873,445.77494,17.337703)"
transform="matrix(0.80174873,0,0,0.80174873,445.77495,14.517649)"
inkscape:transform-center-x="3.2442312e-06" /><path
id="path6"
style="fill:#ffffff;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
inkscape:transform-center-y="-10.700688"
d="m 649.59996,20.692099 52.86152,35.81815 V 88.851767 L 649.59996,121.82248 596.73844,88.851765 V 56.510248 Z"
inkscape:transform-center-y="-11.822122"
d="m 649.59996,11.868888 49.16646,39.571907 V 87.171717 L 649.59996,123.59777 600.4335,87.171714 V 51.440793 Z"
sodipodi:nodetypes="ccccccc"
inkscape:transform-center-x="-3.5000013e-06" /><path
sodipodi:type="star"
@ -144,7 +147,7 @@
inkscape:randomized="0"
d="M 72.788858,-0.84280396 136.71632,45.603221 112.2982,120.75446 l -79.018692,0 L 8.861391,45.603217 Z"
inkscape:transform-center-y="-10.963325"
transform="matrix(0.77072736,0,0,0.77072736,738.96626,19.478952)"
transform="matrix(0.77072736,0,0,0.77072736,738.96626,16.635767)"
inkscape:transform-center-x="2.911147e-06" /><path
sodipodi:type="star"
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
@ -160,9 +163,9 @@
inkscape:rounded="0"
inkscape:randomized="0"
d="M 72.788858,-0.84280396 131.00076,99.983171 l -116.423807,-3e-6 z"
inkscape:transform-center-y="13.37152"
transform="matrix(-0.94002452,0,0,-0.94002452,281.6233,129.60578)"
inkscape:transform-center-x="-3.5441139e-06" /><rect
inkscape:transform-center-y="-20.021908"
transform="matrix(0.94002452,0,0,0.94002452,144.77667,10.368405)"
inkscape:transform-center-x="8.3514896e-06" /><rect
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="rect8"
width="85.237946"

Before

Width:  |  Height:  |  Size: 6.1 KiB

After

Width:  |  Height:  |  Size: 6.2 KiB

View file

@ -15,4 +15,4 @@
transform="translate(-581.86667)"><path
id="path6"
style="fill:#ffffff;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
d="M 649.59996,5.3149838 715.72918,50.123127 V 90.582038 L 649.59996,131.82806 583.47073,90.582036 V 50.123125 Z" /></g></svg>
d="m 649.59996,11.868888 49.16646,39.571907 V 87.171717 L 649.59996,123.59777 600.4335,87.171714 V 51.440793 Z" /></g></svg>

Before

Width:  |  Height:  |  Size: 664 B

After

Width:  |  Height:  |  Size: 662 B

View file

@ -16,4 +16,4 @@
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="path7"
d="M 72.788858,-0.84280396 136.71632,45.603221 112.2982,120.75446 l -79.018692,0 L 8.861391,45.603217 Z"
transform="matrix(0.9748178,0,0,0.9748178,724.11075,4.3394261)" /></g></svg>
transform="matrix(0.77072736,0,0,0.77072736,738.96626,16.635767)" /></g></svg>

Before

Width:  |  Height:  |  Size: 741 B

After

Width:  |  Height:  |  Size: 743 B

View file

@ -16,4 +16,4 @@
id="path9"
cx="67.73333"
cy="67.73333"
r="60.523819" /></g></svg>
r="49.017395" /></g></svg>

Before

Width:  |  Height:  |  Size: 584 B

After

Width:  |  Height:  |  Size: 584 B

View file

@ -16,4 +16,4 @@
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="path1"
d="m 72.788858,-0.84280396 58.211902,33.60865896 0,67.217316 -58.211905,33.608659 -58.211902,-33.608662 1e-6,-67.217316 z"
transform="matrix(0.99238809,0,0,0.99238809,868.2985,1.9723317)" /></g></svg>
transform="matrix(0.72805306,0,0,0.72805306,887.53916,19.409163)" /></g></svg>

Before

Width:  |  Height:  |  Size: 760 B

After

Width:  |  Height:  |  Size: 761 B

View file

@ -16,4 +16,4 @@
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="path8"
d="M 72.788858,-0.84280396 131.00076,99.983171 l -116.423807,-3e-6 z"
transform="matrix(-1.1132862,0,0,-1.1132862,294.23482,132.96171)" /></g></svg>
transform="matrix(0.94002452,0,0,0.94002452,144.77667,10.368405)" /></g></svg>

Before

Width:  |  Height:  |  Size: 708 B

After

Width:  |  Height:  |  Size: 708 B

View file

@ -15,8 +15,8 @@
transform="translate(-290.93332)"><rect
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="rect8"
width="110.25191"
height="110.25191"
x="303.54068"
y="12.607375"
width="85.237946"
height="85.237946"
x="316.04767"
y="25.114355"
ry="0" /></g></svg>

Before

Width:  |  Height:  |  Size: 665 B

After

Width:  |  Height:  |  Size: 665 B

View file

@ -16,4 +16,4 @@
style="fill:#ffffff;fill-opacity:1;stroke-width:0;stroke-linecap:round;stroke-linejoin:round"
id="path5"
d="M 72.788858,-0.84280396 140.00617,66.374514 72.788855,133.59183 5.5715408,66.37451 Z"
transform="translate(431.34446,1.3588174)" /></g></svg>
transform="matrix(0.80174873,0,0,0.80174873,445.77495,14.517649)" /></g></svg>

Before

Width:  |  Height:  |  Size: 704 B

After

Width:  |  Height:  |  Size: 727 B

View file

@ -1,22 +1,27 @@
#include "dice.h"
#include <memory.h>
int Die_Roll(enum Die_Dice die) {
int const max = die;
return (rand() % max) + 1;
}
static int activeDiceCount = 0;
static enum Dice_Die activeDice[MAX_ACTIVE_DICE];
static int current_active_count = 0;
static enum Die_Dice active_dice_set[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,
};
int Dice_Roll(enum Dice_Die die) {
if (die == COIN) {
return (rand() % 2);
} else {
int const max = die;
return (rand() % max) + 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");

View file

@ -4,16 +4,8 @@
#include "renderer/clay_renderer_SDL3.h"
#include <stdlib.h>
#ifndef MAX_ACTIVE_DICE
#define MAX_ACTIVE_DICE 20
#endif
#ifndef MAX_ROLL_STR_LEN
#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 +15,32 @@ enum Die_Dice {
D100 = 100
};
struct Die_ResultType {
#ifndef MAX_ROLL_STR_LEN
#define MAX_ROLL_STR_LEN 10
#endif
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);
#ifndef MAX_ACTIVE_DICE
#define MAX_ACTIVE_DICE 20
#endif
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 int Dice_Roll(enum Dice_Die die);
extern struct Die_ResultType *Die_GetLastResult(size_t *out_length);
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 void Die_RollActiveSet();
extern struct Dice_ResultType *Dice_GetLastResult(size_t *out_length);
extern struct Dice_ResultType *Dice_GetLastResultTotal();
extern Clay_String Die_ToString(enum Die_Dice die);
extern void Dice_RollActiveSet();
extern Clay_String Dice_ToString(enum Dice_Die die);
#endif // !DICE_H

View file

@ -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,10 +64,9 @@ void DiceSelectorContainer() {
.padding = panelPadding,
},
.clip = {
false, true, Clay_GetScrollOffset(),
true, true, Clay_GetScrollOffset(),
},
}) {
AddDieButton(COIN);
AddDieButton(D4);
AddDieButton(D6);
AddDieButton(D8);
@ -78,6 +74,7 @@ void DiceSelectorContainer() {
AddDieButton(D12);
AddDieButton(D20);
AddDieButton(D100);
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,26 @@ 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 },
},
}) {
TextButton(CLAY_STRING("Roll"), proceedButton, &HandleRollSetButtonInteraction, 0);
CLAY_TEXT(Dice_GetLastResultTotal()->clay_string, CLAY_TEXT_CONFIG({
.H(3),
.textColor = TextColors(0),
}));
TextButton(CLAY_STRING("Clear"), warningButton, &HandleClearSetButtonInteraction, 0);
}
}
}

View file

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

View file

@ -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];

View file

@ -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

View file

@ -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);
}

View file

@ -2,7 +2,7 @@
#define STYLE_H
#include "defs.h"
#include "resources.h"
#include "resources.h" // needed for macros
#include <clay/clay.h>
#include <stdint.h>
@ -70,9 +70,12 @@ fontId = FONT_BOLD,\
// BUTTONS
////////////////////////////////////
constexpr Clay_Color buttonBackground = {
constexpr Clay_Color warningButton = {
177, 56, 52, 255
};
constexpr Clay_Color proceedButton = {
49, 181, 99, 255
};
constexpr Clay_Padding buttonPadding = {
24, 24,
4, 4,
@ -112,8 +115,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