diff --git a/Juliet/julietapp/julietapp_core.c b/Juliet/julietapp/julietapp_core.c index ef58625..2c6a6bb 100644 --- a/Juliet/julietapp/julietapp_core.c +++ b/Juliet/julietapp/julietapp_core.c @@ -38,11 +38,26 @@ internal void ja_frame() { for (WM_Event *event = events.first, *next = 0; event != 0; event = next) { - bool32 take = 0; + JA_WindowState* ws = ja_window_state_from_platform_handle(event->window); + bool32 take = 0; if (event->kind == WM_EventKind_WindowClose) { - ja_state->quit = 1; - take = 1; + if (ws != 0) + { + JA_WindowRequest* request = ja_window_request_from_id(ws->id); + AssertAlways(request != 0); + ja_window_close_request(request); + + if (ja_state->window_request_count == 0) + { + ja_state->quit = 1; + } + } + else + { + ja_state->quit = 1; + } + take = 1; } if (take) @@ -220,6 +235,23 @@ internal JA_WindowState* ja_window_state_from_id(JA_ID id) return ws; } +internal JA_WindowState* ja_window_state_from_platform_handle(WM_Window platform_handle) +{ + JA_WindowState* ws = &ja_nil_window_state; + { + for (JA_WindowState* w_candidate = ja_state->first_window_state; w_candidate != &ja_nil_window_state; + w_candidate = w_candidate->order_next) + { + if (wm_window_match(w_candidate->plat_handle, platform_handle)) + { + ws = w_candidate; + break; + } + } + } + return ws; +} + internal void ja_window_request() { JA_ID id = ja_state->window_request_gen_id++; @@ -234,12 +266,16 @@ internal void ja_window_request() } result->id = id; DLL_push_back(ja_state->window_request_first, ja_state->window_request_last, result); + ja_state->window_request_count += 1; } internal void ja_window_close_request(JA_WindowRequest* request) { DLL_remove(ja_state->window_request_first, ja_state->window_request_last, request); SLL_stack_push(ja_state->free_window_request, request); + + ja_state->window_request_count -= 1; + AssertAlways(ja_state->window_request_count >= 0); } internal JA_WindowRequest* ja_window_request_from_id(JA_ID id) @@ -248,8 +284,11 @@ internal JA_WindowRequest* ja_window_request_from_id(JA_ID id) for EachNode(wr, JA_WindowRequest, ja_state->window_request_first) { - result = wr; - break; + if (wr->id == id) + { + result = wr; + break; + } } return result; } diff --git a/Juliet/julietapp/julietapp_core.h b/Juliet/julietapp/julietapp_core.h index ecaa6f2..b7f4499 100644 --- a/Juliet/julietapp/julietapp_core.h +++ b/Juliet/julietapp/julietapp_core.h @@ -66,6 +66,7 @@ struct JA_State JA_WindowRequest* window_request_first; JA_WindowRequest* window_request_last; JA_WindowRequest* free_window_request; + uint64 window_request_count; // Windows JA_WindowState* free_window_state; @@ -100,6 +101,7 @@ internal void ja_id_list_push(Arena* arena, JA_IDList* list, JA_ID id); // Window Functions internal JA_WindowState* ja_window_state_from_id(JA_ID id); +internal JA_WindowState* ja_window_state_from_platform_handle(WM_Window platform_handle); internal void ja_window_request(); internal void ja_window_close_request(JA_WindowRequest* request); internal JA_WindowRequest* ja_window_request_from_id(JA_ID id);