mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 03:34:15 +00:00
feat(Game): add getter and run functions
This commit is contained in:
@@ -17,7 +17,13 @@ private:
|
||||
public:
|
||||
Game(int width, int height, string name);
|
||||
~Game();
|
||||
void run();
|
||||
|
||||
void run(bool (*func)(Game *g));
|
||||
|
||||
GLFWwindow *getWindow();
|
||||
const char *getName();
|
||||
int getWidth();
|
||||
int getHeight();
|
||||
};
|
||||
|
||||
#endif
|
||||
40
src/game.cpp
40
src/game.cpp
@@ -1,6 +1,7 @@
|
||||
#include "GL/glew.h"
|
||||
#include "game.hpp"
|
||||
#include "GLFW/glfw3.h"
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
@@ -38,12 +39,49 @@ Game::Game(int width, int height, string name) :
|
||||
glEnable(GL_MULTISAMPLE);
|
||||
}
|
||||
|
||||
void Game::run()
|
||||
void Game::run(bool (*func)(Game *g))
|
||||
{
|
||||
while(func(this))
|
||||
{
|
||||
glfwGetWindowSize(this->window, &this->width, &this->height);
|
||||
glViewport(0, 0, this->width, this->height);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glClearColor(0.5f, 0.2f, 0.2f, 1.0f);
|
||||
|
||||
glDisable(GL_MULTISAMPLE);
|
||||
glfwPollEvents();
|
||||
glfwSwapBuffers(this->window);
|
||||
|
||||
GLenum error = glGetError();
|
||||
if (error != glGetError())
|
||||
{
|
||||
std::cerr << error << std::endl;
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
glfwTerminate();
|
||||
}
|
||||
|
||||
GLFWwindow *Game::getWindow()
|
||||
{
|
||||
return this->window;
|
||||
}
|
||||
const char *Game::getName()
|
||||
{
|
||||
return this->name;
|
||||
}
|
||||
|
||||
int Game::getWidth()
|
||||
{
|
||||
return this->height;
|
||||
}
|
||||
|
||||
int Game::getHeight()
|
||||
{
|
||||
return this->width;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,11 @@
|
||||
int main()
|
||||
{
|
||||
Game game(800, 600, "game");
|
||||
game.run();
|
||||
|
||||
auto quit = [](Game *g) {
|
||||
return !glfwWindowShouldClose(g->getWindow());
|
||||
};
|
||||
|
||||
game.run(quit);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user