Fix screen not displaying at startup until resize

This commit is contained in:
Doyle Thai 2017-04-07 14:21:21 +10:00
parent 2aa5bffb79
commit d997f1e945
2 changed files with 87 additions and 61 deletions

View File

@ -287,7 +287,7 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
if (cpu.state == chip8state_load_file) if (cpu.state == chip8state_load_file)
{ {
PlatformFile file = {}; PlatformFile file = {};
if (platform_open_file(L"roms/TETRIS", &file)) if (platform_open_file(L"roms/PONG", &file))
{ {
DQNT_ASSERT((cpu.INIT_ADDRESS + file.size) <= DQNT_ASSERT((cpu.INIT_ADDRESS + file.size) <=
memory.permanentMemSize); memory.permanentMemSize);

View File

@ -13,50 +13,6 @@
#include "dchip8_platform.h" #include "dchip8_platform.h"
#include "dqnt.h" #include "dqnt.h"
FILE_SCOPE bool globalRunning = false;
FILE_SCOPE LARGE_INTEGER globalQueryPerformanceFrequency;
#define win32_error_box(text, title) MessageBox(nullptr, text, title, MB_OK);
FILE_SCOPE LRESULT CALLBACK win32_main_proc_callback(HWND window, UINT msg,
WPARAM wParam,
LPARAM lParam)
{
LRESULT result = 0;
switch (msg)
{
case WM_CLOSE:
case WM_DESTROY:
{
globalRunning = false;
}
break;
default:
{
result = DefWindowProc(window, msg, wParam, lParam);
}
break;
}
return result;
}
inline FILE_SCOPE f32 win32_query_perf_counter_get_time(LARGE_INTEGER start,
LARGE_INTEGER end)
{
f32 result = (f32)(end.QuadPart - start.QuadPart) /
globalQueryPerformanceFrequency.QuadPart;
return result;
}
inline FILE_SCOPE LARGE_INTEGER win32_query_perf_counter_time()
{
LARGE_INTEGER result;
QueryPerformanceCounter(&result);
return result;
}
typedef struct Win32RenderBitmap typedef struct Win32RenderBitmap
{ {
BITMAPINFO info; BITMAPINFO info;
@ -67,6 +23,20 @@ typedef struct Win32RenderBitmap
void *memory; void *memory;
} Win32RenderBitmap; } Win32RenderBitmap;
FILE_SCOPE bool globalRunning = false;
FILE_SCOPE Win32RenderBitmap globalRenderBitmap;
FILE_SCOPE LARGE_INTEGER globalQueryPerformanceFrequency;
#define win32_error_box(text, title) MessageBox(nullptr, text, title, MB_OK);
inline FILE_SCOPE void win32_get_client_dim(HWND window, LONG *width,
LONG *height)
{
RECT clientRect = {};
GetClientRect(window, &clientRect);
*width = clientRect.right - clientRect.left;
*height = clientRect.bottom - clientRect.top;
}
FILE_SCOPE void win32_display_render_bitmap(Win32RenderBitmap renderBitmap, FILE_SCOPE void win32_display_render_bitmap(Win32RenderBitmap renderBitmap,
HDC deviceContext, LONG width, HDC deviceContext, LONG width,
LONG height) LONG height)
@ -91,10 +61,61 @@ FILE_SCOPE void win32_display_render_bitmap(Win32RenderBitmap renderBitmap,
OutputDebugString(L"AlphaBlend() failed."); OutputDebugString(L"AlphaBlend() failed.");
} }
#endif #endif
DeleteDC(stretchDC); DeleteDC(stretchDC);
} }
FILE_SCOPE LRESULT CALLBACK win32_main_proc_callback(HWND window, UINT msg,
WPARAM wParam,
LPARAM lParam)
{
LRESULT result = 0;
switch (msg)
{
case WM_CLOSE:
case WM_DESTROY:
{
globalRunning = false;
}
break;
case WM_PAINT:
{
PAINTSTRUCT paint;
HDC deviceContext = BeginPaint(window, &paint);
LONG clientWidth, clientHeight;
win32_get_client_dim(window, &clientWidth, &clientHeight);
win32_display_render_bitmap(globalRenderBitmap, deviceContext,
clientWidth, clientHeight);
EndPaint(window, &paint);
break;
}
default:
{
result = DefWindowProc(window, msg, wParam, lParam);
}
break;
}
return result;
}
inline FILE_SCOPE f32 win32_query_perf_counter_get_time(LARGE_INTEGER start,
LARGE_INTEGER end)
{
f32 result = (f32)(end.QuadPart - start.QuadPart) /
globalQueryPerformanceFrequency.QuadPart;
return result;
}
inline FILE_SCOPE LARGE_INTEGER win32_query_perf_counter_time()
{
LARGE_INTEGER result;
QueryPerformanceCounter(&result);
return result;
}
FILE_SCOPE inline void win32_parse_key_msg(KeyState *key, MSG msg) FILE_SCOPE inline void win32_parse_key_msg(KeyState *key, MSG msg)
{ {
WPARAM lParam = msg.lParam; WPARAM lParam = msg.lParam;
@ -217,7 +238,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
return -1; return -1;
} }
Win32RenderBitmap renderBitmap = {};
{ // Initialise the renderbitmap { // Initialise the renderbitmap
BITMAPINFOHEADER header = {}; BITMAPINFOHEADER header = {};
header.biSize = sizeof(BITMAPINFOHEADER); header.biSize = sizeof(BITMAPINFOHEADER);
@ -232,20 +252,20 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
header.biClrUsed = 0; header.biClrUsed = 0;
header.biClrImportant = 0; header.biClrImportant = 0;
renderBitmap.info.bmiHeader = header; globalRenderBitmap.info.bmiHeader = header;
renderBitmap.width = header.biWidth; globalRenderBitmap.width = header.biWidth;
renderBitmap.height = header.biHeight; globalRenderBitmap.height = header.biHeight;
renderBitmap.bytesPerPixel = header.biBitCount / 8; globalRenderBitmap.bytesPerPixel = header.biBitCount / 8;
DQNT_ASSERT(renderBitmap.bytesPerPixel >= 1); DQNT_ASSERT(globalRenderBitmap.bytesPerPixel >= 1);
HDC deviceContext = GetDC(mainWindow); HDC deviceContext = GetDC(mainWindow);
renderBitmap.handle = globalRenderBitmap.handle = CreateDIBSection(
CreateDIBSection(deviceContext, &renderBitmap.info, DIB_RGB_COLORS, deviceContext, &globalRenderBitmap.info, DIB_RGB_COLORS,
&renderBitmap.memory, NULL, NULL); &globalRenderBitmap.memory, NULL, NULL);
ReleaseDC(mainWindow, deviceContext); ReleaseDC(mainWindow, deviceContext);
} }
if (!renderBitmap.memory) if (!globalRenderBitmap.memory)
{ {
win32_error_box(L"CreateDIBSection() failed.", nullptr); win32_error_box(L"CreateDIBSection() failed.", nullptr);
return -1; return -1;
@ -277,11 +297,17 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
win32_process_messages(mainWindow, &platformInput); win32_process_messages(mainWindow, &platformInput);
PlatformRenderBuffer platformBuffer = {}; PlatformRenderBuffer platformBuffer = {};
platformBuffer.memory = renderBitmap.memory; platformBuffer.memory = globalRenderBitmap.memory;
platformBuffer.height = renderBitmap.height; platformBuffer.height = globalRenderBitmap.height;
platformBuffer.width = renderBitmap.width; platformBuffer.width = globalRenderBitmap.width;
platformBuffer.bytesPerPixel = renderBitmap.bytesPerPixel; platformBuffer.bytesPerPixel = globalRenderBitmap.bytesPerPixel;
dchip8_update(platformBuffer, platformInput, platformMemory); dchip8_update(platformBuffer, platformInput, platformMemory);
for (i32 i = 0; i < DQNT_ARRAY_COUNT(platformInput.key); i++)
{
if (platformInput.key[i].isDown)
OutputDebugString(L"Key is down\n");
}
} }
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
@ -294,7 +320,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LONG clientHeight = clientRect.bottom - clientRect.top; LONG clientHeight = clientRect.bottom - clientRect.top;
HDC deviceContext = GetDC(mainWindow); HDC deviceContext = GetDC(mainWindow);
win32_display_render_bitmap(renderBitmap, deviceContext, win32_display_render_bitmap(globalRenderBitmap, deviceContext,
clientWidth, clientHeight); clientWidth, clientHeight);
ReleaseDC(mainWindow, deviceContext); ReleaseDC(mainWindow, deviceContext);
} }