From 11c08f975b7e2ed70ca03a42bd9e2eceefc63ae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20GUEZO?= Date: Sat, 15 Nov 2025 14:35:27 +0100 Subject: [PATCH] feat: add game visual and start gObserver --- src/gObserver.cpp | 0 src/game.cpp | 63 ++-------------------------------- src/game.hpp | 19 ++++++----- src/main.cpp | 7 ++-- src/visual.cpp | 87 +++++++++++++++++++++++++++++++++++++++++++++++ src/visual.hpp | 26 ++++++++++++++ 6 files changed, 128 insertions(+), 74 deletions(-) create mode 100644 src/gObserver.cpp create mode 100644 src/visual.cpp create mode 100644 src/visual.hpp diff --git a/src/gObserver.cpp b/src/gObserver.cpp new file mode 100644 index 0000000..e69de29 diff --git a/src/game.cpp b/src/game.cpp index 442fe81..631f4aa 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,73 +2,14 @@ #include "GLFW/glfw3.h" #include -Game::Game(const char *name, int width, int height) +Game::Game() : v("game", 800, 600) { - this->width = width; - this->height = height; - - if(!glfwInit()) - { - std::cerr << "Failed to initialize GLFW"; - exit(1); - } - - 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 - - this->window = glfwCreateWindow(this->width, this->height, name, nullptr, nullptr); - if (!this->window) - { - std::cerr << "Failed to create GLFW window\n"; - glfwTerminate(); - exit(1); - } - - glfwMakeContextCurrent(window); - - glewExperimental = GL_TRUE; - if (glewInit() != GLEW_OK) - { - std::cerr << "Failed to initialize GLEW\n"; - glfwTerminate(); - exit(EXIT_FAILURE); - } - - glEnable(GL_DEPTH_TEST); - glEnable(GL_MULTISAMPLE); -} - -Game::~Game() -{ - if (window) - { - glfwDestroyWindow(window); - } - glfwTerminate(); - std::cout << "Game finished with success." << std::endl; } void Game::run() { - while(!glfwWindowShouldClose(this->window)) + while(!this->finished) { - glfwGetWindowSize(window, &this->width, &this->height); - glViewport(0, 0, this->width, this->height); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - glClearColor(0.2f, 0.2f, 0.2f, 1.0f); - - glDisable(GL_MULTISAMPLE); - glfwPollEvents(); - glfwSwapBuffers(window); - - GLenum error; - if ((error = glGetError()) != GL_NO_ERROR) - std::cout << error << std::endl; } } \ No newline at end of file diff --git a/src/game.hpp b/src/game.hpp index beaecf4..b2c0ac9 100644 --- a/src/game.hpp +++ b/src/game.hpp @@ -1,20 +1,21 @@ #ifndef GAME_HPP #define GAME_HPP -#include "GL/glew.h" -#include "GLFW/glfw3.h" +#include "visual.hpp" + +#include +#include class Game { private: - // window size - int width, height; - // window conf - GLFWwindow *window = nullptr; - + bool finished = false; + int frame = 60; + + Visual v; public: - Game(const char *name, int width, int height); - ~Game(); + Game(); + void run(); }; diff --git a/src/main.cpp b/src/main.cpp index 6e44f28..0ab7d1e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,8 @@ -#include -#include "game.hpp" +#include "visual.hpp" int main() { - Game game {"hello world", 800, 600}; - game.run(); + Visual v("game", 800, 600); + v.run(); return 0; } \ No newline at end of file diff --git a/src/visual.cpp b/src/visual.cpp new file mode 100644 index 0000000..f7323ec --- /dev/null +++ b/src/visual.cpp @@ -0,0 +1,87 @@ +#include "visual.hpp" + +#include +#include + +Visual::Visual(const char *name, int width, int height) : width(width), height(height) +{ + if(!glfwInit()) + { + std::cerr << "Failed to initialize GLFW"; + exit(1); + } + + // opengl 3.3 + 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 + + this->window = glfwCreateWindow(this->width, this->height, name, nullptr, nullptr); + if (!this->window) + { + std::cerr << "Failed to create GLFW window\n"; + glfwTerminate(); + exit(1); + } + + glfwMakeContextCurrent(window); + + glewExperimental = GL_TRUE; + if (glewInit() != GLEW_OK) + { + std::cerr << "Failed to initialize GLEW\n"; + glfwTerminate(); + exit(EXIT_FAILURE); + } + + glEnable(GL_DEPTH_TEST); + glEnable(GL_MULTISAMPLE); +} + +Visual::~Visual() +{ + if (window) + glfwDestroyWindow(window); + + glfwTerminate(); + std::cout << "Game finished with success." << std::endl; +} + +void Visual::run() +{ + while(!glfwWindowShouldClose(this->window)) + { + glfwGetWindowSize(window, &this->width, &this->height); + glViewport(0, 0, this->width, this->height); + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + glClearColor(0.2f, 0.2f, 0.2f, 1.0f); + + glDisable(GL_MULTISAMPLE); + glfwPollEvents(); + glfwSwapBuffers(window); + + GLenum error; + if ((error = glGetError()) != GL_NO_ERROR) + std::cout << error << std::endl; + } +} + +int Visual::getWidth() +{ + return this->width; +} + +int Visual::getHeight() +{ + return this->height; +} + +GLFWwindow* Visual::getWindow() +{ + return this->window; +} \ No newline at end of file diff --git a/src/visual.hpp b/src/visual.hpp new file mode 100644 index 0000000..65d29f2 --- /dev/null +++ b/src/visual.hpp @@ -0,0 +1,26 @@ +#ifndef VISUAL_HPP +#define VISUAL_HPP + +#include "GL/glew.h" +#include "GLFW/glfw3.h" + +class Visual +{ +private: + // window size + int width, height; + // window conf + GLFWwindow *window = nullptr; + +public: + Visual(const char *name, int width, int height); + ~Visual(); + + void run(); + + GLFWwindow *getWindow(); + int getWidth(); + int getHeight(); +}; + +#endif