File: GLWidget.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (106 lines) | stat: -rw-r--r-- 2,396 bytes parent folder | download | duplicates (2)
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
#include "GLWidget.h"

#include "igl.h"
#include "itextstream.h"
#include "ui/iwxgl.h"

#include "GLContext.h"

#include <wx/dcclient.h>

namespace wxutil
{

const int ATTRIBS [] = {
	WX_GL_RGBA,
	WX_GL_DOUBLEBUFFER,
	WX_GL_DEPTH_SIZE, 24,
	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(nullptr)
{
    Bind(wxEVT_PAINT, &GLWidget::OnPaint, this);
}

void GLWidget::SetHasPrivateContext(bool hasPrivateContext)
{
	if (hasPrivateContext)
	{
		_privateContext = new wxGLContext(this);
	}
	else
	{
		DestroyPrivateContext();
	}
}

void GLWidget::DestroyPrivateContext()
{
	if (_privateContext != nullptr)
	{
		_privateContext->UnRef();
		_privateContext = nullptr;
	}
}

GLWidget::~GLWidget()
{
	DestroyPrivateContext();

	if (_registered)
	{
		GlobalWxGlWidgetManager().unregisterGLWidget(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;

		GlobalWxGlWidgetManager().registerGLWidget(this);
	}

    // This is required even though dc is not used otherwise.
    wxPaintDC dc(this);

	// Grab the context for this widget
	if (_privateContext != nullptr)
	{
		// Use the private context for this widget
		SetCurrent(*_privateContext);
	}
	else
	{
		// Use the globally shared context, we rely on this being of type GLContext
		const auto& context = GlobalOpenGLContext().getSharedContext();
		assert(std::dynamic_pointer_cast<GLContext>(context));

		auto wxContext = std::static_pointer_cast<GLContext>(context);
		SetCurrent(wxContext->get());
	}

	if (_renderCallback())
	{
		// Render callback returned true, so drawing took place
		// and we can swap the buffers
		SwapBuffers();
	}
}

} // namespace