default unlimit fps, vsync flag
This commit is contained in:
106
openglwindow.cpp
106
openglwindow.cpp
@@ -1,72 +1,26 @@
|
||||
/*
|
||||
QGL OpenGLWindow
|
||||
Ivan Pelipenko peri4ko@yandex.ru
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU Lesser General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "openglwindow.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QOpenGLContext>
|
||||
#include <QOpenGLPaintDevice>
|
||||
#include <QPainter>
|
||||
#include <qopenglext.h>
|
||||
|
||||
|
||||
OpenGLWindow::OpenGLWindow(QWindow *parent)
|
||||
: QWindow(parent)
|
||||
, m_context(nullptr)
|
||||
, m_device(nullptr)
|
||||
{
|
||||
OpenGLWindow::OpenGLWindow(QWindow *parent) : QWindow(parent) {
|
||||
setFlags(flags() | Qt::FramelessWindowHint);
|
||||
setSurfaceType(QWindow::OpenGLSurface);
|
||||
QSurfaceFormat format = QSurfaceFormat::defaultFormat();
|
||||
// qDebug() << format;
|
||||
#ifdef QT_OPENGL_ES_2
|
||||
format.setRenderableType(QSurfaceFormat::OpenGLES);
|
||||
#else
|
||||
if (QOpenGLContext::openGLModuleType() == QOpenGLContext::LibGL) {
|
||||
format.setVersion(4, 0);
|
||||
format.setProfile(QSurfaceFormat::CoreProfile);
|
||||
format.setVersion(2, 0);
|
||||
format.setProfile(QSurfaceFormat::NoProfile);
|
||||
}
|
||||
#endif
|
||||
format.setDepthBufferSize(24);
|
||||
format.setSamples(8);
|
||||
setFormat(format);
|
||||
QSurfaceFormat::setDefaultFormat(format);
|
||||
}
|
||||
|
||||
|
||||
OpenGLWindow::~OpenGLWindow() {
|
||||
delete m_device;
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::render(QPainter *painter) {
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::initialize() {
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::render() {
|
||||
// if (!m_device) m_device = new QOpenGLPaintDevice;
|
||||
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
|
||||
// m_device->setSize(size() * devicePixelRatio());
|
||||
// m_device->setDevicePixelRatio(devicePixelRatio());
|
||||
// QPainter painter(m_device);
|
||||
// render(&painter);
|
||||
}
|
||||
|
||||
|
||||
@@ -77,10 +31,10 @@ void OpenGLWindow::renderLater() {
|
||||
|
||||
bool OpenGLWindow::event(QEvent *event) {
|
||||
switch (event->type()) {
|
||||
case QEvent::UpdateRequest:
|
||||
renderNow();
|
||||
case QEvent::UpdateRequest:
|
||||
renderNow();
|
||||
return true;
|
||||
default:
|
||||
default:
|
||||
return QWindow::event(event);
|
||||
}
|
||||
}
|
||||
@@ -91,15 +45,54 @@ void OpenGLWindow::exposeEvent(QExposeEvent *event) {
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::setVSync(bool on) {
|
||||
QSurfaceFormat f = requestedFormat();
|
||||
if (on) {
|
||||
if (f.swapInterval() != 1) {
|
||||
f.setSwapInterval(1);
|
||||
setFormat(f);
|
||||
format_change = true;
|
||||
}
|
||||
} else {
|
||||
if (f.swapInterval() != 0) {
|
||||
f.setSwapInterval(0);
|
||||
setFormat(f);
|
||||
format_change = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
bool OpenGLWindow::getVSync() const {
|
||||
return (requestedFormat().swapInterval() == 1);
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::setSamples(int samples) {
|
||||
QSurfaceFormat f = requestedFormat();
|
||||
if (f.samples() != samples) {
|
||||
f.setSamples(samples);
|
||||
setFormat(f);
|
||||
format_change = true;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int OpenGLWindow::getSamples() const {
|
||||
return requestedFormat().samples();
|
||||
}
|
||||
|
||||
|
||||
void OpenGLWindow::renderNow() {
|
||||
if (!isExposed())
|
||||
return;
|
||||
if (!isExposed()) return;
|
||||
bool needsInitialize = false;
|
||||
if (!m_context) {
|
||||
if (!m_context || format_change) {
|
||||
if (m_context) delete m_context;
|
||||
m_context = new QOpenGLContext(this);
|
||||
m_context->setFormat(requestedFormat());
|
||||
m_context->create();
|
||||
needsInitialize = true;
|
||||
format_change = false;
|
||||
}
|
||||
m_context->makeCurrent(this);
|
||||
if (needsInitialize) {
|
||||
@@ -108,5 +101,6 @@ void OpenGLWindow::renderNow() {
|
||||
}
|
||||
render();
|
||||
m_context->swapBuffers(this);
|
||||
frame_cnt++;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user