Hey!
So i am very new to both C++ and glfw in general. I am attempting to make a game engine and i have been following a youtube series but i ran into a problem regarding
the function glfwGetWindowUserPointer.
The Code is the following:
/In a file called Window.h
/
class Window
{
public:
static Window* INSTANCE;
private:
const char *m_Title;
int m_Width, m_Height;
GLFWwindow *m_Window;
bool m_Closed;
static bool m_Keys[1024];
static bool m_MouseButtons[32];
static double mx, my;
public:
Window(const char* name, int widht, int height);
~Window();
void clear() const;
bool closed() const;
void update();
inline int getwidht() const { return m_Width; }
inline int getheight() const { return m_Height; }
static bool isKeyPressed(unsigned int keycode);
private:
bool init();
friend static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
};
}
//In Window.cpp
namespace graphics {
bool Window::m_Keys[1024];
bool Window::m_MouseButtons[32];
double Window::mx, my;
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
Window::Window(const char* title, int widht, int height)
{
m_Title = title;
m_Width = widht;
m_Height = height;
if (!init())
glfwTerminate();
INSTANCE = this;
for (int i = 0; i < 1024; i++)
{
m_Keys[i] = false;
}
for (int i = 0; i < 32; i++)
{
m_MouseButtons[i] = false;
}
}
Window::~Window()
{
glfwTerminate();
}
bool Window::init()
{
if (!glfwInit())
{
std::cout << "Failed to initialize GLFW" << std::endl;
return false;
}
m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!m_Window)
{
std::cout << "failed to create GLFW window" << std::endl;
return false;
}
glfwMakeContextCurrent(m_Window);
glfwGetWindowUserPointer(m_Window);
glfwSetKeyCallback(m_Window, key_callback);
std::cout << "OpenGL" << glGetString(GL_VERSION) << std::endl;
if(glewInit() != GLEW_OK)
{
std::cout << "could not initialize GLEW" << std::endl;
return false;
}
return true;
}
bool Window::isKeyPressed(unsigned int keycode)
{
if (keycode >= 1024)
return false;
return m_Keys[keycode];
}
void Window::clear() const
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
bool Window::closed() const
{
return glfwWindowShouldClose(m_Window);
}
void Window::update()
{
glfwPollEvents();
glfwGetFramebufferSize(m_Window, &m_Width, &m_Height);
glViewport(0, 0, m_Width, m_Height);
glfwSwapBuffers(m_Window);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
Window* win = (Window*) glfwGetWindowUserPointer(window);
win->m_Keys[key] = action != GLFW_RELEASE;
}
}
now the problem is that i cannot type the following:
glfwSetWindowUserPointer(m_window, this);
writing “this”, makes so that there are to many arguments but without it i dont have a pointer.
Are there any easy fixes to this?
I am very new to this so this is probably something small but thank you in advance for the help.