glfwCreateWindow(..)在Visual Studio中返回null

时间:2013-09-20 20:03:55

标签: visual-studio opengl graphic glfw

我正在使用GLFW,只想打开一个空窗口。

我下载了适用于Windows 32的GLFW。 创建了一个空的控制台项目并编写了这段代码:

#include "main.h"
#pragma comment (lib, "glfw3dll")
#pragma comment (lib, "OpenGL32")

#define GLFW_DLL

#include <glfw3.h>
#include <chrono>
#include <iostream>

using namespace std::chrono;


GLFWwindow* window;

bool running = true;


bool initialise(){
return true;
}

void update(double deltaTime){

}

void render(){

}



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

if (!glfwInit)
    return -1;

window = (glfwCreateWindow(800, 600, "Hello World", nullptr, nullptr));

if (window == nullptr){
    glfwTerminate();
    return -1;
}

glfwMakeContextCurrent(window);

if (!initialise()){
    glfwTerminate();
    return -1;
}

auto currentTimeStamp = system_clock::now();
auto prevTimeStamp = system_clock::now();

while (running)
{
    currentTimeStamp = system_clock::now();

    auto elapsed = duration_cast<milliseconds>(currentTimeStamp - prevTimeStamp);
    auto seconds = double(elapsed.count()) / 1000.0;

    update(seconds);

    render();

    glfwPollEvents();

    prevTimeStamp = currentTimeStamp;

}

glfwTerminate();

return -1;
}

我认为我正确地添加了库和标题。 但每次程序在glfwCreateWindow(..)函数之后以-1退出,因为此函数返回null。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:3)

if (!glfwInit)
    return -1;

我不确定glfwInit为什么NULL除非在DLL加载期间发生了真正可怕的事情。

尝试调用 glfwInit()代替:

if( !glfwInit() )
    return -1;