mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-05-26 07:18:47 -04:00
767ba3bb14
* launch.json cwd * bodge to load gci for testing * stub card stat * gameplay bodges * viewport, ub fixes * add release with debug info cmake variant * be fixes, sound stub * viewport h * d_msg_flow BE * be fopAcM_createItemFromEnemyID * update launch configuration to use iso * more audio stubs * Attempt to set viewport and get messages for brightness check * skip opening scene again, fixed JMessage::TResourceContainer::TCResource::Do_destroy * add guards for viewport changes * moar endian swapping to get Link sitting in PROC_OPENING_SCENE and for dialogues * BE d_msg_class i_data * stub bgm start * fix div by 0 error (for now) * TEMP_BROKEN in d_menu_ring * REQUIRES_GX_LINES * properly stub renderingAmap::draw with REQUIRES_GX_LINES * better stubbing outside of stubs * fix event data getting swapped multiple times * evil draw vp fix * Stub log imgui This redirects all spammy logs to an imgui window that is cleared per frame. This fixes the serious performance dip of the logging, and makes the regular log readable. * Oops move those optimization changes I accidentally committed behind a flag DUSK_SELECTED_OPT * gx_line macro in map * fix audio stubbing * switch to CARD API aurora impl * remove kabufuda from link libs * refactor imgui stuff and add input viewer * merge stub log with refactor * accidentally committed a metaforce header shh * basic map loader * ImGuiConsole: Add missing <thread> include * you may now play as luigi (you may now load stages with bridges) * bloom fix * bloom leak fix * cloud shadow fix * add soft reset button to imgui menu * if it broke dont not fix it * i swear i committed this * BE swap indMtx in JPAResource::setPTev * wnark ct fix * frsqrte implementation from kinoko * Fix Clang compile error in JAISeq::prepare_getSeqData_ * Add endian conversions to dMsgFlow_c::getInitNodeIndex This fixes a freeze when Fado tries to stop you from leaving the starting area. * Add RAII GXTexObj wrapper; fix almost all leaks * Update aurora for indirect texturing * Update aurora for CARD fix * Fix Clang build * More d_msg_flow endian fixes Fixes softlock when trying to talk to Fado and possibly other NPCs. * no frame limiter * get pause menu working * proper frame limiting * particle pointer size fix * improve map loader a bit --------- Co-authored-by: Jasper St. Pierre <jstpierre@mecheye.net> Co-authored-by: TakaRikka <takarikka@outlook.com> Co-authored-by: CraftyBoss <talibabdulmaalik@gmail.com> Co-authored-by: Luke Street <luke@street.dev> Co-authored-by: Lurs <2795933+Lurs@users.noreply.github.com> Co-authored-by: PJB3005 <pieterjan.briers+git@gmail.com> Co-authored-by: tgsm <doodrabbit@hotmail.com> Co-authored-by: Max Roncace <me@caseif.net> Co-authored-by: Phillip Stephens <antidote.crk@gmail.com>
104 lines
3.1 KiB
C++
104 lines
3.1 KiB
C++
#ifndef DUSK_TIME_H
|
|
#define DUSK_TIME_H
|
|
|
|
#include <chrono>
|
|
#include <numeric>
|
|
#include <array>
|
|
|
|
#ifdef _WIN32
|
|
#ifndef WIN32_LEAN_AND_MEAN
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#endif
|
|
#ifndef NOMINMAX
|
|
#define NOMINMAX
|
|
#endif
|
|
#include <Windows.h>
|
|
#include <shellapi.h>
|
|
#endif
|
|
|
|
#include "dusk/logging.h"
|
|
|
|
constexpr auto DUSK_FRAME_PERIOD = std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::duration<double>(1001.0 / 30000.0));
|
|
|
|
class Limiter {
|
|
using delta_clock = std::chrono::high_resolution_clock;
|
|
using duration_t = std::chrono::nanoseconds;
|
|
|
|
public:
|
|
void Reset() { m_oldTime = delta_clock::now(); }
|
|
|
|
void Sleep(duration_t targetFrameTime) {
|
|
if (targetFrameTime.count() == 0) {
|
|
return;
|
|
}
|
|
|
|
auto start = delta_clock::now();
|
|
duration_t adjustedSleepTime = SleepTime(targetFrameTime);
|
|
if (adjustedSleepTime.count() > 0) {
|
|
NanoSleep(adjustedSleepTime);
|
|
duration_t overslept = TimeSince(start) - adjustedSleepTime;
|
|
if (overslept < duration_t{targetFrameTime}) {
|
|
m_overheadTimes[m_overheadTimeIdx] = overslept;
|
|
m_overheadTimeIdx = (m_overheadTimeIdx + 1) % m_overheadTimes.size();
|
|
}
|
|
}
|
|
Reset();
|
|
}
|
|
|
|
duration_t SleepTime(duration_t targetFrameTime) {
|
|
const auto sleepTime = duration_t{targetFrameTime} - TimeSince(m_oldTime);
|
|
m_overhead = std::accumulate(m_overheadTimes.begin(), m_overheadTimes.end(), duration_t{}) / m_overheadTimes.size();
|
|
if (sleepTime > m_overhead) {
|
|
return sleepTime - m_overhead;
|
|
}
|
|
return duration_t{0};
|
|
}
|
|
|
|
private:
|
|
delta_clock::time_point m_oldTime;
|
|
std::array<duration_t, 4> m_overheadTimes{};
|
|
size_t m_overheadTimeIdx = 0;
|
|
duration_t m_overhead = duration_t{0};
|
|
|
|
duration_t TimeSince(delta_clock::time_point start) {
|
|
return std::chrono::duration_cast<duration_t>(delta_clock::now() - start);
|
|
}
|
|
|
|
#if _WIN32
|
|
void NanoSleep(const duration_t duration) {
|
|
static bool initialized = false;
|
|
static double countPerNs;
|
|
static size_t numSleeps = 0;
|
|
|
|
// QueryPerformanceFrequency's result is constant, but calling it occasionally
|
|
// appears to stabilize QueryPerformanceCounter. Without it, the game drifts
|
|
// from 60hz to 144hz. (Cursed, but I suspect it's NVIDIA/G-SYNC related)
|
|
if (!initialized || numSleeps++ % 1000 == 0) {
|
|
LARGE_INTEGER freq;
|
|
if (QueryPerformanceFrequency(&freq) == 0) {
|
|
DuskLog.warn("QueryPerformanceFrequency failed: {}", GetLastError());
|
|
return;
|
|
}
|
|
countPerNs = static_cast<double>(freq.QuadPart) / 1e9;
|
|
initialized = true;
|
|
numSleeps = 0;
|
|
}
|
|
|
|
LARGE_INTEGER start, current;
|
|
QueryPerformanceCounter(&start);
|
|
LONGLONG ticksToWait = static_cast<LONGLONG>(duration.count() * countPerNs);
|
|
if (DWORD ms = std::chrono::duration_cast<std::chrono::milliseconds>(duration).count(); ms > 1) {
|
|
::Sleep(ms - 1);
|
|
}
|
|
do {
|
|
QueryPerformanceCounter(¤t);
|
|
_mm_pause(); // Yield CPU
|
|
} while (current.QuadPart - start.QuadPart < ticksToWait);
|
|
}
|
|
#else
|
|
void NanoSleep(const duration_t duration) { std::this_thread::sleep_for(duration); }
|
|
#endif
|
|
};
|
|
|
|
#endif
|