Get rid of the GLFWHandler proxy

Windows can have user pointers...
master
Dorian Wouters 2019-05-12 20:46:12 +02:00
parent b59ad7dfcc
commit 6030092f35
No known key found for this signature in database
GPG Key ID: 6E9DA8063322434B
3 changed files with 37 additions and 114 deletions

View File

@ -1,37 +0,0 @@
#include "GLFWHandler.hpp"
#include "GameWindow.hpp"
namespace diggler {
void GLFWHandler::mouseButtonImpl(GLFWwindow *window, int key, int action, int mods) {
(void)window;
win->cbMouseButton(key, action, mods);
}
void GLFWHandler::cursorPosImpl(GLFWwindow *window, double x, double y) {
(void)window;
win->cbCursorPos(x, y);
}
void GLFWHandler::mouseScrollImpl(GLFWwindow *window, double x, double y) {
(void)window;
win->cbMouseScroll(x, y);
}
void GLFWHandler::keyImpl(GLFWwindow *window, int key, int scancode, int action, int mods) {
(void)window;
win->cbKey(key, scancode, action, mods);
}
void GLFWHandler::unicharImpl(GLFWwindow *window, unsigned int unichar) {
(void)window;
win->cbChar(static_cast<char32>(unichar));
}
void GLFWHandler::resizeImpl(GLFWwindow *window, int w, int h) {
(void)window;
win->cbResize(w, h);
}
}

View File

@ -1,68 +0,0 @@
#ifndef GLFW_HANDLER
#define GLFW_HANDLER
struct GLFWwindow;
namespace diggler {
class GameWindow;
/// C++ wrapper for C-style GLFWwindow callbacks
/// Currently handles one window, but could be modified to handle multiple
/// (using a GLFWwindow* <-> Window instance map)
class GLFWHandler {
private:
GameWindow *win;
void mouseButtonImpl(GLFWwindow *window, int key, int action, int mods);
void cursorPosImpl(GLFWwindow *window, double x, double y);
void mouseScrollImpl(GLFWwindow *window, double x, double y);
void keyImpl(GLFWwindow *window, int key, int scancode, int action, int mods);
void unicharImpl(GLFWwindow *window, unsigned int unichar);
void resizeImpl(GLFWwindow *window, int w, int h);
public:
///
/// @returns GLFWHandler singleton
///
static GLFWHandler& getInstance() {
static GLFWHandler instance;
return instance;
}
static void setWindow(GameWindow *win, GLFWwindow *window) {
getInstance().win = win;
}
static void mouseButton(GLFWwindow *window, int key, int action, int mods) {
getInstance().mouseButtonImpl(window, key, action, mods);
}
static void cursorPos(GLFWwindow *window, double x, double y) {
getInstance().cursorPosImpl(window, x, y);
}
static void mouseScroll(GLFWwindow *window, double x, double y) {
getInstance().mouseScrollImpl(window, x, y);
}
static void key(GLFWwindow *window, int key, int scancode, int action, int mods) {
getInstance().keyImpl(window, key, scancode, action, mods);
}
static void unichar(GLFWwindow *window, unsigned int unichar) {
getInstance().unicharImpl(window, unichar);
}
static void resize(GLFWwindow *window, int w, int h) {
getInstance().resizeImpl(window, w, h);
}
private:
GLFWHandler() {}
GLFWHandler(const GLFWHandler&); // prevent copies
void operator=(const GLFWHandler&);
};
}
#endif

View File

@ -15,7 +15,6 @@
#include "Game.hpp"
#include "GlobalProperties.hpp"
#include "GLFWHandler.hpp"
#include "states/GameState.hpp"
#include "states/MessageState.hpp"
#include "Audio.hpp"
@ -39,6 +38,36 @@ static void glfwErrorCallback(int error, const char *description) {
Log(Error, TAG) << "GLFW Error " << error << ": " << description;
}
void handleMouseButton(GLFWwindow *window, int key, int action, int mods) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbMouseButton(key, action, mods);
}
void handleCursorPos(GLFWwindow *window, double x, double y) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbCursorPos(x, y);
}
void handleMouseScroll(GLFWwindow *window, double x, double y) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbMouseScroll(x, y);
}
void handleKey(GLFWwindow *window, int key, int scancode, int action, int mods) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbKey(key, scancode, action, mods);
}
void handleUnichar(GLFWwindow *window, unsigned int unichar) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbChar(static_cast<char32>(unichar));
}
void handleResize(GLFWwindow *window, int w, int h) {
auto win = reinterpret_cast<GameWindow*>(glfwGetWindowUserPointer(window));
win->cbResize(w, h);
}
GameWindow::GameWindow(Game *G) : G(G) {
Util::MemoryTracker::ScopedCategory sc("GLFW");
if (InstanceCount++ == 0) {
@ -53,8 +82,6 @@ GameWindow::GameWindow(Game *G) : G(G) {
'.' << GLFW_VERSION_REVISION << ", using " << glfwGetVersionString();
}
GLFWHandler::getInstance().setWindow(this, m_window);
m_w = 640; m_h = 480;
if (GlobalProperties::GfxOverrides != nullptr) {
@ -98,12 +125,13 @@ GameWindow::GameWindow(Game *G) : G(G) {
#ifdef DEBUG
render::gl::Debug::enable();
#endif
glfwSetFramebufferSizeCallback(m_window, GLFWHandler::resize);
glfwSetCursorPosCallback(m_window, GLFWHandler::cursorPos);
glfwSetKeyCallback(m_window, GLFWHandler::key);
glfwSetMouseButtonCallback(m_window, GLFWHandler::mouseButton);
glfwSetScrollCallback(m_window, GLFWHandler::mouseScroll);
glfwSetCharCallback(m_window, GLFWHandler::unichar);
glfwSetWindowUserPointer(m_window, this);
glfwSetFramebufferSizeCallback(m_window, handleResize);
glfwSetCursorPosCallback(m_window, handleCursorPos);
glfwSetKeyCallback(m_window, handleKey);
glfwSetMouseButtonCallback(m_window, handleMouseButton);
glfwSetScrollCallback(m_window, handleMouseScroll);
glfwSetCharCallback(m_window, handleUnichar);
glfwSwapInterval(1);
/*GLint bits;
glGetIntegerv(GL_STENCIL_BITS, &bits);