Skip to content

Commit

Permalink
fix(ui): Make RenderBuffer respect DPI scaling on MacOS (endless-sky#…
Browse files Browse the repository at this point in the history
  • Loading branch information
implicitfield authored Sep 8, 2024
1 parent 7697671 commit 86f8193
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
17 changes: 16 additions & 1 deletion source/GameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace {
SDL_GLContext context = nullptr;
int width = 0;
int height = 0;
int drawWidth = 0;
int drawHeight = 0;
bool supportsAdaptiveVSync = false;

// Logs SDL errors and returns true if found
Expand Down Expand Up @@ -330,7 +332,6 @@ void GameWindow::AdjustViewport()

// Find out the drawable dimensions. If this is a high- DPI display, this
// may be larger than the window.
int drawWidth, drawHeight;
SDL_GL_GetDrawableSize(mainWindow, &drawWidth, &drawHeight);
Screen::SetHighDPI(drawWidth > windowWidth || drawHeight > windowHeight);

Expand Down Expand Up @@ -398,6 +399,20 @@ int GameWindow::Height()



int GameWindow::DrawWidth()
{
return drawWidth;
}



int GameWindow::DrawHeight()
{
return drawHeight;
}



bool GameWindow::IsMaximized()
{
return (SDL_GetWindowFlags(mainWindow) & SDL_WINDOW_MAXIMIZED);
Expand Down
4 changes: 4 additions & 0 deletions source/GameWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class GameWindow {
static int Width();
static int Height();

// Last known drawable width & height.
static int DrawWidth();
static int DrawHeight();

static bool IsMaximized();
static bool IsFullscreen();
static void ToggleFullscreen();
Expand Down
7 changes: 5 additions & 2 deletions source/RenderBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ this program. If not, see <https://www.gnu.org/licenses/>.

#include "RenderBuffer.h"

#include "GameWindow.h"
#include "Logger.h"
#include "Screen.h"
#include "Shader.h"
Expand Down Expand Up @@ -165,8 +166,10 @@ RenderBuffer::RenderBuffer(const Point &dimensions)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

multiplier = Point(GameWindow::DrawWidth() / Screen::RawWidth(), GameWindow::DrawHeight() / Screen::RawHeight());

// Attach a blank image to the texture.
const Point scaledSize = size * Screen::Zoom() / 100.0;
const Point scaledSize = size * multiplier * Screen::Zoom() / 100.0;
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, scaledSize.X(), scaledSize.Y(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);

// Attach the texture to the frame buffer.
Expand Down Expand Up @@ -209,7 +212,7 @@ RenderBuffer::RenderTargetGuard RenderBuffer::SetTarget()
glGetIntegerv(GL_VIEWPORT, lastViewport);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);

const Point scaledSize = size * Screen::Zoom() / 100.0;
const Point scaledSize = size * multiplier * Screen::Zoom() / 100.0;
glViewport(0, 0, scaledSize.X(), scaledSize.Y());

static const float CLEAR[] = {0, 0, 0, 0};
Expand Down
2 changes: 2 additions & 0 deletions source/RenderBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@ class RenderBuffer
int lastViewport[4] = {};

float fadePadding[4] = {};

Point multiplier;
};

0 comments on commit 86f8193

Please sign in to comment.