ui allocation scheme

This commit is contained in:
Krzosa Karol
2025-01-10 11:14:56 +01:00
parent 74a60e812d
commit 26d8c176a0
6 changed files with 108 additions and 46 deletions

View File

@@ -273,6 +273,7 @@ type_t type__app_frame_t = { type_kind_struct, s8_const_lit("app_frame_t"), size
{.name = s8_const_lit("next"), .type = &(type_t){type_kind_pointer, s8_const_lit("app_frame_t*"), sizeof(void *), .base = &type__app_frame_t}, .offset = offsetof(app_frame_t, next), .dont_serialize = 0},
{.name = s8_const_lit("delta"), .type = &type__f64, .offset = offsetof(app_frame_t, delta), .dont_serialize = 0},
{.name = s8_const_lit("dpr"), .type = &type__f32, .offset = offsetof(app_frame_t, dpr), .dont_serialize = 0},
{.name = s8_const_lit("frame"), .type = &type__u64, .offset = offsetof(app_frame_t, frame), .dont_serialize = 0},
{.name = s8_const_lit("window_size"), .type = &type__v2f32_t, .offset = offsetof(app_frame_t, window_size), .dont_serialize = 0},
{.name = s8_const_lit("mouse_pos"), .type = &type__v2f32_t, .offset = offsetof(app_frame_t, mouse_pos), .dont_serialize = 0},
{.name = s8_const_lit("event_count"), .type = &type__i32, .offset = offsetof(app_frame_t, event_count), .dont_serialize = 0},
@@ -280,5 +281,5 @@ type_t type__app_frame_t = { type_kind_struct, s8_const_lit("app_frame_t"), size
{.name = s8_const_lit("last_event"), .type = &(type_t){type_kind_pointer, s8_const_lit("app_event_t*"), sizeof(void *), .base = &type__app_event_t}, .offset = offsetof(app_frame_t, last_event), .dont_serialize = 0},
{.name = s8_const_lit("update"), .type = &type__f64, .offset = offsetof(app_frame_t, update), .dont_serialize = 0},
},
.count = 9,
.count = 10,
};

View File

