I’m trying to implement a simple opengl 4 window in a OOP structure in C++. Here is the main function:
#include "Engine.h"
int main()
{
Engine Engine;
if (Engine.Init() == false)
{
std::cout << "Failed to Initialize.";
exit(1);
}
else
Engine.initEventLoop(Engine.engineWindow);
glfwTerminate();
return 0;
}
I have an Engine class which contains Init() and initEventLoop() to initialize GLEW, GLFW and create a context window.
bool Engine::Init()
{
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW3.
";
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
GLEngine.engineWindow = glfwCreateWindow(800, 600, "OpenGL", NULL, NULL);
if (GLEngine.engineWindow == NULL)
{
std::cout << "Failed to create context window";
return false;
}
glfwMakeContextCurrent(GLEngine.engineWindow);
glewExperimental = GL_TRUE;
const GLenum err = glewInit();
if (err != GLEW_OK) {
fprintf(stderr, "Failed to initialize GLEW
");
fprintf(stderr, "Error: %s
", glewGetErrorString(err));
return -1;
}
else
{
std::cout << "Engine Initialized.
";
return true;
}
}
void Engine::initEventLoop(GLFWwindow *mWindow)
{
while (!glfwWindowShouldClose(mWindow))
{
//some code...
glfwSwapBuffers(mWindow);
glfwPollEvents();
//more code here;
if (glfwGetKey(mWindow, GLFW_KEY_ESCAPE) == GL_TRUE)
glfwSetWindowShouldClose(mWindow, GL_TRUE);
}
}
This works fine if everything is in one source file but when implementing OOP it crashes. As soon as I run the program the glfw window pops up and crashes with a stopped working message. I tried debugging the program it crashes on reaching the while loop in initEventLoop() function call. Error code:
Unhandled exception at 0x00BA7EC2 in Engine.exe: 0xC0000005: Access violation reading location 0x00000008.
What is wrong in the code?