remove glfw+gl from forge.c

This commit is contained in:
2025-09-13 16:51:18 +02:00
parent 68ad351d2f
commit f5919f3231
6 changed files with 83 additions and 30 deletions
+11 -30
View File
@@ -1,5 +1,3 @@
#include <GLFW/glfw3.h>
#include <glad/gl.h>
#include <linmath.h> #include <linmath.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -12,47 +10,30 @@
void error_callback(int error, const char *description) { void error_callback(int error, const char *description) {
fprintf(stderr, "Error %d: %s\n", error, description); fprintf(stderr, "Error %d: %s\n", error, description);
glfwTerminate(); close_window(0, true);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
static void key_callback(Window *window, int key, static void key_callback(Window *window, int key,
__attribute__((unused)) int scancode, int action, __attribute__((unused)) int scancode, int action,
int mods) { __attribute__((unused)) int mods) {
// close window on escape key // close window on escape key
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS && mods == 0) { if (escape_key(key, action)) {
glfwSetWindowShouldClose(window, GLFW_TRUE); close_window(window, false);
} }
} }
void loop(Window *window, ShaderProgram program) { void loop(Window *window, ShaderProgram program) {
// TODO remove GLFW references Context context;
// TODO split into smaller functions
int width, height;
glfwGetFramebufferSize(window, &width, &height);
vec2 resolution = {(float)width, (float)height};
glViewport(0, 0, width, height); context = get_window_context(window);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4 m, p, mvp; apply_program(program, context);
mat4x4_identity(m);
mat4x4_ortho(p, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f);
mat4x4_mul(mvp, p, m);
glUseProgram(program.program); update_window(window);
glUniformMatrix4fv(program.mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glUniform1f(program.itime_location, (const GLfloat)glfwGetTime());
glUniform2fv(program.ires_location, 1, (const GLfloat *)&resolution);
glBindVertexArray(program.vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwSwapBuffers(window);
glfwPollEvents();
} }
void forge_run(Parameters params) { void forge_run(Parameters params) {
// TODO remove GLFW references
Window *window; Window *window;
File fragment_shader = read_file(params.frag_path); File fragment_shader = read_file(params.frag_path);
@@ -68,11 +49,11 @@ void forge_run(Parameters params) {
if (program.error) { if (program.error) {
fprintf(stderr, "Failed to compile shaders\n"); fprintf(stderr, "Failed to compile shaders\n");
glfwTerminate(); close_window(window, true);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
while (!glfwWindowShouldClose(window)) { while (!window_should_close(window)) {
if (params.hot_reload && should_update_file(&fragment_shader)) { if (params.hot_reload && should_update_file(&fragment_shader)) {
update_file(&fragment_shader); update_file(&fragment_shader);
update_program(program, fragment_shader); update_program(program, fragment_shader);
@@ -80,7 +61,7 @@ void forge_run(Parameters params) {
loop(window, program); loop(window, program);
} }
glfwTerminate(); close_window(window, true);
free_file(&fragment_shader); free_file(&fragment_shader);
} }
+20
View File
@@ -72,3 +72,23 @@ void update_program(ShaderProgram program, File fragment_shader) {
} }
glLinkProgram(program.program); glLinkProgram(program.program);
} }
void apply_program(ShaderProgram program, Context context) {
mat4x4 m, p, mvp;
glViewport(0, 0, context.width, context.height);
glClear(GL_COLOR_BUFFER_BIT);
mat4x4_identity(m);
mat4x4_ortho(p, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f);
mat4x4_mul(mvp, p, m);
vec2 resolution = {(float)context.width, (float)context.height};
glUseProgram(program.program);
glUniformMatrix4fv(program.mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp);
glUniform1f(program.itime_location, (const GLfloat)context.time);
glUniform2fv(program.ires_location, 1, (const GLfloat *)&resolution);
glBindVertexArray(program.vertex_array);
glDrawArrays(GL_TRIANGLES, 0, 6);
}
+2
View File
@@ -7,4 +7,6 @@ ShaderProgram init_program(File fragment_shader);
void update_program(ShaderProgram program, File fragment_shader); void update_program(ShaderProgram program, File fragment_shader);
void apply_program(ShaderProgram program, Context context);
#endif #endif
+6
View File
@@ -46,4 +46,10 @@ typedef struct ShaderProgram {
typedef GLFWwindow Window; typedef GLFWwindow Window;
typedef struct Context {
int width;
int height;
double time;
} Context;
#endif #endif
+34
View File
@@ -1,5 +1,6 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <linmath.h> #include <linmath.h>
#include <stdbool.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -93,3 +94,36 @@ Window *init_window(Parameters params,
return window; return window;
} }
void update_window(Window *window) {
// swap front and back buffers
glfwSwapBuffers(window);
// listen to mouse and keyboard events
glfwPollEvents();
}
Context get_window_context(Window *window) {
Context context;
glfwGetFramebufferSize(window, &context.width, &context.height);
context.time = glfwGetTime();
return context;
}
void close_window(Window *window, bool hard) {
if (hard) {
glfwTerminate();
} else {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
bool window_should_close(Window *window) {
return glfwWindowShouldClose(window) == GLFW_TRUE;
}
bool escape_key(int key, int action) {
return key == GLFW_KEY_ESCAPE && action == GLFW_PRESS;
}
+10
View File
@@ -7,4 +7,14 @@ Window *init_window(Parameters params,
void (*error_callback)(int, const char *), void (*error_callback)(int, const char *),
void (*key_callback)(Window *, int, int, int, int)); void (*key_callback)(Window *, int, int, int, int));
void update_window(Window *window);
Context get_window_context(Window *window);
void close_window(Window *window, bool hard);
bool window_should_close(Window *window);
bool escape_key(int key, int action);
#endif #endif