Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/opengl/winapp.hpp
16337 views
1
#if defined(_WIN32)
2
# define WIN32_LEAN_AND_MEAN
3
# include <windows.h>
4
#elif defined(__linux__)
5
# include <X11/X.h>
6
# include <X11/Xlib.h>
7
# include <X11/Xutil.h>
8
#endif
9
10
#include <string>
11
12
#include <GL/gl.h>
13
#if defined(_WIN32)
14
# include <GL/glu.h>
15
#elif defined(__linux__)
16
# include <GL/glx.h>
17
#endif
18
19
#if defined(_WIN32)
20
# define WINCLASS "WinAppWnd"
21
#endif
22
23
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
24
25
class WinApp
26
{
27
public:
28
WinApp(int width, int height, std::string& window_name)
29
{
30
m_width = width;
31
m_height = height;
32
m_window_name = window_name;
33
#if defined(_WIN32)
34
m_hInstance = ::GetModuleHandle(NULL);
35
#endif
36
}
37
38
virtual ~WinApp()
39
{
40
#if defined(_WIN32)
41
::UnregisterClass(WINCLASS, m_hInstance);
42
#endif
43
}
44
45
int create()
46
{
47
#if defined(_WIN32)
48
WNDCLASSEX wcex;
49
50
wcex.cbSize = sizeof(WNDCLASSEX);
51
wcex.style = CS_HREDRAW | CS_VREDRAW;
52
wcex.lpfnWndProc = &WinApp::StaticWndProc;
53
wcex.cbClsExtra = 0;
54
wcex.cbWndExtra = 0;
55
wcex.hInstance = m_hInstance;
56
wcex.hIcon = LoadIcon(0, IDI_APPLICATION);
57
wcex.hCursor = LoadCursor(0, IDC_ARROW);
58
wcex.hbrBackground = 0;
59
wcex.lpszMenuName = 0L;
60
wcex.lpszClassName = WINCLASS;
61
wcex.hIconSm = 0;
62
63
ATOM wc = ::RegisterClassEx(&wcex);
64
65
RECT rc = { 0, 0, m_width, m_height };
66
::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
67
68
m_hWnd = ::CreateWindow(
69
(LPCTSTR)wc, m_window_name.c_str(),
70
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
71
rc.right - rc.left, rc.bottom - rc.top,
72
NULL, NULL, m_hInstance, (void*)this);
73
74
if (!m_hWnd)
75
return -1;
76
77
::ShowWindow(m_hWnd, SW_SHOW);
78
::UpdateWindow(m_hWnd);
79
::SetFocus(m_hWnd);
80
#elif defined(__linux__)
81
m_display = XOpenDisplay(NULL);
82
83
if (m_display == NULL)
84
{
85
return -1;
86
}
87
88
m_WM_DELETE_WINDOW = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
89
90
static GLint visual_attributes[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
91
m_visual_info = glXChooseVisual(m_display, 0, visual_attributes);
92
93
if (m_visual_info == NULL)
94
{
95
XCloseDisplay(m_display);
96
return -2;
97
}
98
99
Window root = DefaultRootWindow(m_display);
100
101
m_event_mask = ExposureMask | KeyPressMask;
102
103
XSetWindowAttributes window_attributes;
104
window_attributes.colormap = XCreateColormap(m_display, root, m_visual_info->visual, AllocNone);
105
window_attributes.event_mask = m_event_mask;
106
107
m_window = XCreateWindow(
108
m_display, root, 0, 0, m_width, m_height, 0, m_visual_info->depth,
109
InputOutput, m_visual_info->visual, CWColormap | CWEventMask, &window_attributes);
110
111
XMapWindow(m_display, m_window);
112
XSetWMProtocols(m_display, m_window, &m_WM_DELETE_WINDOW, 1);
113
XStoreName(m_display, m_window, m_window_name.c_str());
114
#endif
115
116
return init();
117
}
118
119
virtual void cleanup()
120
{
121
#if defined(_WIN32)
122
::DestroyWindow(m_hWnd);
123
#elif defined(__linux__)
124
XDestroyWindow(m_display, m_window);
125
XCloseDisplay(m_display);
126
#endif
127
}
128
129
#if defined(_WIN32)
130
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
131
#endif
132
133
int run()
134
{
135
#if defined(_WIN32)
136
MSG msg;
137
138
::ZeroMemory(&msg, sizeof(msg));
139
140
while (msg.message != WM_QUIT)
141
{
142
if (::PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
143
{
144
::TranslateMessage(&msg);
145
::DispatchMessage(&msg);
146
}
147
else
148
{
149
idle();
150
}
151
}
152
153
return static_cast<int>(msg.wParam);
154
#elif defined(__linux__)
155
m_end_loop = false;
156
157
do {
158
XEvent e;
159
160
if (!XCheckWindowEvent(m_display, m_window, m_event_mask, &e) || !handle_event(e))
161
{
162
idle();
163
}
164
} while (!m_end_loop);
165
166
return 0;
167
#endif
168
}
169
170
protected:
171
172
#if defined(_WIN32)
173
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
174
{
175
WinApp* pWnd;
176
177
if (message == WM_NCCREATE)
178
{
179
LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam);
180
pWnd = (WinApp*)(pCreateStruct->lpCreateParams);
181
::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd);
182
}
183
184
pWnd = GetObjectFromWindow(hWnd);
185
186
if (pWnd)
187
return pWnd->WndProc(hWnd, message, wParam, lParam);
188
else
189
return ::DefWindowProc(hWnd, message, wParam, lParam);
190
}
191
192
inline static WinApp* GetObjectFromWindow(HWND hWnd)
193
{
194
return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
195
}
196
#endif
197
198
#if defined(__linux__)
199
virtual int handle_event(XEvent& e) = 0;
200
#endif
201
202
virtual int init() = 0;
203
virtual int render() = 0;
204
205
virtual void idle() = 0;
206
207
#if defined(_WIN32)
208
HINSTANCE m_hInstance;
209
HWND m_hWnd;
210
#elif defined(__linux__)
211
Display* m_display;
212
XVisualInfo* m_visual_info;
213
Window m_window;
214
long m_event_mask;
215
Atom m_WM_DELETE_WINDOW;
216
bool m_end_loop;
217
#endif
218
int m_width;
219
int m_height;
220
std::string m_window_name;
221
cv::TickMeter m_timer;
222
};
223
224