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;
}
謝謝你的sooo多,我正試圖解決這一問題的長期洙! – NYBoss00
沒問題! :d – InternetAussie