diff --git a/alv b/alv new file mode 100755 index 0000000..b606f89 Binary files /dev/null and b/alv differ diff --git a/include/io.h b/include/io.h new file mode 100644 index 0000000..6037247 --- /dev/null +++ b/include/io.h @@ -0,0 +1,13 @@ +#pragma once +#include +#include +#include +#ifdef __cplusplus +extern "C" { +#endif + +char* readFile(const char* filePath); + +#ifdef __cplusplus +} +#endif diff --git a/source/io.c b/source/io.c new file mode 100644 index 0000000..739968b --- /dev/null +++ b/source/io.c @@ -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; +} diff --git a/source/main.cpp b/source/main.cpp index e8f8ece..0c628b1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -2,11 +2,19 @@ #include #include +#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(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(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(); diff --git a/source/shader/fragment.glsl b/source/shader/fragment.glsl new file mode 100644 index 0000000..b446312 --- /dev/null +++ b/source/shader/fragment.glsl @@ -0,0 +1,8 @@ +#version 330 core +out vec4 FragColor; + +void main() { + FragColor = vec4(1.0, 1.0, 1.0, 1.0); +} + + diff --git a/source/shader/vertex.glsl b/source/shader/vertex.glsl new file mode 100644 index 0000000..1b0623f --- /dev/null +++ b/source/shader/vertex.glsl @@ -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); +} + diff --git a/test b/test index 0fa8aa1..3763289 100755 Binary files a/test and b/test differ