소스 검색

Introduce toggle pause

rexim 4 년 전
부모
커밋
a53f56b505
2개의 변경된 파일21개의 추가작업 그리고 5개의 파일을 삭제
  1. 5 2
      README.md
  2. 16 3
      main.c

+ 5 - 2
README.md

@@ -2,6 +2,9 @@
 
 Just a simple OpenGL template that I use on my streams.
 
-## Shaders Hot Reloading
+## Controls
 
-Press <kbd>F5</kbd> to 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.
+|Shortcut|Description|
+|---|
+|<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|

+ 16 - 3
main.c

@@ -106,10 +106,12 @@ bool link_program(GLuint vert_shader, GLuint frag_shader, GLuint *program)
     return program;
 }
 
+// Global variables (fragile people with CS degree look away)
 bool program_failed = false;
 GLuint program = 0;
 GLint resolution_location = 0;
 GLint time_location = 0;
+bool pause = false;
 
 void reload_shaders(void)
 {
@@ -153,8 +155,12 @@ void key_callback(GLFWwindow* window, int key, int scancode, int action, int mod
     (void) action;
     (void) mods;
 
-    if (key == GLFW_KEY_F5 && action == GLFW_PRESS) {
-        reload_shaders();
+    if (action == GLFW_PRESS) {
+        if (key == GLFW_KEY_F5) {
+            reload_shaders();
+        } else if (key == GLFW_KEY_SPACE) {
+            pause = !pause;
+        }
     }
 }
 
@@ -219,6 +225,8 @@ int main()
     glfwSetKeyCallback(window, key_callback);
     glfwSetFramebufferSizeCallback(window, window_size_callback);
 
+    double time = glfwGetTime();
+    double prev_time;
     while (!glfwWindowShouldClose(window)) {
         glClear(GL_COLOR_BUFFER_BIT);
 
@@ -226,13 +234,18 @@ int main()
             glUniform2f(resolution_location,
                         SCREEN_WIDTH,
                         SCREEN_HEIGHT);
-            glUniform1f(time_location, glfwGetTime());
+            glUniform1f(time_location, time);
 
             glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
         }
 
         glfwSwapBuffers(window);
         glfwPollEvents();
+        double cur_time = glfwGetTime();
+        if (!pause) {
+            time += cur_time - prev_time;
+        }
+        prev_time = cur_time;
     }
 
     return 0;