diff --git a/src/forge.c b/src/forge.c index d598964..e83dbbc 100644 --- a/src/forge.c +++ b/src/forge.c @@ -1,5 +1,3 @@ -#include -#include #include #include #include @@ -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); } \ No newline at end of file diff --git a/src/shaders.c b/src/shaders.c index fa6429e..d6c5992 100644 --- a/src/shaders.c +++ b/src/shaders.c @@ -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); } \ No newline at end of file diff --git a/src/shaders.h b/src/shaders.h index c9cdfb2..b89622e 100644 --- a/src/shaders.h +++ b/src/shaders.h @@ -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 \ No newline at end of file diff --git a/src/types.h b/src/types.h index f0ccc3e..a0b1a41 100644 --- a/src/types.h +++ b/src/types.h @@ -46,4 +46,10 @@ typedef struct ShaderProgram { typedef GLFWwindow Window; +typedef struct Context { + int width; + int height; + double time; +} Context; + #endif \ No newline at end of file diff --git a/src/window.c b/src/window.c index 1f3ec46..4a9fc84 100644 --- a/src/window.c +++ b/src/window.c @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -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; } \ No newline at end of file diff --git a/src/window.h b/src/window.h index 2a73274..58711fc 100644 --- a/src/window.h +++ b/src/window.h @@ -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 \ No newline at end of file