Window类是游戏窗口,相当于main
我主要采用了Learn-Computer-Graphics中的窗口类。
main类
#pragma once
#include <iostream>
using namespace std;
#include "Window.h"
int main();
-----------------------------------------------------
#include "main.h"
int main()
{
Window* window = new Window(800, 600);
window->Mainloop();
delete window;
return 0;
}
Window类
#ifndef Window_H
#define Window_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "Input.h"
#include <iostream>
using namespace std;
class Scene;
class Time;
void Resize(GLFWwindow* window, int width, int height);
class Window
{
public:
static Window* Instance;
Window(int width, int height);
~Window();
void InitEvent();
void InitInput();
void Mainloop();
int Width, Height;
void SetDeltaTime(float time);
void SetStartTime(float time);
void SetCurTime(float time);
void InitScene();
void Log(string str);
GLFWwindow* window_ptr;
private:
void KeyDown(Key key);
void KeyUp(Key key);
Scene* m_scene;
Input* _input;
};
#endif
---------------------------------
#include "Window.h"
#include "Scene.h"
#include "Time.h"
#include "EventCenter.h"
#include <boost/bind/bind.hpp>
Window* Window::Instance = NULL;
Window::Window(int width, int height)
{
Width = width;
Height = height;
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
// glfw window creation
// --------------------
window_ptr = glfwCreateWindow(width, height, "LearnOpenGL", NULL, NULL);
if (window_ptr == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window_ptr);
glfwSetFramebufferSizeCallback(window_ptr, Resize);
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return;
}
Window::Instance = this;
}
Window::~Window()
{
glfwTerminate();
}
void Window::InitEvent()
{
EventCenter::Instance->AddEvent<string>("Log", boost::bind(&Window::Log, this, placeholders::_1));
EventCenter::Instance->AddEvent<Key>("KeyDown", boost::bind(&Window::KeyDown, this, placeholders::_1));
EventCenter::Instance->AddEvent<Key>("KeyUp", boost::bind(&Window::KeyUp, this, placeholders::_1));
}
void Window::InitInput()
{
_input = new Input();
}
void Resize(GLFWwindow* window, int width, int height)
{
glfwGetWindowSize(window, &width, &height);
if (width != Window::Instance->Width || Window::Instance->Height)
{
Window::Instance->Width = width;
Window::Instance->Height = height;
glViewport(0, 0, width, height);
printf("resized \n");
}
}
void Window::Mainloop()
{
InitEvent();
InitScene();
InitInput();
float starTime = glfwGetTime();
float currentFrame, lastFrame = starTime;
SetStartTime(starTime);
while (!glfwWindowShouldClose(Window::window_ptr))
{
currentFrame = glfwGetTime();
SetCurTime(currentFrame);
float deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
SetDeltaTime(deltaTime);
_input->Update();
//for each frame
Scene::Instace->Update();
Scene::Instace->LateUpdate();
Scene::Instace->Render();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
// all drawing goes here ..
glfwSwapBuffers(window_ptr);
glfwPollEvents();
}
}
void Window::SetDeltaTime(float time)
{
Time::_deltaTime = time;
}
void Window::SetStartTime(float time)
{
Time::_startTime = time;
}
void Window::SetCurTime(float time)
{
Time::_curTime = time;
}
void Window::InitScene()
{
m_scene = Scene::Init();
}
void Window::Log(string str)
{
cout << str << endl;
}
void Window::KeyDown(Key key)
{
Log(to_string((int)key));
}
void Window::KeyUp(Key key)
{
Log(to_string((int)key));
}