-
Notifications
You must be signed in to change notification settings - Fork 0
/
k15_win32template_opengl_intermediate.c
276 lines (211 loc) · 5.71 KB
/
k15_win32template_opengl_intermediate.c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <GL/GL.h>
#include <stdio.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "gdi32.lib")
#define K15_FALSE 0
#define K15_TRUE 1
typedef unsigned char bool8;
typedef unsigned char byte;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
typedef LRESULT(CALLBACK* WNDPROC)(HWND, UINT, WPARAM, LPARAM);
void printErrorToFile(const char* pFileName)
{
DWORD errorId = GetLastError();
char* textBuffer = 0;
DWORD writtenChars = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 0, errorId,
MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPSTR)&textBuffer, 512, 0);
if (writtenChars > 0)
{
FILE* file = fopen(pFileName, "w");
if (file)
{
fwrite(textBuffer, writtenChars, 1, file);
fflush(file);
fclose(file);
}
}
}
void allocateDebugConsole()
{
AllocConsole();
AttachConsole(ATTACH_PARENT_PROCESS);
freopen("CONOUT$", "w", stdout);
}
void K15_WindowCreated(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_WindowClosed(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_KeyInput(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_MouseButtonInput(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_MouseMove(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_MouseWheel(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
}
void K15_WindowResized(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
RECT clientRect = {0};
GetClientRect(hwnd, &clientRect);
glViewport(0, 0, clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
}
LRESULT CALLBACK K15_WNDPROC(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
bool8 messageHandled = K15_FALSE;
switch (message)
{
case WM_CREATE:
K15_WindowCreated(hwnd, message, wparam, lparam);
break;
case WM_CLOSE:
K15_WindowClosed(hwnd, message, wparam, lparam);
PostQuitMessage(0);
messageHandled = K15_TRUE;
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
K15_KeyInput(hwnd, message, wparam, lparam);
break;
case WM_LBUTTONUP:
case WM_MBUTTONUP:
case WM_RBUTTONUP:
case WM_XBUTTONUP:
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_XBUTTONDOWN:
K15_MouseButtonInput(hwnd, message, wparam, lparam);
break;
case WM_MOUSEMOVE:
K15_MouseMove(hwnd, message, wparam, lparam);
break;
case WM_MOUSEWHEEL:
K15_MouseWheel(hwnd, message, wparam, lparam);
break;
case WM_SIZE:
K15_WindowResized(hwnd, message, wparam, lparam);
break;
}
if (messageHandled == K15_FALSE)
{
return DefWindowProc(hwnd, message, wparam, lparam);
}
return 0;
}
HWND setupWindow(HINSTANCE p_Instance, int p_Width, int p_Height)
{
WNDCLASS wndClass = {0};
wndClass.style = CS_HREDRAW | CS_OWNDC | CS_VREDRAW;
wndClass.hInstance = p_Instance;
wndClass.lpszClassName = "K15_Win32Template";
wndClass.lpfnWndProc = K15_WNDPROC;
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
RegisterClass(&wndClass);
HWND hwnd = CreateWindowA("K15_Win32Template", "Win32 Template",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
p_Width, p_Height, 0, 0, p_Instance, 0);
if (hwnd == INVALID_HANDLE_VALUE)
MessageBox(0, "Error creating Window.\n", "Error!", 0);
else
ShowWindow(hwnd, SW_SHOW);
return hwnd;
}
uint32 getTimeInMilliseconds(LARGE_INTEGER p_PerformanceFrequency)
{
LARGE_INTEGER appTime = {0};
QueryPerformanceFrequency(&appTime);
appTime.QuadPart *= 1000; //to milliseconds
return (uint32)(appTime.QuadPart / p_PerformanceFrequency.QuadPart);
}
void setup(HWND hwnd)
{
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, //Flags
PFD_TYPE_RGBA, //The kind of framebuffer. RGBA or palette.
32, //Colordepth of the framebuffer.
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
24, //Number of bits for the depthbuffer
8, //Number of bits for the stencilbuffer
0, //Number of Aux buffers in the framebuffer.
PFD_MAIN_PLANE,
0,
0, 0, 0
};
HDC mainDC = GetDC(hwnd);
int pixelFormat = ChoosePixelFormat(mainDC, &pfd);
SetPixelFormat(mainDC,pixelFormat, &pfd);
HGLRC glContext = wglCreateContext(mainDC);
wglMakeCurrent(mainDC, glContext);
//set default gl state
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
void doFrame(uint32 p_DeltaTimeInMS, HWND hwnd)
{
HDC mainDC = GetDC(hwnd);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SwapBuffers(mainDC);
}
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
LARGE_INTEGER performanceFrequency;
QueryPerformanceFrequency(&performanceFrequency);
allocateDebugConsole();
HWND hwnd = setupWindow(hInstance, 1024, 768);
if (hwnd == INVALID_HANDLE_VALUE)
return -1;
setup(hwnd);
uint32 timeFrameStarted = 0;
uint32 timeFrameEnded = 0;
uint32 deltaMs = 0;
bool8 loopRunning = K15_TRUE;
MSG msg = {0};
while (loopRunning)
{
timeFrameStarted = getTimeInMilliseconds(performanceFrequency);
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0)
{
if (msg.message == WM_QUIT)
{
loopRunning = K15_FALSE;
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!loopRunning)
{
break;
}
doFrame(deltaMs, hwnd);
timeFrameEnded = getTimeInMilliseconds(performanceFrequency);
deltaMs = timeFrameEnded - timeFrameStarted;
}
return 0;
}