foxygit / fake-08-xbox360 Log in
commit 2486b777a10e429ef7c95eeddef047acd12fddc4
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Mon Apr 20 21:40:11 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Mon Apr 20 21:40:11 2020 -0600

    pass function to console to flush, swap, and wait for v blank

    this should make it easier to implement on other platforms in the future
---
 source/console.cpp |  7 ++++++-
 source/console.h   |  3 ++-
 source/main.cpp    | 17 +++++++++--------
 3 files changed, 17 insertions(+), 10 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index e76d498..49536b6 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -1,4 +1,5 @@
 #include <string>
+#include <functional>

 #include "console.h"
 #include "graphics.h"
@@ -98,8 +99,12 @@ void Console::UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld){

 }

-void Console::FlipBuffer(uint8_t* fb){
+void Console::FlipBuffer(uint8_t* fb, std::function<void()> postFlipFunction){
     _graphics->flipBuffer(fb);
+
+    if (postFlipFunction) {
+        postFlipFunction();
+    }
 }

 void Console::TurnOff() {
diff --git a/source/console.h b/source/console.h
index e071cfd..63fba8d 100644
--- a/source/console.h
+++ b/source/console.h
@@ -1,4 +1,5 @@
 #pragma once
+#include <functional>

 #include "cart.h"
 #include "Input.h"
@@ -23,7 +24,7 @@ class Console {

     void UpdateAndDraw(int frameCount, uint8_t kdown, uint8_t kheld);

-    void FlipBuffer(uint8_t* fb);
+    void FlipBuffer(uint8_t* fb, std::function<void()> postFlipFunction);

     void TurnOff();
 };
diff --git a/source/main.cpp b/source/main.cpp
index af1f893..360091b 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -53,6 +53,12 @@ uint8_t ConvertInputToP8(u32 input){
 	return result;
 }

+void postFlip3dsFunction() {
+	gfxFlushBuffers();
+	gfxSwapBuffers();
+	gspWaitForVBlank();
+}
+
 int main(int argc, char* argv[])
 {
 	int frames = 0;
@@ -114,14 +120,9 @@ int main(int argc, char* argv[])

 		console->UpdateAndDraw(frames, p8kDown, p8kHeld);

-		//send pico 8 screen to framebuffer
-		console->FlipBuffer(fb);
-
-		// Flush and swap framebuffers
-		//possibly pass this as a function to flip buffer to support more platforms?
-		gfxFlushBuffers();
-		gfxSwapBuffers();
-		gspWaitForVBlank();
+		//send pico 8 screen to framebuffer, then call the function to flush and swap buffers, and wait for vblank
+		std::function<void()> postFlip = postFlip3dsFunction;
+		console->FlipBuffer(fb, postFlip);

     	frames++;
 	}