Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions game-window/src/window_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include <math.h>

#include <codecvt>
#include <iomanip>
#include <iostream>
#include <locale>
#include <thread>
#include <utility>

#include "../../logger/include/log.h"
#include "GLFW/glfw3.h"
Expand Down Expand Up @@ -76,18 +77,16 @@ void GLFWGameWindow::setIcon(std::string const &iconPath)

void GLFWGameWindow::setRelativeScale()
{
int fx, fy;
glfwGetFramebufferSize(window, &fx, &fy);
float xs, ys;
glfwGetWindowContentScale(window, &xs, &ys);

int wx, wy;
glfwGetWindowSize(window, &wx, &wy);

relativeScale = (int)floor(((fx / wx) + (fy / wy)) / 2);
xScale = xs;
yScale = ys;
}

int GLFWGameWindow::getRelativeScale() const
std::pair<float, float> GLFWGameWindow::getRelativeScale() const
{
return relativeScale;
return std::make_pair(xScale, yScale);
}

void GLFWGameWindow::getWindowSize(int &width, int &height) const
Expand Down Expand Up @@ -178,7 +177,7 @@ void GLFWGameWindow::swapBuffers()
numFrames = 0;
lastTimePerformance = currentTime;
}

if (limitFpsMode == LimitFPSMode::Limited)
{
while (glfwGetTime() < (lastTimeTargetFPS + 1.0 / targetFPS))
Expand Down Expand Up @@ -218,17 +217,22 @@ void GLFWGameWindow::_glfwCursorPosCallback(GLFWwindow *window, double x,

if (glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED)
{
#ifdef __linux__
double dx = x - user->lastMouseX;
double dy = y - user->lastMouseY;
#else
double dx = (x - user->lastMouseX) * user->getRelativeScale();
double dy = (y - user->lastMouseY) * user->getRelativeScale();
#endif

user->onMouseRelativePosition(dx, dy);
user->lastMouseX = x;
user->lastMouseY = y;
}
else
{
x *= user->getRelativeScale();
y *= user->getRelativeScale();
x *= user->xScale;
y *= user->yScale;

user->onMousePosition(x, y);
}
Expand All @@ -241,8 +245,8 @@ void GLFWGameWindow::_glfwMouseButtonCallback(GLFWwindow *window, int button,
double x, y;
glfwGetCursorPos(window, &x, &y);

x *= user->getRelativeScale();
y *= user->getRelativeScale();
x *= user->xScale;
y *= user->yScale;

user->onMouseButton(x, y, button + 1,
action == GLFW_PRESS ? MouseButtonAction::PRESS
Expand Down Expand Up @@ -368,7 +372,9 @@ void GLFWGameWindow::_glfwKeyCallback(GLFWwindow *window, int key, int scancode,
void GLFWGameWindow::_glfwCharCallback(GLFWwindow *window, unsigned int ch)
{
GLFWGameWindow *user = (GLFWGameWindow *)glfwGetWindowUserPointer(window);

std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> cvt;

user->onKeyboardText(cvt.to_bytes(ch));
}

Expand All @@ -391,4 +397,4 @@ void GLFWGameWindow::_glfwWindowContentScaleCallback(GLFWwindow *window,
{
GLFWGameWindow *user = (GLFWGameWindow *)glfwGetWindowUserPointer(window);
user->setRelativeScale();
}
}
6 changes: 4 additions & 2 deletions game-window/src/window_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <GLFW/glfw3.h>
#include <game_window.h>

#include <utility>

class GLFWGameWindow : public GameWindow
{
private:
Expand All @@ -13,7 +15,7 @@ class GLFWGameWindow : public GameWindow
double lastMouseX = 0.0, lastMouseY = 0.0;
int windowedX = -1, windowedY = -1;
int windowedWidth = -1, windowedHeight = -1;
int relativeScale;
float xScale, yScale;
bool focused = true;

friend class GLFWJoystickManager;
Expand Down Expand Up @@ -41,7 +43,7 @@ class GLFWGameWindow : public GameWindow

void setIcon(std::string const &iconPath) override;

int getRelativeScale() const;
std::pair<float, float> getRelativeScale() const;

void setRelativeScale();

Expand Down