Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Commit

Permalink
improve "Frame Stepper"
Browse files Browse the repository at this point in the history
- added "Hold to advance"
- fixed the issue which required to press the key twice to step one frame
  • Loading branch information
Prevter committed Mar 24, 2024
1 parent 934637d commit 168f4af
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions src/shared/hacks/frame-stepper/frame-stepper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@

namespace openhack::hacks {

static uint32_t s_holdAdvanceTimer = 0;

void FrameStepper::onInit() {
// Set the default value
config::setIfEmpty("hack.framestep.enabled", false);
config::setIfEmpty("hack.framestep.step_key", "C");
config::setIfEmpty("hack.framestep.hold", false);
config::setIfEmpty("hack.framestep.hold_speed", 5.0f);

// Initialize keybind
menu::keybinds::setKeybindCallback("framestep.enabled", []() {
Expand All @@ -16,16 +20,20 @@ namespace openhack::hacks {
}

void FrameStepper::onDraw() {
gui::callback([](){
gui::callback([]() {
gui::tooltip("Allows you to step through the game frame by frame");
menu::keybinds::addMenuKeybind("framestep.enabled", "Frame Stepper", [](){
menu::keybinds::addMenuKeybind("framestep.enabled", "Frame Stepper", []() {
bool enabled = !config::get<bool>("hack.framestep.enabled");
config::set("hack.framestep.enabled", enabled);
});
});
gui::toggleSetting("Frame Stepper", "hack.framestep.enabled", [](){
gui::toggleSetting("Frame Stepper", "hack.framestep.enabled", []() {
gui::width(90);
gui::keybind("Step key", "hack.framestep.step_key");
gui::checkbox("Hold to advance", "hack.framestep.hold");
gui::tooltip("Hold the step key to advance the game frame by frame");
gui::inputFloat("Hold speed", "hack.framestep.hold_speed", 0.f, FLT_MAX, "%.0f");
gui::tooltip("How many frames to skip when holding the step key. (0 = play at normal speed)");
gui::width();
}, ImVec2(0, 0), 150);
}
Expand All @@ -34,15 +42,35 @@ namespace openhack::hacks {
return config::get<bool>("hack.framestep.enabled", false);
}

void FrameStepper::gameUpdate(float* dt) {
void FrameStepper::gameUpdate(float *dt) {
if (!config::get<bool>("hack.framestep.enabled", false)) return;

auto step = 240.f;
if (config::get<bool>("hack.display.tps_bypass")) {
step = config::get<float>("hack.display.tps", 240);
if (config::get<bool>("hack.display.physics_bypass")) {
step = config::get<float>("hack.display.pfps", 240);
}

auto holdTimer = static_cast<uint32_t>(config::get<float>("hack.framestep.hold_speed", 5));

bool shouldStep = false;
if (config::get<bool>("hack.framestep.hold")) {
shouldStep = utils::isKeyDown(config::get<std::string>("hack.framestep.step_key"));
if (shouldStep) {
s_holdAdvanceTimer++;
if (s_holdAdvanceTimer >= holdTimer) {
s_holdAdvanceTimer = 0;
shouldStep = true;
} else {
shouldStep = false;
}
} else {
s_holdAdvanceTimer = 0;
}
} else {
shouldStep = utils::isKeyPressed(config::get<std::string>("hack.framestep.step_key"));
}

if (utils::isKeyPressed(config::get<std::string>("hack.framestep.step_key"))) {
if (shouldStep) {
*dt = 1.0f / step;
} else {
*dt = 0.0f;
Expand Down

0 comments on commit 168f4af

Please sign in to comment.