From 71ab55ad22abd7ba8d6c41fa4c9ff5aa69db8284 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Tue, 26 Jan 2021 08:45:23 +0100 Subject: Initialize OpenGL window --- Makefile | 2 +- src/app.c | 47 ++++++++++++++++++++++++++++++++++++++++++----- src/app.h | 11 +++++------ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index b31e5c8..e7770ac 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ LIB_DIR := $(BUILD_DIR) # Compiler and linker configuration. CC := gcc CFLAGS := -Wall -Wextra -pedantic -std=c99 -LDFLAGS := +LDFLAGS := -lGL -lGLEW -lglfw LDLIBS := RELEASE_CFLAGS := -DNDEBUG -O2 DEBUG_CFLAGS := -DDEBUG -g diff --git a/src/app.c b/src/app.c index a39bff0..99f5cff 100644 --- a/src/app.c +++ b/src/app.c @@ -4,8 +4,37 @@ static inline bool app_init(AppState *state, PlatformAPI platform) { platform.log("INIT"); - state->lt_memory = platform.calloc(LT_MEMORY_SIZE, sizeof(u8)); - state->st_memory = platform.calloc(ST_MEMORY_SIZE, sizeof(u8)); + + // Initialize GLFW. + if (!glfwInit()) { + platform.log("ERROR: failed to initialize GLFW"); + return false; + } + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + + // // Initialize window. + GLFWwindow* window = glfwCreateWindow(400, 300, "Hello MIC", NULL, NULL); + if (window == NULL) { + platform.log("ERROR: failed to create GLFW window"); + glfwTerminate(); + return false; + } + glfwMakeContextCurrent(window); + + // Initialize GLEW. + if(glewInit() != GLEW_OK) { + platform.log("ERROR: glew initialization"); + glfwTerminate(); + return false; + } + + // Initialize viewport. + glViewport(0, 0, 400, 300); + + // Initialize application state. + state->window = window; return true; } @@ -29,9 +58,17 @@ app_unload(AppState *state, PlatformAPI platform) { static inline bool app_step(AppState *state, PlatformAPI platform) { - (void)state; // Unused parameter. - platform.log("STEP"); - platform.sleep(100000); + (void)platform; // Unused parameter. + if (glfwWindowShouldClose(state->window)) { + return false; + } + + glClearColor(1.0f, 0.0f, 0.4f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + + glfwSwapBuffers(state->window); + glfwPollEvents(); + return true; } diff --git a/src/app.h b/src/app.h index 8edbe58..613360f 100644 --- a/src/app.h +++ b/src/app.h @@ -1,16 +1,15 @@ #ifndef MIC_APP_H #define MIC_APP_H +#include +#include + #include "shorthand.h" #include "platform.h" -#define LT_MEMORY_SIZE GB(2) -#define ST_MEMORY_SIZE MB(100) - typedef struct AppState { - // Long and short term memory. - char *lt_memory; - char *st_memory; + // OpenGL + GLFWwindow *window; } AppState; // Function pointers for the AppAPI. -- cgit v1.2.1