From 6fd20124218af64fd4c56ba5a62bcec6ab9c2cb3 Mon Sep 17 00:00:00 2001 From: Moritz Ruge Date: Thu, 6 Aug 2026 13:57:18 +0200 Subject: init 2 --- main.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 main.c (limited to 'main.c') diff --git a/main.c b/main.c new file mode 100644 index 0000000..32aff4e --- /dev/null +++ b/main.c @@ -0,0 +1,88 @@ +#include +#define GLFW_INCLUDE_NONE +#include +#include +#include +#include + +// struct and function declaration +// ---------------------------------------------------------------------------- +void framebuffer_size_callback(GLFWwindow* window, int width, int height); +void processInput(GLFWwindow* window); +// ---------------------------------------------------------------------------- +// END + +int main(void) { + + // GLFW settings init and which version of opengl we should use + // and which subset of opengl profiles + glfwInit(); + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + + + // Window settings + // create window + int w_height = 1280; + int w_width = 820; + GLFWwindow* window = glfwCreateWindow(w_height, w_width, "Test", NULL, NULL); + if (window == NULL) { + printf("Failed to create GLFW window\n"); + glfwTerminate(); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + return -1; + } + // init window + glfwMakeContextCurrent(window); + + // init GLAD + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + printf("Failed to initialize GLAD\n"); + return -1; + } + + // opengl viewport + framebuffer_size_callback(window, w_width, w_height); + + // ------------------------------------------------------------------------- + // render loop + // ------------------------------------------------------------------------- + while (!glfwWindowShouldClose(window)) { + // input + processInput(window); + + // rendering commands here + // ----------------------- + glClearColor(0.2f, 0.3f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + // check and call events and swap the buffers + glfwSwapBuffers(window); + glfwPollEvents(); + } + + glfwTerminate(); + return 0; +} + +// END render loop +// ----------------------------------------------------------------------------- + +// Functions +// ----------------------------------------------------------------------------- +// glfw: whenever the window size changed (by OS or user resize) this callback function executes +// --------------------------------------------------------------------------------------------- +void framebuffer_size_callback(GLFWwindow* window, int width, int height) { + glViewport(0, 0, width, height); +} + +// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly +// --------------------------------------------------------------------------------------------------------- +void processInput(GLFWwindow* window) { + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1); + if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) glfwSetWindowShouldClose(window, 1); +} + + -- cgit v1.2.3