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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
}
|