-
Notifications
You must be signed in to change notification settings - Fork 47
/
GLWidget.cpp
99 lines (83 loc) · 2.07 KB
/
GLWidget.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
#include "GLWidget.h"
#include "igl.h"
#include "itextstream.h"
#include <wx/dcclient.h>
namespace wxutil
{
const int ATTRIBS [] = {
WX_GL_RGBA,
WX_GL_DOUBLEBUFFER,
WX_GL_DEPTH_SIZE, 16,
0
};
GLWidget::GLWidget(wxWindow *parent, const std::function<bool()>& renderCallback, const std::string& name) :
wxGLCanvas(parent, wxID_ANY, ATTRIBS, wxDefaultPosition, wxDefaultSize,
wxFULL_REPAINT_ON_RESIZE | wxWANTS_CHARS,
wxString(name.c_str(), *wxConvCurrent)),
_registered(false),
_renderCallback(renderCallback),
_privateContext(NULL)
{
Connect(wxEVT_PAINT, wxPaintEventHandler(GLWidget::OnPaint), NULL, this);
}
void GLWidget::SetHasPrivateContext(bool hasPrivateContext)
{
if (hasPrivateContext)
{
_privateContext = new wxGLContext(this);
}
else
{
DestroyPrivateContext();
}
}
void GLWidget::DestroyPrivateContext()
{
if (_privateContext != NULL)
{
_privateContext->UnRef();
_privateContext = NULL;
}
}
GLWidget::~GLWidget()
{
DestroyPrivateContext();
if (_registered)
{
GlobalOpenGL().unregisterGLCanvas(this);
}
}
void GLWidget::OnPaint(wxPaintEvent& WXUNUSED(event))
{
// Got this check from the wxWidgets sources, they assert the widget to be shown
// "although on MSW it works even if the window is still hidden, it doesn't
// work in other ports (notably X11-based ones) and documentation mentions
// that SetCurrent() can only be called for a shown window, so check for it"
if (!IsShownOnScreen()) return;
// Make sure this widget is registered
if (!_registered)
{
_registered = true;
GlobalOpenGL().registerGLCanvas(this);
}
// This is required even though dc is not used otherwise.
wxPaintDC dc(this);
// Grab the contex for this widget
if (_privateContext != NULL)
{
// Use the private context for this widget
SetCurrent(*_privateContext);
}
else
{
// Use the globally shared context
SetCurrent(GlobalOpenGL().getwxGLContext());
}
if (_renderCallback())
{
// Render callback returned true, so drawing took place
// and we can swap the buffers
SwapBuffers();
}
}
} // namespace