Skip to content

Commit

Permalink
Add swap interval control to QtGame. There's no longer any need to re…
Browse files Browse the repository at this point in the history
…start to change vsync settings.

Refs #2815.
  • Loading branch information
Safety0ff committed Jul 13, 2011
1 parent 6205899 commit c394a15
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 11 deletions.
1 change: 1 addition & 0 deletions lib/framework/wzapp.cpp
Expand Up @@ -218,6 +218,7 @@ WzMainWindow *WzMainWindow::instance()

void WzMainWindow::initializeGL()
{
QtGameWidget::initializeGL();
}

void WzMainWindow::drawPixmap(int XPos, int YPos, QPixmap *pix)
Expand Down
6 changes: 3 additions & 3 deletions lib/qtgame/Makefile.am
@@ -1,4 +1,4 @@
AM_CPPFLAGS = $(WZ_CPPFLAGS) $(QT4_CFLAGS)
AM_CPPFLAGS = $(WZ_CPPFLAGS) $(QT4_CFLAGS) $(GLEW_CFLAGS)
AM_CFLAGS = $(WZ_CFLAGS)
AM_CXXFLAGS = $(WZ_CXXFLAGS) $(QT4_CFLAGS)

Expand All @@ -12,5 +12,5 @@ BUILT_SOURCES = $(MOCEDFILES)
CLEANFILES = $(MOCEDFILES)

noinst_LIBRARIES = libqtgame.a
noinst_HEADERS = qtgame.h $(MOCHEADER)
libqtgame_a_SOURCES = qtgame.cpp qtgame_moc.cpp
noinst_HEADERS = qtgame.h swapinterval.h $(MOCHEADER)
libqtgame_a_SOURCES = qtgame.cpp qtgame_moc.cpp swapinterval.cpp
23 changes: 22 additions & 1 deletion lib/qtgame/qtgame.cpp
@@ -1,6 +1,7 @@
#include "qtgame.h"

#include "lib/framework/wzglobal.h"
#include "swapinterval.h"

#if defined(WZ_CC_MSVC)
#include "qtgame.h.moc" // this is generated on the pre-build event.
Expand Down Expand Up @@ -196,14 +197,34 @@ void QtGameWidget::updateResolutionList()
#endif
}

QGLFormat QtGameWidget::adjustFormat(const QGLFormat &format)
{
QGLFormat adjusted(format);
mSwapInterval = adjusted.swapInterval();
adjusted.setSwapInterval(0);
return adjusted;
}

void QtGameWidget::initializeGL()
{
setSwapInterval(mSwapInterval);
}

QtGameWidget::QtGameWidget(QSize curResolution, const QGLFormat &format, QWidget *parent, Qt::WindowFlags f, const QGLWidget *shareWidget)
: QGLWidget(format, parent, shareWidget, f), mOriginalResolution(0, 0), mMinimumSize(0, 0)
: QGLWidget(adjustFormat(format), parent, shareWidget, f), mOriginalResolution(0, 0), mMinimumSize(0, 0)
{
mWantedSize = curResolution;
mResolutionChanged = false;
updateResolutionList();
}

void QtGameWidget::setSwapInterval(int interval)
{
mSwapInterval = interval;
makeCurrent();
::setSwapInterval(*this, &mSwapInterval);
}

