diff --git a/engine/breeze/defines.h b/engine/breeze/defines.h index d6b7a82..dce74e8 100644 --- a/engine/breeze/defines.h +++ b/engine/breeze/defines.h @@ -22,7 +22,7 @@ typedef double f64; #define BZ_ASSERT(e) assert(e) #define BZ_MAX(a, b) (((a) > (b)) ? (a) : (b)) -#define BZ_MIN(a, b) BZ_MAX(b, a) +#define BZ_MIN(a, b) (((a) < (b)) ? (a) : (b)) #define BZ_ABS(a) ((a >= 0) ? (a) : (-(a))) #define DEBUG_MODE diff --git a/engine/breeze/ui/ui.c b/engine/breeze/ui/ui.c index 124df47..d5ca5bc 100644 --- a/engine/breeze/ui/ui.c +++ b/engine/breeze/ui/ui.c @@ -1,6 +1,7 @@ #include "ui.h" #include "../core/logger.h" #include "../memory/memory.h" +#include "../memory/stack_alloc.h" #include "../util/object_pool.h" #include "../util/string.h" #include "../util/array.h" @@ -58,6 +59,9 @@ typedef struct BzUI { BzUITextShadowStyle *textShadowStyles; BzUIBorderStyle *borderStyles; + // TODO: Use arena (when implemented) instead of stack allocator. + BzStackAlloc strArena; + u64 currFrame; u64 keyIdCount; // Per-frame } BzUI; @@ -108,6 +112,9 @@ BzUI *bzUICreate() { ui->textShadowStyles = bzArrayCreate(BzUITextShadowStyle, 10); ui->borderStyles = bzArrayCreate(BzUIBorderStyle, 10); + // 10kb should be enough + ui->strArena = bzStackAllocCreate(10240); + hmput(ui->nodeMap, ui->root->key, ui->root); return ui; } @@ -123,6 +130,8 @@ void bzUIDestroy(BzUI *ui) { bzArrayDestroy(ui->textShadowStyles); bzArrayDestroy(ui->borderStyles); + bzStackAllocDestroy(&ui->strArena); + bzFree(ui); } @@ -148,6 +157,8 @@ void bzUIBegin(BzUI *ui, i32 width, i32 height) { bzArrayClear(ui->textShadowStyles); bzArrayClear(ui->borderStyles); + bzStackAllocReset(&ui->strArena); + ui->currFrame++; ui->root->lastFrame = ui->currFrame; ui->keyIdCount = 1; @@ -568,6 +579,23 @@ void bzUISetBoxShadowStyle(BzUI *ui, BzUINode *node, BzUIBoxShadowStyle style) { void bzUISetTextStyle(BzUI *ui, BzUINode *node, BzUITextStyle style) { BZ_ASSERT(ui && node); BZ_ASSERT(node->flags & BZ_UI_DRAW_TEXT); + BZ_ASSERT(style.text); + char *str = style.text; + char prev = '\0'; + while (*str) { + if (*str == '#' && prev == '#') { + str--; + break; + } + prev = *str; + str++; + } + size_t strLen = str - style.text; + char *text = bzStackAlloc(&ui->strArena, strLen + 1); + bzMemMove(text, style.text, strLen); + text[strLen] = '\0'; + style.text = text; + BZ_UI_SET_STYLE(ui, node, textStyleIdx, textStyles, style); } void bzUISetTextShadowStyle(BzUI *ui, BzUINode *node, BzUITextShadowStyle style) { diff --git a/engine/breeze/ui/ui.h b/engine/breeze/ui/ui.h index 16626f0..4208e16 100644 --- a/engine/breeze/ui/ui.h +++ b/engine/breeze/ui/ui.h @@ -126,8 +126,7 @@ typedef struct BzUIBorderStyle { } BzUIBorderStyle; typedef struct BzUITextStyle { - const char *text; - + char *text; Font font; f32 fontSize; f32 fontSpacing;