Question

I am following one of the tutorials from the internet on glfw to make a simple 2D game.

My program is stuck at a point where I have to create a vertex array to store my quad vertices and draw them on the screen. But my program crashes at line where I generateBuffers and throws the below exception

Unhandled exception at 0x00000000 in glfwtest.exe: 0xC0000005: Access violation.

This is my GameWindow.h file

#include <iostream>
#include <GL\glew.h>
#include <GL\glfw.h>

class GameWindow{

private:
    bool _running;
    GLfloat _height;
    GLfloat _width;
    GLuint _vertexBufferID;

public:
    void setRunning(bool nRunning);
    bool getRunning();
    GameWindow(bool running);
    void Render();
    void Update();
};

Below is my code for the GameWindow.cpp

#include "GameWindow.h"

typedef struct{
    GLfloat positionCoordinates[3];
}vertexData;

#define Square_Size 100

vertexData vertices[] = {
    {0.0f,0.0f,0.0f},
    {Square_Size,0.0f,0.0f},
    {Square_Size,Square_Size,0.0f},
    {0.0f,Square_Size,0.0f}
 };

void GameWindow::setRunning(bool nRunning){
    _running = nRunning;
}

bool GameWindow::getRunning(){
    return _running;
}  

GameWindow::GameWindow(bool running){
    _running = running;
    _height = 800;
    _width = 800*16/9;
    _vertexBufferID = 0;
    glClearColor(1.0,1.0,1.0,1.0);  //Set the initial screen to White
    glViewport(0.0f,0.0f,_width, _height);
    glMatrixMode(GL_PROJECTION);   
    gluOrtho2D(0,_width,0,_height);
    glMatrixMode(GL_MODELVIEW);

    //generate and make your vertex array ready for usage
    glGenBuffers(1,&_vertexBufferID);
    glBindBuffer(GL_ARRAY_BUFFER,_vertexBufferID);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW);

    //tells opengl to enable vertex array. Normally for other states we use glEnable
    glEnableClientState(GL_VERTEX_ARRAY);
    glVertexPointer(3,GL_FLOAT,sizeof(vertexData),(GLvoid*)offsetof(vertexData,positionCoordinates));
}

void GameWindow::Render(){
     glClear(GL_COLOR_BUFFER_BIT);
     glColor3f(1.0f,0.0f,0.0f);
     glDrawArrays(GL_QUADS,0,4);
     glfwSwapBuffers();
}

void GameWindow::Update(){

}

And finally this is my Main

#include "GameWindow.h"

GameWindow *gameWindow;

int main(int argc,char** argv){

    glfwInit();
    glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE,GL_TRUE);
    glfwOpenWindow(640,480,8,8,8,8,0,0,GLFW_WINDOW);
    gameWindow = new GameWindow(true);

    while(gameWindow->getRunning()){
        gameWindow->Render();
        gameWindow->Update();
        gameWindow->setRunning(glfwGetWindowParam(GLFW_OPENED));
    }

    delete gameWindow;
    glfwTerminate();
}

Also, my program could not recognize glGenBuffers(..) saying it was undefined and I had to add the glew library to solve the issue though in the tutorials the glew library was never added. I think the problem is something to do with glew? but I am not able to fix it! Hope someone with experience in glfw can provide some help

Was it helpful?

Solution

call glewInit () after glwOpenWindow.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top