foxygit / fake-08-xbox360 Log in
commit 908095209d8c329e7f7813814531936db79c955c
Author:     jtothebell <j.to.the.bell@gmail.com>
AuthorDate: Sun Mar 7 20:01:03 2021 -0700
Commit:     jtothebell <j.to.the.bell@gmail.com>
CommitDate: Thu Mar 11 19:22:05 2021 -0700

    Add screen stretching to sdl2 platforms

    TODO: reduce code duplication
    untested for now on switch and wii u
---
 platform/SDL2/source/SDL2Host.cpp     | 61 ++++++++++++++++++++++++++++-------
 platform/switch/source/SwitchHost.cpp | 34 ++++++++++++++++++-
 platform/vita/source/VitaHost.cpp     | 37 ++++++++++++++++++---
 platform/wiiu/source/WiiUHost.cpp     | 36 ++++++++++++++++++---
 source/host.h                         |  5 +--
 5 files changed, 150 insertions(+), 23 deletions(-)

diff --git a/platform/SDL2/source/SDL2Host.cpp b/platform/SDL2/source/SDL2Host.cpp
index 2d1ea00..107e7f4 100644
--- a/platform/SDL2/source/SDL2Host.cpp
+++ b/platform/SDL2/source/SDL2Host.cpp
@@ -16,22 +16,24 @@ using namespace std;
 // sdl
 #include <SDL2/SDL.h>

-#define SCREEN_SIZE_X 512
-#define SCREEN_SIZE_Y 512
+#define WINDOW_SIZE_X 1280
+#define WINDOW_SIZE_Y 720
+
+#define SCREEN_SIZE_X 640
+#define SCREEN_SIZE_Y 640


 #define SAMPLERATE 22050
 #define SAMPLESPERBUF (SAMPLERATE / 30)
 #define NUM_BUFFERS 2

-const int __screenWidth = SCREEN_SIZE_X;
-const int __screenHeight = SCREEN_SIZE_Y;
+int screenWidth = SCREEN_SIZE_X;
+int screenHeight = SCREEN_SIZE_Y;

 const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;

-
-StretchOption stretch;
+StretchOption stretch = PixelPerfectStretch;
 uint32_t last_time;
 uint32_t now_time;
 uint32_t frame_time;
@@ -39,6 +41,7 @@ uint32_t targetFrameTimeMs;

 uint8_t currKDown;
 uint8_t currKHeld;
+bool stretchKeyPressed = false;

 Color* _paletteColors;
 Audio* _audio;
@@ -54,21 +57,19 @@ void *pixels;
 uint8_t *base;
 int pitch;

+SDL_Rect DestR;
+
 bool audioInitialized = false;


 void postFlipFunction(){
     // We're done rendering, so we end the frame here.
     SDL_UnlockTexture(texture);
-    SDL_RenderCopy(renderer, texture, NULL, NULL);
+    SDL_RenderCopy(renderer, texture, NULL, &DestR);

     SDL_RenderPresent(renderer);
 }

-
-
-
-
 void audioCleanup(){
     audioInitialized = false;

@@ -115,7 +116,7 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
         return;
     }

