PDA

View Full Version : Opengl window crashes on startup!!?



Mercury01
06-04-2015, 12:44 AM
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.\n";
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\n");
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
return -1;
}
else
{
std::cout << "Engine Initialized.\n\n";
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?

advorak
06-09-2015, 04:24 PM
I haven't done a lot of C++ code, but if the second batch of code you included is all that is in the Engine.h file, you have not really created a true class. There is no class definition, no constructor declared. It looks like you simply created two functions. If that's all you want to do, cool. Just remove the class nomenclature from in front of the function names and call them like regular C.