diff options
Diffstat (limited to 'src/windows.c')
-rw-r--r-- | src/windows.c | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/src/windows.c b/src/windows.c new file mode 100644 index 0000000..a439ebe --- /dev/null +++ b/src/windows.c @@ -0,0 +1,148 @@ +#include <windows.h> +#include <gl/GL.h> +#include <stdio.h> + +#include "game.hpp" + +struct wincontext_t { + HWND window; + HDC device_context; + i32 width; + i32 height; +}; + +static wincontext_t wincontext; +static input_t input; + +LRESULT WndProc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) +{ + LRESULT result = 0; + switch (message) { + case WM_DESTROY: + case WM_QUIT: + wincontext.running = 0; + break; + case WM_KEYDOWN: + switch (wparam) { + case VK_ESCAPE: + input.escape.current = 1; + break; + default: + break; + } + break; + case WM_KEYUP: + switch (wparam) { + case VK_ESCAPE: + input.escape.current = 0; + break; + default: + break; + } + break; + case WM_SIZE: + wincontext.width = LOWORD(lparam); + wincontext.height = HIWORD(lparam); + glViewport(wincontext.width, wincontext.height); + break; + default: + result = DefWindowProc(window, message, wparam, lparam); + } + return result; +} + +void update_input(input_t *input) +{ + input->escape.last = input->escape.current; +} + +i32 init_win(HINSTANCE instance, i32 show_cmd, i32 width, i32 height) +{ + WNDCLASSA window_class = {}; + window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC; + window_class.lpfnWndProc = (WNDPROC)WndProc; + window_class.hInstance = instance; + window_class.hCursor = LoadCursor(0, IDC_ARROW); + window_class.lpszClassName = "window class"; + if (!RegisterClass(&window_class)) { + printf("error: failed to register window class\n"); + return 0; + } + + wincontext.width = width; + wincontext.height = height; + wincontext.window = CreateWindow(window_class.lpszClassName, "game", WS_OVERLAPPEDWINDOW, + CW_USEDEFAULT, CW_USEDEFAULT, wincontext.width, wincontext.height, 0, 0, instance, 0); + if (!wincontext.window) { + printf("error: failed to create a window\n"); + return 0; + } + + wincontext.running = 1; + + wincontext.device_context = GetDC(wincontext.window); + if (!wincontext.device_context) { + printf("error: failed to get device context\n"); + return 0; + } + + PIXELFORMATDESCRIPTOR pixel_format_desc = {}; + pixel_format_desc.nSize = sizeof(PIXELFORMATDESCRIPTOR); + pixel_format_desc.nVersion = 1; + pixel_format_desc.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; + pixel_format_desc.cColorBits = 32; + pixel_format_desc.cAlphaBits = 8; + pixel_format_desc.cDepthBits = 24; + + i32 pixel_format = ChoosePixelFormat(wincontext.device_context, &pixel_format_desc); + if (!pixel_format) { + printf("error: failed to choose pixel format\n"); + return 0; + } + if (!SetPixelFormat(wincontext.device_context, pixel_format, &pixel_format_desc)) { + printf("error: failed to set pixel format\n"); + return 0; + } + + HGLRC gl_context = wglCreateContext(wincontext.device_context); + if (!gl_context) { + printf("error: failed to create gl context\n"); + return 0; + } + if (!wglMakeCurrent(wincontext.device_context, gl_context)) { + printf("error: failed to make gl context current\n"); + return 0; + } + + ShowWindow(wincontext.window, show_cmd); + UpdateWindow(wincontext.window); + + return 1; +} + +void handle_win_events() +{ + MSG messages; + while (PeekMessage(&messages, 0, 0, 0, PM_REMOVE)) { + TranslateMessage(&messages); + DispatchMessage(&messages); + } +} + +int WinMain(HINSTANCE instance, HINSTANCE prev_instance, LPSTR cmdline, int show_cmd) +{ + if (!init_win(instance, show_cmd, 1600, 900)) + return 1; + + if (!init_game()) { + } + + while (wincontext.running) { + handle_win_events(); + game_update_and_render(input); + update_input(&input); + SwapBuffers(wincontext.device_context); + } + + return 0; +} |