1

So I have been working on a game through C++ with GLFW and GLEW and everything was going fine until I implemented GLEW then I get an error that I have never encountered before and I don't have the foggiest on what to do, I have googled it and done some research but to no avail. I have noticed that in the stack frame section it says something to do with this line of code in my main.cpp

std::cout << glGetString(GL_VERSION) << std::endl;

Also it says something to do with memory. I'll leave the rest of my code down below and if there's any info you need just ask and I will try my best to provide it

So I just discovered if i take out

std::cout << glGetString(GL_VERSION) << std::endl;

then it works however the window isn't created.

Where do I go from here? Any idea?

   #include "src\graphics\window.h"
int main() {
    using namespace benji;
    using namespace graphics;

    Window window("Benji Engine", 1280, 720);
    glClearColor(0.2f, 0.3f, 0.8f, 1.0f);
    std::cout << glGetString(GL_VERSION) << std::endl;``

    while (!window.closed()) {
        std::cout << window.getWidth() << ", " << window.getHeight() << std::endl;
        window.clear();
        glBegin(GL_TRIANGLES);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(0.0f, 0.5f);
        glVertex2f(0.5f, -0.5f);
        glEnd();
        window.update();
    }

    return 0;
}

main.h

 #pragma once
    class main
    {
    public:
        main();
        ~main();
    };

window.cpp

#include "window.h"

namespace benji { namespace graphics {


void windowResize(GLFWwindow *window, int width, int height);

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 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);
    glfwSetWindowSizeCallback(m_Window, windowResize);

    if (glewInit != GLEW_OK) {
        std::cout << "GLEW FAILED!" << std::endl;
        return false;
    }
    return true;


}

void Window::clear() const {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}

void Window::update(){
    glfwPollEvents();
    glfwSwapBuffers(m_Window);


}

    bool Window::closed() const {
    return glfwWindowShouldClose(m_Window) == 1;
}

    void windowResize(GLFWwindow *window, int width, int height) {
        glViewport(0, 0, width, height);
    }
}}

window.h

 #pragma once
    #include <iostream>
    #include <GL\glew.h>
    #include <GLFW\glfw3.h>

namespace benji {
    namespace graphics {
        class Window {
        private:
            const char *m_Title;
            int m_Width, m_Height;
            GLFWwindow *m_Window;
            bool m_Closed;

        public:
            Window(const char *title, int width, int height);
            ~Window();
            bool closed() const;
            void update();
            void clear() const;

             int getWidth() const {
                return m_Width;

            }
             int getHeight() const { return m_Height; }
        private:
            bool init();

        };
}

}
  • 2
    What is the full error? And do you have a debug callstack or any other information? – ssell Feb 23 '17 at 20:09
  • The only other error I can see is Exception thrown: read access violation. _First was nullptr. – Ryan Midgley Feb 23 '17 at 20:23
  • If `glGetString` is indeed crashing, it is because your OpenGL has not been properly initialized. Are you getting any error outputs during your `Window::init`? This is also supported by the fact that when you remove the call to `glGetString` it does not crash, but no window is opened. See also "[Why Could glGetString(GL_VERSION) Be Causing a Seg Fault?](http://stackoverflow.com/questions/6288759/why-could-glgetstringgl-version-be-causing-a-seg-fault)" – ssell Feb 23 '17 at 20:35
  • I have been following a tutorial to initialize OpenGL as it's the first time that I have done it and well it seems to be right to me,,, so im not sure. – Ryan Midgley Feb 23 '17 at 21:04

1 Answers1

1

In Window.cpp:

if (glewInit != GLEW_OK) {
    std::cout << "GLEW FAILED!" << std::endl;
    return false;
}

glewInit() is a function, not a variable. You need to call it as a function. I'm surprised this even compiled.

All other OpenGL functions that come from after version 1.1 will throw errors to the effect of ACCESS_VIOLATION READING ADDRESS 0x00000000 or some similar error, because if glewInit() is not properly called, none of the function macros provided by GLEW will point to valid function pointers.

Xirema
  • 19,889
  • 4
  • 32
  • 68
  • `I'm surprised this even compiled`, so am I if this wasn't just an error in copying the code over to the question. But good catch, I managed to look right past it multiple times. – ssell Feb 23 '17 at 21:17
  • @ssell I stopped using GLEW a while ago (switched to glBinding) and can't remember if GLEW makes everything decay to function pointers, or just the `gl*` functions themselves. If it does it for everything, that would at least explain why the program compiles (but doesn't run correctly). – Xirema Feb 23 '17 at 21:19
  • So would you recommend not using GLEW? – Ryan Midgley Feb 23 '17 at 21:26
  • @RyanMidgley I'm not going to make recommendations on that front. I personally like the interface for glBinding more than I like the interface for GLEW, but not everyone is going to feel the same way. – Xirema Feb 23 '17 at 21:32
  • Just gonna let you know how I fixed it, I forgot the parenthesis after glewInit. Something really insignificant but that's the issue haha – Ryan Midgley Feb 24 '17 at 06:39