commit 5be2ce30d3dd5d0db12d8ddd4e64f7b91f52b7e4
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed May 27 21:12:49 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Wed May 27 21:12:49 2020 -0600
Fix framerate changing when switching carts
---
platform/3ds/source/3dsHost.cpp | 2 +-
platform/switch/source/SwitchHost.cpp | 16 ++++++++--------
source/host.h | 7 +++----
source/main.cpp | 7 +++----
source/vm.cpp | 2 +-
source/vm.h | 4 ++--
6 files changed, 18 insertions(+), 20 deletions(-)
diff --git a/platform/3ds/source/3dsHost.cpp b/platform/3ds/source/3dsHost.cpp
index ecd3717..a6dfc10 100644
--- a/platform/3ds/source/3dsHost.cpp
+++ b/platform/3ds/source/3dsHost.cpp
@@ -180,7 +180,7 @@ void Host::oneTimeCleanup(){
gfxExit();
}
-void Host::setTargetFps(uint8_t targetFps){
+void Host::setTargetFps(int targetFps){
targetFrameTimeMs = 1000.0 / (double)targetFps;
}
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index 6f4f469..13a1112 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -179,7 +179,7 @@ void Host::oneTimeCleanup(){
framebufferClose(&fb);
}
-void Host::setTargetFps(uint8_t targetFps){
+void Host::setTargetFps(int targetFps){
targetFrameTimeMs = 1000.0 / (double)targetFps;
}
@@ -217,25 +217,25 @@ bool Host::shouldQuit() {
return lpressed && rpressed;
}
+const double NANOSECONDS_TO_MILLISECONDS = 1.0 / 1000000.0;
void Host::waitForTargetFps(){
- now_time = svcGetSystemTick();
+ now_time = armGetSystemTick();
frame_time = now_time - last_time;
last_time = now_time;
- //not sure how to do this on switch?
- /*
- double frameTimeMs = frame_time / CPU_TICKS_PER_MSEC;
+ u64 frameTimeNs = armTicksToNs(frame_time);
+ double frameTimeMs = frameTimeNs * NANOSECONDS_TO_MILLISECONDS;
//sleep for remainder of time
if (frameTimeMs < targetFrameTimeMs) {
double msToSleep = targetFrameTimeMs - frameTimeMs;
+ u64 nsToSleep = msToSleep * 1000 * 1000;
- svcSleepThread(msToSleep * 1000 * 1000);
+ svcSleepThread(nsToSleep);
- last_time += CPU_TICKS_PER_MSEC * msToSleep;
+ last_time += armNsToTicks(nsToSleep);
}
- */
}
diff --git a/source/host.h b/source/host.h
index 545be13..571245e 100644
--- a/source/host.h
+++ b/source/host.h
@@ -16,9 +16,9 @@ class Host {
void oneTimeSetup();
- void setTargetFps(uint8_t targetFps);
+ void setTargetFps(int targetFps);
- bool mainLoop();
+ bool mainLoop();
void scanInput();
uint8_t getKeysDown();
@@ -39,7 +39,6 @@ class Host {
void oneTimeCleanup();
-
-
+ double deltaTMs();
};
\ No newline at end of file
diff --git a/source/main.cpp b/source/main.cpp
index a13f69f..9f00a4a 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -29,12 +29,11 @@ int main(int argc, char* argv[])
// Main loop
Logger::Write("Starting main loop\n");
- uint8_t targetFps = vm->GetTargetFps();
- host->setTargetFps(targetFps);
-
-
while (host->mainLoop())
{
+ int targetFps = vm->GetTargetFps();
+ host->setTargetFps(targetFps);
+
host->waitForTargetFps();
host->scanInput();
diff --git a/source/vm.cpp b/source/vm.cpp
index 81629b1..f7f9d39 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -303,7 +303,7 @@ void Vm::QueueCartChange(std::string filename){
_cartChangeQueued = true;
}
-uint8_t Vm::GetTargetFps() {
+int Vm::GetTargetFps() {
return _targetFps;
}
diff --git a/source/vm.h b/source/vm.h
index 55476a0..d6cbff6 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -17,7 +17,7 @@ class Vm {
lua_State* _luaState;
Input* _input;
- uint8_t _targetFps;
+ int _targetFps;
int _picoFrameCount;
bool _hasUpdate;
@@ -50,7 +50,7 @@ class Vm {
void QueueCartChange(std::string newcart);
- uint8_t GetTargetFps();
+ int GetTargetFps();
int GetFrameCount();
};