I'm doing a little project of mine where I'm basically creating my own c++ game engine / framework for creating graphics and or simple games. I use OpenGL with GLFW. My goal is to have something similar to various graphics frameworks, like raylib or openFrameworks (but stripped down of course)
Actually so far everything works fine, but I can't figure out how to properly separate the input from the window class, as having the window handle input seems rather clunky to me and just clutters the window class.
This is a quick oversimplified recreation of my window class. (I didn't include the enum class with the keycodes.)
#pragma once
#include "../extern/GLFW/glfw3.h"
#include <string>
class Window {
private:
GLFWwindow* mWindow;
int mWidth;
int mHeight;
std::string mTitle;
public:
Window();
~Window();
void createWindow(std::string title, int width, int height);
void mainLoop();
GLFWwindow* getWindow() const { return mWindow; }
// Input
private:
bool Window::getKeyStatus(KEY key) {
static void keyCallback(GLFWwindow* mWindow, int key, int scancode, int action, int mods);
bool isKeyDown(KEY key);
};
And this is the implementation plus
#include "Window.h"
#include <iostream>
Window::Window() {}
Window::~Window() {}
void Window::createWindow(std::string title, int width, int height) {
if (!glfwInit());
mWindow = glfwCreateWindow(width, height, title.c_str(), nullptr, nullptr);
if (!getWindow()) {
glfwTerminate();
}
glfwSetWindowUserPointer(getWindow(), this);
glfwMakeContextCurrent(getWindow());
glfwSetKeyCallback(mWindow, keyCallback);
}
void Window::mainLoop() {
while (!glfwWindowShouldClose(getWindow())) {
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
/* Swap front and back buffers */
glfwSwapBuffers(getWindow());
/* Poll for and process events */
glfwPollEvents();
if (isKeyDown(KEY::A)) {
std::cout << "A down" << std::endl;
}
if (isKeyDown(KEY::B)) {
std::cout << "B down" << std::endl;
}
}
glfwTerminate();
}
void Window::keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
Window* win = (Window*)glfwGetWindowUserPointer(window);
if (key == (int)KEY::ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GL_TRUE);
} else {
win->currentKeyState[key] = action;
}
}
bool Window::getKeyStatus(KEY key) {
return glfwGetKey(mWindow, (int)key);
}
bool Window::isKeyDown(KEY key) {
bool down = false;
if (getKeyStatus(key) == 1) {
down = true;
}
return down;
}
How do I proceed from that? My main problem is that I don't seem to be able to connect my window and the input class. Should I use inheritance or friend classes. Should I have glfw's callbacks in the window class (which I assume) or should I move them to the input class? How can I connect those two classes so that I don't have to always use window pointers, like "isKeyDown(GLFWwindow* window, Key keycode)", but instead only "isKeyDown(Key keycode)". If it's not too much too ask can somebody write a simplistic input class?
Thanks in advance