feat: cleaned up and created 'ceramic/'

This commit is contained in:
Sara Gerretsen 2025-10-26 20:05:00 +01:00
parent 2d669e6411
commit f32494a29f
12 changed files with 265 additions and 195 deletions

View file

@ -1,24 +1,13 @@
#include "application.h"
#include "style.h"
#include "elements.h"
#include "ceramic/style.h"
#include "ceramic/elements.h"
#include <SDL3/SDL.h>
#include <clay/clay.h>
namespace application {
static void SampleHeader() {
elements::Header(CLAY_STRING("Left Panel"), 2, {
.textColor = style::color::white
});
}
Clay_RenderCommandArray RenderApplication() {
Clay_BeginLayout();
CLAY(CLAY_ID("OuterContainer"), style::Window()) {
CLAY_AUTO_ID(style::PanelContainer(0, {
.layout = { .sizing = { CLAY_SIZING_PERCENT(0.15), CLAY_SIZING_GROW() } }
})) {
SampleHeader();
}
}
return Clay_EndLayout();
}

View file

@ -21,7 +21,7 @@ void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intpt
}) {
Clay_OnHover(onHovered, onHoveredData);
elements::Body(text, {
.textColor = style::TextColor(0),
.textColor = style::textColor,
.textAlignment = CLAY_TEXT_ALIGN_CENTER,
});
}
@ -65,7 +65,7 @@ void Toggle(Clay_String label, Clay_Color selected, bool &state) {
},
}) { }
Body(label, {
.textColor = style::TextColor(0)
.textColor = style::textColor
});
}
}

View file

@ -2,13 +2,14 @@
#define ELEMENTS_H
#include <clay/clay.h>
#include "style.h"
namespace elements {
typedef void(*OnHoveredFn)(Clay_ElementId element, Clay_PointerData pointer, intptr_t data);
void TextButton(Clay_String text, Clay_Color color, OnHoveredFn onHovered, intptr_t onHoveredData = 0);
void Toggle(Clay_String label, Clay_Color selected, bool &state);
void Body(Clay_String string, Clay_TextElementConfig baseCfg = {});
void Header(Clay_String string, size_t header, Clay_TextElementConfig baseCfg = {});
void Body(Clay_String string, Clay_TextElementConfig baseCfg = {.textColor = style::textColor});
void Header(Clay_String string, size_t header, Clay_TextElementConfig baseCfg = {.textColor = style::textColor});
}
#endif // !ELEMENTS_H

28
src/ceramic/resources.cpp Normal file
View file

