diff options
author | Bad Diode <bd@badd10de.dev> | 2021-01-26 08:45:23 +0100 |
---|---|---|
committer | Bad Diode <bd@badd10de.dev> | 2021-01-26 10:33:56 +0100 |
commit | 71ab55ad22abd7ba8d6c41fa4c9ff5aa69db8284 (patch) | |
tree | 7da231ec95dfccbbb4ca1f895a308550339e9e1a | |
parent | e24186fc1917c5e8b91924af0c3c7c55816ff5d6 (diff) | |
download | mic-opengl-example-71ab55ad22abd7ba8d6c41fa4c9ff5aa69db8284.tar.gz mic-opengl-example-71ab55ad22abd7ba8d6c41fa4c9ff5aa69db8284.zip |
Initialize OpenGL window
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/app.c | 47 | ||||
-rw-r--r-- | src/app.h | 11 |
3 files changed, 48 insertions, 12 deletions
@@ -19,7 +19,7 @@ LIB_DIR := $(BUILD_DIR) | |||
19 | # Compiler and linker configuration. | 19 | # Compiler and linker configuration. |
20 | CC := gcc | 20 | CC := gcc |
21 | CFLAGS := -Wall -Wextra -pedantic -std=c99 | 21 | CFLAGS := -Wall -Wextra -pedantic -std=c99 |
22 | LDFLAGS := | 22 | LDFLAGS := -lGL -lGLEW -lglfw |
23 | LDLIBS := | 23 | LDLIBS := |
24 | RELEASE_CFLAGS := -DNDEBUG -O2 | 24 | RELEASE_CFLAGS := -DNDEBUG -O2 |
25 | DEBUG_CFLAGS := -DDEBUG -g | 25 | DEBUG_CFLAGS := -DDEBUG -g |
@@ -4,8 +4,37 @@ | |||
4 | static inline bool | 4 | static inline bool |
5 | app_init(AppState *state, PlatformAPI platform) { | 5 | app_init(AppState *state, PlatformAPI platform) { |
6 | platform.log("INIT"); | 6 | platform.log("INIT"); |
7 | state->lt_memory = platform.calloc(LT_MEMORY_SIZE, sizeof(u8)); | 7 | |
8 | state->st_memory = platform.calloc(ST_MEMORY_SIZE, sizeof(u8)); | 8 | // Initialize GLFW. |
9 | if (!glfwInit()) { | ||
10 | platform.log("ERROR: failed to initialize GLFW"); | ||
11 | return false; | ||
12 | } | ||
13 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); | ||
14 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); | ||
15 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
16 | |||
17 | // // Initialize window. | ||
18 | GLFWwindow* window = glfwCreateWindow(400, 300, "Hello MIC", NULL, NULL); | ||
19 | if (window == NULL) { | ||
20 | platform.log("ERROR: failed to create GLFW window"); | ||
21 | glfwTerminate(); | ||
22 | return false; | ||
23 | } | ||
24 | glfwMakeContextCurrent(window); | ||
25 | |||
26 | // Initialize GLEW. | ||
27 | if(glewInit() != GLEW_OK) { | ||
28 | platform.log("ERROR: glew initialization"); | ||
29 | glfwTerminate(); | ||
30 | return false; | ||
31 | } | ||
32 | |||
33 | // Initialize viewport. | ||
34 | glViewport(0, 0, 400, 300); | ||
35 | |||
36 | // Initialize application state. | ||
37 | state->window = window; | ||
9 | return true; | 38 | return true; |
10 | } | 39 | } |
11 | 40 | ||
@@ -29,9 +58,17 @@ app_unload(AppState *state, PlatformAPI platform) { | |||
29 | 58 | ||
30 | static inline bool | 59 | static inline bool |
31 | app_step(AppState *state, PlatformAPI platform) { | 60 | app_step(AppState *state, PlatformAPI platform) { |
32 | (void)state; // Unused parameter. | 61 | (void)platform; // Unused parameter. |
33 | platform.log("STEP"); | 62 | if (glfwWindowShouldClose(state->window)) { |
34 | platform.sleep(100000); | 63 | return false; |
64 | } | ||
65 | |||
66 | glClearColor(1.0f, 0.0f, 0.4f, 1.0f); | ||
67 | glClear(GL_COLOR_BUFFER_BIT); | ||
68 | |||
69 | glfwSwapBuffers(state->window); | ||
70 | glfwPollEvents(); | ||
71 | |||
35 | return true; | 72 | return true; |
36 | } | 73 | } |
37 | 74 | ||
@@ -1,16 +1,15 @@ | |||
1 | #ifndef MIC_APP_H | 1 | #ifndef MIC_APP_H |
2 | #define MIC_APP_H | 2 | #define MIC_APP_H |
3 | 3 | ||
4 | #include <GL/glew.h> | ||
5 | #include <GLFW/glfw3.h> | ||
6 | |||
4 | #include "shorthand.h" | 7 | #include "shorthand.h" |
5 | #include "platform.h" | 8 | #include "platform.h" |
6 | 9 | ||
7 | #define LT_MEMORY_SIZE GB(2) | ||
8 | #define ST_MEMORY_SIZE MB(100) | ||
9 | |||
10 | typedef struct AppState { | 10 | typedef struct AppState { |
11 | // Long and short term memory. | 11 | // OpenGL |
12 | char *lt_memory; | 12 | GLFWwindow *window; |
13 | char *st_memory; | ||
14 | } AppState; | 13 | } AppState; |
15 | 14 | ||
16 | // Function pointers for the AppAPI. | 15 | // Function pointers for the AppAPI. |