added imgui to the template
This commit is contained in:
parent
52c70961c3
commit
2364038c5c
13
include/io.h
Normal file
13
include/io.h
Normal 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
20
source/io.c
Normal 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;
|
||||||
|
}
|
||||||
@ -2,11 +2,19 @@
|
|||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "io.h"
|
||||||
|
|
||||||
// ImGui includes
|
// ImGui includes
|
||||||
#include "imgui/imgui.h"
|
#include "imgui/imgui.h"
|
||||||
#include "imgui/imgui_impl_glfw.h"
|
#include "imgui/imgui_impl_glfw.h"
|
||||||
#include "imgui/imgui_impl_opengl3.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
|
// Callback to resize the viewport when the window is resized
|
||||||
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
||||||
(void)window; // Unused parameter
|
(void)window; // Unused parameter
|
||||||
@ -14,9 +22,7 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// 1. Initialize GLFW
|
|
||||||
glfwInit();
|
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_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
@ -26,8 +32,7 @@ int main() {
|
|||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Required on Mac
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// 2. Create the Window
|
GLFWwindow *window = glfwCreateWindow( SCR_WIDTH, SCR_HEIGHT , "OpenGLTemplate", NULL, NULL);
|
||||||
GLFWwindow *window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
||||||
if (window == NULL) {
|
if (window == NULL) {
|
||||||
std::cout << "Failed to create GLFW window" << std::endl;
|
std::cout << "Failed to create GLFW window" << std::endl;
|
||||||
glfwTerminate();
|
glfwTerminate();
|
||||||
@ -36,57 +41,68 @@ int main() {
|
|||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||||
|
|
||||||
// 3. Initialize GLAD (Load function pointers)
|
|
||||||
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
|
if (!gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress))) {
|
||||||
std::cout << "Failed to initialize GLAD" << std::endl;
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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 Setup Start ---
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
ImGuiIO &io = ImGui::GetIO();
|
ImGuiIO &io = ImGui::GetIO();
|
||||||
(void)io;
|
(void)io;
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
|
||||||
ImGui::StyleColorsDark();
|
ImGui::StyleColorsDark();
|
||||||
// ImGui::StyleColorsLight();
|
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
|
||||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||||
ImGui_ImplOpenGL3_Init(glsl_version);
|
ImGui_ImplOpenGL3_Init(glsl_version);
|
||||||
|
|
||||||
// 4. The Render Loop
|
|
||||||
while (!glfwWindowShouldClose(window)) {
|
while (!glfwWindowShouldClose(window)) {
|
||||||
// Input
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
|
|
||||||
// --- ImGui Frame Start ---
|
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
ImGui_ImplGlfw_NewFrame();
|
ImGui_ImplGlfw_NewFrame();
|
||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
|
|
||||||
// Create a simple window
|
|
||||||
ImGui::Begin("Settings");
|
ImGui::Begin("Settings");
|
||||||
ImGui::Text("Hello from Dear ImGui!");
|
ImGui::Text("FPS: %.1f", static_cast<double>(ImGui::GetIO().Framerate));
|
||||||
ImGui::End();
|
ImGui::End();
|
||||||
// --- ImGui Frame End ---
|
glUseProgram(shaderProgram);
|
||||||
|
|
||||||
// Rendering commands here
|
|
||||||
glClearColor(0.01f, 0.01f, 0.01f, 1.0f); // Set clear color (Dark Gray)
|
glClearColor(0.01f, 0.01f, 0.01f, 1.0f); // Set clear color (Dark Gray)
|
||||||
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
|
glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
|
||||||
|
|
||||||
|
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
|
||||||
// Swap buffers and poll IO events
|
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
// 5. Clean up
|
// 5. Clean up
|
||||||
|
|
||||||
ImGui_ImplOpenGL3_Shutdown();
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
ImGui_ImplGlfw_Shutdown();
|
ImGui_ImplGlfw_Shutdown();
|
||||||
ImGui::DestroyContext();
|
ImGui::DestroyContext();
|
||||||
|
|||||||
8
source/shader/fragment.glsl
Normal file
8
source/shader/fragment.glsl
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
#version 330 core
|
||||||
|
out vec4 FragColor;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
FragColor = vec4(1.0, 1.0, 1.0, 1.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
9
source/shader/vertex.glsl
Normal file
9
source/shader/vertex.glsl
Normal 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);
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user