2017-04-23 112 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