Skip to content

Commit

Permalink
real time mode and update gd.h
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Jun 22, 2021
1 parent cd7524b commit ca44282
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 9 deletions.
2 changes: 1 addition & 1 deletion libraries/gd.h
42 changes: 38 additions & 4 deletions src/hooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,50 @@ void Hooks::init() {
ADD_GD_HOOK(0x20D810, PlayLayer::onQuit);
ADD_GD_HOOK(0x1E60E0, PlayLayer::onEditor);

ADD_GD_HOOK(0x205460, PlayLayer::updateVisiblity);

ADD_GD_HOOK(0x1E4620, PauseLayer_init);

ADD_GD_HOOK(0x1f4ff0, PlayerObject_ringJump);
ADD_GD_HOOK(0xef0e0, GameObject_activateObject);
ADD_GD_HOOK(0x10ed50, GJBaseGameLayer_bumpPlayer);
}

// yes these are global, too lazy to store them in replaysystem or smth
// not like theyre used anywhere else atm
bool g_disable_render = false;
float g_left_over = 0.f;

void __fastcall Hooks::CCScheduler_update_H(CCScheduler* self, int, float dt) {
auto& rs = ReplaySystem::get_instance();
if (rs.is_playing() || rs.is_recording() && gd::GameManager::sharedState()->getPlayLayer()) {
auto fps = rs.get_replay().get_fps();
const auto fps = rs.get_replay().get_fps();
auto speedhack = self->getTimeScale();
dt = 1.f / fps / speedhack;

const float target_dt = 1.f / fps / speedhack;

if (!rs.real_time_mode)
return CCScheduler_update(self, target_dt);

// todo: find ways to disable more render stuff
g_disable_render = true;

// TODO: not have this min()
// doing the commented out if below causes really weird stutters for some reason
const int times = min(static_cast<int>((dt + g_left_over) / target_dt), 150);
// if the fps is really low then dont run it a lot of times
// if (dt > 1.f / 10.f) {
// times = min(times, 100);
// }
for (int i = 0; i < times; ++i) {
if (i == times - 1)
g_disable_render = false;
CCScheduler_update(self, target_dt);
}
g_left_over += dt - target_dt * times;
} else {
CCScheduler_update(self, dt);
}
CCScheduler_update(self, dt);
}

void __fastcall Hooks::CCKeyboardDispatcher_dispatchKeyboardMSG_H(CCKeyboardDispatcher* self, int, int key, bool down) {
Expand Down Expand Up @@ -211,4 +240,9 @@ void __fastcall Hooks::GJBaseGameLayer_bumpPlayer_H(gd::GJBaseGameLayer* self, i
bool b = object->m_hasBeenActivatedP2;
GJBaseGameLayer_bumpPlayer(self, player, object);
_handle_activated_object(a, b, object);
}
}

void __fastcall Hooks::PlayLayer::updateVisiblity_H(gd::PlayLayer* self, int) {
if (!g_disable_render)
updateVisiblity(self);
}
2 changes: 2 additions & 0 deletions src/hooks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Hooks {
// maybe hook the destructor instead ?
_THISCALL_HOOK(onQuit, void*, gd::PlayLayer)
_THISCALL_HOOK(onEditor, void*, gd::PlayLayer, void*)

_THISCALL_HOOK(updateVisiblity, void, gd::PlayLayer)
}

_THISCALL_HOOK(PauseLayer_init, bool, gd::PauseLayer)
Expand Down
18 changes: 18 additions & 0 deletions src/overlay_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ bool OverlayLayer::init() {
label->setPosition({win_size.width - 55, win_size.height - 155});
addChild(label);

auto toggle = gd::CCMenuItemToggler::create(check_off_sprite, check_on_sprite, this, menu_selector(OverlayLayer::on_toggle_real_time));
toggle->setPosition({win_size.width - 35, -190});
toggle->toggle(rs.real_time_mode);
menu->addChild(toggle);

label = CCLabelBMFont::create("Real Time", "bigFont.fnt");
label->setAnchorPoint({1, 0.5});
label->setScale(0.8f);
label->setPosition({win_size.width - 55, win_size.height - 190});
addChild(label);

sprite = CCSprite::create("square02b_001.png");
sprite->setColor({0, 0, 0});
sprite->setOpacity(69);
Expand Down Expand Up @@ -245,4 +256,11 @@ void OverlayLayer::on_frame(CCObject*) {
m_x_pos_toggle->toggle(false);
m_frame_toggle->toggle(false);
ReplaySystem::get_instance().set_default_type(ReplayType::FRAME);
}

void OverlayLayer::on_toggle_real_time(CCObject* toggle_) {
auto toggle = cast<gd::CCMenuItemToggler*>(toggle_);
if (toggle != nullptr) {
ReplaySystem::get_instance().real_time_mode = !toggle->isOn(); // why is it flipped
}
}
2 changes: 2 additions & 0 deletions src/overlay_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ class OverlayLayer : public gd::FLAlertLayer, public CCTextFieldDelegate, public
void on_x_pos(CCObject*);
void on_frame(CCObject*);

void on_toggle_real_time(CCObject*);

virtual void keyBackClicked();
virtual void keyDown(enumKeyCodes key) {
// keyDown overwrites keyBackClicked, how fun
Expand Down
4 changes: 2 additions & 2 deletions src/practice_fixes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ struct CheckpointData {
float rotation;
bool buffer_orb;
static CheckpointData from_player(gd::PlayerObject* player) {
return { player->yAccel, player->getRotation(), player->canBufferOrb };
return { player->m_yAccel, player->getRotation(), player->m_hasJustHeld };
};
void apply(gd::PlayerObject* player) {
player->yAccel = y_accel;
player->m_yAccel = y_accel;
player->setRotation(rotation);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/replay_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ void ReplaySystem::on_reset() {
else
replay.remove_actions_after(get_frame());
const auto& actions = replay.get_actions();
bool holding = play_layer->m_player1->isActuallyHolding;
bool holding = play_layer->m_player1->m_isHolding;
if ((holding && actions.empty()) || (!actions.empty() && actions.back().hold != holding)) {
record_action(holding, true, false);
if (holding) {
Hooks::PlayLayer::releaseButton(play_layer, 0, true);
Hooks::PlayLayer::pushButton(play_layer, 0, true);
play_layer->m_player1->canBufferOrb = true;
play_layer->m_player1->m_hasJustHeld = true;
}
} else if (!actions.empty() && actions.back().hold && holding && !practice_fixes.checkpoints.empty() && practice_fixes.checkpoints.top().player1.buffer_orb) {
Hooks::PlayLayer::releaseButton(play_layer, 0, true);
Expand Down
2 changes: 2 additions & 0 deletions src/replay_system.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,6 @@ class ReplaySystem {
inline void set_frame_advance(bool b) { frame_advance = b; }

unsigned get_frame();

bool real_time_mode = true; // fuck it we going public
};

0 comments on commit ca44282

Please sign in to comment.