-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPong.cpp
More file actions
165 lines (134 loc) · 4.76 KB
/
Pong.cpp
File metadata and controls
165 lines (134 loc) · 4.76 KB
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
#define _CRT_SECURE_NO_DEPRECATE
#include "utils.cpp"
#include <windows.h>
#include <string>
#include <stdio.h>
using namespace std;
global_variable bool running = true;
struct Render_State {
int height, width;
void* memory;
BITMAPINFO bitmap_info;
};
global_variable Render_State render_state;
#include "platform_common.cpp"
#include "renderer.cpp"
#include "game.cpp"
LRESULT CALLBACK window_callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
LRESULT result = 0;
switch (uMsg) {
case WM_CLOSE:
case WM_DESTROY: {
running = false;
} break;
case WM_SIZE: {
RECT rect;
GetClientRect(hwnd, &rect);
render_state.width = rect.right - rect.left;
render_state.height = rect.bottom - rect.top;
int size = render_state.width * render_state.height * sizeof(unsigned int);
if (render_state.memory) VirtualFree(render_state.memory, 0, MEM_RELEASE);
render_state.memory = VirtualAlloc(0, size, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
render_state.bitmap_info.bmiHeader.biSize = sizeof(render_state.bitmap_info.bmiHeader);
render_state.bitmap_info.bmiHeader.biWidth = render_state.width;
render_state.bitmap_info.bmiHeader.biHeight = render_state.height;
render_state.bitmap_info.bmiHeader.biPlanes = 1;
render_state.bitmap_info.bmiHeader.biBitCount = 32;
render_state.bitmap_info.bmiHeader.biCompression = BI_RGB;
} break;
default: {
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
}
}
return result;
}
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {
// Create Window Class
WNDCLASS window_class = {};
window_class.style = CS_HREDRAW | CS_VREDRAW;
window_class.lpszClassName = "Game Window Class";
window_class.lpfnWndProc = window_callback;
// Register Class
RegisterClass(&window_class);
// Create Window
HWND window = CreateWindow(window_class.lpszClassName, "Pong", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 1280, 720, 0, 0, hInstance, 0);
HDC hdc = GetDC(window);
Input input = {};
float delta_time = 0.016666f;
LARGE_INTEGER frame_begin_time;
QueryPerformanceCounter(&frame_begin_time);
float performance_frequency;
{
LARGE_INTEGER perf;
QueryPerformanceFrequency(&perf);
performance_frequency = (float)perf.QuadPart;
}
while (running) {
// Input
MSG message;
for (int i = 0; i < BUTTON_COUNT; i++) {
input.buttons[i].changed = false;
}
while (PeekMessage(&message, window, 0, 0, PM_REMOVE)) {
switch (message.message) {
case WM_KEYUP:
case WM_KEYDOWN: {
u32 vk_code = (u32)message.wParam;
bool is_down = ((message.lParam & (1 << 31)) == 0);
switch (vk_code) {
#define process_button(b, vk)\
case vk:{\
input.buttons[b].changed = is_down != input.buttons[b].is_down;\
input.buttons[b].is_down = is_down;\
} break;
process_button(BUTTON_UP, VK_UP);
process_button(BUTTON_DOWN, VK_DOWN);
process_button(BUTTON_LEFT, VK_LEFT);
process_button(BUTTON_RIGHT, VK_RIGHT);
process_button(BUTTON_ENTER, VK_RETURN);
process_button(BUTTON_BACKSPACE, VK_BACK);
process_button(BUTTON_SPACE, VK_SPACE);
process_button(BUTTON_A, 'A');
process_button(BUTTON_B, 'B');
process_button(BUTTON_C, 'C');
process_button(BUTTON_D, 'D');
process_button(BUTTON_E, 'E');
process_button(BUTTON_F, 'F');
process_button(BUTTON_G, 'G');
process_button(BUTTON_H, 'H');
process_button(BUTTON_I, 'I');
process_button(BUTTON_J, 'J');
process_button(BUTTON_K, 'K');
process_button(BUTTON_L, 'L');
process_button(BUTTON_M, 'M');
process_button(BUTTON_N, 'N');
process_button(BUTTON_O, 'O');
process_button(BUTTON_P, 'P');
process_button(BUTTON_Q, 'Q');
process_button(BUTTON_R, 'R');
process_button(BUTTON_S, 'S');
process_button(BUTTON_T, 'T');
process_button(BUTTON_U, 'U');
process_button(BUTTON_V, 'V');
process_button(BUTTON_W, 'W');
process_button(BUTTON_X, 'X');
process_button(BUTTON_Y, 'Y');
process_button(BUTTON_Z, 'Z');
}
} break;
default: {
TranslateMessage(&message);
DispatchMessage(&message);
}
}
}
// Simulate
simulate_game(&input, delta_time);
// Render
StretchDIBits(hdc, 0, 0, render_state.width, render_state.height, 0, 0, render_state.width, render_state.height, render_state.memory, &render_state.bitmap_info, DIB_RGB_COLORS, SRCCOPY);
LARGE_INTEGER frame_end_time;
QueryPerformanceCounter(&frame_end_time);
delta_time = (float)(frame_end_time.QuadPart - frame_begin_time.QuadPart) / performance_frequency;
frame_begin_time = frame_end_time;
}
}