2017-04-23 51 views
0

我想跟着游戏引擎教程,当视频运行此代码时,它会打开一个窗口。当我这样做时,它会提供错误消息,以防止未创建窗口。GLFW不会初始化我的窗口

#include "window.h" 

namespace sparky { 

namespace graphics { 

    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" << std::endl; 
      return false; 
     } 

     m_Window = glfwCreateWindow(m_Width, m_Height, m_Title, NULL, NULL); 

     if (!m_Window) 
     { 
      std::cout << "Window Not Created" << std::endl; 
      return false; 
     } 

     glfwMakeContextCurrent(m_Window); 
     return true; 

    } 

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

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

} 

} 

这是我在window.cpp代码,我得到的窗口不会创建错误行,这是我在window.h

#pragma once 
#include <GLFW/glfw3.h> 
#include <iostream> 

namespace sparky { 

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() const; 
    private: 
     bool init(); 


    }; 

} 

} 

和我的主类

#include <GLFW/glfw3.h> 
#include <iostream> 
#include "src/graphics/window.h" 

int main() { 

using namespace sparky; 
using namespace graphics; 

Window window("Sparks Fly", 800, 600); 

while (!window.closed()) { 
    window.update(); 
} 

system("PAUSE"); 
return 0; 
} 

回答

0

你问题出在这条线上:

 if (!glfwInit) { 

贝卡使用glfwInit是一个库函数,它会(假设你链接正确)总是有一个不是nullptr的有效地址。因此,!glfwInit将其更改为false,并在世界范围内审阅if语句。

这可以通过简单地调用功能,它不能转换为一个布尔值是固定的:

 if(!glfwInit()) { 
+0

谢谢你的sooo多,我正试图解决这一问题的长期洙! – NYBoss00

+0

没问题! :d – InternetAussie