From bf13f269fe27271db43445db67332a8e156888cd Mon Sep 17 00:00:00 2001 From: Patedam Date: Sat, 24 Oct 2026 18:17:43 -0400 Subject: [PATCH] almost there (open window) --- Juliet/core/core.c | 2 +- Juliet/core/core.h | 27 +++++++++------- Juliet/core/core_arena.c | 2 +- Juliet/core/core_hash.c | 3 +- Juliet/julietapp/julietapp_core.c | 53 ++++++++++++++++++++++++++----- Juliet/julietapp/julietapp_core.h | 26 +++++++++++---- 6 files changed, 84 insertions(+), 29 deletions(-) diff --git a/Juliet/core/core.c b/Juliet/core/core.c index e63d834..26b2208 100644 --- a/Juliet/core/core.c +++ b/Juliet/core/core.c @@ -27,4 +27,4 @@ internal int32 safe_cast_int32(int64 val) #define STB_SPRINTF_IMPLEMENTATION #define STB_SPRINTF_STATIC -#include "external/stb/stb_sprintf.h" +#include "external/stb_sprintf/stb_sprintf.h" diff --git a/Juliet/core/core.h b/Juliet/core/core.h index d5d8444..8bff571 100644 --- a/Juliet/core/core.h +++ b/Juliet/core/core.h @@ -7,7 +7,7 @@ #define STB_SPRINTF_DECORATE(name) juliet_##name #define STB_SPRINTF_STATIC -#include "external/stb/stb_sprintf.h" +#include "external/stb_sprintf/stb_sprintf.h" #define internal static #define global static @@ -119,30 +119,33 @@ #define CheckNil(nil, ptr) ((ptr) == 0 || (ptr) == nil) #define SetNil(nil, ptr) ((ptr) = nil) +// Doubly-linked-lists // clang-format off -#define DLL_insert_NPZ(nil, first, last, ptr, node, next, previous) \ +#define DLL_insert_next_prev_zero(nil, first, last, ptr, node, next, previous) \ (CheckNil(nil, first) ? ((first) = (last) = (node), SetNil(nil, (node)->next), SetNil(nil, (node)->previous)) \ : CheckNil(nil, ptr) ? ((node)->next = (first), (first)->previous = (node), (first) = (node), SetNil(nil, (node)->previous)) \ : ((ptr) == (last)) ? ((last)->next = (node), (node )->previous = (last), (last ) = (node), SetNil(nil, (node)->next)) \ : (((CheckNil(nil, ptr) && CheckNil(nil, (ptr)->next)) ? (0) : ((ptr)->next->previous = (node))), \ ((node)->next = (ptr)->next), ((ptr)->next = (node)), ((node)->previous = (ptr)))) -// clang-format on -#define DLL_push_back_NPZ(nil, first, last, node, next, previous) \ - DLL_insert_NPZ(nil, first, last, last, node, next, previous) -#define DLL_remove_NPZ(nil, first, last, node, next, previous) \ +#define DLL_push_back_next_prev_zero(nil, first, last, node, next, previous) \ + DLL_insert_next_prev_zero(nil, first, last, last, node, next, previous) +#define DLL_remove_next_prev_zero(nil, first, last, node, next, previous) \ (((node) == (first) ? (first) = (node)->next : (0)), ((node) == (last) ? (last) = (last)->previous : (0)), \ (CheckNil(nil, (node)->previous) ? (0) : ((node)->previous->next = (node)->next)), \ (CheckNil(nil, (node)->next) ? (0) : ((node)->next->previous = (node)->previous))) +// clang-format on // Single (link & head) lists (= stacks) -#define SLL_stack_push_node(first, node, next) ((node)->next = (first), (first) = (node)) -#define SLL_stack_pop_node(first, next) ((first) = (first)->next); -#define SLL_stack_push(first, node) SLL_stack_push_node(first, node, next) -#define SLL_stack_pop(first) SLL_stack_pop_node(first, next) +#define SLL_stack_push_next(first, node, next) ((node)->next = (first), (first) = (node)) +#define SLL_stack_pop_next(first, next) ((first) = (first)->next); +#define SLL_stack_push(first, node) SLL_stack_push_next(first, node, next) +#define SLL_stack_pop(first) SLL_stack_pop_next(first, next) // Double linked list helpers -#define DLL_push_back(first, last, node) DLL_push_back_NPZ(0, first, last, node, next, previous) -#define DLL_remove(first, last, node) DLL_remove_NPZ(0, first, last, node, next, previous) +#define DLL_push_back_next_prev(first, last, node, next, previous) \ + DLL_push_back_next_prev_zero(0, first, last, node, next, previous) +#define DLL_push_back(first, last, node) DLL_push_back_next_prev_zero(0, first, last, node, next, previous) +#define DLL_remove(first, last, node) DLL_remove_next_prev_zero(0, first, last, node, next, previous) // ============================================================================= // Helpers diff --git a/Juliet/core/core_arena.c b/Juliet/core/core_arena.c index c6a36db..339f3a6 100644 --- a/Juliet/core/core_arena.c +++ b/Juliet/core/core_arena.c @@ -101,7 +101,7 @@ internal void* arena_push(Arena* arena, uint64 size, uint64 align, bool32 zero) } new_block->base_position = current->base_position + current->reserved; - SLL_stack_push_node(arena->current, new_block, previous); + SLL_stack_push_next(arena->current, new_block, previous); current = new_block; pos_pre = AlignPow2(current->position, align); diff --git a/Juliet/core/core_hash.c b/Juliet/core/core_hash.c index 767f96e..8f29e3b 100644 --- a/Juliet/core/core_hash.c +++ b/Juliet/core/core_hash.c @@ -21,5 +21,6 @@ internal uint64 uint64_hash_from_string(String8 string) internal uint64 uint64_hash_from_seed_string(uint64 seed, String8 string) { - return 0; + uint64 result = XXH3_64bits_withSeed(string.str, string.size, seed); + return result; } diff --git a/Juliet/julietapp/julietapp_core.c b/Juliet/julietapp/julietapp_core.c index 60d8b65..393b34f 100644 --- a/Juliet/julietapp/julietapp_core.c +++ b/Juliet/julietapp/julietapp_core.c @@ -5,8 +5,12 @@ internal void ja_init() ja_state->arena = arena; // Window cache - ja_state->window_state_gen_id = 1; - ja_state->first_window_state = ja_state->last_window_state = &ja_nil_window_state; + { + ja_state->window_state_gen_id = 1; + ja_state->window_state_slots_count = 64; + ja_state->window_state_slots = push_array(arena, JA_WindowStateSlot, ja_state->window_state_slots_count); + ja_state->first_window_state = ja_state->last_window_state = &ja_nil_window_state; + } } internal void ja_frame() @@ -46,11 +50,10 @@ internal void ja_frame() // Window management (open/render) { - for - EachIndex(slot_idx, ja_state->window_state_gen_id) - { - JA_WindowState* ws = ja_window_state_from_id(slot_idx); - } + for (uint64 id = 1; id < ja_state->window_state_gen_id + 1; ++id) + { + JA_WindowState* ws = ja_window_state_from_id(id); + } } scratch_end(scratch); @@ -75,10 +78,44 @@ internal JA_WindowState* ja_window_state_from_id(JA_ID id) } else { - uint64 hash = uint64_hash_from_string(str8_struct(&id)); + uint64 hash = uint64_hash_from_string(str8_struct(&id)); + uint64 slot_idx = hash % ja_state->window_state_slots_count; + JA_WindowStateSlot* slot = &ja_state->window_state_slots[slot_idx]; + for (JA_WindowState* w = slot->first; w != 0; w = w->hash_next) + { + if (w->id == id) + { + ws = w; + break; + } + } } // allocate / open window if none found for this id + if (ws == &ja_nil_window_state) + { + ws = ja_state->free_window_state; + if (ws != 0) + { + SLL_stack_pop_next(ja_state->free_window_state, order_next); + } + else + { + ws = push_array_no_zero(ja_state->arena, JA_WindowState, 1); + } + MemoryZeroStruct(ws); + + ws->id = id; + ws->arena = arena_alloc(); + + // hook up the window + uint64 hash = uint64_hash_from_string(str8_struct(&id)); + uint64 slot_idx = hash % ja_state->window_state_slots_count; + JA_WindowStateSlot* slot = &ja_state->window_state_slots[slot_idx]; + DLL_push_back_next_prev_zero(&ja_nil_window_state, ja_state->first_window_state, ja_state->last_window_state, + ws, order_next, order_previous); + DLL_push_back_next_prev(slot->first, slot->last, ws, hash_next, hash_previous); + } return ws; } diff --git a/Juliet/julietapp/julietapp_core.h b/Juliet/julietapp/julietapp_core.h index 10115a7..8a881c0 100644 --- a/Juliet/julietapp/julietapp_core.h +++ b/Juliet/julietapp/julietapp_core.h @@ -10,12 +10,23 @@ typedef uint64 JA_ID; typedef struct JA_WindowState JA_WindowState; struct JA_WindowState { + // links and metadata JA_WindowState* order_next; JA_WindowState* order_previous; JA_WindowState* hash_next; JA_WindowState* hash_previous; JA_ID id; - WM_Window plat_handle; + + // Info and handle + Arena* arena; + WM_Window plat_handle; +}; + +typedef struct JA_WindowStateSlot JA_WindowStateSlot; +struct JA_WindowStateSlot +{ + JA_WindowState* first; + JA_WindowState* last; }; typedef struct JA_State JA_State; @@ -26,11 +37,14 @@ struct JA_State bool32 quit; // Windows - JA_WindowState* first_window_state; - JA_WindowState* last_window_state; - JA_WindowState* window_state_last_accessed; - JA_ID window_state_last_accessed_id; - JA_ID window_state_gen_id; + JA_WindowState* free_window_state; + uint64 window_state_slots_count; + JA_WindowStateSlot* window_state_slots; + JA_WindowState* first_window_state; + JA_WindowState* last_window_state; + JA_ID window_state_last_accessed_id; + JA_ID window_state_gen_id; + JA_WindowState* window_state_last_accessed; }; // =============================================================================