feat: add game visual and start gObserver

This commit is contained in:
2025-11-15 14:35:27 +01:00
parent 2bf123323d
commit 11c08f975b
6 changed files with 128 additions and 74 deletions

0
src/gObserver.cpp Normal file
View File

View File

@@ -2,73 +2,14 @@
#include "GLFW/glfw3.h" #include "GLFW/glfw3.h"
#include <iostream> #include <iostream>
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() 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;
} }
} }

View File

@@ -1,20 +1,21 @@
#ifndef GAME_HPP #ifndef GAME_HPP
#define GAME_HPP #define GAME_HPP
#include "GL/glew.h" #include "visual.hpp"
#include "GLFW/glfw3.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
class Game class Game
{ {
private: private:
// window size bool finished = false;
int width, height; int frame = 60;
// window conf
GLFWwindow *window = nullptr; Visual v;
public: public:
Game(const char *name, int width, int height); Game();
~Game();
void run(); void run();
}; };

View File

@@ -1,9 +1,8 @@
#include <iostream> #include "visual.hpp"
#include "game.hpp"
int main() int main()
{ {
Game game {"hello world", 800, 600}; Visual v("game", 800, 600);
game.run(); v.run();
return 0; return 0;
} }

87
src/visual.cpp Normal file
View File

@@ -0,0 +1,87 @@
#include "visual.hpp"
#include <GLFW/glfw3.h>
#include <iostream>
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;
}

26
src/visual.hpp Normal file
View File

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