tor e gullig och jag har borjat pa events

This commit is contained in:
Anton Ljungdahl 2025-05-22 22:06:33 +02:00
parent 82117af3b8
commit e09bc15d70
9 changed files with 377 additions and 128 deletions

View File

@ -34,7 +34,7 @@ entry_point()
// Init systems // Init systems
Arena *arena = arena_alloc(); Arena *arena = arena_alloc();
os_event_init();
r_init(); r_init();
//test_arena(arena); //test_arena(arena);
@ -53,9 +53,31 @@ entry_point()
DispatchMessageW(&Message); DispatchMessageW(&Message);
} }
for(U64 i = 0; i < os_events_received_on_frame(); i += 1)
{
OS_Event *event = &os_events.list[i];
if(event->kind != OS_EventKind_Null)
{
LOG("%s ", os_key_display_string[event->key]);
if(event->kind == OS_EventKind_Release)
{
LOG(" RELEASE");
}
if(event->kind == OS_EventKind_Press)
{
LOG(" PRESS");
}
LOG("\n");
}
}
r_render(); r_render();
os_event_reset();
//r_log_debug_messages(); //r_log_debug_messages();
if(g_do_exit) if(g_do_exit)
{ {

View File

@ -0,0 +1,117 @@
char * os_key_display_string[113] =
{
"Null",
"Esc",
"F1",
"F2",
"F3",
"F4",
"F5",
"F6",
"F7",
"F8",
"F9",
"F10",
"F11",
"F12",
"F13",
"F14",
"F15",
"F16",
"F17",
"F18",
"F19",
"F20",
"F21",
"F22",
"F23",
"F24",
"Tick",
"0",
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"Minus",
"Equal",
"Backspace",
"Tab",
"Q",
"W",
"E",
"R",
"T",
"Y",
"U",
"I",
"O",
"P",
"LeftBracket",
"RightBracket",
"BackSlash",
"CapsLock",
"A",
"S",
"D",
"F",
"G",
"H",
"J",
"K",
"L",
"Semicolon",
"Quote",
"Return",
"Shift",
"Z",
"X",
"C",
"V",
"B",
"N",
"M",
"Comma",
"Period",
"Slash",
"Ctrl",
"Alt",
"Space",
"Menu",
"ScrollLock",
"Pause",
"Insert",
"Home",
"PageUp",
"Delete",
"End",
"PageDown",
"Up",
"Left",
"Down",
"Right",
"NumLock",
"NumSlash",
"NumStar",
"NumMinus",
"NumPlus",
"NumPeriod",
"Num0",
"Num1",
"Num2",
"Num3",
"Num4",
"Num5",
"Num6",
"Num7",
"Num8",
"Num9",
"LeftMouseButton",
"MiddleMouseButton",
"RightMouseButton",
};

View File

@ -117,3 +117,5 @@ typedef enum OS_Key
} }
OS_Key; OS_Key;
extern char * os_key_display_string[113];

View File

@ -1,2 +1,3 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~ //~
#include "os/generated/os_core.meta.c"

View File

@ -3,6 +3,8 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~ //~
#define OS_MAX_EVENTS 2048
#include "os/generated/os_core.meta.h" #include "os/generated/os_core.meta.h"
typedef struct OS_Handle OS_Handle; typedef struct OS_Handle OS_Handle;
@ -38,6 +40,7 @@ struct OS_EventList
U64 count; U64 count;
OS_Event *first; OS_Event *first;
OS_Event *last; OS_Event *last;
OS_Event list[OS_MAX_EVENTS];
}; };
@ -50,4 +53,9 @@ internal B32 os_commit(void *ptr, U64 size);
internal void os_decommit(void *ptr, U64 size); internal void os_decommit(void *ptr, U64 size);
internal void os_release(void *ptr, U64 size); internal void os_release(void *ptr, U64 size);
internal U64 os_events_idx();
internal void os_event_init();
internal U64 os_events_received_on_frame();
internal void os_event_reset();
#endif /* OS_CORE_H */ #endif /* OS_CORE_H */

View File

