foxygit / fake-08-xbox360 Log in
commit b05d6822efbf202c4793155c163a87a684996561
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sun Apr 26 12:23:20 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sun Apr 26 12:23:20 2020 -0600

    Center pico 8 screen (and slight clean up)
---
 source/console.cpp  |  4 ++--
 source/console.h    |  2 +-
 source/graphics.cpp | 35 +++++++++++++++++------------------
 source/graphics.h   |  2 +-
 source/main.cpp     |  7 +++++--
 5 files changed, 26 insertions(+), 24 deletions(-)

diff --git a/source/console.cpp b/source/console.cpp
index d9dcfaf..0534838 100644
--- a/source/console.cpp
+++ b/source/console.cpp
@@ -197,8 +197,8 @@ void Console::UpdateAndDraw(
     _picoFrameCount++;
 }

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

     if (postFlipFunction) {
         postFlipFunction();
diff --git a/source/console.h b/source/console.h
index 3994c78..b9c8018 100644
--- a/source/console.h
+++ b/source/console.h
@@ -34,7 +34,7 @@ class Console {
       uint8_t kdown,
       uint8_t kheld);

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

     void TurnOff();

diff --git a/source/graphics.cpp b/source/graphics.cpp
index a1dcf19..68a7568 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -3,6 +3,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <string>
+#include <algorithm>

 #include "graphics.h"

@@ -10,6 +11,9 @@

 #include "logger.h"

+const uint8_t PicoScreenWidth = 128;
+const uint8_t PicoScreenHeight = 128;
+
 const Color BgGray = BG_GRAY_COLOR;

 const Color PaletteColors[] = {
@@ -31,16 +35,6 @@ const Color PaletteColors[] = {
 	COLOR_15
 };

-//not included in 3ds std? copied and pasted here for ease
-template<typename _Tp>
-    constexpr const _Tp&
-    clamp(const _Tp& __val, const _Tp& __lo, const _Tp& __hi)
-    {
-      __glibcxx_assert(!(__hi < __lo));
-      return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val;
-    }
-
-
 //call initialize to make sure defaults are correct
 Graphics::Graphics(std::string fontdata) {
 	this->_gfxState_color = 7;
@@ -583,11 +577,11 @@ void Graphics::clip() {
 void Graphics::clip(short x, short y, short w, short h) {
 	short xe = x + w;
 	short ye = y + h;
-	_gfxState_clip_xb = clamp(x, (short)0, (short)127);
-	_gfxState_clip_yb = clamp(y, (short)0, (short)127);
+	_gfxState_clip_xb = std::clamp(x, (short)0, (short)127);
+	_gfxState_clip_yb = std::clamp(y, (short)0, (short)127);

-	_gfxState_clip_xe = clamp(xe, (short)0, (short)127);
-	_gfxState_clip_ye = clamp(ye, (short)0, (short)127);
+	_gfxState_clip_xe = std::clamp(xe, (short)0, (short)127);
+	_gfxState_clip_ye = std::clamp(ye, (short)0, (short)127);
 }


@@ -658,17 +652,22 @@ void Graphics::palt(uint8_t c, bool t){
 	}
 }

-void Graphics::flipBuffer(uint8_t* fb) {
+void Graphics::flipBuffer(uint8_t* fb, int width, int height) {
 	short x, y;
+
+	short xOffset = width / 2 - PicoScreenWidth / 2;
+	short yOffset = height / 2 - PicoScreenHeight / 2;
 	//todo: test if it is faster to convert colors to uint24_ts and write one instead of 3 (assuming these are )
     for(x = 0; x < 128; x++) {
     	for(y = 0; y < 128; y++) {
 			uint8_t c = _pico8_fb[x*128 + y];
 			Color col = PaletteColors[_gfxState_screenPaletteMap[c]];

-			fb[((x*240)+ (239 - y))*3+0] = col.Blue;
-			fb[((x*240)+ (239 - y))*3+1] = col.Green;
-			fb[((x*240)+ (239 - y))*3+2] = col.Red;
+			int pixIdx = (((x + xOffset)*240)+ (239 - (y + yOffset)))*3;
+
+			fb[pixIdx + 0] = col.Blue;
+			fb[pixIdx + 1] = col.Green;
+			fb[pixIdx + 2] = col.Red;
     	}
     }
 }
diff --git a/source/graphics.h b/source/graphics.h
index d285f5e..999f111 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -187,7 +187,7 @@ class Graphics {
 	void cursor(short x, short y, uint8_t col);


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

 };

diff --git a/source/main.cpp b/source/main.cpp
index 4e8edfd..f1e72c9 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -17,6 +17,9 @@
 #include "tests/test_base.h"


+const int ScreenWidth = 400;
+const int ScreenHeight = 240;
+
 //3ds specific helper function
 uint8_t ConvertInputToP8(u32 input){
 	uint8_t result = 0;
@@ -63,7 +66,7 @@ void clear3dsFrameBuffer() {
 	#endif
 	uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
 	//clear whole top framebuffer
-	memset(fb, bgcolor, 240*400*3);
+	memset(fb, bgcolor, ScreenHeight*ScreenWidth*3);

 }

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

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

-		console->FlipBuffer(fb, postFlip);
+		console->FlipBuffer(fb, ScreenWidth, ScreenHeight, postFlip);

 	}