commit 3fdb7d4ff72c9fbb5ca944e1323ab55a88de0c1a
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Apr 25 13:18:00 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Apr 25 13:18:00 2020 -0600
First pass at detecting and honoring fps
---
source/console.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++----------
source/console.h | 14 ++++++++-
source/main.cpp | 62 ++++++++++++++++++++++++++++++++------
3 files changed, 137 insertions(+), 26 deletions(-)
diff --git a/source/console.cpp b/source/console.cpp
index b7fb479..eb30251 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -31,6 +31,8 @@ Console::Console(){
Logger::Write("Initializing global api\n");
initPicoApi(_graphics, _input);
//initGlobalApi(_graphics);
+
+ _targetFps = 30;
}
Console::~Console(){
@@ -45,6 +47,7 @@ Console::~Console(){
void Console::LoadCart(std::string filename){
Logger::Write("Calling Cart Constructor\n");
Cart *cart = new Cart(filename);
+ _picoFrameCount = 0;
_graphics->setSpriteSheet(cart->SpriteSheetString);
_graphics->setSpriteFlags(cart->SpriteFlagsString);
@@ -91,32 +94,80 @@ void Console::LoadCart(std::string filename){
lua_register(_luaState, "btnp", btnp);
luaL_dostring(_luaState, _loadedCart->LuaString.c_str());
-}
-void Console::UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld){
- _input->SetState(kdown, kheld);
- // Push the fib function on the top of the lua stack
- lua_getglobal(_luaState, "_update");
+ // Push the _init function on the top of the lua stack (or nil if it doesn't exist)
+ lua_getglobal(_luaState, "_init");
- // Push any arguments (the number 13 in this case) on the stack
- //lua_pushnumber(_luaState, 13);
-
- // call the function with 0 argument, returning 0 resulta. Note that the function actually
- // returns 2 results -- we just don't want them
- lua_pcall(_luaState, 0, 0, 0);
-
- // Get the result from the lua stack
- //int result = (int)lua_tointeger(_luaState, -1);
+ if (lua_isfunction(_luaState, -1)) {
+ lua_call(_luaState, 0, 0);
+ }
- // Clean up. If we don't do this last step, we'll leak stack memory.
+ //pop the _init fuction off the stack now that we're done with it
lua_pop(_luaState, 0);
+ //check for update, mark correct target fps
+ lua_getglobal(_luaState, "_update");
+ if (lua_isfunction(_luaState, -1)) {
+ _hasUpdate = true;
+ _targetFps = 30;
+ }
+ lua_pop(_luaState, 0);
+ if (!_hasUpdate){
+ lua_getglobal(_luaState, "_update60");
+ if (lua_isfunction(_luaState, -1)) {
+ _hasUpdate = true;
+ _targetFps = 60;
+ }
+ lua_pop(_luaState, 0);
+ }
lua_getglobal(_luaState, "_draw");
- lua_pcall(_luaState, 0, 0, 0);
+ if (lua_isfunction(_luaState, -1)) {
+ _hasDraw = true;
+ }
lua_pop(_luaState, 0);
+}
+
+//how to call lua from c: https://www.cs.usfca.edu/~galles/cs420/lecture/LuaLectures/LuaAndC.html
+void Console::UpdateAndDraw(
+ uint64_t ticksSinceLastCall,
+ std::function<void()> clearFbFunction,
+ uint8_t kdown,
+ uint8_t kheld)
+{
+ if (clearFbFunction) {
+ clearFbFunction();
+ }
+
+ _input->SetState(kdown, kheld);
+
+ if (_hasUpdate){
+ // Push the _update function on the top of the lua stack
+ if (_targetFps == 60) {
+ lua_getglobal(_luaState, "_update60");
+ } else {
+ lua_getglobal(_luaState, "_update");
+ }
+
+ //we already checked that its a function, so we should be able to call it
+ lua_call(_luaState, 0, 0);
+
+ //pop the update fuction off the stack now that we're done with it
+ lua_pop(_luaState, 0);
+ }
+
+ if (_hasDraw) {
+ lua_getglobal(_luaState, "_draw");
+
+ lua_call(_luaState, 0, 0);
+
+ //pop the update fuction off the stack now that we're done with it
+ lua_pop(_luaState, 0);
+ }
+
+ _picoFrameCount++;
}
void Console::FlipBuffer(uint8_t* fb, std::function<void()> postFlipFunction){
@@ -131,3 +182,7 @@ void Console::TurnOff() {
lua_close(_luaState);
}
+uint8_t Console::GetTargetFps() {
+ return _targetFps;
+}
+
diff --git a/source/console.h b/source/console.h
index 63fba8d..40fab7e 100644
--- a/source/console.h
+++ b/source/console.h
@@ -16,16 +16,28 @@ class Console {
lua_State* _luaState;
Input* _input;
+ uint8_t _targetFps;
+
+ uint16_t _picoFrameCount;
+ bool _hasUpdate;
+ bool _hasDraw;
+
public:
Console();
~Console();
void LoadCart(std::string filename);
- void UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld);
+ void UpdateAndDraw(
+ uint64_t ticksSinceLastCall,
+ std::function<void()> clearFbFunction,
+ uint8_t kdown,
+ uint8_t kheld);
void FlipBuffer(uint8_t* fb, std::function<void()> postFlipFunction);
void TurnOff();
+
+ uint8_t GetTargetFps();
};
diff --git a/source/main.cpp b/source/main.cpp
index 5b0e9a1..4e8edfd 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -55,6 +55,18 @@ uint8_t ConvertInputToP8(u32 input){
return result;
}
+void clear3dsFrameBuffer() {
+ #if _TEST
+ int bgcolor = 255;
+ #else
+ int bgcolor = 0;
+ #endif
+ uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
+ //clear whole top framebuffer
+ memset(fb, bgcolor, 240*400*3);
+
+}
+
//3ds specific helper function
void postFlip3dsFunction() {
gfxFlushBuffers();
@@ -64,7 +76,8 @@ void postFlip3dsFunction() {
int main(int argc, char* argv[])
{
- int frames = 0;
+ u64 last_time = 0, now_time = 0, frame_time = 0;
+
Logger::Initialize();
Logger::Write("created Logger\n");
gfxInitDefault();
@@ -76,7 +89,6 @@ int main(int argc, char* argv[])
//test or not both hardcoded to loading test cart as of now
#if _TEST
- int bgcolor = 255;
consoleInit(GFX_BOTTOM, NULL);
Logger::Write("Loading cart\n");
@@ -85,17 +97,51 @@ int main(int argc, char* argv[])
#else
console.LoadCart("testcart.p8");
- int bgcolor = 0;
#endif
// Main loop
Logger::Write("Starting main loop\n");
+ uint8_t targetFps = console->GetTargetFps();
+ double targetFrametimeMs = 1000.0 / (double)targetFps;
+
+ std::function<void()> clearFb = clear3dsFrameBuffer;
+ std::function<void()> postFlip = postFlip3dsFunction;
+
while (aptMainLoop())
{
//Scan all the inputs. This should be done once for each frame
hidScanInput();
+ now_time = svcGetSystemTick();
+ frame_time = now_time - last_time;
+ last_time = now_time;
+
+ double frameTimeMs = frame_time / CPU_TICKS_PER_MSEC;
+
+ #if _TEST
+ consoleClear();
+ printf("svcGetSystemTick(): %lld \n", now_time);
+ printf("frame time (ticks): %lld \n", frame_time);
+ printf("frame time (ms): %f \n", frameTimeMs);
+ printf("target fps: %d \n", targetFps);
+ printf("target frame time (ms): %f \n", targetFrametimeMs);
+ #endif
+
+ //sleep for remainder of time
+ if (frameTimeMs < targetFrametimeMs) {
+ double msToSleep = targetFrametimeMs - frameTimeMs;
+
+ #if _TEST
+ printf("sleeping for : %f ms\n", msToSleep);
+ #endif
+
+ svcSleepThread(msToSleep * 1000 * 1000);
+
+ last_time += CPU_TICKS_PER_MSEC * msToSleep;
+ }
+
+
//hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
u32 kDown = hidKeysDown();
u32 kHeld = hidKeysHeld();
@@ -110,9 +156,6 @@ int main(int argc, char* argv[])
//_update();
- uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
- //clear whole top framebuffer
- memset(fb, bgcolor, 240*400*3);
//uint8_t* fb_b = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
//clear whole framebuffer
@@ -121,13 +164,14 @@ int main(int argc, char* argv[])
//cart draw
//_draw();
- console->UpdateAndDraw(frames, p8kDown, p8kHeld);
+ console->UpdateAndDraw(frame_time, clear3dsFrameBuffer, p8kDown, p8kHeld);
//send pico 8 screen to framebuffer, then call the function to flush and swap buffers, and wait for vblank
- std::function<void()> postFlip = postFlip3dsFunction;
+
+ uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
+
console->FlipBuffer(fb, postFlip);
- frames++;
}