feat(game): move main default config to game class

This commit is contained in:
2025-11-14 22:17:56 +01:00
parent 652bd75cf1
commit 2bf123323d
4 changed files with 103 additions and 110 deletions

74
src/game.cpp Normal file
View File

@@ -0,0 +1,74 @@
#include "game.hpp"
#include "GLFW/glfw3.h"
#include <iostream>
Game::Game(const char *name, int width, int height)
{
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))
{
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;
}
}

21
src/game.hpp Normal file
View File

@@ -0,0 +1,21 @@
#ifndef GAME_HPP
#define GAME_HPP
#include "GL/glew.h"
#include "GLFW/glfw3.h"
class Game
{
private:
// window size
int width, height;
// window conf
GLFWwindow *window = nullptr;
public:
Game(const char *name, int width, int height);
~Game();
void run();
};
#endif

View File

@@ -1,114 +1,9 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
// vertex shader
const char* vertexShaderSource = R"(
#version 330 core
layout(location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0);
}
)";
// fragment shader
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(0.8, 0.3, 0.02, 1.0);
}
)";
#include "game.hpp"
int main()
{
if(!glfwInit())
{
std::cerr << "Failed to initialize GLFW\n";
return -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
GLFWwindow* window = glfwCreateWindow(800, 600, "Triangle Test", nullptr, nullptr);
if(!window)
{
std::cerr << "Failed to create GLFW window\n";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
if(glewInit() != GLEW_OK)
{
std::cerr << "Failed to initialize GLEW\n";
return -1;
}
// --- Vertex data ---
float vertices[] =
{
0.0f, 0.5f, 0.0f, // top
0.5f, -0.5f, 0.0f, // right
-0.5f, -0.5f, 0.0f // left
};
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// --- Compile shaders ---
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// --- Render loop ---
while(!glfwWindowShouldClose(window))
{
glClearColor(0.2f,0.3f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwDestroyWindow(window);
glfwTerminate();
Game game {"hello world", 800, 600};
game.run();
return 0;
}