-
Notifications
You must be signed in to change notification settings - Fork 0
/
k15_win32template.c
212 lines (159 loc) · 4.32 KB
/
k15_win32template.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
#define _CRT_SECURE_NO_WARNINGS
#include <windows.h>
#include <stdio.h>
#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.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* p_FileName)
{
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(p_FileName, "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 p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
void K15_WindowClosed(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
void K15_KeyInput(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
void K15_MouseButtonInput(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
void K15_MouseMove(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
void K15_MouseWheel(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
}
LRESULT CALLBACK K15_WNDPROC(HWND p_HWND, UINT p_Message, WPARAM p_wParam, LPARAM p_lParam)
{
bool8 messageHandled = K15_FALSE;
switch (p_Message)
{
case WM_CREATE:
K15_WindowCreated(p_HWND, p_Message, p_wParam, p_lParam);
break;
case WM_CLOSE:
K15_WindowClosed(p_HWND, p_Message, p_wParam, p_lParam);
PostQuitMessage(0);
messageHandled = K15_TRUE;
break;
case WM_KEYDOWN:
case WM_KEYUP:
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
K15_KeyInput(p_HWND, p_Message, p_wParam, p_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(p_HWND, p_Message, p_wParam, p_lParam);
break;
case WM_MOUSEMOVE:
K15_MouseMove(p_HWND, p_Message, p_wParam, p_lParam);
break;
case WM_MOUSEWHEEL:
K15_MouseWheel(p_HWND, p_Message, p_wParam, p_lParam);
break;
}
if (messageHandled == K15_FALSE)
{
return DefWindowProc(p_HWND, p_Message, p_wParam, p_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()
{
allocateDebugConsole();
}
void doFrame(uint32 p_DeltaTimeInMS)
{
}
int CALLBACK WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nShowCmd)
{
LARGE_INTEGER performanceFrequency;
QueryPerformanceFrequency(&performanceFrequency);
HWND hwnd = setupWindow(hInstance, 1024, 768);
if (hwnd == INVALID_HANDLE_VALUE)
return -1;
setup();
uint32 timeFrameStarted = 0;
uint32 timeFrameEnded = 0;
uint32 deltaMs = 0;
bool8 loopRunning = K15_TRUE;
MSG msg = {0};
while (loopRunning)
{
timeFrameStarted = getTimeInMilliseconds(performanceFrequency);
while (PeekMessage(&msg, hwnd, 0, 0, PM_REMOVE) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
loopRunning = K15_FALSE;
}
doFrame(deltaMs);
timeFrameEnded = getTimeInMilliseconds(performanceFrequency);
deltaMs = timeFrameEnded - timeFrameStarted;
}
return 0;
}