Add basic input parsing from Win32
This commit is contained in:
parent
de4365126d
commit
8c29047ff3
@ -1,7 +1,7 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "dchip8_platform.h"
|
#include "dchip8_platform.h"
|
||||||
|
|
||||||
void dchip8_update(PlatformRenderBuffer *renderBuffer)
|
void dchip8_update(PlatformRenderBuffer *renderBuffer, PlatformInput *input)
|
||||||
{
|
{
|
||||||
ASSERT(renderBuffer.bytesPerPixel == 4);
|
ASSERT(renderBuffer.bytesPerPixel == 4);
|
||||||
|
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
|
|
||||||
#include "dchip8_platform.h"
|
#include "dchip8_platform.h"
|
||||||
|
|
||||||
void dchip8_update(PlatformRenderBuffer *renderBuffer);
|
void dchip8_update(PlatformRenderBuffer *renderBuffer, PlatformInput *input);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -11,4 +11,35 @@ typedef struct PlatformRenderBuffer
|
|||||||
void *memory;
|
void *memory;
|
||||||
} PlatformRenderBuffer;
|
} PlatformRenderBuffer;
|
||||||
|
|
||||||
|
enum Key
|
||||||
|
{
|
||||||
|
key_up,
|
||||||
|
key_down,
|
||||||
|
key_left,
|
||||||
|
key_right,
|
||||||
|
key_escape,
|
||||||
|
key_count,
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct KeyState
|
||||||
|
{
|
||||||
|
bool isDown;
|
||||||
|
u32 transitionCount;
|
||||||
|
} KeyState;
|
||||||
|
|
||||||
|
typedef struct PlatformInput
|
||||||
|
{
|
||||||
|
union {
|
||||||
|
KeyState key[key_count];
|
||||||
|
struct
|
||||||
|
{
|
||||||
|
KeyState up;
|
||||||
|
KeyState down;
|
||||||
|
KeyState left;
|
||||||
|
KeyState right;
|
||||||
|
KeyState escape;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
} PlatformInput;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -87,6 +87,56 @@ FILE_SCOPE void win32_display_render_bitmap(Win32RenderBitmap renderBitmap,
|
|||||||
DeleteDC(alphaBlendDC);
|
DeleteDC(alphaBlendDC);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FILE_SCOPE inline void win32_parse_key_msg(KeyState *key, MSG msg)
|
||||||
|
{
|
||||||
|
WPARAM lParam = msg.lParam;
|
||||||
|
bool keyIsDown = ((lParam >> 30) & 1);
|
||||||
|
bool keyTransitioned = ((lParam >> 31) & 1);
|
||||||
|
|
||||||
|
key->isDown = keyIsDown;
|
||||||
|
if (keyTransitioned) key->transitionCount++;
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE_SCOPE void win32_process_messages(HWND window, PlatformInput *input)
|
||||||
|
{
|
||||||
|
MSG msg;
|
||||||
|
while (PeekMessage(&msg, window, 0, 0, PM_REMOVE))
|
||||||
|
{
|
||||||
|
|
||||||
|
switch (msg.message)
|
||||||
|
{
|
||||||
|
case WM_SYSKEYDOWN:
|
||||||
|
case WM_SYSKEYUP:
|
||||||
|
case WM_KEYDOWN:
|
||||||
|
case WM_KEYUP:
|
||||||
|
{
|
||||||
|
switch (msg.wParam)
|
||||||
|
{
|
||||||
|
case VK_UP: win32_parse_key_msg(&input->up, msg); break;
|
||||||
|
case VK_DOWN: win32_parse_key_msg(&input->down, msg); break;
|
||||||
|
case VK_LEFT: win32_parse_key_msg(&input->left, msg); break;
|
||||||
|
case VK_RIGHT: win32_parse_key_msg(&input->right, msg); break;
|
||||||
|
|
||||||
|
case VK_ESCAPE:
|
||||||
|
{
|
||||||
|
win32_parse_key_msg(&input->escape, msg);
|
||||||
|
if (input->escape.isDown) globalRunning = false;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
TranslateMessage(&msg);
|
||||||
|
DispatchMessage(&msg);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
||||||
LPSTR lpCmdLine, int nShowCmd)
|
LPSTR lpCmdLine, int nShowCmd)
|
||||||
{
|
{
|
||||||
@ -106,7 +156,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||||||
NULL, // HICON hIconSm
|
NULL, // HICON hIconSm
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!RegisterClassEx(&wc)) {
|
if (!RegisterClassEx(&wc))
|
||||||
|
{
|
||||||
win32_error_box(L"RegisterClassEx() failed.", nullptr);
|
win32_error_box(L"RegisterClassEx() failed.", nullptr);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@ -181,52 +232,61 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
|
|||||||
|
|
||||||
while (globalRunning)
|
while (globalRunning)
|
||||||
{
|
{
|
||||||
|
////////////////////////////////////////////////////////////////////////
|
||||||
|
// Update State
|
||||||
|
////////////////////////////////////////////////////////////////////////
|
||||||
LARGE_INTEGER startFrameTime = win32_query_perf_counter_time();
|
LARGE_INTEGER startFrameTime = win32_query_perf_counter_time();
|
||||||
|
|
||||||
MSG msg;
|
|
||||||
while (PeekMessage(&msg, mainWindow, 0, 0, PM_REMOVE))
|
|
||||||
{
|
{
|
||||||
TranslateMessage(&msg);
|
PlatformInput input = {};
|
||||||
DispatchMessage(&msg);
|
win32_process_messages(mainWindow, &input);
|
||||||
|
|
||||||
|
PlatformRenderBuffer platformBuffer = {};
|
||||||
|
platformBuffer.memory = renderBitmap.memory;
|
||||||
|
platformBuffer.height = renderBitmap.height;
|
||||||
|
platformBuffer.width = renderBitmap.width;
|
||||||
|
dchip8_update(&platformBuffer, &input);
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Rendering
|
// Rendering
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
PlatformRenderBuffer renderBuffer = {};
|
{
|
||||||
renderBuffer.memory = renderBitmap.memory;
|
RECT clientRect = {};
|
||||||
renderBuffer.height = renderBitmap.height;
|
GetClientRect(mainWindow, &clientRect);
|
||||||
renderBuffer.width = renderBitmap.width;
|
LONG clientWidth = clientRect.right - clientRect.left;
|
||||||
dchip8_update(&renderBuffer);
|
LONG clientHeight = clientRect.bottom - clientRect.top;
|
||||||
|
|
||||||
HDC deviceContext = GetDC(mainWindow);
|
HDC deviceContext = GetDC(mainWindow);
|
||||||
win32_display_render_bitmap(renderBitmap, deviceContext, clientWidth,
|
win32_display_render_bitmap(renderBitmap, deviceContext,
|
||||||
clientHeight);
|
clientWidth, clientHeight);
|
||||||
ReleaseDC(mainWindow, deviceContext);
|
ReleaseDC(mainWindow, deviceContext);
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
// Frame Limiting
|
// Frame Limiting
|
||||||
////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////
|
||||||
LARGE_INTEGER endWorkTime = win32_query_perf_counter_time();
|
|
||||||
f32 workTimeInS =
|
|
||||||
win32_query_perf_counter_get_time(startFrameTime, endWorkTime);
|
|
||||||
if (workTimeInS < targetSecondsPerFrame)
|
|
||||||
{
|
{
|
||||||
DWORD remainingTimeInMs =
|
LARGE_INTEGER endWorkTime = win32_query_perf_counter_time();
|
||||||
(DWORD)((targetSecondsPerFrame - workTimeInS) * 1000);
|
f32 workTimeInS =
|
||||||
Sleep(remainingTimeInMs);
|
win32_query_perf_counter_get_time(startFrameTime, endWorkTime);
|
||||||
|
if (workTimeInS < targetSecondsPerFrame)
|
||||||
|
{
|
||||||
|
DWORD remainingTimeInMs =
|
||||||
|
(DWORD)((targetSecondsPerFrame - workTimeInS) * 1000);
|
||||||
|
Sleep(remainingTimeInMs);
|
||||||
|
}
|
||||||
|
|
||||||
|
LARGE_INTEGER endFrameTime = win32_query_perf_counter_time();
|
||||||
|
frameTimeInS =
|
||||||
|
win32_query_perf_counter_get_time(startFrameTime, endFrameTime);
|
||||||
|
f32 msPerFrame = 1000.0f * frameTimeInS;
|
||||||
|
|
||||||
|
wchar_t windowTitleBuffer[128] = {};
|
||||||
|
_snwprintf_s(windowTitleBuffer, ARRAY_COUNT(windowTitleBuffer),
|
||||||
|
ARRAY_COUNT(windowTitleBuffer),
|
||||||
|
L"dchip-8 | %5.2f ms/f", msPerFrame);
|
||||||
|
SetWindowText(mainWindow, windowTitleBuffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
LARGE_INTEGER endFrameTime = win32_query_perf_counter_time();
|
|
||||||
frameTimeInS =
|
|
||||||
win32_query_perf_counter_get_time(startFrameTime, endFrameTime);
|
|
||||||
f32 msPerFrame = 1000.0f * frameTimeInS;
|
|
||||||
|
|
||||||
wchar_t windowTitleBuffer[128] = {};
|
|
||||||
_snwprintf_s(windowTitleBuffer, ARRAY_COUNT(windowTitleBuffer),
|
|
||||||
ARRAY_COUNT(windowTitleBuffer), L"dchip-8 | %5.2f ms/f",
|
|
||||||
msPerFrame);
|
|
||||||
SetWindowText(mainWindow, windowTitleBuffer);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user