foxygit / fake-08-xbox360 Log in
commit 39bc683a558c455c71e3e147a0823f18cdaa5e30
Author:     Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Thu Apr 8 17:25:00 2021 -0600
Commit:     Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Thu Apr 8 17:25:00 2021 -0600

    Revert wii u from using common sdl2

    it was only giving a black screen, and I need to turn off audio anyway
    So for now, Wii U doesn't share code with other SDL2 platforms
---
 platform/wiiu/Makefile            |   2 +-
 platform/wiiu/source/WiiUHost.cpp | 281 ++++++++++++++++++++++++++++++++++----
 2 files changed, 256 insertions(+), 27 deletions(-)

diff --git a/platform/wiiu/Makefile b/platform/wiiu/Makefile
index db03111..cdfaa01 100644
--- a/platform/wiiu/Makefile
+++ b/platform/wiiu/Makefile
@@ -22,7 +22,7 @@ PKGCONF		:=	$(PORTLIBS_PATH)/wiiu/bin/powerpc-eabi-pkg-config
 #-------------------------------------------------------------------------------
 TARGET		:=	FAKE-08
 BUILD		:=	build
-SOURCES		:=	${SOURCES} ../SDL2Common/source source
+SOURCES		:=	${SOURCES} source
 INCLUDES	:=	${INCLUDES}
 ROMFS		:=	romfs
 LIBRARIES	:=	SDL2_gfx SDL2_image SDL2_mixer SDL2_ttf sdl2 \
diff --git a/platform/wiiu/source/WiiUHost.cpp b/platform/wiiu/source/WiiUHost.cpp
index ddf2607..6ae0cd7 100644
--- a/platform/wiiu/source/WiiUHost.cpp
+++ b/platform/wiiu/source/WiiUHost.cpp
@@ -22,8 +22,11 @@ using namespace std;
 #include <SDL2/SDL.h>
 #include <SDL2/SDL_events.h>

-#define WINDOW_SIZE_X 1280
-#define WINDOW_SIZE_Y 720
+#define SCREEN_SIZE_X 640
+#define SCREEN_SIZE_Y 640
+
+#define WIN_WIDTH 1280
+#define WIN_HEIGHT 720

 #define JOY_A     0
 #define JOY_B     1
@@ -42,44 +45,198 @@ using namespace std;
 #define JOY_RIGHT 14
 #define JOY_DOWN  15

-#define WINDOW_FLAGS SDL_WINDOW_SHOWN
+#define SAMPLERATE 22050
+#define SAMPLESPERBUF (SAMPLERATE / 30)
+#define NUM_BUFFERS 2
+
+int screenWidth = SCREEN_SIZE_X;
+int screenHeight = SCREEN_SIZE_Y;
+
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+
+StretchOption stretch = PixelPerfectStretch;
+uint32_t last_time;
+uint32_t now_time;
+uint32_t frame_time;
+uint32_t targetFrameTimeMs;
+
+uint8_t currKDown;
+uint8_t currKHeld;
+bool lDown = false;
+bool rDown = false;
+bool stretchKeyPressed = false;

-#define RENDERER_FLAGS SDL_RENDERER_ACCELERATED
-#define PIXEL_FORMAT SDL_PIXELFORMAT_BGRA8888
+Color* _paletteColors;

+Audio* _audio;
+
+SDL_Window* window;
 SDL_Event event;
+SDL_Renderer *renderer;
+SDL_Texture *texture = NULL;
+SDL_Rect DestR;
+SDL_AudioSpec want, have;
+SDL_AudioDeviceID dev;
+int quit = 0;
+void *pixels;
+uint8_t *base;
+int pitch;

-string _desktopSdl2SettingsDir = "fs:/vol/external01/fake08";
-string _desktopSdl2SettingsPrefix = "fs:/vol/external01/fake08/";
-string _desktopSdl2customBiosLua = "sd:/p8carts/\"\n"
-        "pausebtn = \"+\"";


