From c0c028ec1207e4e5bd9f46cf51622e4c9f4b169e Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 18 Jan 2025 22:25:19 +0000 Subject: [PATCH] Add pause and fast-forward keys --- main.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/main.c b/main.c index 24484c4..79ad85c 100644 --- a/main.c +++ b/main.c @@ -27,6 +27,8 @@ #define MAX_SHADER_SIZE (8 * 1024) +#define FASTFORWARD_RATE 100 + #if defined(CPOW_DTHETA) && defined(POWRATE) #error "Cannot define CPOW_DTHETA and POWRATE simulataneously" #endif @@ -94,6 +96,8 @@ static params_t params = { .scale = STARTSCALE, }; +static bool paused = false, fastforward = false; + static void mouse_button_callback(GLFWwindow *window, int button, int action, int mods) { @@ -119,6 +123,27 @@ mouse_button_callback(GLFWwindow *window, int button, int action, int mods) params.centre.im = zy; } +static void +key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) +{ + (void)window; + (void)scancode; + (void)mods; + + switch (key) { + case GLFW_KEY_SPACE: + if (action == GLFW_PRESS) + paused = !paused; + break; + case GLFW_KEY_F: + if (action == GLFW_PRESS) + fastforward = true; + else if (action == GLFW_RELEASE) + fastforward = false; + break; + } +} + static VkShaderModule load_shader_module(VkDevice dev, const char *path) { static char buf[MAX_SHADER_SIZE] @@ -153,8 +178,9 @@ int main(void) mode->width, mode->height, "GPU Fractal", monitor, NULL); assert(window); - // Register mouse click callback + // Register input callbacks glfwSetMouseButtonCallback(window, mouse_button_callback); + glfwSetKeyCallback(window, key_callback); // Initialise Vulkan instance. const VkApplicationInfo app_info = { @@ -646,6 +672,10 @@ int main(void) result = vkQueuePresentKHR(queue, &present_info); assert(result == VK_SUCCESS); + if (paused) + continue; + + for (int i = 0; i < (fastforward ? FASTFORWARD_RATE : 1); ++i) { #ifdef JULIA_DTHETA double complex julia = params.julia.re + I * params.julia.im; julia *= julia_rotz; @@ -667,6 +697,7 @@ int main(void) #ifdef POWRATE params.zpow += POWRATE; #endif + } } vkDeviceWaitIdle(dev);