4741
|
1 #include "Window.hpp"
|
|
2 #include <gl/GL.h>
|
|
3
|
|
4 #pragma comment(lib, "opengl32.lib")
|
|
5
|
|
6 const wchar_t *Window::Window_Class_Name = L"GLTSF";
|
|
7
|
|
8 Window::Window() : my_Handle(0),
|
|
9 my_Device_Context(0),
|
|
10 my_GL_Context(0),
|
|
11 my_Class_Registered(false),
|
|
12 my_Listener(0)
|
|
13 {
|
|
14
|
|
15 }
|
|
16
|
|
17 Window::~Window()
|
|
18 {
|
|
19 Finalize();
|
|
20 Show_Cursor();
|
|
21 }
|
|
22
|
|
23 void Window::Initialize(const std::wstring &Title, const Video_Mode &Mode, bool Fullscreen)
|
|
24 {
|
|
25 Finalize();
|
|
26
|
|
27 my_Video_Mode = Mode;
|
|
28 if (!my_Video_Mode.Is_Valid())
|
|
29 throw std::runtime_error("Invalid video mode");
|
|
30
|
|
31 my_Fullscreen = Fullscreen;
|
|
32 Register_Class();
|
|
33 Create_Window(Title, Mode, Fullscreen);
|
|
34 my_IMM.Initialize(my_Handle);
|
|
35 }
|
|
36
|
|
37 void Window::Finalize()
|
|
38 {
|
|
39 my_IMM.Finalize();
|
|
40 Destroy_Window();
|
|
41 Unregister_Class();
|
|
42 }
|
|
43
|
|
44 void Window::Set_Listener(Window_Listener *Listener)
|
|
45 {
|
|
46 my_Listener = Listener;
|
|
47 }
|
|
48
|
|
49 void Window::Show()
|
|
50 {
|
|
51 if (my_Handle)
|
|
52 ShowWindow(my_Handle, SW_SHOW);
|
|
53 }
|
|
54
|
|
55 void Window::Hide()
|
|
56 {
|
|
57 if (my_Handle)
|
|
58 ShowWindow(my_Handle, SW_HIDE);
|
|
59 }
|
|
60
|
|
61 void Window::Handle_Events()
|
|
62 {
|
|
63 MSG Message = {0};
|
|
64 while (PeekMessageW(&Message, NULL, 0, 0, PM_REMOVE))
|
|
65 {
|
|
66 TranslateMessage(&Message);
|
|
67 DispatchMessageW(&Message);
|
|
68 }
|
|
69 }
|
|
70
|
|
71 void Window::Display()
|
|
72 {
|
|
73 if (my_Device_Context && my_GL_Context)
|
|
74 SwapBuffers(my_Device_Context);
|
|
75 }
|
|
76
|
|
77 void Window::Show_Cursor()
|
|
78 {
|
|
79 ShowCursor(TRUE);
|
|
80 }
|
|
81
|
|
82 void Window::Hide_Cursor()
|
|
83 {
|
|
84 ShowCursor(FALSE);
|
|
85 }
|
|
86
|
|
87 HWND Window::Get_Handle()
|
|
88 {
|
|
89 return my_Handle;
|
|
90 }
|
|
91
|
4742
|
92 IMM & Window::Get_IMM()
|
|
93 {
|
|
94 return my_IMM;
|
|
95 }
|
|
96
|
4741
|
97 void Window::Register_Class()
|
|
98 {
|
|
99 WNDCLASSEXW Window_Class = {0};
|
|
100 Window_Class.cbSize = sizeof(Window_Class);
|
|
101 Window_Class.style = 0;
|
|
102 Window_Class.lpfnWndProc = &Window::Window_Procedure;
|
|
103 Window_Class.cbClsExtra = 0;
|
|
104 Window_Class.cbWndExtra = 0;
|
|
105 Window_Class.hInstance = GetModuleHandle(NULL);
|
|
106 Window_Class.hIcon = NULL;
|
|
107 Window_Class.hCursor = NULL;
|
|
108 Window_Class.hbrBackground = NULL;
|
|
109 Window_Class.lpszMenuName = NULL;
|
|
110 Window_Class.lpszClassName = Window_Class_Name;
|
|
111 Window_Class.hIconSm = NULL;
|
|
112 if (0 == RegisterClassExW(&Window_Class))
|
|
113 throw std::runtime_error("Failed to register window class");
|
|
114
|
|
115 my_Class_Registered = true;
|
|
116 }
|
|
117
|
|
118 void Window::Unregister_Class()
|
|
119 {
|
|
120 if (my_Class_Registered)
|
|
121 {
|
|
122 if (0 == UnregisterClassW(Window_Class_Name, GetModuleHandle(NULL)))
|
|
123 printf("Warning: Failed to unregister window class\n");
|
|
124
|
|
125 my_Class_Registered = false;
|
|
126 }
|
|
127 }
|
|
128
|
|
129 void Window::Create_Window(const std::wstring &Title, const Video_Mode &Mode, bool Fullscreen)
|
|
130 {
|
|
131 HDC Screen_DC = GetDC(NULL);
|
|
132 int Left = (GetDeviceCaps(Screen_DC, HORZRES) - my_Video_Mode.Width) / 2;
|
|
133 int Top = (GetDeviceCaps(Screen_DC, VERTRES) - my_Video_Mode.Height) / 2;
|
|
134 int Width = my_Video_Mode.Width;
|
|
135 int Height = my_Video_Mode.Height;
|
|
136 ReleaseDC(NULL, Screen_DC);
|
|
137
|
|
138 DWORD Style = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
|
|
139 if (!my_Fullscreen)
|
|
140 {
|
|
141 RECT Rect = {0, 0, Width, Height};
|
|
142 AdjustWindowRect(&Rect, Style, false);
|
|
143 Width = Rect.right - Rect.left;
|
|
144 Height = Rect.bottom - Rect.top;
|
|
145 }
|
|
146 my_Handle = CreateWindowW(Window_Class_Name, Title.c_str(), Style, Left, Top, Width, Height, NULL, NULL, GetModuleHandle(NULL), this);
|
|
147 if (!my_Handle)
|
|
148 throw std::runtime_error("Failed to create window");
|
|
149
|
|
150 if (Fullscreen)
|
|
151 Switch_To_Fullscreen(Mode);
|
|
152
|
|
153 Create_Context(Mode);
|
|
154
|
|
155 RECT Rect = {0};
|
|
156 GetClientRect(my_Handle, &Rect);
|
|
157 //TODO: ...
|
|
158 }
|
|
159
|
|
160 void Window::Destroy_Window()
|
|
161 {
|
|
162 Destroy_Context();
|
|
163 if (my_Handle)
|
|
164 {
|
|
165 DestroyWindow(my_Handle);
|
|
166 my_Handle = 0;
|
|
167
|
|
168 if (my_Fullscreen)
|
|
169 ChangeDisplaySettings(NULL, 0);
|
|
170 }
|
|
171 }
|
|
172
|
|
173 void Window::Create_Context(const Video_Mode &Mode)
|
|
174 {
|
|
175 my_Device_Context = GetDC(my_Handle);
|
|
176 if (!my_Device_Context)
|
|
177 throw std::runtime_error("Failed to get device context");
|
|
178
|
|
179 PIXELFORMATDESCRIPTOR Pixel_Descriptor = {0};
|
|
180 Pixel_Descriptor.nSize = sizeof(Pixel_Descriptor);
|
|
181 Pixel_Descriptor.nVersion = 1;
|
|
182 Pixel_Descriptor.iLayerType = PFD_MAIN_PLANE;
|
|
183 Pixel_Descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
|
|
184 Pixel_Descriptor.iPixelType = PFD_TYPE_RGBA;
|
|
185 Pixel_Descriptor.cColorBits = static_cast<BYTE>(Mode.Bits_Per_Pixel);
|
|
186 Pixel_Descriptor.cDepthBits = 24;
|
|
187 Pixel_Descriptor.cStencilBits = 8;
|
|
188 Pixel_Descriptor.cAlphaBits = Mode.Bits_Per_Pixel == 32 ? 8 : 0;
|
|
189
|
|
190 int Best_Format = ChoosePixelFormat(my_Device_Context, &Pixel_Descriptor);
|
|
191 if (0 == Best_Format)
|
|
192 throw std::runtime_error("Failed to find suitable pixel format");
|
|
193
|
|
194 PIXELFORMATDESCRIPTOR Actual_Format = {0};
|
|
195 Actual_Format.nSize = sizeof(Actual_Format);
|
|
196 Actual_Format.nVersion = 1;
|
|
197 DescribePixelFormat(my_Device_Context, Best_Format, sizeof(Actual_Format), &Actual_Format);
|
|
198 if (!SetPixelFormat(my_Device_Context, Best_Format, &Actual_Format))
|
|
199 throw std::runtime_error("Failed to set device pixel format");
|
|
200
|
|
201 my_GL_Context = wglCreateContext(my_Device_Context);
|
|
202 if (!my_GL_Context)
|
|
203 throw std::runtime_error("Failed to create OpenGL context");
|
|
204
|
|
205 wglMakeCurrent(my_Device_Context, my_GL_Context);
|
|
206 }
|
|
207
|
|
208 void Window::Destroy_Context()
|
|
209 {
|
|
210 if (my_GL_Context)
|
|
211 {
|
|
212 wglDeleteContext(my_GL_Context);
|
|
213 my_GL_Context = 0;
|
|
214 }
|
|
215 if (my_Device_Context)
|
|
216 {
|
|
217 ReleaseDC(my_Handle, my_Device_Context);
|
|
218 my_Device_Context = 0;
|
|
219 }
|
|
220 }
|
|
221
|
|
222 void Window::Switch_To_Fullscreen(const Video_Mode &Mode)
|
|
223 {
|
|
224 DEVMODE Device_Mode = {0};
|
|
225 Device_Mode.dmSize = sizeof(Device_Mode);
|
|
226 Device_Mode.dmPelsWidth = Mode.Width;
|
|
227 Device_Mode.dmPelsHeight = Mode.Height;
|
|
228 Device_Mode.dmBitsPerPel = Mode.Bits_Per_Pixel;
|
|
229 Device_Mode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
|
|
230
|
|
231 if (DISP_CHANGE_SUCCESSFUL != ChangeDisplaySettings(&Device_Mode, CDS_FULLSCREEN))
|
|
232 throw std::runtime_error("Failed to change to fullscreen mode");
|
|
233
|
|
234 SetWindowLong(my_Handle, GWL_STYLE, WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS);
|
|
235 SetWindowLong(my_Handle, GWL_EXSTYLE, WS_EX_APPWINDOW);
|
|
236
|
|
237 SetWindowPos(my_Handle, HWND_TOP, 0, 0, Mode.Width, Mode.Height, SWP_FRAMECHANGED);
|
|
238 }
|
|
239
|
|
240 LRESULT CALLBACK Window::Window_Procedure(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
|
|
241 {
|
|
242 switch (Message)
|
|
243 {
|
|
244 case WM_CREATE:
|
|
245 {
|
|
246 LONG This = reinterpret_cast<LONG>(reinterpret_cast<CREATESTRUCT *>(lParam)->lpCreateParams);
|
|
247 SetWindowLongPtr(Handle, GWLP_USERDATA, This);
|
|
248 return 0;
|
|
249 }
|
|
250 break;
|
|
251 case WM_DESTROY:
|
|
252 PostQuitMessage(0);
|
|
253 return 0;
|
|
254 break;
|
|
255 default:
|
|
256 {
|
|
257 Window* Win = reinterpret_cast<Window *>(GetWindowLongPtr(Handle, GWLP_USERDATA));
|
|
258 if (Win)
|
|
259 return Win->Handle_Message(Handle, Message, wParam, lParam);
|
|
260 }
|
|
261 break;
|
|
262 }
|
|
263 return DefWindowProcW(Handle, Message, wParam, lParam);
|
|
264 }
|
|
265
|
|
266 #define Call_Listener(x)\
|
|
267 if (my_Listener) my_Listener->x
|
|
268
|
|
269 LRESULT Window::Handle_Message(HWND Handle, UINT Message, WPARAM wParam, LPARAM lParam)
|
|
270 {
|
|
271 bool IMM_Message = false;
|
|
272 LRESULT Result = my_IMM.Handle_Message(Handle, Message, wParam, lParam, IMM_Message);
|
|
273 if (IMM_Message)
|
|
274 return Result;
|
|
275
|
|
276 switch (Message)
|
|
277 {
|
|
278 case WM_SIZE:
|
|
279 Call_Listener(On_Resized(LOWORD(lParam), HIWORD(lParam)));
|
|
280 break;
|
|
281 case WM_CLOSE:
|
|
282 Call_Listener(On_Close());
|
|
283 break;
|
|
284 case WM_KEYDOWN:
|
|
285 Call_Listener(On_Key_Down(wParam));
|
|
286 break;
|
|
287 case WM_KEYUP:
|
|
288 Call_Listener(On_Key_Up(wParam));
|
|
289 break;
|
|
290 case WM_CHAR:
|
|
291 Call_Listener(On_Char(wParam));
|
|
292 break;
|
4742
|
293 case WM_SETFOCUS:
|
|
294 my_IMM.Focus_Gained();
|
|
295 break;
|
|
296 case WM_KILLFOCUS:
|
|
297 my_IMM.Focus_Lost();
|
|
298 break;
|
4741
|
299 default:
|
|
300 return DefWindowProcW(Handle, Message, wParam, lParam);
|
|
301 break;
|
|
302 }
|
|
303 return 0;
|
|
304 }
|