Ad

Opengl Window Initialization Issue

- 1 answer

I'm new to glfw and I'm trying to encapsulate it in a class to make my code cleaner. So in my class, I've made the basic window creation but when I execute the program, it always enter in the if statement where I check if the initialization went right or not and I don't know why. This is what I have

#include "include/Window.h"
#include <iostream>

Window::Window(int x, int y) {
    _window = glfwCreateWindow(x, y, "Heightmap Visualizer", NULL, NULL);
}

Window::~Window() {
    glfwTerminate();
}

int Window::createWindow() {
    if (!_window) {
        std::cerr << "went here" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(_window);
    run();
    glfwTerminate();
    return 0;
}

void Window::run() {
    while (!glfwWindowShouldClose(_window)) {
        glClear(GL_COLOR_BUFFER_BIT);
        glfwSwapBuffers(_window);
        glfwPollEvents();
    }
}

This is the header file

#include <GLFW/glfw3.h>

class Window {
public:
    Window(int x, int y);
    ~Window();
    int createWindow();
    void run();

private:
    GLFWwindow* _window;
};
Ad

Answer

Make sure to glfwInit().

Also good luck in your jorney learning GL and C++!

Ad
source: stackoverflow.com
Ad