bool QtGameWidget::setResolution(const QSize res, int rate, int depth)
{
#ifdef WZ_WS_X11
Expand Down
8 changes: 8 additions & 0 deletions lib/qtgame/qtgame.h
Expand Up @@ -10,6 +10,7 @@ class QtGameWidget : public QGLWidget
private:
QSize mOriginalResolution, mCurrentResolution, mWantedSize, mMinimumSize;
int mOriginalRefreshRate, mCurrentRefreshRate;
int mSwapInterval;
int mOriginalDepth, mCurrentDepth;
QList<QSize> mResolutions;
bool mResolutionChanged;
Expand All @@ -19,12 +20,19 @@ class QtGameWidget : public QGLWidget
void updateResolutionList();
bool setResolution(const QSize res, int rate, int depth);

QGLFormat adjustFormat(const QGLFormat &format);
protected:
virtual void initializeGL();

public:
QtGameWidget(QSize curResolution, const QGLFormat &format, QWidget *parent = 0, Qt::WindowFlags f = 0, const QGLWidget *shareWidget = 0);
~QtGameWidget() { if (mResolutionChanged) restoreResolution(); }
QList<QSize> availableResolutions() const { return mResolutions; }
bool isMouseTrapped() { return mCursorTrapped; }

int swapInterval() const { return mSwapInterval; }
void setSwapInterval(int interval);

public slots:
void setMinimumResolution(QSize res);

Expand Down
86 changes: 86 additions & 0 deletions lib/qtgame/swapinterval.cpp
@@ -0,0 +1,86 @@
#include "swapinterval.h"
#include "lib/framework/wzglobal.h"
#include "lib/framework/opengl.h"

#if defined(WZ_WS_X11)

#include <GL/glxew.h>
// X11 polution
#ifdef Status
#undef Status
#endif
#ifdef CursorShape
#undef CursorShape
#endif
#ifdef Bool
#undef Bool
#endif

#include <QtGui/QX11Info>
#include <QtOpenGL/QGLWidget>


void setSwapInterval(QGLWidget const &glWidget, int * interval)
{
QGLContext const &context = *glWidget.context();
QX11Info const &xinfo = glWidget.x11Info();
if (GLXEW_EXT_swap_control)
{
unsigned clampedInterval;
if (*interval < 0)
*interval = 0;
glXSwapIntervalEXT(xinfo.display(), glWidget.winId(), *interval);
glXQueryDrawable(xinfo.display(), glWidget.winId(), GLX_SWAP_INTERVAL_EXT, &clampedInterval);
*interval = clampedInterval;
}
else if (xinfo.display() && strstr(glXQueryExtensionsString(xinfo.display(), xinfo.screen()), "GLX_MESA_swap_control"))
{
typedef int (* PFNGLXSWAPINTERVALMESAPROC)(unsigned);
typedef int (* PFNGLXGETSWAPINTERVALMESAPROC)(void);
PFNGLXSWAPINTERVALMESAPROC glXSwapIntervalMESA = (PFNGLXSWAPINTERVALMESAPROC) context.getProcAddress("glXSwapIntervalMESA");
PFNGLXGETSWAPINTERVALMESAPROC glXGetSwapIntervalMESA = (PFNGLXGETSWAPINTERVALMESAPROC) context.getProcAddress("glXGetSwapIntervalMESA");

if (glXSwapIntervalMESA && glXGetSwapIntervalMESA)
{
if (*interval < 0)
*interval = 0;
glXSwapIntervalMESA(*interval);
*interval = glXGetSwapIntervalMESA();
}
}
else if (GLXEW_SGI_swap_control)
{
if (*interval < 1)
*interval = 1;
if (glXSwapIntervalSGI(*interval))
{
// Error, revert to default
*interval = 1;
glXSwapIntervalSGI(1);
}
}
else
{
*interval = -1;
}
}

#elif defined(WZ_WS_WIN)
#include <GL/wglew.h>
#include <QtOpenGL/QGLWidget>

void setSwapInterval(QGLWidget const &, int * interval)
{
if (WGLEW_EXT_swap_control)
{
if (*interval < 0)
*interval = 0;
wglSwapIntervalEXT(*interval);
*interval = wglGetSwapIntervalEXT();
}
else
{
*interval = -1;
}
}
#endif
7 changes: 7 additions & 0 deletions lib/qtgame/swapinterval.h
@@ -0,0 +1,7 @@
#ifndef QTGAME_SWAPINTERVAL_H
#define QTGAME_SWAPINTERVAL_H

class QGLWidget;
extern void setSwapInterval(QGLWidget const &glWidget, int * interval);

#endif // QTGAME_SWAPINTERVAL_H
18 changes: 18 additions & 0 deletions lib/qtgame/swapinterval.mm
@@ -0,0 +1,18 @@
#import "swapinterval.h"
#import "lib/framework/wzglobal.h"

#ifdef WZ_OS_MAC

#import <AppKit/NSOpenGL.h>

void setSwapInterval(QGLWidget const &, int * interval)
{
NSOpenGLContext *context = [NSOpenGLContext currentContext];
if (*interval >= 0)
{
[context setValues:interval forParameter:NSOpenGLCPSwapInterval];
}
[context getValues:interval forParameter:NSOpenGLCPSwapInterval];
}

#endif // WZ_OS_MAC
8 changes: 8 additions & 0 deletions macosx/Warzone.xcodeproj/project.pbxproj
Expand Up @@ -440,6 +440,7 @@
4355E13210D6028C00A19EE4 /* theoradec.h in Headers */ = {isa = PBXBuildFile; fileRef = 4355E12E10D6028C00A19EE4 /* theoradec.h */; settings = {ATTRIBUTES = (Public, ); }; };
4355E13310D6028C00A19EE4 /* theoraenc.h in Headers */ = {isa = PBXBuildFile; fileRef = 4355E12F10D6028C00A19EE4 /* theoraenc.h */; settings = {ATTRIBUTES = (Public, ); }; };
4371B60F11D93FD1005A67AB /* pngpriv.h in Headers */ = {isa = PBXBuildFile; fileRef = 4371B60D11D93FD0005A67AB /* pngpriv.h */; };
437FB24D13CE5CA900E7EF5B /* swapinterval.mm in Sources */ = {isa = PBXBuildFile; fileRef = 437FB24B13CE5CA900E7EF5B /* swapinterval.mm */; };
438BDDF31129DC9A00998660 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 438BDDD71129DC9A00998660 /* InfoPlist.strings */; };
43A6285B13A6C4A400C6B786 /* geometry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43A6285913A6C4A400C6B786 /* geometry.cpp */; };
43A8417811028EDD00733CCB /* pointtree.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43A8417611028EDD00733CCB /* pointtree.cpp */; };
Expand Down Expand Up @@ -1525,6 +1526,9 @@
4355E12E10D6028C00A19EE4 /* theoradec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = theoradec.h; path = external/libtheora/include/theora/theoradec.h; sourceTree = "<group>"; };
4355E12F10D6028C00A19EE4 /* theoraenc.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = theoraenc.h; path = external/libtheora/include/theora/theoraenc.h; sourceTree = "<group>"; };
4371B60D11D93FD0005A67AB /* pngpriv.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = pngpriv.h; path = external/libpng/pngpriv.h; sourceTree = "<group>"; };
437FB24913CE5CA900E7EF5B /* swapinterval.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = swapinterval.cpp; path = ../lib/qtgame/swapinterval.cpp; sourceTree = SOURCE_ROOT; };
437FB24A13CE5CA900E7EF5B /* swapinterval.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = swapinterval.h; path = ../lib/qtgame/swapinterval.h; sourceTree = SOURCE_ROOT; };
437FB24B13CE5CA900E7EF5B /* swapinterval.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = swapinterval.mm; path = ../lib/qtgame/swapinterval.mm; sourceTree = SOURCE_ROOT; };
438BDDD81129DC9A00998660 /* cs */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = cs; path = cs.lproj/InfoPlist.strings; sourceTree = "<group>"; };
438BDDD91129DC9A00998660 /* da */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = da; path = da.lproj/InfoPlist.strings; sourceTree = "<group>"; };
438BDDDA1129DC9A00998660 /* de */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.strings; name = de; path = de.lproj/InfoPlist.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2633,6 +2637,9 @@
4313231513BE86360023280C /* QtGame */ = {
isa = PBXGroup;
children = (
437FB24913CE5CA900E7EF5B /* swapinterval.cpp */,
437FB24A13CE5CA900E7EF5B /* swapinterval.h */,
437FB24B13CE5CA900E7EF5B /* swapinterval.mm */,
4313231713BE866B0023280C /* qtgame.cpp */,
4313231813BE866B0023280C /* qtgame.h */,
4313231913BE866B0023280C /* qtgame.h.qwth */,
Expand Down Expand Up @@ -4171,6 +4178,7 @@
43A6285B13A6C4A400C6B786 /* geometry.cpp in Sources */,
4313231A13BE866B0023280C /* qtgame.cpp in Sources */,
43DD1BCE13662006003AA9EC /* netlobby.cpp in Sources */,
437FB24D13CE5CA900E7EF5B /* swapinterval.mm in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
5 changes: 3 additions & 2 deletions po/POTFILES.in
Expand Up @@ -186,6 +186,7 @@ lib/netplay/netqueue.cpp
lib/netplay/netsocket.cpp
lib/netplay/nettypes.cpp
lib/qtgame/qtgame.cpp
lib/qtgame/swapinterval.cpp
lib/script/codeprint.cpp
lib/script/event.cpp
lib/script/eventsave.cpp
Expand All @@ -210,6 +211,8 @@ lib/widget/label.cpp
lib/widget/slider.cpp
lib/widget/tip.cpp
lib/widget/widget.cpp
po/custom/mac-infoplist.txt
po/custom/warzone2100.desktop.txt
src/action.cpp
src/advvis.cpp
src/ai.cpp
Expand Down Expand Up @@ -316,5 +319,3 @@ src/warcam.cpp
src/warzoneconfig.cpp
src/wavecast.cpp
src/wrappers.cpp
po/custom/warzone2100.desktop.txt
po/custom/mac-infoplist.txt
3 changes: 2 additions & 1 deletion src/frontend.cpp
Expand Up @@ -765,7 +765,7 @@ static bool startVideoOptionsMenu(void)
addTextButton(FRONTEND_TEXTURESZ_R, FRONTEND_POS4M-55, FRONTEND_POS4Y, textureSize, 0);

// Vsync
addTextButton(FRONTEND_VSYNC, FRONTEND_POS5X-35, FRONTEND_POS5Y, _("Vertical sync*"), 0);
addTextButton(FRONTEND_VSYNC, FRONTEND_POS5X-35, FRONTEND_POS5Y, _("Vertical sync"), 0);

if (war_GetVsync())
{
Expand Down Expand Up @@ -954,6 +954,7 @@ bool runVideoOptionsMenu(void)
war_SetVsync(true);
widgSetString(psWScreen, FRONTEND_VSYNC_R, _("On"));
}
WzMainWindow::instance()->setSwapInterval(war_GetVsync());
break;
}

Expand Down
8 changes: 4 additions & 4 deletions src/main.cpp
Expand Up @@ -1264,10 +1264,6 @@ int main(int argc, char *argv[])
int w = pie_GetVideoBufferWidth();
int h = pie_GetVideoBufferHeight();

if (war_GetVsync())
{
format.setSwapInterval(1);
}
if (war_getFSAA())
{
format.setSampleBuffers(true);
Expand All @@ -1280,6 +1276,7 @@ int main(int argc, char *argv[])
QMessageBox::critical(NULL, "Oops!", "Warzone2100 failed to create an OpenGL context. This probably means that your graphics drivers are out of date. Try updating them!");
return EXIT_FAILURE;
}

screenWidth = w;
screenHeight = h;
if (war_getFullscreen())
Expand All @@ -1301,6 +1298,9 @@ int main(int argc, char *argv[])
mainwindow.setMinimumSize(w, h);
mainwindow.setMaximumSize(w, h);
}

mainwindow.setSwapInterval(war_GetVsync());

mainwindow.setReadyToPaint();

char buf[256];
Expand Down

0 comments on commit c394a15

Please sign in to comment.