Compare commits

..

9 commits

14 changed files with 297 additions and 11 deletions

View file

@ -30,7 +30,7 @@ AttributeMacros:
BinPackArguments: true
BinPackParameters: true
BitFieldColonSpacing: Both
BreakAfterAttributes: Never
BreakAfterAttributes: Always
BreakAfterJavaFieldAnnotations: false
BreakArrays: false
BreakBeforeBinaryOperators: None

3
.gitmodules vendored
View file

@ -4,6 +4,9 @@
[submodule "vendor/SDL3_ttf"]
path = vendor/SDL3_ttf
url = https://github.com/libsdl-org/SDL_ttf.git
[submodule "vendor/SDL3_image"]
path = vendor/SDL3_image
url = https://github.com/libsdl-org/SDL_image.git
[submodule "vendor/ceramic"]
path = vendor/ceramic
url = forgejo@git.objectionable.solutions:Sara/clay-ceramic.git

View file

@ -11,12 +11,23 @@ set(CMAKE_C_STANDARD 23)
file(GLOB_RECURSE source_files . src/**.cpp src/**.c vendor/renderer/**.c vendor/ceramic/**.cpp)
include_directories(vendor/)
set(SDL_VENDORED ON)
add_subdirectory(vendor/SDL3/ EXCLUDE_FROM_ALL)
set(SDLTTF_VENDORED ON)
add_subdirectory(vendor/SDL3_ttf/ EXCLUDE_FROM_ALL)
add_subdirectory(vendor/SDL3_ttf EXCLUDE_FROM_ALL)
set(SDLIMAGE_AVIF OFF)
set(SDLIMAGE_BMP OFF)
set(SDLIMAGE_TIF OFF)
set(SDLIMAGE_WEBP OFF)
set(SDLIMAGE_VENDORED ON)
add_subdirectory(vendor/SDL3_image EXCLUDE_FROM_ALL)
add_executable(dice-gui ${source_files})
target_link_libraries(dice-gui PRIVATE SDL3_ttf::SDL3_ttf SDL3::SDL3)
target_link_libraries(dice-gui PRIVATE
SDL3_ttf::SDL3_ttf
SDL3_image::SDL3_image
SDL3::SDL3)
add_custom_target(copy_assets
COMMAND ${CMAKE_COMMAND} -E

View file

@ -1,14 +1,21 @@
#include "application.h"
#include "ceramic/style.h"
#include "ceramic/elements.h"
#include "dice_gui.h"
#include <ceramic/style.h>
#include <ceramic/elements.h>
#include <SDL3/SDL.h>
#include <clay/clay.h>
constexpr Clay_LayoutConfig centerContainer {
.sizing = {CLAY_SIZING_GROW(), CLAY_SIZING_GROW()},
.childAlignment = {CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER},
};
namespace application {
Clay_RenderCommandArray RenderApplication() {
Clay_BeginLayout();
CLAY(CLAY_ID("OuterContainer"), cera::Window()) {
CLAY_AUTO_ID(cera::LeftPanelContainer()) {
CLAY(CLAY_ID("Window"), cera::Window()) {
CLAY(CLAY_ID("Content"), {.layout = centerContainer}) {
DiceContainer();
}
}
return Clay_EndLayout();

62
src/dice_data.cpp Normal file
View file

@ -0,0 +1,62 @@
#include "dice_data.h"
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <cstring>
namespace active_dice {
struct DieState {
Die type;
uint8_t value;
};
constexpr size_t maxActiveDice{16};
DieState activeDice[maxActiveDice];
size_t numActiveDice{0};
void Add(Die die) {
if(numActiveDice < maxActiveDice) {
activeDice[numActiveDice].type = die;
activeDice[numActiveDice].value = RollDie(die);
++numActiveDice;
}
}
void Remove(size_t at) {
if(at >= numActiveDice) {
return;
}
if(at < numActiveDice - 1) {
std::memmove(activeDice + at, activeDice + at + 1, sizeof(DieState) * (numActiveDice - at));
}
--numActiveDice;
}
Die Get(size_t at) {
if(at < numActiveDice) {
return activeDice[at].type;
} else return D_COIN;
}
uint8_t GetValue(size_t at) {
if(at < numActiveDice) {
return activeDice[at].value;
} else return 0;
}
size_t Count() {
return numActiveDice;
}
void ReRoll() {
for(size_t i{0}; i < numActiveDice; ++i) {
activeDice[i].value = RollDie(activeDice[i].type);
}
}
}
uint8_t RollDie(Die die) {
if(die == D_COIN) {
return std::rand() % 2;
}
return 1 + std::rand() % (die-1);
}

51
src/dice_data.h Normal file
View file

@ -0,0 +1,51 @@
#pragma once
#include <cstddef>
#include <cstdint>
enum Die {
D_COIN = 2,
D4 = 4,
D6 = 6,
D8 = 8,
D10 = 10,
D12 = 12,
D20 = 20,
D100 = 100,
};
constexpr Die diceIndex[]{D_COIN, D4, D6, D8, D10, D12, D20, D100};
static inline size_t DieToIndex(Die die) {
switch(die) {
default:
case D_COIN:
return 0;
case D4:
return 1;
case D6:
return 2;
case D8:
return 3;
case D10:
return 4;
case D12:
return 5;
case D20:
return 6;
case D100:
return 7;
}
}
namespace active_dice {
void Add(Die die);
void Remove(size_t at);
Die Get(size_t at);
uint8_t GetValue(size_t at);
size_t Count();
void ReRoll();
} // namespace active_dice
uint8_t RollDie(Die die);

147
src/dice_gui.cpp Normal file
View file

@ -0,0 +1,147 @@
#include "dice_gui.h"
#include "SDL3/SDL_render.h"
#include "ceramic/style.h"
#include "dice_data.h"
#include "renderer/ui_data.h"
#include <ceramic/elements.h>
#include <ceramic/resources.h>
#include <clay/clay.h>
#include <cstdint>
extern SDL_Renderer *renderer;
SDL_Texture *dieImages[8];
SDL_Texture *hoveredDieImages[8];
Clay_Color const dieImageColors[8]{
{230, 184, 48, 255},
{177, 56, 52, 255},
{115, 177, 52, 255},
{52, 177, 125, 255},
{52, 177, 176, 255},
{52, 93, 177, 255},
{177, 52, 140, 255},
{95, 52, 177, 255},
};
char const *dieImagePaths[8]{
"assets/icons/d2.svg",
"assets/icons/d4.svg",
"assets/icons/d6.svg",
"assets/icons/d8.svg",
"assets/icons/d10.svg",
"assets/icons/d12.svg",
"assets/icons/d20.svg",
"assets/icons/d10.svg",
};
Clay_String const dieUiStrings[8] {
CLAY_STRING("C"),
CLAY_STRING("4"),
CLAY_STRING("6"),
CLAY_STRING("8"),
CLAY_STRING("10"),
CLAY_STRING("12"),
CLAY_STRING("20"),
CLAY_STRING("100")
};
static void LoadDieImagesIfNeeded() {
static bool diceImagesLoaded{false};
if(!diceImagesLoaded) {
diceImagesLoaded = true;
Clay_Color color{};
for(size_t i{0}; i < 8; ++i) {
color = dieImageColors[i];
dieImages[i] = cera::LoadAndStoreTexture(renderer, dieImagePaths[i]);
SDL_SetTextureColorMod(dieImages[i], color.r, color.g, color.b);
hoveredDieImages[i] = cera::LoadAndStoreTexture(renderer, dieImagePaths[i]);
color = cera::ToHoveredColor(dieImageColors[i]);
SDL_SetTextureColorMod(hoveredDieImages[i], color.r, color.g, color.b);
}
}
}
static void DieButton(Die die, int32_t showNumber, Clay_Sizing size, cera::OnHoveredFn onHover, intptr_t data) {
size_t const index{DieToIndex(die)};
char resultStr[6]="";
int32_t len = SDL_snprintf(resultStr, 6, "%d", showNumber);
Clay_String string = UiData_StoreString(resultStr, len);
CLAY_AUTO_ID({
.layout = {
.sizing = {CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100)},
.childAlignment = {CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER}
},
.image = {.imageData = Clay_Hovered() ? hoveredDieImages[index] : dieImages[index]}
}) {
Clay_OnHover(onHover, data);
cera::Header(string, 2, {
.textColor = cera::color::white
});
}
}
static void AddDieButtonHovered(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
if(pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
active_dice::Add((Die)data);
}
}
static void ActiveDieButtonHovered(Clay_ElementId element, Clay_PointerData pointer, intptr_t data) {
if(pointer.state == CLAY_POINTER_DATA_PRESSED_THIS_FRAME) {
active_dice::Remove((size_t)data);
}
}
void DiceContainer() {
LoadDieImagesIfNeeded();
Clay_Sizing const addDieButtonSize{CLAY_SIZING_FIXED(100), CLAY_SIZING_FIXED(100)};
CLAY(CLAY_ID("DiceSelector"), cera::PanelContainer({
.layout={
.sizing={CLAY_SIZING_FIT(), CLAY_SIZING_GROW()},
.childAlignment{CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER}
},
})) {
CLAY_AUTO_ID({
.layout = {
.sizing = {CLAY_SIZING_FIT(), CLAY_SIZING_FIT()},
.layoutDirection = CLAY_TOP_TO_BOTTOM,
},
.clip = {
.horizontal = false,
.vertical = true,
.childOffset = Clay_GetScrollOffset(),
}
}) {
DieButton(D4, D4, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D4);
DieButton(D6, D6, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D6);
DieButton(D8, D8, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D8);
DieButton(D10, D10, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D10);
DieButton(D12, D12, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D12);
DieButton(D20, D20, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D20);
DieButton(D100, D100, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D100);
DieButton(D_COIN, D_COIN, addDieButtonSize, &AddDieButtonHovered, (intptr_t)D_COIN);
}
}
Clay_Sizing const activeDieButtonSize{CLAY_SIZING_FIXED(200), CLAY_SIZING_FIXED(200)};
CLAY(CLAY_ID("DiceActive"), cera::PanelContainer({
.layout={
.sizing={CLAY_SIZING_GROW(), CLAY_SIZING_GROW()},
.childAlignment={CLAY_ALIGN_X_CENTER, CLAY_ALIGN_Y_CENTER}
}
})) {
CLAY_AUTO_ID({
.layout = {
.sizing = {CLAY_SIZING_FIT(), CLAY_SIZING_FIT()},
},
.clip = {
.horizontal = true,
.vertical = false,
.childOffset = Clay_GetScrollOffset(),
},
}) {
for(size_t i{0}; i < active_dice::Count(); ++i) {
DieButton(active_dice::Get(i), active_dice::GetValue(i), activeDieButtonSize, &ActiveDieButtonHovered, (intptr_t)i);
}
}
}
}

3
src/dice_gui.h Normal file
View file

@ -0,0 +1,3 @@
#pragma once
void DiceContainer();

View file

@ -137,6 +137,7 @@ int main(int argc, char *argv[]) {
SDL_RenderPresent(renderer);
SDL_Delay(10);
}
cera::CleanupResources();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

2
vendor/SDL3 vendored

@ -1 +1 @@
Subproject commit 137b0b2bee2bb9dd76e2c260970ccf7ff0621692
Subproject commit be1d44279c715c5ac879f5698bc604fe9070ff0c

1
vendor/SDL3_image vendored Submodule

@ -0,0 +1 @@
Subproject commit 3bbb15f654d48251c164e4dc4f5ae8b9ee0eae71

2
vendor/SDL3_ttf vendored

@ -1 +1 @@
Subproject commit 6b6bd588e8646360b08f624fb601cc2ec75c6ada
Subproject commit 215872a0f91d24a4013f106832c32d879fba48db

2
vendor/ceramic vendored

@ -1 +1 @@
Subproject commit c372a4db3a28dd292793756f0ca36e9460d24b33
Subproject commit 2dcdcd394c5b0c7e4875f965dfe789d3c71ad217

View file

@ -4,7 +4,7 @@
const size_t uiDataLength = UI_DATA_SIZE;
static size_t utilized = 9;
static size_t utilized = 0;
static char uiDataArena[UI_DATA_SIZE];
static char *uiDataWriter = uiDataArena;