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 <stdbool.h>
#include <stdio.h>
@@ -12,47 +10,30 @@
void error_callback(int error, const char *description) {
fprintf(stderr, "Error %d: %s\n", error, description);
glfwTerminate();
close_window(0, true);
exit(EXIT_FAILURE);
}
static void key_callback(Window *window, int key,
__attribute__((unused)) int scancode, int action,
int mods) {
__attribute__((unused)) int mods) {
// close window on escape key
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS && mods == 0) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
if (escape_key(key, action)) {
close_window(window, false);
}
}
void loop(Window *window, ShaderProgram program) {
// TODO remove GLFW references
// TODO split into smaller functions
int width, height;
glfwGetFramebufferSize(window, &width, &height);
vec2 resolution = {(float)width, (float)height};
Context context;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
context = get_window_context(window);
mat4x4 m, p, mvp;
mat4x4_identity(m);
mat4x4_ortho(p, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f);
mat4x4_mul(mvp, p, m);
apply_program(program, context);
glUseProgram(program.program);
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();
update_window(window);
}
void forge_run(Parameters params) {
// TODO remove GLFW references
Window *window;
File fragment_shader = read_file(params.frag_path);
@@ -68,11 +49,11 @@ void forge_run(Parameters params) {
if (program.error) {
fprintf(stderr, "Failed to compile shaders\n");
glfwTerminate();
close_window(window, true);
exit(EXIT_FAILURE);
}
while (!glfwWindowShouldClose(window)) {
while (!window_should_close(window)) {
if (params.hot_reload && should_update_file(&fragment_shader)) {
update_file(&fragment_shader);
update_program(program, fragment_shader);
@@ -80,7 +61,7 @@ void forge_run(Parameters params) {
loop(window, program);
}
glfwTerminate();
close_window(window, true);
free_file(&fragment_shader);
}
+20
View File
@@ -71,4 +71,24 @@ void update_program(ShaderProgram program, File fragment_shader) {
return;
}
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 apply_program(ShaderProgram program, Context context);
#endif
+6
View File
@@ -46,4 +46,10 @@ typedef struct ShaderProgram {
typedef GLFWwindow Window;
typedef struct Context {
int width;
int height;
double time;
} Context;
#endif
+34
View File
@@ -1,5 +1,6 @@
#include <GLFW/glfw3.h>
#include <linmath.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -92,4 +93,37 @@ Window *init_window(Parameters params,
use_window(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 (*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