diff options
-rw-r--r-- | src/main.c | 129 |
1 files changed, 127 insertions, 2 deletions
@@ -31,8 +31,8 @@ typedef struct Context { | |||
31 | GLFWwindow *window; | 31 | GLFWwindow *window; |
32 | u32 gl_version_major; | 32 | u32 gl_version_major; |
33 | u32 gl_version_minor; | 33 | u32 gl_version_minor; |
34 | u32 win_width; | 34 | s32 win_width; |
35 | u32 win_height; | 35 | s32 win_height; |
36 | char win_title[256]; | 36 | char win_title[256]; |
37 | bool win_resizable; | 37 | bool win_resizable; |
38 | 38 | ||
@@ -132,6 +132,12 @@ init_context(Context *ctx) { | |||
132 | } | 132 | } |
133 | 133 | ||
134 | void | 134 | void |
135 | init_debug_overlay(Context *ctx) { | ||
136 | // TODO: Add debug text to context and initialize here | ||
137 | (void)ctx; | ||
138 | } | ||
139 | |||
140 | void | ||
135 | setup_callbacks(Context *ctx) { | 141 | setup_callbacks(Context *ctx) { |
136 | glfwSetErrorCallback(glfw_error_callback); | 142 | glfwSetErrorCallback(glfw_error_callback); |
137 | glfwSetKeyCallback(ctx->window, glfw_key_callback); | 143 | glfwSetKeyCallback(ctx->window, glfw_key_callback); |
@@ -167,12 +173,130 @@ update(Context *ctx) { | |||
167 | 173 | ||
168 | void | 174 | void |
169 | render(Context *ctx) { | 175 | render(Context *ctx) { |
176 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); | ||
170 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 177 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
178 | |||
179 | glfwGetFramebufferSize(ctx->window, &ctx->win_width, &ctx->win_height); | ||
180 | glViewport(0, 0, ctx->win_width, ctx->win_height); | ||
181 | |||
182 | |||
183 | // TODO: Only need to be done once on the initialization function. | ||
184 | // Prepare quad rendering. | ||
185 | f32 vertices[] = { | ||
186 | // position // tex_coords | ||
187 | 1.0f, 1.0f, 1.0f, 1.0f, | ||
188 | 1.0f, -1.0f, 1.0f, 0.0f, | ||
189 | -1.0f, -1.0f, 0.0f, 0.0f, | ||
190 | -1.0f, 1.0f, 0.0f, 1.0f, | ||
191 | }; | ||
192 | GLuint indices[] = { | ||
193 | 0, 1, 3, | ||
194 | 1, 2, 3 | ||
195 | }; | ||
196 | |||
197 | // Vertex array object. | ||
198 | u32 vao; | ||
199 | glGenVertexArrays(1, &vao); | ||
200 | glBindVertexArray(vao); | ||
201 | |||
202 | // Vertex buffer object setup. | ||
203 | u32 vbo; | ||
204 | glGenBuffers(1, &vbo); | ||
205 | glBindBuffer(GL_ARRAY_BUFFER, vbo); | ||
206 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); | ||
207 | glEnableVertexAttribArray(0); | ||
208 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)0); | ||
209 | glEnableVertexAttribArray(1); | ||
210 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void*)(2 * sizeof(f32))); | ||
211 | |||
212 | // Element buffer object setup. | ||
213 | u32 ebo; | ||
214 | glGenBuffers(1, &ebo); | ||
215 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); | ||
216 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); | ||
217 | |||
218 | // Texture atlas. | ||
219 | u8 texture[] = { | ||
220 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
221 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
223 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
224 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
225 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
226 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
227 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, | ||
228 | }; | ||
229 | u32 tex_id; | ||
230 | glGenTextures(1, &tex_id); | ||
231 | glBindTexture(GL_TEXTURE_2D, tex_id); | ||
232 | glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, 16, 8, 0, GL_RED, | ||
233 | GL_UNSIGNED_BYTE, &texture); | ||
234 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
235 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
236 | glBindTexture(GL_TEXTURE_2D, 0); | ||
237 | |||
238 | // Unbind objects to avoid accidental modifications. | ||
239 | glBindVertexArray(0); | ||
240 | glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
241 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||
242 | |||
243 | const char* vert_src = | ||
244 | "#version 330 core\n" | ||
245 | "layout (location = 0) in vec2 position;\n" | ||
246 | "layout (location = 1) in vec2 tex_coords;\n" | ||
247 | "out vec2 tex;\n" | ||
248 | "void main() {\n" | ||
249 | " gl_Position = vec4(position.x, position.y, 0.0, 1.0);\n" | ||
250 | " tex = tex_coords;\n" | ||
251 | "}"; | ||
252 | |||
253 | // Fragment shader. | ||
254 | const char* frag_src = | ||
255 | "#version 330 core\n" | ||
256 | "in vec2 tex;\n" | ||
257 | "out vec4 frag;\n" | ||
258 | "uniform usampler2D tex_sampler;\n" | ||
259 | "void main() {\n" | ||
260 | " uint val = texture(tex_sampler, tex).r;\n" | ||
261 | " frag = vec4(val, val, val, 1.0);\n" | ||
262 | "}\n"; | ||
263 | |||
264 | u32 program = compile_program(ctx, vert_src, frag_src); | ||
265 | |||
266 | glUseProgram(program); | ||
267 | glBindVertexArray(vao); | ||
268 | glBindTexture(GL_TEXTURE_2D, tex_id); | ||
269 | |||
270 | // TODO: How to pass the parameters for each box? | ||
271 | // NOTE: We want to render the text as a single draw call if possible. | ||
272 | // glEnable(GL_BLEND); | ||
273 | // glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | ||
274 | // // glBlendFunc(GL_ONE, GL_ONE); | ||
275 | // // glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); | ||
276 | // // glBlendFunc(GL_SRC_ALPHA, GL_ONE); | ||
277 | // // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); | ||
278 | // // glBlendFunc(GL_ONE, GL_ZERO); | ||
279 | // // glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); | ||
280 | // // glBlendFunc(GL_DST_ALPHA,GL_ONE); | ||
281 | // // glBlendFunc(GL_DST_ALPHA, GL_SRC_ALPHA); | ||
282 | // // glBlendFunc(GL_DST_ALPHA, GL_DST_ALPHA); | ||
283 | // // glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_DST_ALPHA); | ||
284 | // // glBlendEquation(GL_FUNC_ADD); | ||
285 | // | ||
286 | // glBindTexture(GL_TEXTURE_2D, text_tex); // TODO: Bind the texture atlas. | ||
287 | glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); | ||
288 | // glDisable(GL_BLEND); | ||
289 | |||
171 | glfwSwapBuffers(ctx->window); | 290 | glfwSwapBuffers(ctx->window); |
172 | } | 291 | } |
173 | 292 | ||
174 | int | 293 | int |
175 | main(void) { | 294 | main(void) { |
295 | |||
296 | // | ||
297 | // Initialization. | ||
298 | // | ||
299 | |||
176 | Context ctx = (Context){ | 300 | Context ctx = (Context){ |
177 | .gl_version_major = 3, | 301 | .gl_version_major = 3, |
178 | .gl_version_minor = 2, | 302 | .gl_version_minor = 2, |
@@ -182,6 +306,7 @@ main(void) { | |||
182 | }; | 306 | }; |
183 | init_context(&ctx); | 307 | init_context(&ctx); |
184 | setup_callbacks(&ctx); | 308 | setup_callbacks(&ctx); |
309 | init_debug_overlay(&ctx); | ||
185 | 310 | ||
186 | // | 311 | // |
187 | // Main loop. | 312 | // Main loop. |