1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
#include "utils.c"
#include "math.c"
#include "platform_common.c"
#include <windows.h>
struct
{
// Platform non-specific part
int width, height;
u32 *pixels;
// Platform specific part
BITMAPINFO bitmap_info;
}typedef Render_Buffer;
global_variable Render_Buffer render_buffer;
#include "software_rendering.c"
#include "game.c"
internal LRESULT
CALLBACK window_callback(HWND window, UINT message, WPARAM w_param, LPARAM l_param)
{
LRESULT result = 0;
switch(message)
{
case WM_CLOSE:
case WM_DESTROY:
{
running = false;
}break;
case WM_SIZE:
{
// get width and height
RECT rect;
GetWindowRect(window, &rect);
render_buffer.width = rect.right - rect.left;
render_buffer.height = rect.bottom - rect.top;
if(render_buffer.pixels)
{
//free buffer
VirtualFree(render_buffer.pixels, 0, MEM_RELEASE);
}
// allocate the buffer
render_buffer.pixels = VirtualAlloc(0, sizeof(u32)*render_buffer.width*render_buffer.height,
MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
//fill the bitmap_info
render_buffer.bitmap_info.bmiHeader.biSize = sizeof(render_buffer.bitmap_info.bmiHeader);
render_buffer.bitmap_info.bmiHeader.biWidth = render_buffer.width;
render_buffer.bitmap_info.bmiHeader.biHeight = render_buffer.height;
render_buffer.bitmap_info.bmiHeader.biPlanes = 1;
render_buffer.bitmap_info.bmiHeader.biBitCount = 32;
render_buffer.bitmap_info.bmiHeader.biCompression = BI_RGB;
render_buffer.bitmap_info.bmiHeader.biSizeImage = 0;
}break;
default:
{
result = DefWindowProcA(window, message, w_param, l_param);
}
}
return result;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
WNDCLASSA window_class = {0};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = "Game_Window_Class";
window_class.lpfnWndProc = window_callback;
RegisterClassA(&window_class);
HWND window = CreateWindowExA(0, window_class.lpszClassName, "PusPus",
WS_VISIBLE | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, 1366, 768, 0, 0, 0, 0);
HDC hdc = GetDC(window);
Input input = {0};
while(running)
{
//Input
MSG message;
while(PeekMessageA(&message, window, 0, 0, PM_REMOVE))
{
switch(message.message)
{
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
{
u32 vk_code = (u32)message.wParam;
b32 was_dowm = ((message.lParam & (1 << 30)) != 0);
b32 is_down = ((message.lParam & (1 << 31)) == 0);
if(vk_code == VK_LEFT)
input.buttons[BUTTON_LEFT].is_down = is_down;
input.buttons[BUTTON_LEFT].changed = true;
}break;
default:
{
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
//Simulation
simulate_game(&input);
//Render
StretchDIBits(hdc, 0, 0, render_buffer.width, render_buffer.height, 0, 0, render_buffer.width,
render_buffer.height, render_buffer.pixels, &render_buffer.bitmap_info , DIB_RGB_COLORS, SRCCOPY);
}
return (0);
}
|