@ -4,122 +4,122 @@
//~ Table with key enums. //~ Table with key enums.
// TODO(anton): Extend this table to include description strings etc when necessary. // TODO(anton): Extend this table to include description strings etc when necessary.
@table(name) @table(name, display_string)
OS_KeyTable: OS_KeyTable:
{ {
{Null } {Null, "Null" }
{Esc } {Esc, "Esc" }
{F1 } {F1, "F1" }
{F2 } {F2, "F2" }
{F3 } {F3, "F3" }
{F4 } {F4, "F4" }
{F5 } {F5, "F5" }
{F6 } {F6, "F6" }
{F7 } {F7, "F7" }
{F8 } {F8, "F8" }
{F9 } {F9, "F9" }
{F10 } {F10, "F10" }
{F11 } {F11, "F11" }
{F12 } {F12, "F12" }
{F13 } {F13, "F13" }
{F14 } {F14, "F14" }
{F15 } {F15, "F15" }
{F16 } {F16, "F16" }
{F17 } {F17, "F17" }
{F18 } {F18, "F18" }
{F19 } {F19, "F19" }
{F20 } {F20, "F20" }
{F21 } {F21, "F21" }
{F22 } {F22, "F22" }
{F23 } {F23, "F23" }
{F24 } {F24, "F24" }
{Tick } {Tick, "Tick" }
{0 } {0, "0" }
{1 } {1, "1" }
{2 } {2, "2" }
{3 } {3, "3" }
{4 } {4, "4" }
{5 } {5, "5" }
{6 } {6, "6" }
{7 } {7, "7" }
{8 } {8, "8" }
{9 } {9, "9" }
{Minus } {Minus, "Minus" }
{Equal } {Equal, "Equal" }
{Backspace } {Backspace, "Backspace" }
{Tab } {Tab, "Tab" }
{Q } {Q, "Q" }
{W } {W, "W" }
{E } {E, "E" }
{R } {R, "R" }
{T } {T, "T" }
{Y } {Y, "Y" }
{U } {U, "U" }
{I } {I, "I" }
{O } {O, "O" }
{P } {P, "P" }
{LeftBracket } {LeftBracket, "LeftBracket" }
{RightBracket } {RightBracket, "RightBracket" }
{BackSlash } {BackSlash, "BackSlash" }
{CapsLock } {CapsLock, "CapsLock" }
{A } {A, "A" }
{S } {S, "S" }
{D } {D, "D" }
{F } {F, "F" }
{G } {G, "G" }
{H } {H, "H" }
{J } {J, "J" }
{K } {K, "K" }
{L } {L, "L" }
{Semicolon } {Semicolon, "Semicolon" }
{Quote } {Quote, "Quote" }
{Return } {Return, "Return" }
{Shift } {Shift, "Shift" }
{Z } {Z, "Z" }
{X } {X, "X" }
{C } {C, "C" }
{V } {V, "V" }
{B } {B, "B" }
{N } {N, "N" }
{M } {M, "M" }
{Comma } {Comma, "Comma" }
{Period } {Period, "Period" }
{Slash } {Slash, "Slash" }
{Ctrl } {Ctrl, "Ctrl" }
{Alt } {Alt, "Alt" }
{Space } {Space, "Space" }
{Menu } {Menu, "Menu" }
{ScrollLock } {ScrollLock, "ScrollLock" }
{Pause } {Pause, "Pause" }
{Insert } {Insert, "Insert" }
{Home } {Home, "Home" }
{PageUp } {PageUp, "PageUp" }
{Delete } {Delete, "Delete" }
{End } {End, "End" }
{PageDown } {PageDown, "PageDown" }
{Up } {Up, "Up" }
{Left } {Left, "Left" }
{Down } {Down, "Down" }
{Right } {Right, "Right" }
{NumLock } {NumLock, "NumLock" }
{NumSlash } {NumSlash, "NumSlash" }
{NumStar } {NumStar, "NumStar" }
{NumMinus } {NumMinus, "NumMinus" }
{NumPlus } {NumPlus, "NumPlus" }
{NumPeriod } {NumPeriod, "NumPeriod" }
{Num0 } {Num0, "Num0" }
{Num1 } {Num1, "Num1" }
{Num2 } {Num2, "Num2" }
{Num3 } {Num3, "Num3" }
{Num4 } {Num4, "Num4" }
{Num5 } {Num5, "Num5" }
{Num6 } {Num6, "Num6" }
{Num7 } {Num7, "Num7" }
{Num8 } {Num8, "Num8" }
{Num9 } {Num9, "Num9" }
{LeftMouseButton } {LeftMouseButton, "LeftMouseButton" }
{MiddleMouseButton } {MiddleMouseButton, "MiddleMouseButton"}
{RightMouseButton } {RightMouseButton, "RightMouseButton" }
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -131,3 +131,9 @@ OS_KeyTable:
` OS_Key_$(a.name),`; ` OS_Key_$(a.name),`;
` OS_Key_COUNT` ` OS_Key_COUNT`
} }
@table_gen_data(`char *`) os_key_display_string:
{
@expand(OS_KeyTable a)
`"$(a.display_string)",`;
}

View File

