I am working on creating a game engine using GLFW. To begin, I set up a window class in C++ to test how making a window works. For some reason, when I call the glfwCreateWindow function, it returns NULL instead of GLFWwindow*. Here is my code (I have a header file that defines the class and then the c++ file that defines the class):
Here is the window.h header file:
#pragma once
#include <iostream>
#include <GLFW/glfw3.h>
namespace harmony { namespace graphics {
class Window
{
private:
const char* m_Title;
int m_Width, m_Height;
GLFWwindow* m_Window;
bool m_Closed;
public:
Window(const char* name, int width, int height);
~Window();
bool closed() const;
void update() const;
private:
bool init();
};
} }
And here is the c++ window.cpp file:
#include "window.h"
namespace harmony { namespace graphics {
Window::Window(const char* title, int width, int height)
{
m_Title = title;
m_Width = width;
m_Height = height;
if (!init())
glfwTerminate();
}
Window::~Window()
{
glfwTerminate();
}
bool Window::init()
{
if (!glfwInit)
{
std::cout << "Failed to initialized" << std::endl;
return false;
}
m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL);
if (!m_Window)
{
std::cout << "Failed to create window!" << std::endl;
return false;
}
glfwMakeContextCurrent(m_Window);
return true;
}
bool Window::closed() const
{
return glfwWindowShouldClose(m_Window);
}
void Window::update() const
{
glfwPollEvents();
glfwSwapBuffers(m_Window);
}
} }
And lastly here's my main.cpp:
#include "window.h"
int main()
{
using namespace harmony;
using namespace graphics;
Window window("Yay it works!", 800, 600);
while (!window.closed()) {
window.update();
}
return 0;
}
I looked it up and there are only a couple cases (in the source code of glfwCreateWindow) where it returns NULL, but I haven't found a good way to troubleshoot which one.