added imgui to the template

This commit is contained in:
Afonso Clerigo Mendes de Sousa 2026-02-26 18:29:40 +00:00
parent 52c70961c3
commit 2364038c5c
7 changed files with 86 additions and 20 deletions

BIN
alv Executable file

Binary file not shown.

13
include/io.h Normal file
View File

@ -0,0 +1,13 @@
#pragma once
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
char* readFile(const char* filePath);
#ifdef __cplusplus
}
#endif

20
source/io.c Normal file
View File

@ -0,0 +1,20 @@
#include "io.h"
char* readFile(const char* filePath) {
FILE* file = fopen(filePath, "rb");
if (file == NULL) {
fprintf(stderr, "Error: Could not open file %s\n", filePath);
return NULL;
}
fseek(file, 0, SEEK_END);
long length = ftell(file);
fseek(file, 0, SEEK_SET);
char* buffer = (char*)malloc(length + 1);
fread(buffer, 1, length, file);
buffer[length] = '\0';
fclose(file);
return buffer;
}

View File

@ -2,11 +2,19 @@
#include <GLFW/glfw3.h>
#include <iostream>
#include "io.h"
// ImGui includes
#include "imgui/imgui.h"
#include "imgui/imgui_impl_glfw.h"
#include "imgui/imgui_impl_opengl3.h"
char* fragmentShaderFile = "./source/shader/fragment.glsl";
char* vertexShaderFile = "./source/shader/vertex.glsl";
const unsigned int SCR_WIDTH = 1920;
const unsigned int SCR_HEIGHT = 1080;
// Callback to resize the viewport when the window is resized
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
(void)window; // Unused parameter
@ -14,9 +22,7 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
}
int main() {
// 1. Initialize GLFW
glfwInit();
// Set OpenGL version to 3.3 Core Profile - For MacOS, you may need to uncomment the next line
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
@ -26,8 +32,7 @@ int main() {
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
#endif
// 2. Create the Window
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
GLFWwindow *window = glfwCreateWindow( SCR_WIDTH, SCR_HEIGHT , "OpenGLTemplate", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
@ -36,57 +41,68 @@ int main() {
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
// 3. Initialize GLAD (Load function pointers)
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
// --- ImGui Setup Start ---
// Compile shaders
char* vertexShaderSource = readFile(vertexShaderFile);
char* fragmentShaderSource = readFile(fragmentShaderFile);
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
delete[] vertexShaderSource;
delete[] fragmentShaderSource;
// --- ImGui Setup Start ---
IMGUI_CHECKVERSION();
ImGui::CreateContext();
ImGuiIO &io = ImGui::GetIO();
(void)io;
// Setup Dear ImGui style
ImGui::StyleColorsDark();
// ImGui::StyleColorsLight();
// Setup Platform/Renderer backends
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init(glsl_version);
// 4. The Render Loop
while (!glfwWindowShouldClose(window)) {
// Input
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// --- ImGui Frame Start ---
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGlfw_NewFrame();
ImGui::NewFrame();
// Create a simple window
ImGui::Begin("Settings");
ImGui::Text("Hello from Dear ImGui!");
ImGui::Text("FPS: %.1f", static_cast<double>(ImGui::GetIO().Framerate));
ImGui::End();
// --- ImGui Frame End ---
glUseProgram(shaderProgram);
// Rendering commands here
glClearColor(0.01f, 0.01f, 0.01f, 1.0f); // Set clear color (Dark Gray)
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
// Swap buffers and poll IO events
glfwSwapBuffers(window);
glfwPollEvents();
}
// 5. Clean up
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();

View File

@ -0,0 +1,8 @@
#version 330 core
out vec4 FragColor;
void main() {
FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

View File

@ -0,0 +1,9 @@
#version 330 core
layout (location = 0) in vec2 aPos;
out vec3 vColor;
void main() {
gl_Position = vec4(aPos, 0.0, 1.0);
}

BIN
test

Binary file not shown.