feat: engine class

This commit is contained in:
2025-10-13 16:51:07 +02:00
parent f1a24f576b
commit 4e2c62aaf4
6 changed files with 261 additions and 135 deletions

View File

@@ -2,144 +2,15 @@
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
#endif*/
#include <iostream>
#include <vector>
#include <memory>
#include "../lib/glew/GL/glew.h"
#include "../lib/glfw/glfw3.h"
#include "../lib/glm/glm.hpp"
#include "../lib/glm/gtc/matrix_transform.hpp"
#include "vertex.h"
#include "shader.h"
#include "buffer.h"
#include "state.h"
#include "mesh.h"
#include "entity.h"
#include "model.h"
#include "camera.h"
#include "world.h"
#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600
#include "engine.h"
int main()
{
// Initialize OpenGL
if (!glfwInit()) {
std::cerr << "Failed to initialize glfw\n";
return 1;
}
Engine engine;
glfwWindowHint(GLFW_RESIZABLE, false);
glfwWindowHint(GLFW_SAMPLES, 8);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
// glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
// glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* win = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT,
"Daniel Poveda", nullptr, nullptr);
if (win == nullptr) {
std::cerr << "Failed to create opengl window\n";
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(win);
glEnable(GL_DEPTH_TEST);
glEnable(GL_SCISSOR_TEST);
std::cout << "OpenGL initialized, version: " << glGetString(GL_VERSION)
<< "\n";
// Initialize GLEW
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (err != GLEW_OK) {
std::cerr << "Failed to initialize GLEW: "
<< glewGetErrorString(err) << "\n";
glfwTerminate();
return 1;
}
// Initialize default shader
State::default_shader_ =
std::make_shared<Shader>("data/vertex.glsl", "data/fragment.glsl");
if (std::strlen(State::default_shader_->error()) > 0) {
std::cerr << "Failed to initialize shaders: "
<< State::default_shader_->error() << "\n";
}
// Create world
World world;
// Create camera
auto camera = std::make_shared<Camera>();
camera->set_position(glm::vec3(0.0f, 0.0f, 6.0f));
camera->set_projection(glm::perspective(
glm::radians(45.0f),
static_cast<float>(SCREEN_WIDTH) / SCREEN_HEIGHT, 0.1f, 100.0f));
camera->set_viewport(glm::ivec4(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT));
camera->set_clear_color(glm::vec3(0.1f, 0.1f, 0.1f));
world.add_entity(camera);
// Create triangle mesh
std::vector<Vertex> vertices = {
{{0.0f, 0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{-0.5f, -0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}}};
std::vector<uint16_t> indices = {0, 1, 2};
auto buffer = std::make_shared<Buffer>(vertices, indices);
auto mesh = std::make_shared<Mesh>();
mesh->add_buffer(buffer);
// Create 9 models in a 3x3 grid
std::vector<std::shared_ptr<Model>> models;
for (int row = 0; row < 3; ++row) {
for (int col = 0; col < 3; ++col) {
auto model = std::make_shared<Model>(mesh);
model->set_position(
glm::vec3(-3.0f + static_cast<float>(col) * 3.0f,
0.0f, static_cast<float>(-row) * 3.0f));
models.push_back(model);
world.add_entity(model);
}
}
// Main loop
double angle = 0.0;
double lastTime = glfwGetTime();
while (!glfwWindowShouldClose(win)
&& !glfwGetKey(win, GLFW_KEY_ESCAPE)) {
// Delta time
double currentTime = glfwGetTime();
double deltaTime = currentTime - lastTime;
lastTime = currentTime;
// Update angle
angle += 32.0 * deltaTime;
// Update rotation for all models
for (auto& model : models) {
model->set_rotation(glm::vec3(
0.0f, glm::radians(static_cast<float>(angle)),
0.0f));
}
// Update and draw world
world.update(static_cast<float>(deltaTime));
world.draw();
// Refresh screen
glfwSwapBuffers(win);
glfwPollEvents();
}
// Shutdown
glfwTerminate();
engine.initialize();
engine.run();
engine.destroy();
return 0;
}