-
Notifications
You must be signed in to change notification settings - Fork 0
/
Window.cpp
112 lines (87 loc) · 1.88 KB
/
Window.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
#include "Window.h"
Window* window = NULL;
Window::Window()
{
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
switch (msg) {
case WM_CREATE:
{
// Event fired when the window will be created
window->onCreate();
break;
}
case WM_DESTROY:
{
//Event fired when the window is destroyed
window->onDestroy();
::PostQuitMessage(0);
break;
}
default:
{
return ::DefWindowProc(hwnd, msg, wparam, lparam);
}
}
return NULL;
}
bool Window::init()
{
// Setting up WNDCLASSEX object
WNDCLASSEX wc;
wc.cbClsExtra = NULL;
wc.cbSize = sizeof(WNDCLASSEX);
wc.cbWndExtra = NULL;
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hInstance = NULL;
wc.lpszClassName = L"MyWindowClass";
wc.lpszMenuName = L"";
wc.style = NULL;
wc.lpfnWndProc = &WndProc;
if (!::RegisterClassEx(&wc)) // if the registration of class fails, the function will return false
return false;
if (!window)
window = this;
// Creation of the window
m_hwnd=::CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, L"MyWindowClass", L"DirectX Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768, NULL, NULL, NULL, NULL);
// If the the creation of the window fails return false
if (!m_hwnd)
return false;
// show the window
::ShowWindow(m_hwnd, SW_SHOW);
::UpdateWindow(m_hwnd);
m_is_run = true;
return true;
}
bool Window::broadcast()
{
MSG msg;
while (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
window->onUpdate();
Sleep(0);
return true;
}
bool Window::release()
{
// Destroy the window
if (::DestroyWindow(m_hwnd))
return false;
return true;
}
bool Window::isRun()
{
return m_is_run;
}
void Window::onDestroy()
{
m_is_run = false;
}
Window::~Window()
{
}