Copy text strings into memory arena for UI

This commit is contained in:
2023-12-22 16:46:20 +01:00
parent 331f40f4cb
commit b48372bfa5
3 changed files with 30 additions and 3 deletions

View File

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

View File

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

View File

@@ -126,8 +126,7 @@ typedef struct BzUIBorderStyle {
} BzUIBorderStyle;
typedef struct BzUITextStyle {
const char *text;
char *text;
Font font;
f32 fontSize;
f32 fontSpacing;