-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenGL_OverlayWindow.cpp
More file actions
151 lines (126 loc) · 5.31 KB
/
OpenGL_OverlayWindow.cpp
File metadata and controls
151 lines (126 loc) · 5.31 KB
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
#include "OpenGL_OverlayWindow.h"
#include <dwmapi.h>
std::map<HWND,OpenGL_OverlayWindow*> OpenGL_OverlayWindow::instances=std::map<HWND,OpenGL_OverlayWindow*>();
OpenGL_OverlayWindow::OpenGL_OverlayWindow(HINSTANCE caller, std::string WindowName, Rect rect){
killme=false;
inited=false;
this->threadCaller=caller;
windowClassName=WindowName.c_str();
// WINDOW INNITIALISATION
WNDCLASSEX app;
memset(&app, 0, sizeof(app));
app.cbSize = sizeof(WNDCLASSEX);
app.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
app.style = CS_HREDRAW | CS_VREDRAW;
app.lpfnWndProc = OpenGL_OverlayWindow::WindowClassMsgHandler;
app.cbClsExtra = 0;
app.cbWndExtra = 0;
app.hInstance = threadCaller;
app.hIcon = LoadIcon(NULL, IDI_APPLICATION);
app.hCursor = LoadCursor(NULL, IDC_ARROW);
app.hbrBackground = (HBRUSH)CreateSolidBrush(0x00000000);
app.lpszClassName = windowClassName;//IDENTIFIER OF THE APP
if(!RegisterClassEx(&app)) {MessageBox(NULL, "RegisterClassEx - failed", "Error", MB_OK | MB_ICONERROR);killme=true;}
Window = CreateWindowEx(
WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST,
windowClassName,
("Overlay Window - "+WindowName).c_str(),
WS_VISIBLE | WS_POPUP,
rect.x,
rect.y,
rect.w,
rect.h,
NULL,
NULL,
threadCaller,
NULL
);
if(!Window) {MessageBox(NULL, "CreateWindowEx - failed", "Error", MB_OK | MB_ICONERROR);killme=true;}
DWM_BLURBEHIND bb = {0};
bb.dwFlags = DWM_BB_ENABLE | DWM_BB_BLURREGION;
bb.hRgnBlur = CreateRectRgn(0, 0, -1, -1);
bb.fEnable = TRUE;
DwmEnableBlurBehindWindow(Window, &bb);
// init opengl context and ling it to "mainWindow"
PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_SUPPORT_COMPOSITION | // Format Must Support Composition
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
8, // An Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
24, // 16Bit Z-Buffer (Depth Buffer)
8, // Some Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};
HDC handelDC = GetDC(Window);
SetPixelFormat(handelDC, ChoosePixelFormat(handelDC, &pfd), &pfd);
openglContextHandle = wglCreateContext(handelDC);
ReleaseDC(Window, handelDC);
instances[Window]=this;
}
OpenGL_OverlayWindow::~OpenGL_OverlayWindow(){
wglMakeCurrent(NULL, NULL);
wglDeleteContext(openglContextHandle) ;
instances.erase(Window);
DestroyWindow(Window);
UnregisterClass(windowClassName,threadCaller);
}
void OpenGL_OverlayWindow::init(){
tagRECT *rectPtr=new tagRECT();
GetWindowRect(Window,rectPtr);
HDC handelDC = GetDC(Window);
wglMakeCurrent(handelDC, openglContextHandle);
GLInit();
GLResize(rectPtr->right-rectPtr->left,rectPtr->bottom-rectPtr->top);
ReleaseDC(Window, handelDC);
inited=true;
delete rectPtr;
}
void OpenGL_OverlayWindow::Tick(){
if (!inited)MessageBox(NULL, "Contexte not Inited", "Error", MB_OK | MB_ICONERROR);
/// GLTick(timeelapsed in milliseconds)
std::chrono::time_point<std::chrono::system_clock> _now=std::chrono::high_resolution_clock::now();
GLTick(std::chrono::duration_cast<std::chrono::milliseconds>(_now-lastTime).count());
lastTime=_now;
/// GLRender()
//get overlay window device contect render and releas it
SetWindowPos(Window ,HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE);// make the window TOPMOST
HDC hdc = GetDC(Window);
wglMakeCurrent(hdc, openglContextHandle);//make the stack thread opengl master current
GLRender();
SwapBuffers(hdc);
ReleaseDC(Window, hdc);
// Handle window messages
MSG msg; if (PeekMessage(&msg, Window, 0, 0, PM_REMOVE)) DispatchMessage(&msg);
}
LRESULT OpenGL_OverlayWindow::WindowClassMsgHandler(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
break;
case WM_DESTROY:
for (std::pair<HWND,OpenGL_OverlayWindow*> instance : instances) {
if (instance.first==hWnd)instance.second->killme=true;
}break;
default:
return DefWindowProc(hWnd,msg,wParam,lParam);
}
return 0;
}
int OpenGL_OverlayWindow::Run(){
init();
while(!killme) {
Tick();
Sleep(2);
}return 0;
}