Skip to content

Commit

Permalink
fixed a window rescaling bug
Browse files Browse the repository at this point in the history
  • Loading branch information
KaleiAlma committed Jul 22, 2024
1 parent dba9749 commit 0503b40
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
13 changes: 6 additions & 7 deletions LuaSTG/Core/Graphics/SwapChain_OpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -368,33 +368,32 @@ namespace Core::Graphics
bool SwapChain_OpenGL::present()
{
Vector2U wsize = m_window->getSize();
Core::Vector2F scale_dim = Core::Vector2F((float) wsize.x / m_canvas_size.x, (float) wsize.y / m_canvas_size.y);
Vector2F scale_dim = Vector2F((float) wsize.x / m_canvas_size.x, (float) wsize.y / m_canvas_size.y);
float scale = std::min(scale_dim.x, scale_dim.y);
Vector2F d;

if (scale_dim.x > scale_dim.y)
{
d.x = (wsize.x - scale * m_canvas_size.x) / 2;
d.x = ((float)wsize.x - m_canvas_size.x * scale) * 0.5;
d.y = 0;
}
else
{
d.x = 0;
d.y = (wsize.y - scale * m_canvas_size.y) / 2;
d.y = ((float)wsize.y - m_canvas_size.y * scale) * 0.5;
}

glBindFramebuffer(GL_READ_FRAMEBUFFER, rdr_fbo);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glViewport(0, 0, wsize.x, wsize.y);
glScissor(0, 0, wsize.x, wsize.y);
glClearColor(0.f, 0.f, 0.f, 0.f);
glClearDepth(1.f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, wsize.x, wsize.y);
glScissor(0, 0, wsize.x, wsize.y);

glBlitFramebuffer(
0, 0, m_canvas_size.x, m_canvas_size.y,
(wsize.x - scale * (m_canvas_size.x + d.x)) / 2, (wsize.y + scale * (m_canvas_size.y + d.y)) / 2,
(wsize.x + scale * (m_canvas_size.x + d.x)) / 2, (wsize.y - scale * (m_canvas_size.y + d.y)) / 2,
d.x, scale * m_canvas_size.y + d.y, scale * m_canvas_size.x + d.x, d.y,
GL_COLOR_BUFFER_BIT, GL_LINEAR
);

Expand Down
5 changes: 4 additions & 1 deletion LuaSTG/Core/Graphics/Window_SDL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "Core/Type.hpp"
#include "Core/i18n.hpp"
#include "SDL_events.h"
#include "SDL_mouse.h"
#include "SDL_video.h"
#include "glad/gl.h"
#include "SDL.h"
Expand Down Expand Up @@ -154,6 +153,9 @@ namespace Core::Graphics
int version = gladLoadGL((GLADloadfunc) SDL_GL_GetProcAddress);
spdlog::info("[core] OpenGL {}.{}", GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version));
spdlog::info("[core] {}", (const char*)glGetString(GL_VERSION));
GLint s;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &s);
spdlog::info("[core] max texture size: {}", s);

#ifndef __APPLE__
#ifndef NDEBUG
Expand Down Expand Up @@ -454,6 +456,7 @@ namespace Core::Graphics

Vector2U Window_SDL::getSize()
{
SDL_GetWindowSize(sdl_window, (int*)&sdl_window_width, (int*)&sdl_window_height);
return { sdl_window_width, sdl_window_height };
}
bool Window_SDL::setSize(Vector2U v)
Expand Down
1 change: 0 additions & 1 deletion LuaSTG/Core/Graphics/Window_SDL.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// #include "Platform/Monitor.hpp"
// #include "Platform/WindowSizeMoveController.hpp"
// #include "Platform/RuntimeLoader/DesktopWindowManager.hpp"
#include <SDL_mouse.h>
#include <SDL_rect.h>
#include <SDL_surface.h>
#include <SDL_video.h>
Expand Down

0 comments on commit 0503b40

Please sign in to comment.