-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.cpp
171 lines (148 loc) · 4.01 KB
/
application.cpp
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
#include <wtypes.h>
#include <strsafe.h>
module application;
namespace GDE::Core {
/**
* Displays an error in console
*
* @param _functionName The place the error is located
*/
void displayError(const std::string _functionName) {
OS::Windows::displayError(_functionName);
}
/**
* Safely stops the entire program
*/
void safeStop() {
OS::Windows::safeStop();
}
}
namespace GDE::OS::Windows {
/**
* Displays an error in console (Window version
*
* @param _functionName The place the error is located
*/
void displayError(const std::string _functionName) {
int wchars_num = MultiByteToWideChar(CP_UTF8, 0, _functionName.c_str(), -1, NULL, 0);
wchar_t* wstr = new wchar_t[wchars_num];
MultiByteToWideChar(CP_UTF8, 0, _functionName.c_str(), -1, wstr, wchars_num);
LPTSTR lpszFunction = wstr;
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError();
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dw, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT, (lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
StringCchPrintf((LPTSTR)lpDisplayBuf, LocalSize(lpDisplayBuf) / sizeof(TCHAR), TEXT("%s failed with error %d: %s"), lpszFunction, dw, lpMsgBuf);
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
LocalFree(lpMsgBuf);
LocalFree(lpDisplayBuf);
delete[] wstr;
}
/**
* Safely stops the entire program (Windows version)
*/
void safeStop() {
exit(EXIT_FAILURE);
}
/**
* Window procedure for base game windows
*
* @param _wndHandle Handle to the window
* @param _message The message sent to the window
* @param _wParam Params for the message
* @param _lParam Other params for the message
*/
LRESULT CALLBACK basicGameWindowProc(HWND _wndHandle, UINT _message, WPARAM _wParam, LPARAM _lParam) {
switch (_message) {
case WM_QUIT:
case WM_DESTROY:
//quit = true;
//exit(EXIT_SUCCESS);
break;
/*
case WM_SIZE:
frame.width = LOWORD(_lParam);
frame.height = HIWORD(_lParam);
break;
*/
case WM_KILLFOCUS:
Core::hasFocus = false;
memset(GDE::Input::keyboardState, 0, 256 * sizeof(GDE::Input::keyboardState[0]));
GDE::Input::clearInputs();
//mouse.buttons = 0;
break;
case WM_SETFOCUS:
Core::hasFocus = true;
break;
case WM_SYSKEYDOWN:
case WM_SYSKEYUP:
case WM_KEYDOWN:
case WM_KEYUP:
if (Core::hasFocus) {
static bool keyIsDown, keyWasDown;
keyIsDown = ((_lParam & (1 << 31)) == 0);
keyWasDown = ((_lParam & (1 << 30)) != 0);
if (keyIsDown != keyWasDown)
GDE::Input::keyboardState[(uint8_t)_wParam] = keyIsDown;
if (keyIsDown && !keyWasDown)
GDE::Input::downKeys[(uint8_t)_wParam] = keyIsDown;
/*
if (keyIsDown) {
switch (_wParam) {
case VK_ESCAPE:
quit = true;
break;
}
}
*/
if (!keyIsDown && keyWasDown)
GDE::Input::upKeys[(uint8_t)_wParam] = !keyIsDown;
}
break;
/*
case WM_MOUSEMOVE:
mouse.x = LOWORD(_lParam);
mouse.y = frame.h - 1 - HIWORD(_lParam);
break;
case WM_LBUTTONDOWN:
mouse.buttons |= MOUSE_LEFT;
break;
case WM_LBUTTONUP:
mouse.buttons &= ~MOUSE_LEFT;
break;
case WM_MBUTTONDOWN:
mouse.buttons |= MOUSE_MIDDLE;
break;
case WM_MBUTTONUP:
mouse.buttons &= ~MOUSE_MIDDLE;
break;
case WM_RBUTTONDOWN:
mouse.buttons |= MOUSE_RIGHT;
break;
case WM_RBUTTONUP:
mouse.buttons &= ~MOUSE_RIGHT;
break;
case WM_XBUTTONDOWN:
if (GET_XBUTTON_WPARAM(_wParam) == XBUTTON1)
mouse.buttons |= MOUSE_X1;
else
mouse.buttons |= MOUSE_X2;
break;
case WM_XBUTTONUP:
if (GET_XBUTTON_WPARAM(_wParam) == XBUTTON1)
mouse.buttons &= ~MOUSE_X1;
else
mouse.buttons &= ~MOUSE_X2;
break;
case WM_MOUSEWHEEL:
printf("%s\n", _wParam & 0b10000000000000000000000000000000 ? "Down" : "Up");
break;
*/
default:
return DefWindowProc(_wndHandle, _message, _wParam, _lParam);
break;
}
return 0;
}
}