From f32494a29f97d2ea0959c2139053716e5f886dfd Mon Sep 17 00:00:00 2001 From: Sara Date: Sun, 26 Oct 2025 20:05:00 +0100 Subject: [PATCH] feat: cleaned up and created 'ceramic/' --- .clang-format | 204 +++++++++++++++++++++++++++++++++ src/application.cpp | 15 +-- src/{ => ceramic}/elements.cpp | 4 +- src/{ => ceramic}/elements.h | 5 +- src/ceramic/resources.cpp | 28 +++++ src/{ => ceramic}/resources.h | 4 +- src/{ => ceramic}/style.cpp | 49 ++------ src/{ => ceramic}/style.h | 27 ++--- src/main.cpp | 8 +- src/resources.cpp | 32 ------ src/thread_pool.cpp | 54 --------- src/thread_pool.h | 30 ----- 12 files changed, 265 insertions(+), 195 deletions(-) create mode 100644 .clang-format rename src/{ => ceramic}/elements.cpp (96%) rename src/{ => ceramic}/elements.h (83%) create mode 100644 src/ceramic/resources.cpp rename src/{ => ceramic}/resources.h (73%) rename src/{ => ceramic}/style.cpp (64%) rename src/{ => ceramic}/style.h (81%) delete mode 100644 src/resources.cpp delete mode 100644 src/thread_pool.cpp delete mode 100644 src/thread_pool.h diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..5c9a9a3 --- /dev/null +++ b/.clang-format @@ -0,0 +1,204 @@ +--- +BasedOnStyle: Microsoft +AccessModifierOffset: -4 +AlignAfterOpenBracket: DontAlign +AlignArrayOfStructures: None +AlignConsecutiveAssignments: None +AlignConsecutiveBitFields: None +AlignConsecutiveDeclarations: None +AlignConsecutiveMacros: None +AlignEscapedNewlines: Right +AlignOperands: DontAlign +AlignTrailingComments: + Kind: Always + OverEmptyLines: 0 +AllowAllArgumentsOnNextLine: true +AllowAllParametersOfDeclarationOnNextLine: true +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +AllowShortEnumsOnASingleLine: false +AllowShortFunctionsOnASingleLine: None +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: All +AllowShortLoopsOnASingleLine: false +AlwaysBreakAfterDefinitionReturnType: None +AlwaysBreakAfterReturnType: None +AlwaysBreakBeforeMultilineStrings: false +AlwaysBreakTemplateDeclarations: MultiLine +AttributeMacros: + - __capability +BinPackArguments: true +BinPackParameters: true +BitFieldColonSpacing: Both +BreakAfterAttributes: Never +BreakAfterJavaFieldAnnotations: false +BreakArrays: false +BreakBeforeBinaryOperators: None +BreakBeforeBraces: Custom +BraceWrapping: + AfterCaseLabel: false + AfterClass: false + AfterControlStatement: Never + AfterEnum: false + AfterFunction: false + AfterNamespace: false + AfterObjCDeclaration: false + AfterStruct: false + AfterUnion: false + AfterExternBlock: false + BeforeCatch: false + BeforeElse: false + BeforeLambdaBody: false + BeforeWhile: false + IndentBraces: false + SplitEmptyFunction: false + SplitEmptyRecord: false + SplitEmptyNamespace: false +BreakBeforeConceptDeclarations: Always +BreakBeforeInlineASMColon: OnlyMultiline +BreakBeforeTernaryOperators: true +BreakConstructorInitializers: BeforeComma +BreakInheritanceList: BeforeColon +BreakStringLiterals: true +ColumnLimit: 0 +CommentPragmas: "^ IWYU pragma:" +CompactNamespaces: false +ConstructorInitializerIndentWidth: 4 +ContinuationIndentWidth: 4 +Cpp11BracedListStyle: true +DerivePointerAlignment: false +DisableFormat: false +EmptyLineAfterAccessModifier: Never +EmptyLineBeforeAccessModifier: LogicalBlock +ExperimentalAutoDetectBinPacking: false +FixNamespaceComments: true +ForEachMacros: + - foreach + - Q_FOREACH + - BOOST_FOREACH +IfMacros: + - KJ_IF_MAYBE +IncludeBlocks: Preserve +IncludeCategories: + - Regex: ^"(llvm|llvm-c|clang|clang-c)/ + Priority: 2 + SortPriority: 0 + CaseSensitive: false + - Regex: ^(<|"(gtest|gmock|isl|json)/) + Priority: 3 + SortPriority: 0 + CaseSensitive: false + - Regex: .* + Priority: 1 + SortPriority: 0 + CaseSensitive: false +IncludeIsMainRegex: (Test)?$ +IncludeIsMainSourceRegex: "" +IndentAccessModifiers: false +IndentCaseBlocks: false +IndentCaseLabels: false +IndentExternBlock: AfterExternBlock +IndentGotoLabels: true +IndentPPDirectives: None +IndentRequiresClause: true +IndentWidth: 4 +IndentWrappedFunctionNames: false +InsertBraces: true +InsertNewlineAtEOF: true +InsertTrailingCommas: None +IntegerLiteralSeparator: + Binary: 0 + BinaryMinDigits: 0 + Decimal: 0 + DecimalMinDigits: 0 + Hex: 0 + HexMinDigits: 0 +JavaScriptQuotes: Leave +JavaScriptWrapImports: true +KeepEmptyLinesAtTheStartOfBlocks: true +LambdaBodyIndentation: Signature +Language: Cpp +LineEnding: DeriveLF +MacroBlockBegin: "" +MacroBlockEnd: "" +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +ObjCBinPackProtocolList: Auto +ObjCBlockIndentWidth: 4 +ObjCBreakBeforeNestedBlockParam: true +ObjCSpaceAfterProperty: false +ObjCSpaceBeforeProtocolList: true +PPIndentWidth: -1 +PackConstructorInitializers: BinPack +PenaltyBreakAssignment: 2 +PenaltyBreakBeforeFirstCallParameter: 19 +PenaltyBreakComment: 300 +PenaltyBreakFirstLessLess: 120 +PenaltyBreakOpenParenthesis: 0 +PenaltyBreakString: 1000 +PenaltyBreakTemplateDeclaration: 10 +PenaltyExcessCharacter: 1000000 +PenaltyIndentedWhitespace: 0 +PenaltyReturnTypeOnItsOwnLine: 1000 +PointerAlignment: Right +QualifierAlignment: Leave +ReferenceAlignment: Pointer +ReflowComments: true +RemoveBracesLLVM: false +RemoveSemicolon: false +RequiresClausePosition: OwnLine +RequiresExpressionIndentation: OuterScope +SeparateDefinitionBlocks: Leave +ShortNamespaceLines: 1 +SortIncludes: CaseSensitive +SortJavaStaticImport: Before +SortUsingDeclarations: LexicographicNumeric +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: true +SpaceAroundPointerQualifiers: Default +SpaceBeforeAssignmentOperators: true +SpaceBeforeCaseColon: false +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: Never +SpaceBeforeParensOptions: + AfterControlStatements: true + AfterForeachMacros: true + AfterFunctionDeclarationName: false + AfterFunctionDefinitionName: false + AfterIfMacros: false + AfterOverloadedOperator: false + AfterRequiresInClause: false + AfterRequiresInExpression: false + BeforeNonEmptyParentheses: false +SpaceBeforeRangeBasedForLoopColon: true +SpaceBeforeSquareBrackets: false +SpaceInEmptyBlock: false +SpaceInEmptyParentheses: false +SpacesBeforeTrailingComments: 1 +SpacesInAngles: Never +SpacesInCStyleCastParentheses: false +SpacesInConditionalStatement: false +SpacesInContainerLiterals: true +SpacesInLineCommentPrefix: + Minimum: 1 + Maximum: -1 +SpacesInParentheses: false +SpacesInSquareBrackets: false +Standard: Latest +StatementAttributeLikeMacros: + - Q_EMIT +StatementMacros: + - Q_UNUSED + - QT_REQUIRE_VERSION +TabWidth: 4 +UseTab: Always +WhitespaceSensitiveMacros: + - BOOST_PP_STRINGIZE + - CF_SWIFT_NAME + - NS_SWIFT_NAME + - PP_STRINGIZE + - STRINGIZE +UseCRLF: false diff --git a/src/application.cpp b/src/application.cpp index decafbb..c269306 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -1,24 +1,13 @@ #include "application.h" -#include "style.h" -#include "elements.h" +#include "ceramic/style.h" +#include "ceramic/elements.h" #include #include 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(); } diff --git a/src/elements.cpp b/src/ceramic/elements.cpp similarity index 96% rename from src/elements.cpp rename to src/ceramic/elements.cpp index b261a97..72ffea9 100644 --- a/src/elements.cpp +++ b/src/ceramic/elements.cpp @@ -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 }); } } diff --git a/src/elements.h b/src/ceramic/elements.h similarity index 83% rename from src/elements.h rename to src/ceramic/elements.h index 1f5269e..c34a52e 100644 --- a/src/elements.h +++ b/src/ceramic/elements.h @@ -2,13 +2,14 @@ #define ELEMENTS_H #include +#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 diff --git a/src/ceramic/resources.cpp b/src/ceramic/resources.cpp new file mode 100644 index 0000000..8a2494d --- /dev/null +++ b/src/ceramic/resources.cpp @@ -0,0 +1,28 @@ +#include "resources.h" +#include "style.h" +#include +#include +#include +#include + +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"); +} +} diff --git a/src/resources.h b/src/ceramic/resources.h similarity index 73% rename from src/resources.h rename to src/ceramic/resources.h index ef12346..c341e54 100644 --- a/src/resources.h +++ b/src/ceramic/resources.h @@ -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 diff --git a/src/style.cpp b/src/ceramic/style.cpp similarity index 64% rename from src/style.cpp rename to src/ceramic/style.cpp index 7fab4b6..1e24225 100644 --- a/src/style.cpp +++ b/src/ceramic/style.cpp @@ -1,22 +1,23 @@ #include "style.h" +#include "SDL3/SDL_stdinc.h" #include "resources.h" #include 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; } } diff --git a/src/style.h b/src/ceramic/style.h similarity index 81% rename from src/style.h rename to src/ceramic/style.h index 9ba6024..218ff7b 100644 --- a/src/style.h +++ b/src/ceramic/style.h @@ -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 diff --git a/src/main.cpp b/src/main.cpp index 543d5f0..d649347 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include "application.h" #include "input.h" -#include "resources.h" +#include "ceramic/resources.h" #include #include #include @@ -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; diff --git a/src/resources.cpp b/src/resources.cpp deleted file mode 100644 index 845ef9f..0000000 --- a/src/resources.cpp +++ /dev/null @@ -1,32 +0,0 @@ -#include "resources.h" -#include "style.h" -#include -#include -#include -#include - -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(); -} -} diff --git a/src/thread_pool.cpp b/src/thread_pool.cpp deleted file mode 100644 index 48e4d25..0000000 --- a/src/thread_pool.cpp +++ /dev/null @@ -1,54 +0,0 @@ -#include "thread_pool.h" -#include -#include -#include - -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 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{}; -} diff --git a/src/thread_pool.h b/src/thread_pool.h deleted file mode 100644 index eb55962..0000000 --- a/src/thread_pool.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef THREAD_POOL_H -#define THREAD_POOL_H - -#include -#include -#include -#include -#include - -namespace threading { -typedef std::function TaskFunc; - -class ThreadPool { -public: ThreadPool(); - ~ThreadPool(); - void ScheduleTask(TaskFunc jobs); - size_t GetThreadCount(); -private: - void ThreadFn(); - std::queue taskQueue{}; - std::vector threads{}; - std::mutex lock{}; - std::condition_variable threadNotifier{}; - bool shutdown{ false }; -}; - -extern ThreadPool tasks; -} - -#endif // !THREAD_POOL_H