feat(Game): add getter and run functions

This commit is contained in:
2025-11-19 10:44:39 +01:00
parent 3892d29f4e
commit 6ccbc1c4e9
3 changed files with 53 additions and 4 deletions

View File

@@ -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

View File

@@ -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;
}

View File

@@ -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;
}