由于某种原因,我无法创建GLFW上下文

时间:2019-09-01 19:06:33

标签: c++ glfw glew

我目前正在使用GLFW和GLEW(以前很高兴)编写3D引擎。但是,当调用glfwMakeContextCurrent时,它将失败。这是我第一次遇到此错误,因为很高兴不需要调用任何init函数。

我搜索了可能的解决方案,但似乎没有一个适合我。但是,当我尝试使用this教程中的代码时,确实很接近我的代码-真的很接近我的代码教程,一切都按预期工作。

所以,我的问题是:       如何以某种方式解决GLFW问题?

Main.cpp:

#include <iostream>


#include "Engine3.h"


void UserCreate() {}
void UserUpdate(float ElapsedTime) {}

static void UserCursorEvent(GLFWwindow* window, double xpos, double ypos) { }

static void UserMouseButtonEvent(GLFWwindow* window, int button, int action, int mods) { }

static void UserKeyEvent(GLFWwindow* window, int key, int scancode, int action, int mods) { }

int main(void) {



    ENGINE e;

    e.AppTitle = "Game";

    if (e.Init(800, 600, ENGINE3_WINDOWED))
    {
        e.UserCreationCallback = &UserCreate;
        e.UserUpdateCallback = &UserUpdate;

        e.SetKeyCallback(&UserKeyEvent);
        e.SetCursorCallback(&UserCursorEvent);
        e.SetMouseCallback(&UserMouseButtonEvent);

        e.Begin();
    }
    system("pause");

    return 0;
}




Engine3.h:

#pragma once

#include <iostream>


#define GLEW_STATIC
#include<GL/glew.h>
#include <GLFW/glfw3.h>


#define ENGINE3_WINDOWED 0
#define ENGINE3_FULLSCREEN 1

#define Log(msg) std::cout << msg << std::endl;
#define _IN_
#define _OUT_

class ENGINE {


public:

    //Creates window of size [x,y] on primary monitor
    bool Init(_IN_ int Width, 
              _IN_ int Height, 
              _IN_ int Mode);  

    //Inits OpenGL and starts the engine
    void Begin();

    void (*UserCreationCallback)(),
         (*UserUpdateCallback)(_OUT_ float ElapsedTime),
         (*UserDestructionCallback)();

    //Sets callback for keyboard events
    void SetKeyCallback(_IN_ GLFWkeyfun KeyFun);

    //Sets callback for cursor position update 
    void SetCursorCallback(_IN_ GLFWcursorposfun CursorFun);

    //Sets callback for mouse click events
    void SetMouseCallback(_IN_ GLFWmousebuttonfun MouseFun);


    const char* AppTitle = nullptr;
    GLFWwindow* Window = nullptr;

private:
    void MainLoop();     
};

Engine3.cpp:

#include "Engine3.h"

bool ENGINE::Init(int width, int height, int mode)
{
#ifdef  GLEW_MX 
    #undef GLEW_MX 
#endif //  GLEW_MX 


    if (!glfwInit()) {
        Log("ERROR::GLFW::INIT_ERROR"); 
        return false; //If the GLFW init failed, there's no need to terminate it
    }

    GLFWwindow* window = nullptr;
    const GLFWvidmode* currentVidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); //Retrive video mode from primary monitor

    //Some hints to ensure compatibility
    glfwWindowHint(GLFW_RED_BITS, currentVidmode->redBits); //Set depth of R chanel to value of a monitor
    glfwWindowHint(GLFW_GREEN_BITS, currentVidmode->greenBits); //Set depth of G chanel to value of a monitor
    glfwWindowHint(GLFW_BLUE_BITS, currentVidmode->blueBits); //Set depth of B chanel to value of a monitor
    glfwWindowHint(GLFW_REFRESH_RATE, currentVidmode->refreshRate); //Set refresh rate to value of a monitor
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); //Make, so window couldn't be resized by user
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); //Set minimum required version of OpenGL to 4.0
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); 
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //Just for safety, 'cause there's no need to use old legacy stuff

    //Check, what mode is used and act accordingly
    if (mode == ENGINE3_FULLSCREEN)
        window = glfwCreateWindow(width, height, ENGINE::AppTitle, glfwGetPrimaryMonitor(), NULL); 
    else if(mode == ENGINE3_WINDOWED)
        window = glfwCreateWindow(width, height, ENGINE::AppTitle, NULL, NULL); 

    if (!window) {
        Log("ERROR::GLFW::WINDOW_OPENING_ERROR"); //Throw an error
        glfwTerminate(); //But if the window could't be opened, GLFW must be terminated
        return false;
    }

    glfwMakeContextCurrent(ENGINE::Window); //Activate window
    ENGINE::Window = window; //Save window for later use

    if (!glfwGetCurrentContext()) {
        Log("ERROR::GLFW::UNABLE_TO_CREATE_CONTEXT");
        return false;
    }

    return true;
}

void ENGINE::Begin()
{
    glewExperimental = GL_TRUE;

    if (GLenum err = (glewInit() != GLEW_OK)) { 
        Log("ERROR::GLEW::UNABLE_TO_INIT" << std::endl << glewGetErrorString(err));
        return; 
    }

    ENGINE::UserCreationCallback();

    GLsizei winSizeX, winSizeY;
    glfwGetFramebufferSize(ENGINE::Window, &winSizeX, &winSizeY);
    glViewport(0, 0, winSizeX, winSizeY);


    ENGINE::MainLoop();

    glfwTerminate();
}




void ENGINE::MainLoop() 
{

    float prevFrameTime = .0f;
    GLint RendererSetupSuccess = 1;

    if (!ENGINE::renderer.InitRenderer()){
        Log("ERROR::RENDERER::INIT_ERROR");
        return;
    }
    ENGINE::renderer = renderer;


    while (!glfwWindowShouldClose(ENGINE::Window))
    {
        GLfloat currenTtime = (GLfloat)glfwGetTime();
        GLfloat elapsed = currenTtime - prevFrameTime;
        prevFrameTime = currenTtime;


        Log("Update");      
        glfwPollEvents();
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(ENGINE::Window);
    }
}

每次运行程序时,它都会引发“ ERROR :: GLFW :: UNABLE_TO_CREATE_CONTEXT”错误。我不知道发生了什么 需要任何帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

在分配之前,您正在使用ENGINE::Window;所以是NULL。这是glfwMakeContextCurrent函数失败的原因。

使用:

glfwMakeContextCurrent(window);

代替:

glfwMakeContextCurrent(ENGINE::Window);

答案 1 :(得分:0)

glfwMakeContextCurrent(ENGINE::Window); //Activate window
ENGINE::Window = window; //Save window for later use

ENGINE::Window仍然是nullptr,因此您只是将nullptr传递给glfwContext创建。更改顺序,或使用glfwMakeContextCurrent(window); // the local variable