LesDingeriesDeDjalimSurOpenGL/src/core/engine.cpp
Djalim Simaila 1041ec48a2 ♻️ (engine.cpp,onCreate.cpp,showFps.cpp,CMakeLists.txt): remove unused headers and clean up build file
Remove unnecessary #include directives from engine.cpp, onCreate.cpp,
and showFps.cpp to reduce compile time and improve readability.
Also delete extraneous blank lines in CMakeLists.txt for cleaner
configuration.
2025-10-15 10:02:30 +02:00

106 lines
2.5 KiB
C++

#include "engine.h"
#include <cstdlib>
#include <iostream>
#include <vector>
djalim::OpenGlEngine::OpenGlEngine(const char* title, int width, int height) {
// initialize GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
exit(1);
}
loadHints();
this->window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!this->window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
exit(1);
}
glfwMakeContextCurrent(this->window);
// initialize GLEW
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
exit(1);
}
loadCallbacks();
bool loaded = shaderProgram.loadShaders("../assets/shaders/vertex.glsl", "../assets/shaders/fragment.glsl");
if (!loaded) {
std::cerr << "Failed to load shaders correctly" << std::endl;
exit(1);
}
onCreate();
}
void djalim::OpenGlEngine::createObject(std::string name, std::string textures, std::vector<float>& vertices){
objects[name];
bool textureLoaded = objects[name].texture.loadTexture(textures);
if (!textureLoaded) {
std::cerr << "Failed to load " << name << " texture!" << std::endl;
exit(1);
}
//glGenVertexArrays(1, &VAO);
glGenVertexArrays(1, &(objects[name].VAO));
//glGenBuffers(1, &VBO);
glGenBuffers(1, &(objects[name].VBO));
//glBindVertexArray(VAO);
glBindVertexArray(objects[name].VAO);
//glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ARRAY_BUFFER, objects[name].VBO);
std::cout << "Loading 3D objects with "<< vertices.size() << " vertices"<< std::endl;
glBufferData(GL_ARRAY_BUFFER, sizeof(float)*vertices.size(), vertices.data(), GL_STATIC_DRAW);
// Attribut de position
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// Attribut de coordonnée de texture
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
}
djalim::OpenGlEngine::~OpenGlEngine() {
onDestroy();
glfwDestroyWindow(window);
glfwTerminate();
}
void djalim::OpenGlEngine::start(){
// Main loop
double currentSeconds;
while (!glfwWindowShouldClose(this->window)) {
currentSeconds = glfwGetTime();
elapsedSeconds = currentSeconds-previousSeconds;
previousSeconds = currentSeconds;
glfwPollEvents();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glClearColor(0, 0.5, 0.5, 1.0f);
onUpdate();
glfwSwapBuffers(this->window);
}
}