-
Notifications
You must be signed in to change notification settings - Fork 1
/
OpenGLWnd.hpp
118 lines (93 loc) · 2.81 KB
/
OpenGLWnd.hpp
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
#pragma once
//---------------------------------------------------------------------------//
//
// OpenGLWnd.hpp
// OpenGL で描画するウィンドウの基底クラス
// Copyright (C) 2005-2016 tapetums
//
//---------------------------------------------------------------------------//
#include <gl/gl.h>
#include <gl/glext.h>
#pragma comment(lib, "opengl32.lib")
#include "AeroWnd.hpp"
//---------------------------------------------------------------------------//
// 前方宣言
//---------------------------------------------------------------------------//
namespace tapetums
{
class OpenGLWnd;
}
//---------------------------------------------------------------------------//
class tapetums::OpenGLWnd : public tapetums::AeroWnd
{
protected:
HDC m_dc { nullptr };
HGLRC m_glrc { nullptr };
public:
OpenGLWnd() = default;
~OpenGLWnd() = default;
OpenGLWnd(const OpenGLWnd&) = delete;
OpenGLWnd& operator= (const OpenGLWnd&) = delete;
OpenGLWnd(OpenGLWnd&& rhs) noexcept = default;
OpenGLWnd& operator=(OpenGLWnd&& rhs) noexcept = default;
public:
virtual void Update() = 0;
protected:
bool CreateContext(HWND hwnd);
void ReleaseContext();
};
//---------------------------------------------------------------------------//
inline bool tapetums::OpenGLWnd::CreateContext(HWND hwnd)
{
m_dc = ::GetDC(hwnd);
constexpr PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
32, // color
0, 0, // R
0, 0, // G
0, 0, // B
0, 0, // A
0, // AC
0, 0, 0, 0, // RGBA
24, // depth
8, // stencil
0, // aux
PFD_MAIN_PLANE,
0, // reserved
0, // layer mask
0, // visible mask
0 // damage mask
};
const auto pxfmt = ::ChoosePixelFormat(m_dc, &pfd);
if ( pxfmt == 0 )
{
ShowLastError(TEXT("OpenGLWnd::CreateContext::ChoosePixelFormat()"));
ReleaseContext();
return false;
}
const auto result = ::SetPixelFormat(m_dc, pxfmt, &pfd);
if ( ! result )
{
ShowLastError(TEXT("OpenGLWnd::CreateContext::SetPixelFormat()"));
ReleaseContext();
return false;
}
m_glrc = ::wglCreateContext(m_dc);
::wglMakeCurrent(m_dc, m_glrc);
return true;
}
//---------------------------------------------------------------------------//
inline void tapetums::OpenGLWnd::ReleaseContext()
{
::wglMakeCurrent(m_dc, nullptr);
::wglDeleteContext(m_glrc);
m_glrc = nullptr;
::ReleaseDC(m_hwnd, m_dc);
m_dc = nullptr;
}
//---------------------------------------------------------------------------//
// OpenGLWnd.hpp