Browse Source

Add stepping back/forth in time

rexim 4 years ago
parent
commit
a18bc3cf76
2 changed files with 13 additions and 1 deletions
  1. 1 0
      README.md
  2. 12 1
      main.c

+ 1 - 0
README.md

@@ -8,3 +8,4 @@ Just a simple OpenGL template that I use on my streams.
 |------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
 | <kbd>F5</kbd>    | Reload [main.frag](./main.frag) and [main.vert](./main.vert) shaders. Red screen indicates a compilation or linking error, check the output of the program if you see it. |
 | <kbd>SPACE</kbd> | Pause/unpause the time uniform variable in shaders                                                                                                                        |
+| <kbd>←</kbd><kbd>→</kbd> | In pause mode step back/forth in time. |

+ 12 - 1
main.c

@@ -1,3 +1,4 @@
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -14,6 +15,7 @@
 
 #define SCREEN_WIDTH 1024
 #define SCREEN_HEIGHT 768
+#define MANUAL_TIME_STEP 0.1
 
 float lerp(float a, float b, float t)
 {
@@ -115,6 +117,7 @@ bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
 
 // Global variables (fragile people with CS degree look away)
 bool program_failed = false;
+double time = 0.0;
 GLuint program = 0;
 GLint resolution_uniform = 0;
 GLint time_uniform = 0;
@@ -168,6 +171,14 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
         } else if (key == GLFW_KEY_SPACE) {
             pause = !pause;
         }
+
+        if (pause) {
+            if (key == GLFW_KEY_LEFT) {
+                time -= MANUAL_TIME_STEP;
+            } else if (key == GLFW_KEY_RIGHT) {
+                time += MANUAL_TIME_STEP;
+            }
+        }
     }
 }
 
@@ -237,7 +248,7 @@ int main()
     glfwSetKeyCallback(window, key_callback);
     glfwSetFramebufferSizeCallback(window, window_size_callback);
 
-    double time = glfwGetTime();
+    time = glfwGetTime();
     double prev_time;
     while (!glfwWindowShouldClose(window)) {
         glClear(GL_COLOR_BUFFER_BIT);