-    SDL_CreateWindowAndRenderer(SCREEN_SIZE_X, SCREEN_SIZE_Y, 0, &window, &renderer);
+    SDL_CreateWindowAndRenderer(WINDOW_SIZE_X, WINDOW_SIZE_Y, 0, &window, &renderer);
     if (!window)
     {
         fprintf(stderr, "Error creating window.\n");
@@ -131,6 +132,11 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){

     _audio = audio;
     audioSetup();
+
+    DestR.x = WINDOW_SIZE_X / 2 - SCREEN_SIZE_X / 2;
+    DestR.y = WINDOW_SIZE_Y / 2 - SCREEN_SIZE_Y / 2;
+    DestR.w = SCREEN_SIZE_X;
+    DestR.h = SCREEN_SIZE_Y;

     last_time = 0;
     now_time = 0;
@@ -143,6 +149,7 @@ void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
 void Host::oneTimeCleanup(){
     audioCleanup();

+    SDL_DestroyTexture(texture);
     SDL_DestroyRenderer(renderer);
     SDL_DestroyWindow(window);
     SDL_Quit();
@@ -153,10 +160,39 @@ void Host::setTargetFps(int targetFps){
 }

 void Host::changeStretch(){
+    if (stretchKeyPressed) {
+        if (stretch == PixelPerfectStretch) {
+            stretch = PixelPerfect;
+            screenWidth = PicoScreenWidth;
+            screenHeight = PicoScreenHeight;
+        }
+        else if (stretch == PixelPerfect) {
+            stretch = StretchToFit;
+            screenWidth = WINDOW_SIZE_Y;
+            screenHeight = WINDOW_SIZE_Y;
+        }
+        else if (stretch == StretchToFit) {
+            stretch = StretchToFill;
+            screenWidth = WINDOW_SIZE_X;
+            screenHeight = WINDOW_SIZE_Y;
+        }
+        else if (stretch == StretchToFill) {
+            stretch = PixelPerfectStretch;
+            screenWidth = SCREEN_SIZE_X;
+            screenHeight = SCREEN_SIZE_Y;
+        }
+
+        DestR.x = WINDOW_SIZE_X / 2 - screenWidth / 2;
+        DestR.y = WINDOW_SIZE_Y / 2 - screenHeight / 2;
+        DestR.w = screenWidth;
+        DestR.h = screenHeight;
+    }
 }
+
 InputState_t Host::scanInput(){
     currKDown = 0;
     currKHeld = 0;
+    stretchKeyPressed = false;

     while (SDL_PollEvent(&event)) {
         switch (event.type) {
@@ -171,6 +207,7 @@ InputState_t Host::scanInput(){
                     case SDLK_z:     currKDown |= P8_KEY_X; break;
                     case SDLK_x:     currKDown |= P8_KEY_O; break;
                     case SDLK_c:     currKDown |= P8_KEY_X; break;
+                    case SDLK_r:     stretchKeyPressed = true; break;
                 }
                 break;
             case SDL_QUIT:
diff --git a/platform/switch/source/SwitchHost.cpp b/platform/switch/source/SwitchHost.cpp
index 3405fb7..311dd2f 100644
--- a/platform/switch/source/SwitchHost.cpp
+++ b/platform/switch/source/SwitchHost.cpp
@@ -48,10 +48,15 @@ uint8_t currKDown;
 uint8_t currKHeld;
 bool lDown = false;
 bool rDown = false;
+bool stretchKeyPressed = false;

 const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;

+int screenWidth = SCREEN_SIZE_X;
+int screenHeight = SCREEN_SIZE_Y;
+
+StretchOption stretch = PixelPerfectStretch;
 u64 last_time;
 u64 now_time;
 u64 frame_time;
@@ -195,12 +200,39 @@ void Host::setTargetFps(int targetFps){
 }

 void Host::changeStretch(){
+    if (stretchKeyPressed) {
+        if (stretch == PixelPerfectStretch) {
+            stretch = PixelPerfect;
+            screenWidth = PicoScreenWidth;
+            screenHeight = PicoScreenHeight;
+        }
+        else if (stretch == PixelPerfect) {
+            stretch = StretchToFit;
+            screenWidth = WIN_HEIGHT;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFit) {
+            stretch = StretchToFill;
+            screenWidth = WIN_WIDTH;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFill) {
+            stretch = PixelPerfectStretch;
+            screenWidth = SCREEN_SIZE_X;
+            screenHeight = SCREEN_SIZE_Y;
+        }

+        DestR.x = WIN_WIDTH / 2 - screenWidth / 2;
+        DestR.y = WIN_HEIGHT / 2 - screenHeight / 2;
+        DestR.w = screenWidth;
+        DestR.h = screenHeight;
+    }
 }

 InputState_t Host::scanInput(){
     currKDown = 0;
     uint8_t kUp = 0;
+    stretchKeyPressed = false;

     while (SDL_PollEvent(&event)) {
         switch (event.type) {
@@ -216,7 +248,7 @@ InputState_t Host::scanInput(){
                     case JOY_B:     currKDown |= P8_KEY_O; break;

                     case JOY_L: lDown = true; break;
-                    case JOY_R: rDown = true; break;
+                    case JOY_R: rDown = true; stretchKeyPressed = true; break;
                 }
                 break;

diff --git a/platform/vita/source/VitaHost.cpp b/platform/vita/source/VitaHost.cpp
index ec816b3..85be669 100644
--- a/platform/vita/source/VitaHost.cpp
+++ b/platform/vita/source/VitaHost.cpp
@@ -55,14 +55,14 @@ using namespace std;
 #define SDLK_VITA_SELECT 10
 #define SDLK_VITA_START 11

-const int __screenWidth = SCREEN_SIZE_X;
-const int __screenHeight = SCREEN_SIZE_Y;
+int screenWidth = SCREEN_SIZE_X;
+int screenHeight = SCREEN_SIZE_Y;

 const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;


-StretchOption stretch;
+StretchOption stretch = PixelPerfectStretch;
 uint32_t last_time;
 uint32_t now_time;
 uint32_t frame_time;
@@ -72,6 +72,7 @@ uint8_t currKDown;
 uint8_t currKHeld;
 bool lDown = false;
 bool rDown = false;
+bool stretchKeyPressed = false;

 Color* _paletteColors;

@@ -215,6 +216,33 @@ void Host::setTargetFps(int targetFps){
 }

 void Host::changeStretch(){
+    if (stretchKeyPressed) {
+        if (stretch == PixelPerfectStretch) {
+            stretch = PixelPerfect;
+            screenWidth = PicoScreenWidth;
+            screenHeight = PicoScreenHeight;
+        }
+        else if (stretch == PixelPerfect) {
+            stretch = StretchToFit;
+            screenWidth = WIN_HEIGHT;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFit) {
+            stretch = StretchToFill;
+            screenWidth = WIN_WIDTH;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFill) {
+            stretch = PixelPerfectStretch;
+            screenWidth = SCREEN_SIZE_X;
+            screenHeight = SCREEN_SIZE_Y;
+        }
+
+        DestR.x = WIN_WIDTH / 2 - screenWidth / 2;
+        DestR.y = WIN_HEIGHT / 2 - screenHeight / 2;
+        DestR.w = screenWidth;
+        DestR.h = screenHeight;
+    }
 }

 InputState_t Host::scanInput(){
@@ -222,6 +250,7 @@ InputState_t Host::scanInput(){
     uint8_t kUp = 0;
     int prevJxDir = jxDir;
     int prevJyDir = jyDir;
+    stretchKeyPressed = false;

     while (SDL_PollEvent(&event)) {
         switch (event.type) {
@@ -236,7 +265,7 @@ InputState_t Host::scanInput(){
                     case SDLK_VITA_CROSS:     currKDown |= P8_KEY_X; break;
                     case SDLK_VITA_CIRCLE:     currKDown |= P8_KEY_O; break;
                     case SDLK_VITA_LTRIGGER: lDown = true; break;
-                    case SDLK_VITA_RTRIGGER: rDown = true; break;
+                    case SDLK_VITA_RTRIGGER: rDown = true; stretchKeyPressed = true; break;
                 }
                 break;

diff --git a/platform/wiiu/source/WiiUHost.cpp b/platform/wiiu/source/WiiUHost.cpp
index 4894191..2e5a6a2 100644
--- a/platform/wiiu/source/WiiUHost.cpp
+++ b/platform/wiiu/source/WiiUHost.cpp
@@ -49,14 +49,14 @@ using namespace std;
 #define SAMPLESPERBUF (SAMPLERATE / 30)
 #define NUM_BUFFERS 2

-const int __screenWidth = SCREEN_SIZE_X;
-const int __screenHeight = SCREEN_SIZE_Y;
+int screenWidth = SCREEN_SIZE_X;
+int screenHeight = SCREEN_SIZE_Y;

 const int PicoScreenWidth = 128;
 const int PicoScreenHeight = 128;


-StretchOption stretch;
+StretchOption stretch = PixelPerfectStretch;
 uint32_t last_time;
 uint32_t now_time;
 uint32_t frame_time;
@@ -66,6 +66,7 @@ uint8_t currKDown;
 uint8_t currKHeld;
 bool lDown = false;
 bool rDown = false;
+bool stretchKeyPressed = false;

 Color* _paletteColors;

@@ -209,6 +210,33 @@ void Host::setTargetFps(int targetFps){
 }

 void Host::changeStretch(){
+    if (stretchKeyPressed) {
+        if (stretch == PixelPerfectStretch) {
+            stretch = PixelPerfect;
+            screenWidth = PicoScreenWidth;
+            screenHeight = PicoScreenHeight;
+        }
+        else if (stretch == PixelPerfect) {
+            stretch = StretchToFit;
+            screenWidth = WIN_HEIGHT;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFit) {
+            stretch = StretchToFill;
+            screenWidth = WIN_WIDTH;
+            screenHeight = WIN_HEIGHT;
+        }
+        else if (stretch == StretchToFill) {
+            stretch = PixelPerfectStretch;
+            screenWidth = SCREEN_SIZE_X;
+            screenHeight = SCREEN_SIZE_Y;
+        }
+
+        DestR.x = WIN_WIDTH / 2 - screenWidth / 2;
+        DestR.y = WIN_HEIGHT / 2 - screenHeight / 2;
+        DestR.w = screenWidth;
+        DestR.h = screenHeight;
+    }
 }

 InputState_t Host::scanInput(){
@@ -229,7 +257,7 @@ InputState_t Host::scanInput(){
                     case JOY_B:     currKDown |= P8_KEY_O; break;

                     case JOY_L: lDown = true; break;
-                    case JOY_R: rDown = true; break;
+                    case JOY_R: rDown = true; stretchKeyPressed = true; break;
                 }
                 break;

diff --git a/source/host.h b/source/host.h
index 2948643..3519d45 100644
--- a/source/host.h
+++ b/source/host.h
@@ -10,6 +10,7 @@ enum StretchOption {
   PixelPerfect,
   PixelPerfectStretch,
   StretchToFit,
+  StretchToFill,
   StretchAndOverflow
 };

@@ -18,7 +19,7 @@ class Host {
     Host();

     void oneTimeSetup(Color* paletteColors, Audio* audio);
-
+
     void setTargetFps(int targetFps);

     bool shouldRunMainLoop();
@@ -46,4 +47,4 @@ class Host {
     const char* logFilePrefix();

     std::string customBiosLua();
-};
\ No newline at end of file
+};