mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 04:34:14 +00:00
feat(Game): add getter and run functions
This commit is contained in:
@@ -17,7 +17,13 @@ private:
|
|||||||
public:
|
public:
|
||||||
Game(int width, int height, string name);
|
Game(int width, int height, string name);
|
||||||
~Game();
|
~Game();
|
||||||
void run();
|
|
||||||
|
void run(bool (*func)(Game *g));
|
||||||
|
|
||||||
|
GLFWwindow *getWindow();
|
||||||
|
const char *getName();
|
||||||
|
int getWidth();
|
||||||
|
int getHeight();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
40
src/game.cpp
40
src/game.cpp
@@ -1,6 +1,7 @@
|
|||||||
#include "GL/glew.h"
|
#include "GL/glew.h"
|
||||||
#include "game.hpp"
|
#include "game.hpp"
|
||||||
#include "GLFW/glfw3.h"
|
#include "GLFW/glfw3.h"
|
||||||
|
#include <cstdlib>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
|
||||||
@@ -38,12 +39,49 @@ Game::Game(int width, int height, string name) :
|
|||||||
glEnable(GL_MULTISAMPLE);
|
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()
|
Game::~Game()
|
||||||
{
|
{
|
||||||
glfwTerminate();
|
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()
|
int main()
|
||||||
{
|
{
|
||||||
Game game(800, 600, "game");
|
Game game(800, 600, "game");
|
||||||
game.run();
|
|
||||||
|
auto quit = [](Game *g) {
|
||||||
|
return !glfwWindowShouldClose(g->getWindow());
|
||||||
|
};
|
||||||
|
|
||||||
|
game.run(quit);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user