@@ -110,6 +110,7 @@ struct app_frame_t {
app_frame_t* next;
f64 delta;
f32 dpr;
u64 frame;
v2f32_t window_size;
v2f32_t mouse_pos;
i32 event_count;

View File

@@ -179,6 +179,7 @@ void meta_app(ma_arena_t *arena) {
app_frame_t *next;
f64 delta;
f32 dpr;
u64 frame;
v2f32_t window_size;
v2f32_t mouse_pos;

View File

@@ -364,6 +364,7 @@ int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int n
frame.mouse_pos = w32_get_mouse_pos(w32_window_handle);
frame.delta = time_delta;
frame.update = time_update;
frame.frame = frame_counter;
b32 waited = w32_get_events(tcx.temp, &frame, wait_for_events);
if (waited) {

View File

@@ -34,35 +34,39 @@ gb globals_t *globals;
fn void app_init(void) {
ma_arena_t *perm = &tcx._perm;
globals = ma_push_type(perm, globals_t);
// globals->gfx = ma_push_type(perm, gfx_t);
ma_arena_t *ui_arena = ma_push_arena(perm, mib(1));
ui_init(ui_arena);
rn_init(perm);
ui_init(perm);
}
fn b32 app_update(app_frame_t *frame) {
ui_begin_frame();
assert(frame != NULL);
ui_begin_frame(frame);
defer_block(ui_begin_build(), ui_end_build()) {
defer_block(ui_push_list_container(), ui_pop_parent()) {
if (ui_push_expander("app_event_t")) {
if (r2f32_contains(r2f32(0, 0, 200, 200), frame->mouse_pos)) {
if (ui_push_expander("mouse_wheel_delta: v3f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("z: f64 = value");
ui_pop_expander();
}
}
ui_label("kind: app_event_kind_t = value");
ui_label("ctrl: b8 = value");
ui_label("shift: b8 = value");
if (ui_push_expander("pos: v2f64_t")) {
if (ui_push_expander("inner_pos: v2f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("x_: f64 = value");
ui_label("y_: f64 = value");
ui_pop_expander();
}
ui_label("y: f64 = value");
if (ui_push_expander("inner_pos: v2f64_t")) {
ui_label("x: f64 = value");
ui_label("y: f64 = value");
ui_label("y__: f64 = value");
if (ui_push_expander("_inner_pos: v2f64_t")) {
ui_label("x____: f64 = value");
ui_label("y____: f64 = value");
ui_pop_expander();
}
ui_pop_expander();
@@ -82,9 +86,9 @@ fn b32 app_update(app_frame_t *frame) {
}
rn_begin();
rn_draw_stringf(&rn_state.main_font, v2f32(0,0), black_color_global, "delta: %f update: %f", frame->delta, frame->update);
ui_draw();
rn_draw_stringf(&rn_state.main_font, v2f32(0,frame->window_size.y - 100), black_color_global, "ui_boxes: %d delta: %f update: %f", ui->allocated_boxes, frame->delta, frame->update);
rn_end(frame->window_size, white_color_global);
#if 0

View File

@@ -26,8 +26,6 @@ struct ui_size_t {
typedef struct ui_box_t ui_box_t;
struct ui_box_t {
ui_id_t id;
// recreated every frame in building code
ui_box_t *next;
ui_box_t *prev;
@@ -38,7 +36,13 @@ struct ui_box_t {
ui_size_t semantic_size[ui_axis2_count];
b32 grow_axis[ui_axis2_count];
// computed by layout system
// between frames, state saving
ui_id_t id; // all is reset every frame up to this
u64 last_frame_touched_index;
ui_box_t *hash_next;
ui_box_t *hash_prev;
// computed by layout system every frame
f32 iter_pos[ui_axis2_count];
f32 computed_rel_pos[ui_axis2_count];
f32 computed_size[ui_axis2_count];
@@ -48,6 +52,7 @@ struct ui_box_t {
typedef struct ui_t ui_t;
struct ui_t {
ma_arena_t *box_arena; // required to be only used for boxes
app_frame_t *frame;
i32 allocated_boxes;
ui_box_t *box_array; // first item on arena
@@ -55,12 +60,24 @@ struct ui_t {
ui_box_t *top;
ui_box_t *first_free;
ui_box_t *first_hash;
ui_box_t *last_hash;
};
ui_t *ui = NULL;
ui_id_t ui_null_id;
ui_id_t ui_id_null;
ui_box_t *ui_alloc_box(ui_id_t id) {
ui_box_t *ui_find_box(ui_id_t id) {
if (id.value == 0) return NULL;
for (ui_box_t *it = ui->first_hash; it; it = it->hash_next) {
if (it->id.value == id.value) {
return it;
}
}
return NULL;
}
ui_box_t *ui_alloc_box(void) {
ui_box_t *result = NULL;
if (ui->first_free) {
SLLS_POP_AND_STORE(ui->first_free, result);
@@ -68,10 +85,22 @@ ui_box_t *ui_alloc_box(ui_id_t id) {
result = ma_push_type(ui->box_arena, ui_box_t);
ui->allocated_boxes += 1;
}
result->id = id;
return result;
}
ui_box_t *ui_build_box(ui_id_t id) {
ui_box_t *box = ui_find_box(id);
if (box) {
memory_zero(box, offsetof(ui_box_t, id));
} else {
box = ui_alloc_box();
DLLQ_APPEND_MOD(ui->first_hash, ui->last_hash, box, hash_next, hash_prev);
}
box->last_frame_touched_index = ui->frame->frame;
box->id = id;
return box;
}
void ui_init(ma_arena_t *arena) {
assert(arena != tcx.temp);
@@ -79,19 +108,20 @@ void ui_init(ma_arena_t *arena) {
ui = ma_push_type(arena, ui_t);
ui->box_arena = arena;
ui->box_array = ui_alloc_box(ui_null_id);
ui->box_array = ma_push_type(arena, ui_box_t);
SLLS_PUSH(ui->first_free, ui->box_array);
}
void ui_begin_build(void) {
ui->top = ui->root = ui_alloc_box(ui_null_id);
ui->top = ui->root = ui_build_box(ui_id_null);
}
void ui_end_build(void) {
assert(ui->top == ui->root);
}
void ui_begin_frame(void) {
void ui_begin_frame(app_frame_t *frame) {
ui->frame = frame;
}
ui_id_t ui_id_from_string(s8_t string) {
@@ -117,8 +147,8 @@ ui_box_t *ui_pop_parent(void) {
}
ui_box_t *ui_spacer(char *spacing_char) {
ui_id_t id = ui_null_id;
ui_box_t *box = ui_alloc_box(id);
ui_id_t id = ui_id_null;
ui_box_t *box = ui_build_box(id);
ui_push_box(box);
v2f32_t spacing = rn_measure_string(&rn_state.main_font, s8_from_char(spacing_char));
box->semantic_size[0] = (ui_size_t){ui_size_kind_pixels, spacing.x, 0.5};
@@ -127,8 +157,8 @@ ui_box_t *ui_spacer(char *spacing_char) {
}
ui_box_t *ui_push_list_container(void) {
ui_id_t id = ui_null_id;
ui_box_t *box = ui_alloc_box(id);
ui_id_t id = ui_id_null;
ui_box_t *box = ui_build_box(id);
ui_push_parent(box);
box->semantic_size[0] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
box->semantic_size[1] = (ui_size_t){ui_size_kind_children_sum, 0, 0.5};
@@ -139,7 +169,7 @@ ui_box_t *ui_push_list_container(void) {
ui_box_t *ui_push_expander(char *str, ...) {
S8_FMT(tcx.temp, str, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *box = ui_alloc_box(id);
ui_box_t *box = ui_build_box(id);
ui_push_box(box);
box->string = string;
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
@@ -163,7 +193,7 @@ void ui_pop_expander() {
ui_box_t *ui_label(char *fmt, ...) {
S8_FMT(tcx.temp, fmt, string);
ui_id_t id = ui_id_from_string(string);
ui_box_t *box = ui_alloc_box(id);
ui_box_t *box = ui_build_box(id);
ui_push_box(box);
box->string = string;
box->semantic_size[0] = (ui_size_t){ui_size_kind_text_content, 0, 0.5};
@@ -231,7 +261,7 @@ void ui_iter_advance_postorder(ui_postorder_iter_t *iter) {
}
void ui_test_stringify_preorder(sb8_t *sb, ui_box_t *box) {
sb8_printf(sb, "%S", box->string);
sb8_printf(sb, "%S\n", box->string);
for (ui_box_t *it = box->first; it; it = it->next) {
ui_test_stringify_preorder(sb, it);
}
@@ -241,20 +271,46 @@ void ui_test_stringify_postorder(sb8_t *sb, ui_box_t *box) {
for (ui_box_t *it = box->first; it; it = it->next) {
ui_test_stringify_postorder(sb, it);
}
sb8_printf(sb, "%S", box->string);
sb8_printf(sb, "%S\n", box->string);
}
// @todo: rename DLLQ_REMOVE_[MOD] to [EX]
void ui_end_frame(void) {
for (i32 i = 0; i < ui->allocated_boxes; i += 1) {
ui_box_t *it = ui->box_array + i;
zero_struct(it);
SLLS_PUSH(ui->first_free, it);
for (ui_box_t *box = ui->first_hash, *next = NULL; box; box = next) {
next = box->hash_next;
b32 touched_this_frame = ui->frame->frame <= box->last_frame_touched_index;
b32 is_null = box->id.value == 0;
if (!touched_this_frame || is_null) {
DLLQ_REMOVE_MOD(ui->first_hash, ui->last_hash, box, hash_next, hash_prev);
zero_struct(box);
SLLS_PUSH(ui->first_free, box);
}
}
}
void ui_draw(void) {
rn_font_t *font = &rn_state.main_font;
{
ma_temp_t scratch = ma_begin_scratch();
sb8_t *sb = sb8_serial_begin(scratch.arena);
ui_test_stringify_preorder(sb, ui->root);
s8_t recursive_string = sb8_serial_end(scratch.arena, sb);
sb = sb8_serial_begin(scratch.arena);
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
sb8_printf(sb, "%S\n", it.box->string);
}
s8_t iter_string = sb8_serial_end(scratch.arena, sb);
debugf("%S", recursive_string);
debugf("ITER:\n%S", iter_string);
assert(s8_equal(recursive_string, iter_string));
ma_end_scratch(scratch);
}
// compute standalone sizes: (pixels, text_content)
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
@@ -304,13 +360,10 @@ void ui_draw(void) {
// solve violations
// compute relative positions
v2f32_t pos = {0,0};
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;
ui_box_t *parent = box->parent;
if (box == ui->root) continue;
if (box == ui->root) continue; // @todo: remove
for (i32 i = 0; i < ui_axis2_count; i += 1) {
f32 *pos = &box->computed_rel_pos[i];
@@ -328,6 +381,7 @@ void ui_draw(void) {
box->rect = r2f32_mindim(pos, size);
}
// actually draw
for (ui_preorder_iter_t it = ui_iterate_preorder(ui->root); ui_preorder_iter_is_valid(it); ui_iter_advance_preorder(&it)) {
ui_box_t *box = it.box;