summaryrefslogtreecommitdiff
path: root/src/openglgraphics.cpp
diff options
context:
space:
mode:
authorThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-27 10:51:33 +0000
committerThorbjørn Lindeijer <bjorn@lindeijer.nl>2024-02-27 10:51:33 +0000
commit254c3db4a39aa35274fd7cd4ec2cccde5b92d71b (patch)
tree01c1b5ad71794c02b03d9c45f2c4b5043bc03163 /src/openglgraphics.cpp
parent81e4f170d8ba4ccbcfa1e6c07bd0522dfc3b6e08 (diff)
downloadmana-254c3db4a39aa35274fd7cd4ec2cccde5b92d71b.tar.gz
mana-254c3db4a39aa35274fd7cd4ec2cccde5b92d71b.tar.bz2
mana-254c3db4a39aa35274fd7cd4ec2cccde5b92d71b.tar.xz
mana-254c3db4a39aa35274fd7cd4ec2cccde5b92d71b.zip
Added VSync and windowed fullscreen options
The configuration and setup UI were adjusted to the new options. This also fixes issues in applying new video settings. Default resolution was changed from 800x600 to 1280x720. VSync is enabled by default while FPS limit was disabled. Display aspect ratio for the resolution options. I had to work around some macOS issues: * Don't change window size when it appears to be "maximized", since it just changes the rendering area while leaving the window maximized. * Unset fullscreen display mode temporarily to allow changing resolutions, otherwise the rendering area no longer matches the screen and mouse input is also off. * Removed SDL_WINDOW_ALLOW_HIGHDPI for now because it causes issues on macOS, since we're not actually handling the scaling factor. A Video class and an SDLGraphics subclass were split off from Graphics. This setup has Less duplication and leaves the OpenGLGraphics and SDLGraphics better separated. Fixes #57 Fixes #58
Diffstat (limited to 'src/openglgraphics.cpp')
-rw-r--r--src/openglgraphics.cpp133
1 files changed, 36 insertions, 97 deletions
diff --git a/src/openglgraphics.cpp b/src/openglgraphics.cpp
index 008f1daa..2f83eea7 100644
--- a/src/openglgraphics.cpp
+++ b/src/openglgraphics.cpp
@@ -42,98 +42,21 @@ const unsigned int vertexBufSize = 500;
GLuint OpenGLGraphics::mLastImage = 0;
-OpenGLGraphics::OpenGLGraphics():
- mAlpha(false), mTexture(false), mColorAlpha(false),
- mSync(false),
- mReduceInputLag(true)
+OpenGLGraphics::OpenGLGraphics(SDL_Window *window, SDL_GLContext glContext)
+ : mWindow(window)
+ , mContext(glContext)
{
+ Image::setLoadAsOpenGL(true);
+
mFloatTexArray = new GLfloat[vertexBufSize * 4];
mIntTexArray = new GLint[vertexBufSize * 4];
mIntVertArray = new GLint[vertexBufSize * 4];
-}
-
-OpenGLGraphics::~OpenGLGraphics()
-{
- delete[] mFloatTexArray;
- delete[] mIntTexArray;
- delete[] mIntVertArray;
-}
-
-void OpenGLGraphics::setSync(bool sync)
-{
- if (mSync == sync)
- return;
-
- mSync = sync;
-
- if (mContext)
- SDL_GL_SetSwapInterval(sync ? 1 : 0);
-}
-
-void OpenGLGraphics::setReduceInputLag(bool reduceInputLag)
-{
- mReduceInputLag = reduceInputLag;
-}
-
-bool OpenGLGraphics::setVideoMode(int w, int h, bool fs)
-{
- logger->log("Setting video mode %dx%d %s",
- w, h, fs ? "fullscreen" : "windowed");
-
- // TODO_SDL2: Support SDL_WINDOW_ALLOW_HIGHDPI, but check handling of clip area
-
- int windowFlags = SDL_WINDOW_OPENGL;
-
- if (fs)
- {
- windowFlags |= SDL_WINDOW_FULLSCREEN;
- }
- else
- {
- // Resizing currently not supported on Windows, where it would require
- // reuploading all textures.
-#if !defined(_WIN32)
- windowFlags |= SDL_WINDOW_RESIZABLE;
-#endif
- }
-
- SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
-
- SDL_Window *window = SDL_CreateWindow("Mana",
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- w, h, windowFlags);
- if (!window) {
- logger->log("Failed to create window: %s", SDL_GetError());
- return false;
- }
- SDL_SetWindowMinimumSize(window, 640, 480);
-
- SDL_GLContext glContext = SDL_GL_CreateContext(window);
- if (!glContext) {
- logger->log("Failed to create OpenGL context: %s", SDL_GetError());
- return false;
- }
-
- mTarget = window;
- mContext = glContext;
- mWidth = w;
- mHeight = h;
- mFullscreen = fs;
-
- if (mSync)
- {
- SDL_GL_SetSwapInterval(1);
- }
+ SDL_GL_GetDrawableSize(mWindow, &mWidth, &mHeight);
// Setup OpenGL
- glViewport(0, 0, w, h);
+ glViewport(0, 0, mWidth, mHeight);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);
- int gotDoubleBuffer;
- SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &gotDoubleBuffer);
- logger->log("Using OpenGL %s double buffering.",
- (gotDoubleBuffer ? "with" : "without"));
char const *glExtensions = (char const *)glGetString(GL_EXTENSIONS);
GLint texSize;
@@ -154,18 +77,34 @@ bool OpenGLGraphics::setVideoMode(int w, int h, bool fs)
Image::mTextureSize = texSize;
logger->log("OpenGL texture size: %d pixels%s", Image::mTextureSize,
rectTex ? " (rectangle textures)" : "");
+}
- return true;
+OpenGLGraphics::~OpenGLGraphics()
+{
+ SDL_GL_DeleteContext(mContext);
+
+ delete[] mFloatTexArray;
+ delete[] mIntTexArray;
+ delete[] mIntVertArray;
+}
+
+void OpenGLGraphics::setVSync(bool sync)
+{
+ SDL_GL_SetSwapInterval(sync ? 1 : 0);
+}
+
+void OpenGLGraphics::setReduceInputLag(bool reduceInputLag)
+{
+ mReduceInputLag = reduceInputLag;
}
-void OpenGLGraphics::videoResized(int w, int h)
+void OpenGLGraphics::videoResized(int width, int height)
{
_endDraw();
- mWidth = w;
- mHeight = h;
+ SDL_GL_GetDrawableSize(mWindow, &mWidth, &mHeight);
- glViewport(0, 0, w, h);
+ glViewport(0, 0, mWidth, mHeight);
_beginDraw();
}
@@ -174,7 +113,7 @@ static inline void drawQuad(Image *image,
int srcX, int srcY, int dstX, int dstY,
int width, int height)
{
- if (image->getTextureType() == GL_TEXTURE_2D)
+ if (Image::getTextureType() == GL_TEXTURE_2D)
{
// Find OpenGL normalized texture coordinates.
const float texX1 = static_cast<float>(srcX) /
@@ -236,7 +175,7 @@ static inline void drawRescaledQuad(Image *image,
int width, int height,
int desiredWidth, int desiredHeight)
{
- if (image->getTextureType() == GL_TEXTURE_2D)
+ if (Image::getTextureType() == GL_TEXTURE_2D)
{
// Find OpenGL normalized texture coordinates.
const float texX1 = static_cast<float>(srcX) /
@@ -391,7 +330,7 @@ void OpenGLGraphics::drawImagePattern(Image *image, int x, int y, int w, int h)
unsigned int vp = 0;
const unsigned int vLimit = vertexBufSize * 4;
// Draw a set of textured rectangles
- if (image->getTextureType() == GL_TEXTURE_2D)
+ if (Image::getTextureType() == GL_TEXTURE_2D)
{
float texX1 = static_cast<float>(srcX) / tw;
float texY1 = static_cast<float>(srcY) / th;
@@ -526,7 +465,7 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image,
const unsigned int vLimit = vertexBufSize * 4;
// Draw a set of textured rectangles
- if (image->getTextureType() == GL_TEXTURE_2D)
+ if (Image::getTextureType() == GL_TEXTURE_2D)
{
const auto tw = static_cast<float>(image->getTextureWidth());
const auto th = static_cast<float>(image->getTextureHeight());
@@ -641,7 +580,7 @@ void OpenGLGraphics::drawRescaledImagePattern(Image *image,
void OpenGLGraphics::updateScreen()
{
- SDL_GL_SwapWindow(mTarget);
+ SDL_GL_SwapWindow(mWindow);
/*
* glFinish flushes all OpenGL commands and makes sure they have been
@@ -685,10 +624,10 @@ void OpenGLGraphics::_endDraw()
popClipArea();
}
-SDL_Surface* OpenGLGraphics::getScreenshot()
+SDL_Surface *OpenGLGraphics::getScreenshot()
{
int w, h;
- SDL_GL_GetDrawableSize(mTarget, &w, &h);
+ SDL_GL_GetDrawableSize(mWindow, &w, &h);
GLint pack = 1;
SDL_Surface *screenshot = SDL_CreateRGBSurface(
@@ -772,7 +711,7 @@ void OpenGLGraphics::popClipArea()
void OpenGLGraphics::setColor(const gcn::Color& color)
{
- mColor = color;
+ Graphics::setColor(color);
glColor4ub(color.r, color.g, color.b, color.a);
mColorAlpha = (color.a != 255);