From ac65fb1c06e013d1dbb7728c6daff04ec5be2889 Mon Sep 17 00:00:00 2001 From: Edgaru089 Date: Mon, 22 Apr 2024 05:46:05 +0800 Subject: [PATCH] Time scaling with the Spell key --- app.c | 4 ++++ app.h | 1 + app_render.cpp | 7 +++++++ input.c | 6 ++++++ 4 files changed, 18 insertions(+) diff --git a/app.c b/app.c index a19aba5..4734e3e 100644 --- a/app.c +++ b/app.c @@ -29,6 +29,7 @@ App *app_NewApp() { app->time = gametime_NewSystem(app); app->switch_level = NULL; + app->timescale = 1.0; app->clear_color = 0; app->wantQuit = false; app->paused = false; @@ -71,6 +72,9 @@ void app_Advance(App *app, Duration deltaTime) { input_Advance(app->input); + if (1.0 - app->timescale > EPS) + deltaTime.microseconds = deltaTime.microseconds * app->timescale; + if (!app->paused) { gametime_Advance(app->time, deltaTime); particle_Advance(app->particle, deltaTime); diff --git a/app.h b/app.h index 5c768f5..f951afc 100644 --- a/app.h +++ b/app.h @@ -27,6 +27,7 @@ typedef struct _App { System_GameTime *time; char *switch_level; + double timescale; uint32_t clear_color; bool wantQuit, debugOn; bool paused; diff --git a/app_render.cpp b/app_render.cpp index ca0b586..c576002 100644 --- a/app_render.cpp +++ b/app_render.cpp @@ -12,6 +12,7 @@ #include "render_util.h" #include "util/vector.h" #include +#include #include @@ -178,5 +179,11 @@ void app_Render(App *app) { RECT rect = {.left = SCREEN_WIDTH / 2 - 10, .top = 100, .right = SCREEN_WIDTH / 2 + 10, .bottom = 200}; drawtext("Game Paused", &rect, DT_CENTER | DT_NOCLIP); } + if (abs(app->timescale - 1.0) > EPS) { + RECT rect = {.left = SCREEN_WIDTH / 2 - 10, .top = 50, .right = SCREEN_WIDTH / 2 + 10, .bottom = 150}; + char buf[128]; + snprintf(buf, sizeof(buf), "*** TIMESCALE %.2lf ***", app->timescale); + drawtext(buf, &rect, DT_CENTER | DT_NOCLIP); + } } } diff --git a/input.c b/input.c index 0f44195..6ccbb22 100644 --- a/input.c +++ b/input.c @@ -64,6 +64,12 @@ void input_Advance(System_Input *sys) { } } + if (input_IsPressed(sys->keys[input_Key_Spell])) { + sys->super->timescale = 0.25; + } else { + sys->super->timescale = 1.0; + } + if (sys->keys[input_Key_Escape] == JustPressed) { if (!sys->super->paused) fprintf(stderr, "[input_Advance] Pausing\n");