@ -2,12 +2,78 @@
//~ //~
global HWND g_win32_window_handle; global HWND g_win32_window_handle;
global OS_EventList os_events = {0};
global U64 os_events_free_idx = 0;
global U32 os_events_reached_max = 0;
internal void
os_event_init()
{
for(U64 i = 0; i < OS_MAX_EVENTS; i+= 1)
{
os_events.list[i].kind = OS_EventKind_Null;
}
os_events.count = OS_MAX_EVENTS;
os_events_free_idx = 0;
os_events_reached_max = 0;
}
internal U64
os_events_idx()
{
U64 current = os_events_free_idx;
os_events_free_idx += 1;
if(os_events_free_idx == os_events.count)
{
os_events_free_idx = 0;
os_events_reached_max = 1;
}
return current;
}
internal U64
os_events_received_on_frame()
{
U64 out;
if(os_events_reached_max)
{
out = os_events.count;
}
else
{
out = os_events_free_idx + 1;
}
return out;
}
internal void
os_event_reset()
{
os_events_free_idx = 0;
os_events_reached_max = 0;
}
internal OS_Event *
os_event_push(OS_EventKind kind, OS_Handle window)
{
U64 idx = os_events_idx();
OS_Event *out = &os_events.list[idx];
out->window = window;
out->kind = kind;
return out;
}
internal LRESULT CALLBACK internal LRESULT CALLBACK
DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam) DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
{ {
OS_Handle window = {0};
window.handle[0] = (U64)Window;
LRESULT Result = 0; U32 release = 0;
LRESULT result = 0;
switch (Message) switch (Message)
{ {
case WM_QUIT: case WM_QUIT:
@ -19,18 +85,42 @@ DisplayWndProc(HWND Window, UINT Message, WPARAM WParam, LPARAM LParam)
// NOTE(casey): Anything you want the application to handle, forward to the main thread // NOTE(casey): Anything you want the application to handle, forward to the main thread
// here. // here.
case WM_MOUSEMOVE: case WM_MOUSEMOVE:
case WM_LBUTTONDOWN: {
case WM_LBUTTONUP:
case WM_CHAR:
{
} break; } break;
case WM_LBUTTONUP:
case WM_RBUTTONUP:
case WM_MBUTTONUP:
{
release = 1;
} // Fallthrough to down, why?
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
{
OS_Event *event = os_event_push(release ? OS_EventKind_Release : OS_EventKind_Press, window);
switch(Message)
{
case WM_LBUTTONUP: case WM_LBUTTONDOWN:
{
event->key = OS_Key_LeftMouseButton;
} break;
case WM_RBUTTONUP: case WM_RBUTTONDOWN:
{
event->key = OS_Key_RightMouseButton;
} break;
case WM_MBUTTONUP: case WM_MBUTTONDOWN:
{
event->key = OS_Key_MiddleMouseButton;
} break;
}
} break;
default: default:
{ {
Result = DefWindowProcW(Window, Message, WParam, LParam); result = DefWindowProcW(Window, Message, WParam, LParam);
} break; } break;
} }
return Result; return result;
} }
internal DWORD WINAPI internal DWORD WINAPI

View File

@ -87,7 +87,7 @@ r_init()
.Stereo = FALSE, .Stereo = FALSE,
.SampleDesc = {1, 0}, .SampleDesc = {1, 0},
.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT, .BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
.BufferCount = 2, .BufferCount = R_NUM_FRAMES_IN_FLIGHT,
.Scaling = DXGI_SCALING_STRETCH, .Scaling = DXGI_SCALING_STRETCH,
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD, .SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD,
.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED, .AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED,
@ -116,7 +116,7 @@ r_init()
// Create Render target view (RTV) descriptor heap // Create Render target view (RTV) descriptor heap
D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc = { D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc = {
.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV, .Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
.NumDescriptors = 2, .NumDescriptors = R_NUM_FRAMES_IN_FLIGHT,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE, .Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
.NodeMask = 0 .NodeMask = 0
}; };
@ -138,7 +138,9 @@ r_init()
D3D12_CPU_DESCRIPTOR_HANDLE handle; D3D12_CPU_DESCRIPTOR_HANDLE handle;
ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(r_d3d12_rt->heap, &handle); ID3D12DescriptorHeap_GetCPUDescriptorHandleForHeapStart(r_d3d12_rt->heap, &handle);
for (U32 i = 0; i < 2; ++i) { for (U32 i = 0; i < R_NUM_FRAMES_IN_FLIGHT; i += 1)
{
IDXGISwapChain3_GetBuffer(r_d3d12_state->swapchain, IDXGISwapChain3_GetBuffer(r_d3d12_state->swapchain,
i, i,
&IID_ID3D12Resource, &IID_ID3D12Resource,

View File

@ -8,12 +8,13 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~ //~
#define R_NUM_FRAMES_IN_FLIGHT 3
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//~ //~
#define R_NUM_FRAMES_IN_FLIGHT 2
typedef struct R_D3D12_Command R_D3D12_Command; typedef struct R_D3D12_Command R_D3D12_Command;
struct R_D3D12_Command struct R_D3D12_Command