Skip to content

Commit bb3db20

Browse files
committed
Added some improvements.
(1) removed the possible zombie state for the main window; (2) simplified the window interface accordingly; (3) guaranteed single window class registration (4) using `std::make_unique` (5) substituted asserts with exceptions
1 parent 30ed3d2 commit bb3db20

4 files changed

Lines changed: 55 additions & 41 deletions

File tree

Tutorial1_Window/Game/main.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2323
SOFTWARE.*/
2424

2525
#include <OGL3D/Game/OGame.h>
26+
#include <iostream>
2627

2728
int main()
2829
{
29-
OGame game;
30-
game.run();
30+
try
31+
{
32+
OGame game;
33+
game.run();
34+
}
35+
catch (const std::exception& e)
36+
{
37+
std::cout << "Error: " << e.what() << std::endl;
38+
return -1;
39+
}
40+
3141
return 0;
3242
}

Tutorial1_Window/OGL3D/include/OGL3D/Window/OWindow.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,6 @@ class OWindow
2929
public:
3030
OWindow();
3131
~OWindow();
32-
33-
void onDestroy();
34-
bool isClosed();
3532
private:
3633
void* m_handle = nullptr;
3734
};

Tutorial1_Window/OGL3D/source/OGL3D/Game/OGame.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ SOFTWARE.*/
2828

2929
OGame::OGame()
3030
{
31-
m_display = std::unique_ptr<OWindow>(new OWindow());
31+
m_display = std::make_unique<OWindow>();
3232
}
3333

3434
OGame::~OGame()
@@ -37,16 +37,27 @@ OGame::~OGame()
3737

3838
void OGame::run()
3939
{
40-
MSG msg;
41-
while (m_isRunning && !m_display->isClosed())
40+
41+
while (m_isRunning)
4242
{
43-
if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
43+
MSG msg = {};
44+
if (PeekMessage(&msg, HWND(), NULL, NULL, PM_REMOVE))
4445
{
45-
TranslateMessage(&msg);
46-
DispatchMessage(&msg);
46+
if (msg.message == WM_QUIT)
47+
{
48+
m_isRunning = false;
49+
continue;
50+
}
51+
else
52+
{
53+
TranslateMessage(&msg);
54+
DispatchMessage(&msg);
55+
}
56+
}
57+
else
58+
{
59+
Sleep(1);
4760
}
48-
49-
Sleep(1);
5061
}
5162
}
5263

Tutorial1_Window/OGL3D/source/OGL3D/Window/OWindow.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,18 @@ SOFTWARE.*/
2525
#include <OGL3D/Window/OWindow.h>
2626
#include <OGL3D/Game/OGame.h>
2727
#include <Windows.h>
28-
#include <assert.h>
28+
#include <stdexcept>
29+
#include <functional>
2930

3031
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
3132
{
3233
switch (msg)
3334
{
34-
case WM_DESTROY:
35+
case WM_CLOSE:
3536
{
36-
OWindow* window = (OWindow *)GetWindowLongPtr(hwnd, GWLP_USERDATA);
37-
window->onDestroy();
37+
PostQuitMessage(0);
3838
break;
3939
}
40-
4140
default:
4241
return DefWindowProc(hwnd, msg, wParam, lParam);
4342
}
@@ -47,38 +46,35 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4746

4847
OWindow::OWindow()
4948
{
50-
WNDCLASSEX wc = {};
51-
wc.cbSize = sizeof(WNDCLASSEX);
52-
wc.lpszClassName = L"OGL3DWindow";
53-
wc.lpfnWndProc = &WndProc;
54-
55-
assert(RegisterClassEx(&wc));
56-
49+
static const ATOM windowClassId = std::invoke(
50+
[]() -> ATOM
51+
{
52+
WNDCLASSEX wc = {};
53+
wc.cbSize = sizeof(WNDCLASSEX);
54+
wc.lpszClassName = L"OGL3DWindow";
55+
wc.lpfnWndProc = &WndProc;
56+
return RegisterClassEx(&wc);
57+
}
58+
);
59+
60+
if (!windowClassId) throw std::runtime_error("RegisterClassEx failed");
61+
5762
RECT rc = { 0,0,1024,768 };
5863
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, false);
5964

60-
m_handle = CreateWindowEx(NULL, L"OGL3DWindow", L"PardCode | OpenGL 3D Game", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT,
61-
rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, NULL, NULL);
65+
m_handle = CreateWindowEx(NULL, MAKEINTATOM(windowClassId), L"PardCode | OpenGL 3D Game", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU,
66+
CW_USEDEFAULT, CW_USEDEFAULT,
67+
rc.right - rc.left, rc.bottom - rc.top, HWND(), HMENU(), HINSTANCE(), NULL);
6268

63-
assert(m_handle);
69+
if (!m_handle) throw std::runtime_error("CreateWindowEx failed");
6470

6571
SetWindowLongPtr((HWND)m_handle, GWLP_USERDATA, (LONG_PTR)this);
6672

67-
ShowWindow((HWND)m_handle, SW_SHOW);
68-
UpdateWindow((HWND)m_handle);
73+
ShowWindow(HWND(m_handle), SW_SHOW);
74+
UpdateWindow(HWND(m_handle));
6975
}
7076

7177
OWindow::~OWindow()
7278
{
73-
DestroyWindow((HWND)m_handle);
74-
}
75-
76-
void OWindow::onDestroy()
77-
{
78-
m_handle = nullptr;
79-
}
80-
81-
bool OWindow::isClosed()
82-
{
83-
return !m_handle;
79+
DestroyWindow(HWND(m_handle));
8480
}

0 commit comments

Comments
 (0)