diff options
Diffstat (limited to 'src/main.c')
-rw-r--r-- | src/main.c | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b30280a --- /dev/null +++ b/src/main.c | |||
@@ -0,0 +1,204 @@ | |||
1 | #define GL_SILENCE_DEPRECATION | ||
2 | #define GLFW_INCLUDE_GLCOREARB | ||
3 | #include <GLFW/glfw3.h> | ||
4 | |||
5 | #include <stdio.h> | ||
6 | #include <stdlib.h> | ||
7 | |||
8 | #include "shorthand.h" | ||
9 | |||
10 | // | ||
11 | // Callbacks. | ||
12 | // | ||
13 | |||
14 | void | ||
15 | glfw_error_callback(int error, const char* description) { | ||
16 | (void)error; | ||
17 | fprintf(stderr, "error: %s\n", description); | ||
18 | } | ||
19 | |||
20 | void | ||
21 | glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { | ||
22 | (void)mods; | ||
23 | (void)scancode; | ||
24 | if (action == GLFW_PRESS && | ||
25 | (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_CAPS_LOCK)) { | ||
26 | glfwSetWindowShouldClose(window, GLFW_TRUE); | ||
27 | } | ||
28 | } | ||
29 | |||
30 | typedef struct Context { | ||
31 | GLFWwindow *window; | ||
32 | u32 gl_version_major; | ||
33 | u32 gl_version_minor; | ||
34 | u32 win_width; | ||
35 | u32 win_height; | ||
36 | char win_title[256]; | ||
37 | bool win_resizable; | ||
38 | |||
39 | struct { | ||
40 | f64 prev; | ||
41 | f64 elapsed; | ||
42 | size_t n_frames; | ||
43 | } frame_time; | ||
44 | } Context; | ||
45 | |||
46 | u32 | ||
47 | compile_program(Context *ctx, const char *vert_src, const char *frag_src) { | ||
48 | // Compile vertex shader. | ||
49 | u32 vert_shader = glCreateShader(GL_VERTEX_SHADER); | ||
50 | { | ||
51 | glShaderSource(vert_shader, 1, &vert_src, NULL); | ||
52 | glCompileShader(vert_shader); | ||
53 | int success = 0; | ||
54 | glGetShaderiv(vert_shader, GL_COMPILE_STATUS, &success); | ||
55 | if (!success) { | ||
56 | fprintf(stderr, "error: vertex shader compilation failed\n"); | ||
57 | glfwDestroyWindow(ctx->window); | ||
58 | glfwTerminate(); | ||
59 | exit(EXIT_FAILURE); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // Compile fragment shader. | ||
64 | u32 frag_shader = glCreateShader(GL_FRAGMENT_SHADER); | ||
65 | { | ||
66 | glShaderSource(frag_shader, 1, &frag_src, NULL); | ||
67 | glCompileShader(frag_shader); | ||
68 | int success = 0; | ||
69 | glGetShaderiv(vert_shader, GL_COMPILE_STATUS, &success); | ||
70 | if (!success) { | ||
71 | fprintf(stderr, "error: fragment shader compilation failed\n"); | ||
72 | glfwDestroyWindow(ctx->window); | ||
73 | glfwTerminate(); | ||
74 | exit(EXIT_FAILURE); | ||
75 | } | ||
76 | } | ||
77 | |||
78 | // Link shader program. | ||
79 | u32 program = glCreateProgram(); | ||
80 | { | ||
81 | glAttachShader(program, vert_shader); | ||
82 | glAttachShader(program, frag_shader); | ||
83 | glLinkProgram(program); | ||
84 | int success = 0; | ||
85 | glGetProgramiv(program, GL_LINK_STATUS, &success); | ||
86 | if(!success) { | ||
87 | fprintf(stderr, "error: program shader linking failed\n"); | ||
88 | glfwDestroyWindow(ctx->window); | ||
89 | glfwTerminate(); | ||
90 | exit(EXIT_FAILURE); | ||
91 | } | ||
92 | } | ||
93 | |||
94 | // Delete unused objects. | ||
95 | glDeleteShader(vert_shader); | ||
96 | glDeleteShader(frag_shader); | ||
97 | return program; | ||
98 | } | ||
99 | |||
100 | void | ||
101 | init_context(Context *ctx) { | ||
102 | if (!glfwInit()) { | ||
103 | fprintf(stderr, "error: GLFW initialization failed"); | ||
104 | exit(EXIT_FAILURE); | ||
105 | } | ||
106 | |||
107 | // Init window and OpenGL context. | ||
108 | glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, ctx->gl_version_major); | ||
109 | glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, ctx->gl_version_minor); | ||
110 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | ||
111 | glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); | ||
112 | glfwWindowHint(GLFW_RESIZABLE, ctx->win_resizable); | ||
113 | GLFWwindow* window = glfwCreateWindow( | ||
114 | ctx->win_width, ctx->win_height, ctx->win_title, NULL, NULL); | ||
115 | if (!window) { | ||
116 | fprintf(stderr, "error: couldn't open OpenGL window"); | ||
117 | glfwTerminate(); | ||
118 | exit(EXIT_FAILURE); | ||
119 | } | ||
120 | |||
121 | // Enable current OpenGL context and init GLAD. | ||
122 | glfwMakeContextCurrent(window); | ||
123 | // TODO: Working on macOS for now, need to check if this is needed. | ||
124 | // if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { | ||
125 | // fprintf(stderr, "error: GLAD initialization failed"); | ||
126 | // glfwDestroyWindow(window); | ||
127 | // glfwTerminate(); | ||
128 | // exit(EXIT_FAILURE); | ||
129 | // } | ||
130 | |||
131 | ctx->window = window; | ||
132 | } | ||
133 | |||
134 | void | ||
135 | setup_callbacks(Context *ctx) { | ||
136 | glfwSetErrorCallback(glfw_error_callback); | ||
137 | glfwSetKeyCallback(ctx->window, glfw_key_callback); | ||
138 | } | ||
139 | |||
140 | void | ||
141 | update_frame_time(Context *ctx) { | ||
142 | // Measure frame times and fps. | ||
143 | f64 cur_time = glfwGetTime(); | ||
144 | f64 delta_time = cur_time - ctx->frame_time.prev; | ||
145 | ctx->frame_time.elapsed += delta_time; | ||
146 | ctx->frame_time.n_frames++; | ||
147 | if (ctx->frame_time.elapsed >= 1.0) { | ||
148 | f64 fps = 0; | ||
149 | fps = ctx->frame_time.n_frames / ctx->frame_time.elapsed; | ||
150 | ctx->frame_time.elapsed = 0; | ||
151 | ctx->frame_time.n_frames = 0; | ||
152 | |||
153 | // Update title with timing data. | ||
154 | char title_buf[256 * 2]; | ||
155 | sprintf(title_buf, | ||
156 | "%s [%.2fms, %.2f FPS]", | ||
157 | ctx->win_title, delta_time * 1000, fps); | ||
158 | glfwSetWindowTitle(ctx->window, title_buf); | ||
159 | } | ||
160 | ctx->frame_time.prev = cur_time; | ||
161 | } | ||
162 | |||
163 | void | ||
164 | update(Context *ctx) { | ||
165 | update_frame_time(ctx); | ||
166 | } | ||
167 | |||
168 | void | ||
169 | render(Context *ctx) { | ||
170 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | ||
171 | glfwSwapBuffers(ctx->window); | ||
172 | } | ||
173 | |||
174 | int | ||
175 | main(void) { | ||
176 | Context ctx = (Context){ | ||
177 | .gl_version_major = 3, | ||
178 | .gl_version_minor = 2, | ||
179 | .win_width = 800, | ||
180 | .win_height = 600, | ||
181 | .win_title = "OpenGL experiments", | ||
182 | }; | ||
183 | init_context(&ctx); | ||
184 | setup_callbacks(&ctx); | ||
185 | |||
186 | // | ||
187 | // Main loop. | ||
188 | // | ||
189 | |||
190 | ctx.frame_time.prev = glfwGetTime(); | ||
191 | ctx.frame_time.elapsed = 0; | ||
192 | ctx.frame_time.n_frames = 0; | ||
193 | while (!glfwWindowShouldClose(ctx.window)) { | ||
194 | update(&ctx); | ||
195 | render(&ctx); | ||
196 | glfwPollEvents(); // constant updates. | ||
197 | // glfwWaitEvents(); // updates only when input arrives (better cpu usage). | ||
198 | } | ||
199 | |||
200 | glfwDestroyWindow(ctx.window); | ||
201 | glfwTerminate(); | ||
202 | return EXIT_SUCCESS; | ||
203 | } | ||
204 | |||