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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
#if defined(WITH_QT5)
#if defined(WITH_GLEW)
# define GLEW_NO_GLU
# include <GL/glew.h>
#endif
#include "../../Main.h"
#include "../Graphics/GL/GLTexture.h"
#include "../Graphics/GL/GLFramebuffer.h"
#include "Qt5GfxDevice.h"
#include "../MainApplication.h"
#include "Qt5Widget.h"
#include <QWindow>
#include <QApplication>
#include <QScreen>
namespace nCine::Backends
{
Qt5GfxDevice::Qt5GfxDevice(const WindowMode& windowMode, const GLContextInfo& glContextInfo, const DisplayMode& displayMode, Qt5Widget& widget)
: IGfxDevice(windowMode, glContextInfo, displayMode), widget_(widget), isResizable_(windowMode.isResizable)
{
initDevice(windowMode.isFullscreen);
}
void Qt5GfxDevice::setSwapInterval(int interval)
{
widget_.format().setSwapInterval(interval);
}
void Qt5GfxDevice::setResolution(bool fullscreen, int width, int height)
{
width_ = width;
height_ = height;
theApplication().resizeRootViewport(width, height);
QRect rect = widget_.GetGeometry();
rect.setWidth(width);
rect.setHeight(height);
widget_.setGeometry(rect);
if (fullscreen) {
widget_.setWindowState(widget_.windowState() | Qt::WindowFullScreen);
} else {
widget_.setWindowState(widget_.windowState() & ~Qt::WindowFullScreen);
if (!isResizable_) {
widget_.setMinimumSize(width, height);
widget_.setMaximumSize(width, height);
}
}
}
void Qt5GfxDevice::setResolutionInternal(int width, int height)
{
width_ = width;
height_ = height;
theApplication().resizeRootViewport(width, height);
QRect rect = widget_.GetGeometry();
rect.setWidth(width);
rect.setHeight(height);
widget_.setGeometry(rect);
}
void Qt5GfxDevice::setWindowTitle(const char* windowTitle)
{
widget_.setWindowTitle(windowTitle);
}
void Qt5GfxDevice::setWindowIcon(const char* windowIconFilename)
{
widget_.setWindowIcon(QIcon(windowIconFilename));
}
int Qt5GfxDevice::windowPositionX() const
{
return widget_.pos().x();
}
int Qt5GfxDevice::windowPositionY() const
{
return widget_.pos().y();
}
const Vector2i Qt5GfxDevice::windowPosition() const
{
return Vector2i(widget_.pos().x(), widget_.pos().y());
}
void Qt5GfxDevice::setWindowPosition(int x, int y)
{
QRect geometry = widget_.GetGeometry();
geometry.setX(x);
geometry.setY(y);
widget_.setGeometry(geometry);
}
void Qt5GfxDevice::flashWindow() const
{
QApplication::alert(&widget_, 0);
}
const Qt5GfxDevice::VideoMode& Qt5GfxDevice::currentVideoMode() const
{
return videoModes_[0];
}
void Qt5GfxDevice::updateVideoModes()
{
QScreen* screen = nullptr;
if (widget_.window() && widget_.window()->windowHandle()) {
screen = widget_.window()->windowHandle()->screen();
} else {
screen = QApplication::primaryScreen();
}
videoModes_.resize_for_overwrite(1);
if (screen) {
videoModes_[0].width = screen->size().width();
videoModes_[0].height = screen->size().height();
videoModes_[0].refreshRate = screen->refreshRate();
if (screen->depth() >= 24) {
videoModes_[0].redBits = 8;
videoModes_[0].greenBits = 8;
videoModes_[0].blueBits = 8;
}
}
}
#if defined(WITH_GLEW)
void Qt5GfxDevice::initGlew()
{
const GLenum err = glewInit();
FATAL_ASSERT_MSG(err == GLEW_OK, "GLEW error: {}", glewGetErrorString(err));
glContextInfo_.debugContext = glContextInfo_.debugContext && glewIsSupported("GL_ARB_debug_output");
}
#endif
void Qt5GfxDevice::resetTextureBinding()
{
GLTexture::bindHandle(GL_TEXTURE_2D, 0);
}
void Qt5GfxDevice::bindDefaultDrawFramebufferObject()
{
const GLuint glHandle = widget_.defaultFramebufferObject();
GLFramebuffer::bindHandle(GL_DRAW_FRAMEBUFFER, glHandle);
}
void Qt5GfxDevice::initDevice(bool isFullscreen)
{
QSurfaceFormat format;
format.setRedBufferSize(displayMode_.redBits());
format.setGreenBufferSize(displayMode_.greenBits());
format.setBlueBufferSize(displayMode_.blueBits());
//format.setAlphaBufferSize(displayMode_.alphaBits());
format.setSwapBehavior(displayMode_.isDoubleBuffered() ? QSurfaceFormat::DoubleBuffer : QSurfaceFormat::SingleBuffer);
format.setDepthBufferSize(displayMode_.depthBits());
format.setStencilBufferSize(displayMode_.stencilBits());
format.setVersion(glContextInfo_.majorVersion, glContextInfo_.minorVersion);
#if defined(WITH_OPENGLES)
format.setRenderableType(QSurfaceFormat::OpenGLES);
#endif
format.setProfile(glContextInfo_.coreProfile ? QSurfaceFormat::CoreProfile : QSurfaceFormat::CompatibilityProfile);
if (glContextInfo_.debugContext)
format.setOptions(QSurfaceFormat::DebugContext);
if (isFullscreen) {
widget_.setWindowState(widget_.windowState() | Qt::WindowFullScreen);
}
const int interval = displayMode_.hasVSync() ? 1 : 0;
format.setSwapInterval(interval);
widget_.setFormat(format);
QSurfaceFormat::setDefaultFormat(format);
updateVideoModes();
}
}
#endif
|