Use paintGL instead of paintEvent

Hopefully making GL code less crashy by using the respective initialization code
This commit is contained in:
Ilya Fedin 2023-03-05 15:23:26 +04:00 committed by John Preston
parent 03f8127875
commit f3744c4ba3

View file

@ -12,7 +12,6 @@
#include <QtGui/QtEvents>
#include <QtGui/QOpenGLContext>
#include <QtGui/QWindow>
#include <QtGui/QPaintEngine>
#include <QOpenGLWidget>
namespace Ui::GL {
@ -31,7 +30,7 @@ public:
private:
void initializeGL() override;
void resizeGL(int w, int h) override;
void paintEvent(QPaintEvent *e) override;
void paintGL() override;
void callDeInit();
const std::unique_ptr<Renderer> _renderer;
@ -55,6 +54,7 @@ SurfaceOpenGL::SurfaceOpenGL(
std::unique_ptr<Renderer> renderer)
: RpWidgetBase<QOpenGLWidget, SurfaceTraits>(parent)
, _renderer(std::move(renderer)) {
setUpdateBehavior(QOpenGLWidget::PartialUpdate);
}
SurfaceOpenGL::~SurfaceOpenGL() {
@ -79,30 +79,17 @@ void SurfaceOpenGL::resizeGL(int w, int h) {
_renderer->resize(this, *context()->functions(), w, h);
}
void SurfaceOpenGL::paintEvent(QPaintEvent *e) {
void SurfaceOpenGL::paintGL() {
if (!updatesEnabled() || size().isEmpty() || !isValid()) {
return;
}
auto redirectOffset = QPoint();
const auto rpd = redirected(&redirectOffset);
const auto device = rpd ? rpd : static_cast<QPaintDevice*>(this);
const auto engine = device->paintEngine();
if (!engine) {
return;
}
engine->begin(device);
if (!isValid()) { // The call above could lose the context.
return;
}
const auto f = context()->functions();
if (const auto bg = _renderer->clearColor()) {
f->glClearColor(bg->redF(), bg->greenF(), bg->blueF(), bg->alphaF());
f->glClear(GL_COLOR_BUFFER_BIT);
}
f->glDisable(GL_BLEND);
f->glViewport(0, 0, device->width(), device->height());
_renderer->paint(this, *f);
engine->end();
}
void SurfaceOpenGL::callDeInit() {