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
Arena *arena = arena_alloc();
os_event_init();
r_init();
//test_arena(arena);
@ -53,9 +53,31 @@ entry_point()
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();
os_event_reset();
//r_log_debug_messages();
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;
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"
typedef struct OS_Handle OS_Handle;
@ -38,6 +40,7 @@ struct OS_EventList
U64 count;
OS_Event *first;
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_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 */

View File

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

View File

@ -87,7 +87,7 @@ r_init()
.Stereo = FALSE,
.SampleDesc = {1, 0},
.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT,
.BufferCount = 2,
.BufferCount = R_NUM_FRAMES_IN_FLIGHT,
.Scaling = DXGI_SCALING_STRETCH,
.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD,
.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED,
@ -116,7 +116,7 @@ r_init()
// Create Render target view (RTV) descriptor heap
D3D12_DESCRIPTOR_HEAP_DESC rtv_heap_desc = {
.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV,
.NumDescriptors = 2,
.NumDescriptors = R_NUM_FRAMES_IN_FLIGHT,
.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE,
.NodeMask = 0
};
@ -138,7 +138,9 @@ r_init()
D3D12_CPU_DESCRIPTOR_HANDLE 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,
i,
&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;
struct R_D3D12_Command