135 lines
4.2 KiB
C++
135 lines
4.2 KiB
C++
#include "main.hpp"
|
|
#include <GL/glu.h>
|
|
|
|
// Variables
|
|
bool running = true;
|
|
|
|
// Main
|
|
int main(int argc, char* argv[]) {
|
|
// Initialize SDL
|
|
if (initialize_sdl() != 0) {
|
|
std::cerr << "SDL initialization failed." << std::endl;
|
|
return 1; // SDL initialization failed
|
|
}
|
|
|
|
// Initialize OpenGL
|
|
if (initialize_opengl() != 0) {
|
|
std::cerr << "OpenGL initialization failed." << std::endl;
|
|
cleanup_sdl();
|
|
return 1; // OpenGL initialization failed
|
|
}
|
|
|
|
// Initialize ImGui
|
|
if (initialize_imgui() != 0) {
|
|
std::cerr << "ImGui initialization failed." << std::endl;
|
|
cleanup_opengl();
|
|
cleanup_sdl();
|
|
return 1; // ImGui initialization failed
|
|
}
|
|
|
|
// Triangle vertices
|
|
float vertices[] = {
|
|
0.5f, 0.5f, 0.0f, // top right
|
|
0.5f, -0.5f, 0.0f, // bottom right
|
|
-0.5f, -0.5f, 0.0f, // bottom left
|
|
-0.5f, 0.5f, 0.0f // top left
|
|
};
|
|
|
|
unsigned int indices[] = {
|
|
0, 1, 3, // first triangle
|
|
1, 2, 3 // second triangle
|
|
};
|
|
|
|
// Vertex buffer object (VBO) and vertex array object (VAO) and element buffer object (EBO)
|
|
unsigned int VBO, VAO, EBO;
|
|
|
|
glGenVertexArrays(1, &VAO); // Generate a vertex array object
|
|
glGenBuffers(1, &VBO); // Generate a vertex buffer object
|
|
glGenBuffers(1, &EBO); // Generate an element buffer object
|
|
|
|
glBindVertexArray(VAO); // Bind the vertex array object
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO); // Bind the vertex buffer object
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); // Bind the element buffer object
|
|
|
|
// Upload vertex data to the buffer
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
|
|
|
// Set up vertex attributes
|
|
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
|
|
glEnableVertexAttribArray(0);
|
|
|
|
glBindVertexArray(0); // Unbind the vertex array object
|
|
|
|
// Load shaders
|
|
const char* vertex_shader_source = read_file("shaders/triangle.vert");
|
|
const char* fragment_shader_source = read_file("shaders/triangle.frag");
|
|
|
|
unsigned int vertex_shader = compile_shader(vertex_shader_source, GL_VERTEX_SHADER);
|
|
unsigned int fragment_shader = compile_shader(fragment_shader_source, GL_FRAGMENT_SHADER);
|
|
unsigned int shader_program = create_program(vertex_shader, fragment_shader);
|
|
|
|
// Set the shader program
|
|
glUseProgram(shader_program);
|
|
|
|
// Wireframe toggle
|
|
bool wireframe_mode = false;
|
|
|
|
// Main loop
|
|
while (running) {
|
|
|
|
// Handle events
|
|
while (SDL_PollEvent(&event)) {
|
|
ImGui_ImplSDL3_ProcessEvent(&event); // Process ImGui events
|
|
if (event.type == SDL_EVENT_QUIT) {
|
|
running = false; // Exit loop on quit event
|
|
}
|
|
}
|
|
|
|
// Start the ImGui frame
|
|
new_frame_imgui();
|
|
|
|
// Frame time UI
|
|
frame_time_ui();
|
|
|
|
// Toggle wireframe mode ui
|
|
ImGui::Begin("Wireframe Mode");
|
|
if (ImGui::Checkbox("Enable Wireframe Mode", &wireframe_mode)) {
|
|
if (wireframe_mode) {
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Enable wireframe mode
|
|
} else {
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Disable wireframe mode
|
|
}
|
|
}
|
|
ImGui::End();
|
|
|
|
// Render
|
|
ImGui::Render();
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glBindVertexArray(VAO); // Bind the vertex array object
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); // Draw the triangles
|
|
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
SDL_GL_SwapWindow(window);
|
|
|
|
// Calculate FPS
|
|
calc_frame_time();
|
|
}
|
|
|
|
// Cleanup
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
glDeleteBuffers(1, &EBO);
|
|
glDeleteProgram(shader_program);
|
|
glDeleteShader(vertex_shader);
|
|
glDeleteShader(fragment_shader);
|
|
free((void*)vertex_shader_source);
|
|
free((void*)fragment_shader_source);
|
|
cleanup_imgui();
|
|
cleanup_opengl();
|
|
cleanup_sdl();
|
|
std::cout << "Application exited successfully." << std::endl;
|
|
return 0; // Exit code
|
|
} |