Skip to content

Commit 223334d

Browse files
committed
Tutorial 2 Making Cross-Platform 3D Engine
Tutorial 2 Making Cross-Platform 3D Engine
1 parent bb3db20 commit 223334d

77 files changed

Lines changed: 97203 additions & 21 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ StyleCopReport.xml
8484
*.pidb
8585
*.svclog
8686
*.scc
87+
*.pro.user
8788

8889
!Assets/Meshes/*.obj
8990

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

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

3130
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
3231
{
3332
switch (msg)
3433
{
34+
case WM_DESTROY:
35+
{
36+
OWindow* window = (OWindow*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
37+
break;
38+
}
3539
case WM_CLOSE:
3640
{
3741
PostQuitMessage(0);
@@ -46,32 +50,26 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
4650

4751
OWindow::OWindow()
4852
{
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-
53+
WNDCLASSEX wc = {};
54+
wc.cbSize = sizeof(WNDCLASSEX);
55+
wc.lpszClassName = L"OGL3DWindow";
56+
wc.lpfnWndProc = &WndProc;
57+
58+
auto classId = RegisterClassEx(&wc);
59+
assert(classId);
60+
6261
RECT rc = { 0,0,1024,768 };
6362
AdjustWindowRect(&rc, WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, false);
6463

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);
64+
m_handle = CreateWindowEx(NULL, MAKEINTATOM(classId), L"PardCode | OpenGL 3D Game", WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT,
65+
rc.right - rc.left, rc.bottom - rc.top, NULL, NULL, NULL, NULL);
6866

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

7169
SetWindowLongPtr((HWND)m_handle, GWLP_USERDATA, (LONG_PTR)this);
7270

73-
ShowWindow(HWND(m_handle), SW_SHOW);
74-
UpdateWindow(HWND(m_handle));
71+
ShowWindow((HWND)m_handle, SW_SHOW);
72+
UpdateWindow((HWND)m_handle);
7573
}
7674

7775
OWindow::~OWindow()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#include <OGL3D/Game/OGame.h>
26+
#include <iostream>
27+
28+
int main()
29+
{
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+
41+
return 0;
42+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#pragma once
26+
#include <memory>
27+
28+
class OGraphicsEngine;
29+
class OWindow;
30+
class OGame
31+
{
32+
public:
33+
OGame();
34+
~OGame();
35+
36+
virtual void onCreate();
37+
virtual void onUpdate();
38+
virtual void onQuit();
39+
40+
41+
void run();
42+
void quit();
43+
protected:
44+
bool m_isRunning = true;
45+
std::unique_ptr<OGraphicsEngine> m_graphicsEngine;
46+
std::unique_ptr<OWindow> m_display;
47+
};
48+
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#pragma once
26+
#include <OGL3D/OPrerequisites.h>
27+
#include <OGL3D/Math/OVec4.h>
28+
29+
class OGraphicsEngine
30+
{
31+
public:
32+
OGraphicsEngine();
33+
~OGraphicsEngine();
34+
35+
public:
36+
void clear(const OVec4& color);
37+
};
38+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#pragma once
26+
#include <OGL3D/OPrerequisites.h>
27+
28+
class OVec4
29+
{
30+
public:
31+
OVec4()
32+
{
33+
}
34+
OVec4(f32 x, f32 y, f32 z, f32 w) : x(x), y(y), z(z), w(w)
35+
{
36+
}
37+
~OVec4()
38+
{
39+
}
40+
public:
41+
f32 x = 0, y = 0, z = 0, w = 0;
42+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#pragma once
26+
27+
typedef float f32;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#pragma once
26+
27+
class OWindow
28+
{
29+
public:
30+
OWindow();
31+
~OWindow();
32+
33+
void makeCurrentContext();
34+
void present(bool vsync);
35+
private:
36+
void* m_handle = nullptr;
37+
void* m_context = nullptr;
38+
};
39+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*MIT License
2+
3+
C++ OpenGL 3D Game Tutorial Series (https://github.com/PardCode/OpenGL-3D-Game-Tutorial-Series)
4+
5+
Copyright (c) 2021, PardCode
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE.*/
24+
25+
#include <OGL3D/Game/OGame.h>
26+
#include <OGL3D/Window/OWindow.h>
27+
#include <OGL3D/Graphics/OGraphicsEngine.h>
28+
#include <Windows.h>
29+
30+
OGame::OGame()
31+
{
32+
m_graphicsEngine = std::make_unique<OGraphicsEngine>();
33+
m_display = std::make_unique<OWindow>();
34+
35+
36+
m_display->makeCurrentContext();
37+
}
38+
39+
OGame::~OGame()
40+
{
41+
}
42+
43+
void OGame::onCreate()
44+
{
45+
}
46+
47+
void OGame::onUpdate()
48+
{
49+
m_graphicsEngine->clear(OVec4(1, 0, 0, 1));
50+
51+
52+
m_display->present(false);
53+
}
54+
55+
void OGame::onQuit()
56+
{
57+
}
58+
59+
60+
void OGame::quit()
61+
{
62+
m_isRunning = false;
63+
}

0 commit comments

Comments
 (0)