@ -0,0 +1,28 @@
#include "resources.h"
#include "style.h"
#include <clay/clay.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_render.h>
#include <SDL3_image/SDL_image.h>
namespace resources {
TTF_Font *defaultFont[FONT_MAX];
TTF_TextEngine *textEngine = nullptr;
void SetDefaultFont(char const *path) {
defaultFont[FONT_DEFAULT] = TTF_OpenFont(path, style::baseFontSize * 5);
if (defaultFont[FONT_DEFAULT] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default font '%s': %s", path, SDL_GetError());
exit(6);
}
TTF_SetFontHinting(defaultFont[FONT_DEFAULT], TTF_HINTING_LIGHT_SUBPIXEL);
defaultFont[FONT_BOLD] = TTF_OpenFont(path, style::baseFontSize * 5);
if (defaultFont[FONT_BOLD] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load default bold font '%s': %s", path, SDL_GetError());
exit(6);
}
TTF_SetFontHinting(defaultFont[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
TTF_SetFontStyle(defaultFont[FONT_BOLD], TTF_STYLE_BOLD);
SDL_Log("SetDefaultFont: Success");
}
}

View file

@ -11,9 +11,9 @@ enum Font {
};
extern TTF_TextEngine *textEngine;
extern TTF_Font *fonts[FONT_MAX];
extern TTF_Font *defaultFont[FONT_MAX];
void LoadResources();
void SetDefaultFont(char const *path);
}
#endif // !RESOURCES_H

View file

@ -1,22 +1,23 @@
#include "style.h"
#include "SDL3/SDL_stdinc.h"
#include "resources.h"
#include <clay/clay.h>
namespace style {
Clay_ElementDeclaration ListContainer(size_t depth, Clay_ElementDeclaration baseCfg) {
baseCfg.border = {
PanelBorder(depth),
CLAY_BORDER_ALL(2)
.color = panelBorder,
.width = CLAY_BORDER_ALL(2)
};
baseCfg.cornerRadius = defaultRadiusAll;
return baseCfg;
}
Clay_ElementDeclaration PanelContainer(size_t depth, Clay_ElementDeclaration baseCfg) {
baseCfg.backgroundColor = PanelBackground(depth);
baseCfg.backgroundColor = panelBackground;
baseCfg.border = {
PanelBorder(depth),
CLAY_BORDER_OUTSIDE(2)
.color = panelBorder,
.width = CLAY_BORDER_OUTSIDE(2)
};
baseCfg.cornerRadius = defaultRadiusAll;
baseCfg.layout.padding = CLAY_PADDING_ALL(8);
@ -39,49 +40,23 @@ Clay_ElementDeclaration RightPanelContainer(size_t depth, Clay_ElementDeclaratio
return baseCfg;
}
Clay_Color PanelBackground(size_t idx) {
return {
255*panelBackground[idx],
255*panelBackground[idx],
255*panelBackground[idx],
255
};
}
Clay_Color PanelBorder(size_t idx) {
return {
255*panelBorder[idx],
255*panelBorder[idx],
255*panelBorder[idx],
255
};
}
Clay_Color TextColor(size_t idx) {
return {
255*textColorsP[idx],
255*textColorsP[idx],
255*textColorsP[idx],
255
};
}
Clay_ElementDeclaration Window() {
return {
.layout = {
.sizing = { CLAY_SIZING_GROW(), CLAY_SIZING_GROW() },
.padding = CLAY_PADDING_ALL(0),
.padding = CLAY_PADDING_ALL(windowPadding),
.childGap = 0,
.layoutDirection = CLAY_LEFT_TO_RIGHT,
.layoutDirection = CLAY_LEFT_TO_RIGHT
},
.backgroundColor = windowColor
};
}
Clay_Color ToHoveredColor(Clay_Color color) {
float avg = (color.r + color.g + color.b) / 3.f;
color.r = (color.r - avg) * 0.8f + avg - 30;
color.g = (color.g - avg) * 0.8f + avg - 30;
color.b = (color.b - avg) * 0.8f + avg - 30;
color.r = SDL_clamp((color.r - avg) * 0.8f + avg - 30, 0., 1.);
color.g = SDL_clamp((color.g - avg) * 0.8f + avg - 30, 0., 1.);
color.b = SDL_clamp((color.b - avg) * 0.8f + avg - 30, 0., 1.);
return color;
}
}

View file

@ -9,21 +9,18 @@ namespace style {
// WINDOW STYLE
////////////////////////////////////
constexpr uint16_t windowPadding{ 10 };
constexpr uint16_t windowPadding{1};
constexpr Clay_Color windowColor{20, 20, 20, 255};
////////////////////////////////////
// CONTAINER STYLE
////////////////////////////////////
constexpr uint16_t containerGap{ 10 };
constexpr double defaultRadius{ 5.0 };
constexpr uint16_t containerGap{10};
constexpr double defaultRadius{5.0};
constexpr float panelBackground[] = {
.4f, .3f, .2f
};
constexpr float panelBorder[] = {
.5f, .4f, .3f
};
constexpr Clay_Color panelBackground{ 0,0,0,0 };
constexpr Clay_Color panelBorder { 150, 150, 150, 150 };
constexpr Clay_Padding panelPadding = {
24, 24,
@ -34,8 +31,6 @@ Clay_ElementDeclaration ListContainer(size_t depth, Clay_ElementDeclaration base
Clay_ElementDeclaration PanelContainer(size_t depth, Clay_ElementDeclaration baseCfg);
Clay_ElementDeclaration LeftPanelContainer(size_t depth, Clay_ElementDeclaration baseCfg);
Clay_ElementDeclaration RightPanelContainer(size_t depth, Clay_ElementDeclaration baseCfg);
Clay_Color PanelBackground(size_t idx);
Clay_Color PanelBorder(size_t idx);
Clay_ElementDeclaration Window();
////////////////////////////////////
@ -44,16 +39,10 @@ Clay_ElementDeclaration Window();
constexpr float paragraphGap = 10;
constexpr uint16_t baseFontSize = 16;
constexpr float textColorsP[] = {
0.9f, 0.9f, 0.9f
};
constexpr uint16_t headerSizes[] = {
64, 32,
28, 16
};
constexpr uint16_t headerSizes[] = {64, 32, 28, 16};
Clay_Color TextColor(size_t idx);
constexpr Clay_Color textColor{170, 170, 170, 255};
////////////////////////////////////
// BUTTONS

View file

@ -2,7 +2,7 @@
#include "application.h"
#include "input.h"
#include "resources.h"
#include "ceramic/resources.h"
#include <SDL3/SDL.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_events.h>
@ -90,7 +90,7 @@ void InitClay() {
clayMemorySize = Clay_MinMemorySize();
clayPrimaryArena = Clay_CreateArenaWithCapacityAndMemory(clayMemorySize, SDL_malloc(clayMemorySize));
Clay_Initialize(clayPrimaryArena, { (float)screenWidth, (float)screenHeight }, { HandleClayErrors });
Clay_SetMeasureTextFunction(MeasureText, resources::fonts);
Clay_SetMeasureTextFunction(MeasureText, resources::defaultFont);
Clay_SetLayoutDimensions({ (float)screenWidth, (float)screenHeight });
float x{ 0 }, y{ 0 };
SDL_GetMouseState(&x, &y);
@ -99,10 +99,10 @@ void InitClay() {
int main(int argc, char *argv[]) {
InitSDL();
resources::LoadResources();
resources::SetDefaultFont("assets/AdwaitaSans-Regular.ttf");
LogOutputResolution();
InitClay();
backendData = { renderer, resources::textEngine, resources::fonts };
backendData = { renderer, resources::textEngine, resources::defaultFont };
SDL_Event event;
uint64_t startFrameTime = SDL_GetTicksNS();
double deltaTime = 0.0;

View file

@ -1,32 +0,0 @@
#include "resources.h"
#include "style.h"
#include <clay/clay.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_render.h>
#include <SDL3_image/SDL_image.h>
namespace resources {
TTF_Font *fonts[FONT_MAX];
TTF_TextEngine *textEngine = nullptr;
static inline void LoadFonts() {
fonts[FONT_DEFAULT] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", style::baseFontSize * 5);
if (fonts[FONT_DEFAULT] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans: %s", SDL_GetError());
exit(6);
}
TTF_SetFontHinting(fonts[FONT_DEFAULT], TTF_HINTING_LIGHT_SUBPIXEL);
fonts[FONT_BOLD] = TTF_OpenFont("assets/AdwaitaSans-Regular.ttf", style::baseFontSize * 5);
if (fonts[FONT_BOLD] == nullptr) {
SDL_LogError(SDL_LOG_CATEGORY_ERROR, "TTF_OpenFont failed: Failed to load adwaita sans bold: %s", SDL_GetError());
exit(6);
}
TTF_SetFontHinting(fonts[FONT_BOLD], TTF_HINTING_LIGHT_SUBPIXEL);
TTF_SetFontStyle(fonts[FONT_BOLD], TTF_STYLE_BOLD);
SDL_Log("LoadFonts: Success");
}
void LoadResources() {
LoadFonts();
}
}

View file

@ -1,54 +0,0 @@
#include "thread_pool.h"
#include <algorithm>
#include <thread>
#include <utility>
namespace threading {
ThreadPool::ThreadPool() {
size_t const thread_count{ std::max(std::thread::hardware_concurrency() - 1, 1u) };
this->threads.reserve(thread_count);
for (size_t i{ 0 }; i < thread_count; ++i) {
this->threads.emplace_back(std::bind(&ThreadPool::ThreadFn, this));
}
}
ThreadPool::~ThreadPool() {
{ std::scoped_lock lock{ this->lock };
this->shutdown = true;
this->threadNotifier.notify_all();
}
for (std::thread &thread : this->threads) {
thread.join();
}
}
size_t ThreadPool::GetThreadCount() {
return this->threads.size();
}
void ThreadPool::ScheduleTask(TaskFunc fn) {
std::scoped_lock lock{ this->lock };
this->taskQueue.emplace(std::move(fn));
this->threadNotifier.notify_one();
}
void ThreadPool::ThreadFn() {
TaskFunc function;
for(;;) {
{ std::unique_lock<std::mutex> lock{ this->lock };
while (!this->shutdown && this->taskQueue.empty()) {
this->threadNotifier.wait(lock);
}
if (this->taskQueue.empty()) {
return;
}
function = std::move(this->taskQueue.front());
this->taskQueue.pop();
}
function.operator()();
}
}
ThreadPool tasks{};
}

View file

@ -1,30 +0,0 @@
#ifndef THREAD_POOL_H
#define THREAD_POOL_H
#include <condition_variable>
#include <functional>
#include <mutex>
#include <queue>
#include <thread>
namespace threading {
typedef std::function<void()> TaskFunc;
class ThreadPool {
public: ThreadPool();
~ThreadPool();
void ScheduleTask(TaskFunc jobs);
size_t GetThreadCount();
private:
void ThreadFn();
std::queue<TaskFunc> taskQueue{};
std::vector<std::thread> threads{};
std::mutex lock{};
std::condition_variable threadNotifier{};
bool shutdown{ false };
};
extern ThreadPool tasks;
}
#endif // !THREAD_POOL_H