foxygit / fake-08-xbox360 Log in
commit 4510fe33ca8c61d416d4fd13985b181233fe6773
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed Apr 29 07:50:53 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Wed Apr 29 07:50:53 2020 -0600

    Implement screen stretching
---
 source/console.cpp  | 21 ++++++++++++++++++++-
 source/console.h    | 16 +++++++++++++++-
 source/graphics.cpp | 26 ++++++++++++++++++++++++++
 source/graphics.h   |  2 ++
 source/main.cpp     | 30 +++++++++++++++++++++++++++++-
 5 files changed, 92 insertions(+), 3 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index 67e78b3..4d4af11 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -214,7 +214,26 @@ void Console::UpdateAndDraw(
     _picoFrameCount++;
 }

-void Console::FlipBuffer(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction){
+void Console::FlipBuffer_PP(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction){
+    _graphics->flipBuffer(fb, width, height);
+
+    if (postFlipFunction) {
+        postFlipFunction();
+    }
+}
+
+void Console::FlipBuffer_STF(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction){
+    _graphics->flipBuffer_STF(fb, width, height);
+
+    if (postFlipFunction) {
+        postFlipFunction();
+    }
+}
+
+void Console::FlipBuffer_SAO(
+    uint8_t* fb, int width, int height,
+    uint8_t* fb_o, int width_o, int height_o,
+    std::function<void()> postFlipFunction){
     _graphics->flipBuffer(fb, width, height);

     if (postFlipFunction) {
diff --git a/source/console.h b/source/console.h
index b9c8018..df0722c 100644
--- a/source/console.h
+++ b/source/console.h
@@ -10,6 +10,12 @@ extern "C" {
   #include <lauxlib.h>
 }

+enum StretchOption {
+  PixelPerfect,
+  StretchToFit,
+  StretchAndOverflow
+};
+
 class Console {
     Cart* _loadedCart;
     Graphics* _graphics;
@@ -34,7 +40,15 @@ class Console {
       uint8_t kdown,
       uint8_t kheld);

-    void FlipBuffer(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction);
+    void FlipBuffer_PP(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction);
+
+    void FlipBuffer_STF(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction);
+
+    void FlipBuffer_SAO(
+      uint8_t* fb, int width, int height,
+      uint8_t* fb_o, int width_o, int height_o,
+      std::function<void()> postFlipFunction);
+

     void TurnOff();

diff --git a/source/graphics.cpp b/source/graphics.cpp
index 4daade0..0038d15 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -835,6 +835,32 @@ void Graphics::flipBuffer(uint8_t* fb, int width, int height) {
     }
 }

+void Graphics::flipBuffer_STF(uint8_t* fb, int width, int height) {
+	short x, y;
+
+	//assume landscape
+	double ratio = (double)height / (double)PicoScreenHeight;
+	int stretchedWidth = PicoScreenWidth * ratio;
+
+	short xOffset = width / 2 - stretchedWidth / 2;
+	short yOffset = 0;
+
+    for(x = 0; x < stretchedWidth; x++) {
+    	for(y = 0; y < height; y++) {
+			int picoX = (int)(x / ratio);
+			int picoY = (int)(y / ratio);
+			uint8_t c = _pico8_fb[picoX*128 + picoY];
+			Color col = PaletteColors[_gfxState_screenPaletteMap[c]];
+
+			int pixIdx = (((x + xOffset)*height)+ ((height - 1) - (y + yOffset)))*3;
+
+			fb[pixIdx + 0] = col.Blue;
+			fb[pixIdx + 1] = col.Green;
+			fb[pixIdx + 2] = col.Red;
+    	}
+    }
+}
+
 void Graphics::cursor() {
 	this->cursor(0, 0);
 }
diff --git a/source/graphics.h b/source/graphics.h
index c192b2e..144321c 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -195,5 +195,7 @@ class Graphics {

 	void flipBuffer(uint8_t* fb, int width, int height);

+	void flipBuffer_STF(uint8_t* fb, int width, int height);
+
 };

diff --git a/source/main.cpp b/source/main.cpp
index 3730c53..f426cf8 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -20,6 +20,17 @@
 const int ScreenWidth = 400;
 const int ScreenHeight = 240;

+StretchOption stretch = PixelPerfect;
+
+void ChangeStretch() {
+	if (stretch == PixelPerfect) {
+		stretch = StretchToFit;
+	}
+	else if (stretch == StretchToFit) {
+		stretch = PixelPerfect;
+	}
+}
+
 //3ds specific helper function
 uint8_t ConvertInputToP8(u32 input){
 	uint8_t result = 0;
@@ -111,6 +122,8 @@ int main(int argc, char* argv[])
 	std::function<void()> clearFb = clear3dsFrameBuffer;
 	std::function<void()> postFlip = postFlip3dsFunction;

+	bool rWasDown = false;
+
 	while (aptMainLoop())
 	{
 		//Scan all the inputs. This should be done once for each frame
@@ -154,6 +167,16 @@ int main(int argc, char* argv[])

 		if (lpressed && rpressed) break; // break in order to return to hbmenu

+		if (rWasDown && !rpressed) {
+			ChangeStretch();
+		}
+
+		if (rpressed) {
+			rWasDown = true;
+		} else {
+			rWasDown = false;
+		}
+
 		uint8_t p8kDown = ConvertInputToP8(kDown);
 		uint8_t p8kHeld = ConvertInputToP8(kHeld);

@@ -173,7 +196,12 @@ int main(int argc, char* argv[])

 		uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);

-		console->FlipBuffer(fb, ScreenWidth, ScreenHeight, postFlip);
+		if (stretch == PixelPerfect) {
+			console->FlipBuffer_PP(fb, ScreenWidth, ScreenHeight, postFlip);
+		}
+		else if (stretch == StretchToFit) {
+			console->FlipBuffer_STF(fb, ScreenWidth, ScreenHeight, postFlip);
+		}

 	}