This repository has been archived by the owner on Sep 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PHsOpenGLFrame.cpp
199 lines (161 loc) · 4.5 KB
/
PHsOpenGLFrame.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
2016.01.22
Created by PHJ.
*/
#ifndef _PHS_OPENGL_FRAME_CPP
#define _PHS_OPENGL_FRAME_CPP
#include "PHsOpenGLFrame.h"
namespace PHs
{
/*
TODO : Place your code here
*/
inline namespace PHsFrame
{
using namespace std;
Bool OpenGLFrame::initalizeFrame(const OpenGLFrameAttr &sNewOpenGLFrameAttr)
{
if(this->bCreated)
return false;
WNDCLASS sClass
{
CS_OWNDC, //style
sNewOpenGLFrameAttr.fWndProc, //lpfnWndProc
0, //cbClsExtra
0, //cbWndExtra
this->hFrameInstance = GetModuleHandle(nullptr), //hInstance
LoadIcon(nullptr, IDI_APPLICATION), //hIcon
LoadCursor(nullptr, IDC_ARROW), //hCursor
nullptr, //hbrBackground
nullptr, //lpszMenuName
(this->sFrameClassName = sNewOpenGLFrameAttr.sClassName).c_str() //lpszClassName
};
if(!RegisterClass(&sClass))
{
this->finalizeFrame();
return false;
}
uInt nStyle;
uInt nExtraStyle;
RECT sClientRect
{
//Empty.
};
if(this->bFullscr = sNewOpenGLFrameAttr.bFullscreen)
{
nStyle = WS_POPUP;
nExtraStyle = WS_EX_APPWINDOW;
GetWindowRect(GetDesktopWindow(), &sClientRect);
DEVMODE sDevMode
{
//Empty.
};
sDevMode.dmSize = sizeof(DEVMODE);
sDevMode.dmPelsWidth = sClientRect.right;
sDevMode.dmPelsHeight = sClientRect.bottom;
sDevMode.dmBitsPerPel = sNewOpenGLFrameAttr.nColorBit;
sDevMode.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_BITSPERPEL;
if(ChangeDisplaySettings(&sDevMode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
return this->bFullscr = false;
}
else
{
nStyle = WS_OVERLAPPEDWINDOW;
nExtraStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
sClientRect.right = static_cast <Int>(sNewOpenGLFrameAttr.nWidth);
sClientRect.bottom = static_cast <Int>(sNewOpenGLFrameAttr.nHeight);
}
AdjustWindowRectEx(&sClientRect, nStyle, false, nExtraStyle);
sClientRect.right -= sClientRect.left;
sClientRect.bottom -= sClientRect.top;
if(!(this->hFrameWnd = CreateWindowEx(
nExtraStyle,
sNewOpenGLFrameAttr.sClassName.c_str(),
sNewOpenGLFrameAttr.sTitleText.c_str(),
nStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
sClientRect.right,
sClientRect.bottom,
nullptr,
nullptr,
this->hFrameInstance,
nullptr)))
{
this->finalizeFrame();
return false;
}
PIXELFORMATDESCRIPTOR sPixelFormatDesc
{
sizeof(PIXELFORMATDESCRIPTOR),
1, PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER, PFD_TYPE_RGBA, static_cast <Byte>(sNewOpenGLFrameAttr.nColorBit),
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, static_cast <Byte>(sNewOpenGLFrameAttr.nDepthBit), 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0
};
if(!(this->hFrameDC = GetDC(this->hFrameWnd)))
{
this->finalizeFrame();
return false;
}
Int nPixelFormat = ChoosePixelFormat(this->hFrameDC, &sPixelFormatDesc);
if(!nPixelFormat)
{
this->finalizeFrame();
return false;
}
if(!SetPixelFormat(this->hFrameDC, nPixelFormat, &sPixelFormatDesc))
{
this->finalizeFrame();
return false;
}
if(!(this->hFrameRC = wglCreateContext(this->hFrameDC)))
{
this->finalizeFrame();
return false;
}
if(!wglMakeCurrent(this->hFrameDC, this->hFrameRC))
{
this->finalizeFrame();
return false;
}
SetForegroundWindow(this->hFrameWnd);
SetFocus(this->hFrameWnd);
this->bCreated = true;
/*
Load OpenGL functions.
*/
OpenGLExt::initializeOpenGLExt();
return true;
}
Void OpenGLFrame::finalizeFrame()
{
this->bCreated =
this->bVisible = false;
if(this->bFullscr)
{
ChangeDisplaySettings(nullptr, 0ul);
this->bFullscr = false;
}
if(this->hFrameRC)
{
wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(this->hFrameRC);
}
if(this->hFrameWnd)
{
if(this->hFrameDC)
ReleaseDC(this->hFrameWnd, this->hFrameDC);
DestroyWindow(this->hFrameWnd);
}
UnregisterClass(this->sFrameClassName.c_str(), this->hFrameInstance);
this->hFrameDC = nullptr;
this->hFrameRC = nullptr;
this->hFrameWnd = nullptr;
this->hFrameInstance = nullptr;
}
Void OpenGLFrame::setVisible(Bool bNewVisible)
{
ShowWindow(this->hFrameWnd, (this->bVisible = bNewVisible) ? SW_SHOW : SW_HIDE);
}
}
}
#endif