-Host::Host()
+void postFlipFunction(){
+    //flush switch frame buffers
+    // We're done rendering, so we end the frame here.
+
+
+    SDL_UnlockTexture(texture);
+    SDL_RenderCopy(renderer, texture, NULL, &DestR);
+
+    SDL_RenderPresent(renderer);
+}
+
+
+bool audioInitialized = false;
+
+void audioCleanup(){
+    audioInitialized = false;
+
+    //SDL_CloseAudioDevice(dev);
+}
+
+void FillAudioDeviceBuffer(void* UserData, Uint8* DeviceBuffer, int Length)
 {
-    struct stat st = {0};
+    _audio->FillAudioBuffer(DeviceBuffer, 0, Length / 4);
+}
+
+void audioSetup(){
+    //modifed from SDL docs: https://wiki.libsdl.org/SDL_OpenAudioDevice

-    int res = chdir(getenv("fs:/vol/external01/"));
-    if (res == 0 && stat("fake08", &st) == -1) {
-        res = mkdir("fake08", 0777);
+    //Audio plays but is wrong. maybe a problem with sample rate or endian-ness? haven't investigated thoroughly
+/*
+    SDL_memset(&want, 0, sizeof(want)); // or SDL_zero(want)
+    want.freq = SAMPLERATE;
+    want.format = AUDIO_S16SYS;
+    want.channels = 2;
+    want.samples = 4096;
+    want.callback = FillAudioDeviceBuffer;
+
+
+    dev = SDL_OpenAudioDevice(NULL, 0, &want, &have, 0);
+    if (dev == 0) {
+        Logger_Write("Failed to open audio: %s", SDL_GetError());
+    } else {
+        if (have.format != want.format) {
+            Logger_Write("We didn't get requested audio format.");
+        }
+        SDL_PauseAudioDevice(dev, 0); // start audio playing.
+
+        audioInitialized = true;
     }
+    */
+}
+
+Host::Host() { }

-    string cartdatadir =  "fake08/cdata";
-    if (res == 0 && stat(cartdatadir.c_str(), &st) == -1) {
-        res = mkdir(cartdatadir.c_str(), 0777);
+
+void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
+    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
+    {
+        fprintf(stderr, "SDL could not initialize\n");
+        quit = 1;
+        return;
     }

+	window = SDL_CreateWindow(nullptr, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP);
+	if (!window)
+    {
+        quit = 1;
+        return;
+    }
+
+	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
+	if (!renderer)
+    {
+        quit = 1;
+        return;
+    }
+
+	texture  = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_STREAMING, PicoScreenWidth, PicoScreenHeight);
+	if (texture == NULL)
+    {
+		quit = 1;
+        return;
+	}
+
+    atexit(SDL_Quit);
+
+    DestR.x = WIN_WIDTH / 2 - SCREEN_SIZE_X / 2;
+    DestR.y = WIN_HEIGHT / 2 - SCREEN_SIZE_Y / 2;
+    DestR.w = SCREEN_SIZE_X;
+    DestR.h = SCREEN_SIZE_Y;
+
+    _audio = audio;
+    audioSetup();

-    setPlatformParams(
-        WINDOW_SIZE_X,
-        WINDOW_SIZE_Y,
-        WINDOW_FLAGS,
-        RENDERER_FLAGS,
-        PIXEL_FORMAT,
-        _desktopSdl2SettingsPrefix,
-        _desktopSdl2customBiosLua
-    );
+    for (int i = 0; i < SDL_NumJoysticks(); i++) {
+		if (SDL_JoystickOpen(i) == NULL) {
+			printf("Failed to open joystick %d!\n", i);
+			quit = 1;
+		}
+    }
+
+
+    last_time = 0;
+    now_time = 0;
+    frame_time = 0;
+    targetFrameTimeMs = 0;

+    _paletteColors = paletteColors;
+}
+
+void Host::oneTimeCleanup(){
+    audioCleanup();
+
+    SDL_DestroyTexture(texture);
+    SDL_DestroyRenderer(renderer);
+    SDL_DestroyWindow(window);
+    SDL_Quit();
+}
+
+void Host::setTargetFps(int targetFps){
+    targetFrameTimeMs = 1000 / 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(){
@@ -141,6 +298,69 @@ InputState_t Host::scanInput(){

 }

+bool Host::shouldQuit() {
+    return quit > 0;
+}
+
+void Host::waitForTargetFps(){
+    now_time = SDL_GetTicks();
+    frame_time = now_time - last_time;
+	last_time = now_time;
+
+
+	//sleep for remainder of time
+	if (frame_time < targetFrameTimeMs) {
+		uint32_t msToSleep = targetFrameTimeMs - frame_time;
+
+        SDL_Delay(msToSleep);
+
+		last_time += msToSleep;
+	}
+}
+
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap){
+    //clear screen to all black
+    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
+    SDL_RenderClear(renderer);
+
+    SDL_LockTexture(texture, NULL, &pixels, &pitch);
+
+    for (int y = 0; y < PicoScreenHeight; y ++){
+        for (int x = 0; x < PicoScreenWidth; x ++){
+            uint8_t c = getPixelNibble(x, y, picoFb);
+            Color col = _paletteColors[screenPaletteMap[c]];
+
+            base = ((Uint8 *)pixels) + (4 * ( y * PicoScreenHeight + x));
+            base[0] = col.Red;
+            base[1] = col.Green;
+            base[2] = col.Blue;
+            base[3] = col.Alpha;
+        }
+    }
+
+
+    postFlipFunction();
+}
+
+bool Host::shouldFillAudioBuff(){
+    return false;
+}
+
+void* Host::getAudioBufferPointer(){
+    return nullptr;
+}
+
+size_t Host::getAudioBufferSize(){
+    return 0;
+}
+
+void Host::playFilledAudioBuffer(){
+}
+
+bool Host::shouldRunMainLoop(){
+    return !quit;
+}

 vector<string> Host::listcarts(){
     vector<string> carts;
@@ -168,3 +388,12 @@ vector<string> Host::listcarts(){
     return carts;
 }

+const char* Host::logFilePrefix() {
+    return "";
+}
+
+std::string Host::customBiosLua() {
+    return "cartpath = \"sd:/p8carts/\"\n"
+        "pausebtn = \"+\"";
+}
+