commit ac3e205d9212e10047666b8d24c8f92043a3bb62
Author: gameblabla <gameblabla@protonmail.com>
AuthorDate: Fri Sep 3 01:38:58 2021 +0200
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Fri Mar 18 20:21:18 2022 -0600
GCW0 & Funkey port (Rebase against latest code changes)
---
Makefile | 14 +-
package | 35 ++++
platform/funkey-s/Makefile | 119 +++++++++++
platform/funkey-s/source/FunkeyHost.cpp | 338 ++++++++++++++++++++++++++++++
platform/gcw0/Makefile | 119 +++++++++++
platform/gcw0/source/ODHost.cpp | 351 ++++++++++++++++++++++++++++++++
6 files changed, 975 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
index d8807ea..9246b34 100644
--- a/Makefile
+++ b/Makefile
@@ -41,6 +41,12 @@ clean-sdl:
clean-bittboy:
@$(MAKE) -C platform/bittboy clean
+
+clean-funkey:
+ @$(MAKE) -C platform/funkey-s clean
+
+clean-gcw0:
+ @$(MAKE) -C platform/gcw0 clean
clean-miyoomini:
@$(MAKE) -C platform/miyoomini clean
@@ -66,8 +72,14 @@ sdl2:
sdl:
@$(MAKE) -C platform/SDL1_2
+funkey:
+ @$(MAKE) -C platform/funkey-s
+
bittboy:
@$(MAKE) -C platform/bittboy
+
+gcw0:
+ @$(MAKE) -C platform/gcw0
miyoomini:
@$(MAKE) -C platform/miyoomini
@@ -76,4 +88,4 @@ clean-tests:
@$(MAKE) -C test clean
tests:
- @$(MAKE) -C test
\ No newline at end of file
+ @$(MAKE) -C test
diff --git a/package b/package
new file mode 100755
index 0000000..3e84a16
--- /dev/null
+++ b/package
@@ -0,0 +1,35 @@
+#!/bin/sh
+
+mkdir -p opk
+cp ./icon.png opk/icon.png
+cp ./platform/"$1"/FAKE08 opk/FAKE08
+
+# https://unix.stackexchange.com/questions/219268/how-to-add-new-lines-when-using-echo
+print()
+ case ${IFS- } in
+ (\ *) printf %b\\n "$*";;
+ (*) IFS=\ $IFS
+ printf %b\\n "$*"
+ IFS=${IFS#?}
+esac
+
+# Create GmenuNx entry file plus other things
+
+print '[Desktop Entry]
+Type=Application
+Name=FAKE08
+Comment=PICO-8 Emulator (port gameblabla)
+Exec=FAKE08 %f
+Icon=icon
+Terminal=false
+Type=Application
+Categories=emulators;
+X-OD-NeedsDownscaling=true
+X-OD-Filter=.png,.p8
+selectorbrowser=true
+SelectorFilter=p8,P8,png,PNG
+' > opk/default."$1".desktop
+
+mksquashfs ./opk fake08_"$1".opk -all-root -noappend -no-exports -no-xattrs
+
+rm -r opk
diff --git a/platform/funkey-s/Makefile b/platform/funkey-s/Makefile
new file mode 100644
index 0000000..2df4e9f
--- /dev/null
+++ b/platform/funkey-s/Makefile
@@ -0,0 +1,119 @@
+
+#---------------------------------------------------------------------------------
+# TARGET is the name of the output
+# BUILD is the directory where object files & intermediate files will be placed
+# SOURCES is a list of directories containing source code
+# INCLUDES is a list of directories containing header files
+#
+#---------------------------------------------------------------------------------
+TARGET := FAKE08
+BUILD := build
+SOURCES := ${SOURCES} ../funkey-s/source source
+INCLUDES := ${INCLUDES}
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+CC = /opt/funkey-toolchain-shared/bin/arm-linux-g++
+CXX = /opt/funkey-toolchain-shared/bin/arm-linux-g++
+
+CFLAGS := -Wall -Ofast -fno-PIC -ffunction-sections -flto -DVER_STR=\"$(APP_VERSION)\" \
+ $(DEFINES)
+
+CFLAGS += $(INCLUDE)
+
+CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++17
+
+LIBS := $(CFLAGS) -lc -lgcc -lm -lSDL -lasound -Wl,--as-needed,--gc-sections -s -no-pie
+
+LDFLAGS := $(LIBS)
+
+
+#---------------------------------------------------------------------------------
+# no real need to edit anything past this point unless you need to add additional
+# rules for different file extensions
+#---------------------------------------------------------------------------------
+ifneq ($(BUILD),$(notdir $(CURDIR)))
+#---------------------------------------------------------------------------------
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+export TOPDIR := $(CURDIR)
+
+export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
+ $(foreach dir,$(DATA),$(CURDIR)/$(dir))
+
+export DEPSDIR := $(CURDIR)/$(BUILD)
+
+CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
+CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
+
+#---------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+#---------------------------------------------------------------------------------
+ export LD := $(CC)
+#---------------------------------------------------------------------------------
+else
+#---------------------------------------------------------------------------------
+ export LD := $(CXX)
+#---------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------
+
+export OFILES_BIN := $(addsuffix .o,$(BINFILES))
+export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
+export OFILES := $(OFILES_BIN) $(OFILES_SRC)
+export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
+
+export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I$(CURDIR)/$(BUILD)
+
+export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
+
+
+.PHONY: $(BUILD) clean all
+
+#---------------------------------------------------------------------------------
+all: $(BUILD)
+
+$(BUILD):
+ @[ -d $@ ] || mkdir -p $@
+ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr $(BUILD) $(TARGET)
+
+
+#---------------------------------------------------------------------------------
+else
+.PHONY: all
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+all : $(OUTPUT)
+
+$(OUTPUT) : $(OFILES)
+ $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
+
+$(OFILES_SRC) : $(HFILES_BIN)
+
+#---------------------------------------------------------------------------------
+# you need a rule like this for each extension you use as binary data
+#---------------------------------------------------------------------------------
+%.bin.o %_bin.h : %.bin
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ @$(bin2o)
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------------
diff --git a/platform/funkey-s/source/FunkeyHost.cpp b/platform/funkey-s/source/FunkeyHost.cpp
new file mode 100644
index 0000000..618943d
--- /dev/null
+++ b/platform/funkey-s/source/FunkeyHost.cpp
@@ -0,0 +1,338 @@
+
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include <fstream>
+#include <iostream>
+using namespace std;
+
+#include "../../../source/host.h"
+#include "../../../source/hostVmShared.h"
+#include "../../../source/nibblehelpers.h"
+#include "../../../source/logger.h"
+
+// sdl
+#include <SDL/SDL.h>
+
+#define SCREEN_BPP 16
+
+#define SAMPLERATE 22050
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+
+StretchOption stretch;
+uint32_t last_time;
+uint32_t now_time;
+uint32_t frame_time;
+uint32_t targetFrameTimeMs;
+
+uint8_t currKDown;
+uint8_t currKHeld;
+
+Color* _paletteColors;
+Audio* _audio;
+
+SDL_Event event;
+SDL_Surface *window;
+SDL_Surface *texture;
+SDL_bool done = SDL_FALSE;
+SDL_AudioSpec want, have;
+void *pixels;
+uint16_t *base;
+int pitch;
+
+bool audioInitialized = false;
+
+uint16_t _mapped16BitColors[144];
+
+
+void postFlipFunction(){
+ // We're done rendering, so we end the frame here.
+
+ SDL_SoftStretch(texture, nullptr, window, nullptr);
+
+ SDL_Flip(window);
+}
+
+void audioCleanup(){
+ audioInitialized = false;
+
+ SDL_CloseAudio();
+}
+
+
+void FillAudioDeviceBuffer(void* UserData, Uint8* DeviceBuffer, int Length)
+{
+ _audio->FillMonoAudioBuffer(DeviceBuffer, 0, Length / 2);
+}
+
+void audioSetup(){
+ //modifed from SDL docs: https://wiki.libsdl.org/SDL_OpenAudioDevice
+
+ SDL_memset(&want, 0, sizeof(want));
+ want.freq = SAMPLERATE;
+ want.format = AUDIO_S16SYS;
+ want.channels = 1;
+ want.samples = 1024;
+ want.callback = FillAudioDeviceBuffer;
+
+
+ int audioOpenRes = SDL_OpenAudio(&want, &have);
+ if (audioOpenRes < 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_PauseAudio(0);
+ audioInitialized = true;
+ }
+}
+
+
+Host::Host() {
+ #ifdef _BITTBOY
+ _cartDirectory = "/mnt/roms/PICO-8";
+ #else
+ std::string home = getenv("HOME");
+
+ _cartDirectory = home + "/p8carts";
+ #endif
+ }
+
+void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
+ if (SDL_Init(SDL_INIT_VIDEO) != 0)
+ {
+ fprintf(stderr, "SDL could not initialize\n");
+ return;
+ }
+
+ SDL_WM_SetCaption("FAKE-08", NULL);
+ SDL_ShowCursor(SDL_DISABLE);
+
+ int flags = SDL_SWSURFACE;
+
+ window = SDL_SetVideoMode(0, 0, SCREEN_BPP, flags);
+
+ texture = SDL_CreateRGBSurface(flags, PicoScreenWidth, PicoScreenHeight, SCREEN_BPP, 0, 0, 0, 0);
+
+ _audio = audio;
+ audioSetup();
+
+ last_time = 0;
+ now_time = 0;
+ frame_time = 0;
+ targetFrameTimeMs = 0;
+
+ _paletteColors = paletteColors;
+
+ SDL_PixelFormat *f = window->format;
+
+ for(int i = 0; i < 144; i++){
+ _mapped16BitColors[i] = SDL_MapRGB(f, _paletteColors[i].Red, _paletteColors[i].Green, _paletteColors[i].Blue);
+ }
+}
+
+void Host::oneTimeCleanup(){
+ audioCleanup();
+
+ //SDL_DestroyRenderer(renderer);
+ //SDL_DestroyWindow(window);
+
+ SDL_FreeSurface(texture);
+ SDL_FreeSurface(window);
+
+ SDL_Quit();
+}
+
+void Host::setTargetFps(int targetFps){
+ targetFrameTimeMs = 1000 / targetFps;
+}
+
+void Host::changeStretch(){
+}
+
+InputState_t Host::scanInput(){
+ currKDown = 0;
+ currKHeld = 0;
+
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_RETURN:currKDown |= P8_KEY_PAUSE; break;
+ case SDLK_l: currKDown |= P8_KEY_LEFT; break;
+ case SDLK_r: currKDown |= P8_KEY_RIGHT; break;
+ case SDLK_u: currKDown |= P8_KEY_UP; break;
+ case SDLK_d: currKDown |= P8_KEY_DOWN; break;
+ case SDLK_b: currKDown |= P8_KEY_X; break;
+ case SDLK_a:currKDown |= P8_KEY_O; break;
+ case SDLK_x: currKDown |= P8_KEY_X; break;
+ case SDLK_y: currKDown |= P8_KEY_O; break;
+ case SDLK_q: case SDLK_HOME: done = SDL_TRUE; break;
+ default: break;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_q:
+ done = SDL_TRUE;
+ break;
+ default: break;
+ }
+ break;
+ break;
+ case SDL_QUIT:
+ done = SDL_TRUE;
+ break;
+ }
+ }
+
+ const Uint8* keystate = SDL_GetKeyState(NULL);
+
+ //continuous-response keys
+ if(keystate[SDLK_l]){
+ currKHeld |= P8_KEY_LEFT;
+ }
+ if(keystate[SDLK_r]){
+ currKHeld |= P8_KEY_RIGHT;;
+ }
+ if(keystate[SDLK_u]){
+ currKHeld |= P8_KEY_UP;
+ }
+ if(keystate[SDLK_d]){
+ currKHeld |= P8_KEY_DOWN;
+ }
+ if(keystate[SDLK_b]){
+ currKHeld |= P8_KEY_X;
+ }
+ if(keystate[SDLK_a]){
+ currKHeld |= P8_KEY_O;
+ }
+ if(keystate[SDLK_x]){
+ currKHeld |= P8_KEY_X;
+ }
+ if(keystate[SDLK_y]){
+ currKHeld |= P8_KEY_O;
+ }
+ if(keystate[SDLK_s]){
+ currKHeld |= P8_KEY_PAUSE;
+ }
+
+
+ return InputState_t {
+ currKDown,
+ currKHeld
+ };
+}
+
+bool Host::shouldQuit() {
+ return done == SDL_TRUE;
+}
+
+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 set_pixel(SDL_Surface *surface, int x, int y, uint16_t pixel)
+{
+ uint16_t * const target_pixel = (uint16_t *) ((Uint8 *) surface->pixels
+ + y * surface->pitch
+ + x * surface->format->BytesPerPixel);
+ *target_pixel = pixel;
+}
+*/
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, uint8_t drawMode){
+
+ pixels = texture->pixels;
+
+ for (int y = 0; y < (PicoScreenHeight); y ++){
+ for (int x = 0; x < PicoScreenWidth; x ++){
+ uint8_t c = getPixelNibble(x, y, picoFb);
+ uint16_t col = _mapped16BitColors[screenPaletteMap[c]];
+
+ base = ((uint16_t *)pixels) + ( y * PicoScreenHeight + x);
+ base[0] = col;
+ }
+ }
+
+ postFlipFunction();
+}
+
+bool Host::shouldFillAudioBuff(){
+ return false;
+}
+
+void* Host::getAudioBufferPointer(){
+ return nullptr;
+}
+
+size_t Host::getAudioBufferSize(){
+ return 0;
+}
+
+void Host::playFilledAudioBuffer(){
+}
+
+bool Host::shouldRunMainLoop(){
+ if (shouldQuit()){
+ return false;
+ }
+
+ return true;
+}
+
+vector<string> Host::listcarts(){
+ vector<string> carts;
+
+ DIR *dir;
+ struct dirent *ent;
+ if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+ /* print all the files and directories within directory */
+ while ((ent = readdir (dir)) != NULL) {
+ carts.push_back(_cartDirectory + "/" + ent->d_name);
+ }
+ closedir (dir);
+ } else {
+ /* could not open directory */
+ perror ("");
+ }
+
+
+ return carts;
+}
+
+const char* Host::logFilePrefix() {
+ return "";
+}
+
+std::string Host::customBiosLua() {
+ return "cartpath = \"roms/pico-8/\"\n"
+ "selectbtn = \"z\"\n"
+ "pausebtn = \"esc\""
+ "exitbtn = \"close window\""
+ "sizebtn = \"\"";
+}
+
+std::string Host::getCartDirectory() {
+ return _cartDirectory;
+}
diff --git a/platform/gcw0/Makefile b/platform/gcw0/Makefile
new file mode 100644
index 0000000..3bad88d
--- /dev/null
+++ b/platform/gcw0/Makefile
@@ -0,0 +1,119 @@
+
+#---------------------------------------------------------------------------------
+# TARGET is the name of the output
+# BUILD is the directory where object files & intermediate files will be placed
+# SOURCES is a list of directories containing source code
+# INCLUDES is a list of directories containing header files
+#
+#---------------------------------------------------------------------------------
+TARGET := FAKE08
+BUILD := build
+SOURCES := ${SOURCES} ../gcw0/source source
+INCLUDES := ${INCLUDES}
+
+#---------------------------------------------------------------------------------
+# options for code generation
+#---------------------------------------------------------------------------------
+CC = /opt/gcw0-toolchain/bin/mipsel-linux-g++
+CXX = /opt/gcw0-toolchain/bin/mipsel-linux-g++
+
+CFLAGS := -Wall -Ofast -fdata-sections -ffunction-sections -fno-PIC -mno-shared -mplt -flto -DVER_STR=\"$(APP_VERSION)\" \
+ $(DEFINES)
+
+CFLAGS += $(INCLUDE)
+
+CXXFLAGS := $(CFLAGS) -fno-rtti -std=gnu++17
+
+LIBS := $(CFLAGS) -lc -lgcc -lm -lSDL -lasound -Wl,--as-needed,--gc-sections -s
+
+LDFLAGS := $(LIBS)
+
+
+#---------------------------------------------------------------------------------
+# no real need to edit anything past this point unless you need to add additional
+# rules for different file extensions
+#---------------------------------------------------------------------------------
+ifneq ($(BUILD),$(notdir $(CURDIR)))
+#---------------------------------------------------------------------------------
+
+export OUTPUT := $(CURDIR)/$(TARGET)
+export TOPDIR := $(CURDIR)
+
+export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \
+ $(foreach dir,$(DATA),$(CURDIR)/$(dir))
+
+export DEPSDIR := $(CURDIR)/$(BUILD)
+
+CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c)))
+CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp)))
+
+#---------------------------------------------------------------------------------
+# use CXX for linking C++ projects, CC for standard C
+#---------------------------------------------------------------------------------
+ifeq ($(strip $(CPPFILES)),)
+#---------------------------------------------------------------------------------
+ export LD := $(CC)
+#---------------------------------------------------------------------------------
+else
+#---------------------------------------------------------------------------------
+ export LD := $(CXX)
+#---------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------
+
+export OFILES_BIN := $(addsuffix .o,$(BINFILES))
+export OFILES_SRC := $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o)
+export OFILES := $(OFILES_BIN) $(OFILES_SRC)
+export HFILES_BIN := $(addsuffix .h,$(subst .,_,$(BINFILES)))
+
+export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \
+ $(foreach dir,$(LIBDIRS),-I$(dir)/include) \
+ -I$(CURDIR)/$(BUILD)
+
+export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib)
+
+
+.PHONY: $(BUILD) clean all
+
+#---------------------------------------------------------------------------------
+all: $(BUILD)
+
+$(BUILD):
+ @[ -d $@ ] || mkdir -p $@
+ @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile
+
+#---------------------------------------------------------------------------------
+clean:
+ @echo clean ...
+ @rm -fr $(BUILD) $(TARGET)
+
+
+#---------------------------------------------------------------------------------
+else
+.PHONY: all
+
+DEPENDS := $(OFILES:.o=.d)
+
+#---------------------------------------------------------------------------------
+# main targets
+#---------------------------------------------------------------------------------
+all : $(OUTPUT)
+
+$(OUTPUT) : $(OFILES)
+ $(CC) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
+
+$(OFILES_SRC) : $(HFILES_BIN)
+
+#---------------------------------------------------------------------------------
+# you need a rule like this for each extension you use as binary data
+#---------------------------------------------------------------------------------
+%.bin.o %_bin.h : %.bin
+#---------------------------------------------------------------------------------
+ @echo $(notdir $<)
+ @$(bin2o)
+
+-include $(DEPENDS)
+
+#---------------------------------------------------------------------------------------
+endif
+#---------------------------------------------------------------------------------------
diff --git a/platform/gcw0/source/ODHost.cpp b/platform/gcw0/source/ODHost.cpp
new file mode 100644
index 0000000..8dc21c1
--- /dev/null
+++ b/platform/gcw0/source/ODHost.cpp
@@ -0,0 +1,351 @@
+
+#include <stdio.h>
+#include <string.h>
+#include <dirent.h>
+#include <errno.h>
+
+#include <fstream>
+#include <iostream>
+using namespace std;
+
+#include "../../../source/host.h"
+#include "../../../source/hostVmShared.h"
+#include "../../../source/nibblehelpers.h"
+#include "../../../source/logger.h"
+
+// sdl
+#include <SDL/SDL.h>
+
+#define SCREEN_BPP 16
+
+#define SAMPLERATE 22050
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+
+StretchOption stretch;
+uint32_t last_time;
+uint32_t now_time;
+uint32_t frame_time;
+uint32_t targetFrameTimeMs;
+
+uint8_t currKDown;
+uint8_t currKHeld;
+
+Color* _paletteColors;
+Audio* _audio;
+
+SDL_Event event;
+SDL_Surface *window;
+SDL_Surface *texture;
+SDL_bool done = SDL_FALSE;
+SDL_AudioSpec want, have;
+void *pixels;
+uint16_t *base;
+int pitch;
+
+bool audioInitialized = false;
+
+uint16_t _mapped16BitColors[144];
+
+/*
+ * Gameblabla
+ * Well unfortunately we still have IPU bugs/issues due to how the IPU scaler works.
+ * So for now, we have to use a simple software scaler. (A performance penalty may occur on the RG-300X/RG-350M)
+*/
+//#define OPENDINGUX_IPU 1
+
+
+void postFlipFunction(){
+ // We're done rendering, so we end the frame here.
+#ifdef OPENDINGUX_IPU
+ SDL_Flip(window);
+#else
+ SDL_SoftStretch(texture, nullptr, window, nullptr);
+ SDL_Flip(window);
+#endif
+}
+
+void audioCleanup(){
+ audioInitialized = false;
+
+ SDL_CloseAudio();
+}
+
+
+void FillAudioDeviceBuffer(void* UserData, Uint8* DeviceBuffer, int Length)
+{
+ _audio->FillMonoAudioBuffer(DeviceBuffer, 0, Length / 2);
+}
+
+void audioSetup(){
+ //modifed from SDL docs: https://wiki.libsdl.org/SDL_OpenAudioDevice
+
+ SDL_memset(&want, 0, sizeof(want));
+ want.freq = SAMPLERATE;
+ want.format = AUDIO_S16SYS;
+ want.channels = 1;
+ want.samples = 1024;
+ want.callback = FillAudioDeviceBuffer;
+
+
+ int audioOpenRes = SDL_OpenAudio(&want, &have);
+ if (audioOpenRes < 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_PauseAudio(0);
+ audioInitialized = true;
+ }
+}
+
+
+Host::Host() {
+ #ifdef _BITTBOY
+ _cartDirectory = "/mnt/roms/PICO-8";
+ #else
+ std::string home = getenv("HOME");
+
+ _cartDirectory = home + "/p8carts";
+ #endif
+ }
+
+void Host::oneTimeSetup(Color* paletteColors, Audio* audio){
+ if (SDL_Init(SDL_INIT_VIDEO) != 0)
+ {
+ fprintf(stderr, "SDL could not initialize\n");
+ return;
+ }
+
+ SDL_WM_SetCaption("FAKE-08", NULL);
+ SDL_ShowCursor(SDL_DISABLE);
+
+ int flags = SDL_HWSURFACE | SDL_TRIPLEBUF;
+
+ #ifdef OPENDINGUX_IPU
+ window = SDL_SetVideoMode(128, 128, SCREEN_BPP, flags);
+ #else
+ window = SDL_SetVideoMode(0, 0, SCREEN_BPP, flags);
+ texture = SDL_CreateRGBSurface(flags, PicoScreenWidth, PicoScreenHeight, SCREEN_BPP, 0, 0, 0, 0);
+ #endif
+ _audio = audio;
+ audioSetup();
+
+ last_time = 0;
+ now_time = 0;
+ frame_time = 0;
+ targetFrameTimeMs = 0;
+
+ _paletteColors = paletteColors;
+
+ SDL_PixelFormat *f = window->format;
+
+ for(int i = 0; i < 144; i++){
+ _mapped16BitColors[i] = SDL_MapRGB(f, _paletteColors[i].Red, _paletteColors[i].Green, _paletteColors[i].Blue);
+ }
+}
+
+void Host::oneTimeCleanup(){
+ audioCleanup();
+
+ //SDL_DestroyRenderer(renderer);
+ //SDL_DestroyWindow(window);
+
+ SDL_FreeSurface(window);
+
+ SDL_Quit();
+}
+
+void Host::setTargetFps(int targetFps){
+ targetFrameTimeMs = 1000 / targetFps;
+}
+
+void Host::changeStretch(){
+}
+
+InputState_t Host::scanInput(){
+ currKDown = 0;
+ currKHeld = 0;
+
+ while (SDL_PollEvent(&event)) {
+ switch (event.type) {
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_RETURN:currKDown |= P8_KEY_PAUSE; break;
+ case SDLK_LEFT: currKDown |= P8_KEY_LEFT; break;
+ case SDLK_RIGHT: currKDown |= P8_KEY_RIGHT; break;
+ case SDLK_UP: currKDown |= P8_KEY_UP; break;
+ case SDLK_DOWN: currKDown |= P8_KEY_DOWN; break;
+ case SDLK_SPACE: currKDown |= P8_KEY_X; break;
+ case SDLK_LSHIFT:currKDown |= P8_KEY_O; break;
+ case SDLK_LALT: currKDown |= P8_KEY_X; break;
+ case SDLK_LCTRL: currKDown |= P8_KEY_O; break;
+ case SDLK_RCTRL: case SDLK_HOME: done = SDL_TRUE; break;
+ default: break;
+ }
+ break;
+ case SDL_KEYUP:
+ switch (event.key.keysym.sym)
+ {
+ case SDLK_HOME:
+ done = SDL_TRUE;
+ break;
+ default: break;
+ }
+ break;
+ break;
+ case SDL_QUIT:
+ done = SDL_TRUE;
+ break;
+ }
+ }
+
+ const Uint8* keystate = SDL_GetKeyState(NULL);
+
+ //continuous-response keys
+ if(keystate[SDLK_LEFT]){
+ currKHeld |= P8_KEY_LEFT;
+ }
+ if(keystate[SDLK_RIGHT]){
+ currKHeld |= P8_KEY_RIGHT;;
+ }
+ if(keystate[SDLK_UP]){
+ currKHeld |= P8_KEY_UP;
+ }
+ if(keystate[SDLK_DOWN]){
+ currKHeld |= P8_KEY_DOWN;
+ }
+ if(keystate[SDLK_SPACE]){
+ currKHeld |= P8_KEY_X;
+ }
+ if(keystate[SDLK_LSHIFT]){
+ currKHeld |= P8_KEY_O;
+ }
+ if(keystate[SDLK_LALT]){
+ currKHeld |= P8_KEY_X;
+ }
+ if(keystate[SDLK_LCTRL]){
+ currKHeld |= P8_KEY_O;
+ }
+ if(keystate[SDLK_RETURN]){
+ currKHeld |= P8_KEY_PAUSE;
+ }
+
+
+ return InputState_t {
+ currKDown,
+ currKHeld
+ };
+}
+
+bool Host::shouldQuit() {
+ return done == SDL_TRUE;
+}
+
+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 set_pixel(SDL_Surface *surface, int x, int y, uint16_t pixel)
+{
+ uint16_t * const target_pixel = (uint16_t *) ((Uint8 *) surface->pixels
+ + y * surface->pitch
+ + x * surface->format->BytesPerPixel);
+ *target_pixel = pixel;
+}
+*/
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, uint8_t drawMode){
+ #ifdef OPENDINGUX_IPU
+ pixels = window->pixels;
+ #else
+ pixels = texture->pixels;
+ #endif
+
+ for (int y = 0; y < (PicoScreenHeight); y ++){
+ for (int x = 0; x < PicoScreenWidth; x ++){
+ uint8_t c = getPixelNibble(x, y, picoFb);
+ uint16_t col = _mapped16BitColors[screenPaletteMap[c]];
+
+ base = ((uint16_t *)pixels) + ( y * PicoScreenHeight + x);
+ base[0] = col;
+ }
+ }
+
+ postFlipFunction();
+}
+
+bool Host::shouldFillAudioBuff(){
+ return false;
+}
+
+void* Host::getAudioBufferPointer(){
+ return nullptr;
+}
+
+size_t Host::getAudioBufferSize(){
+ return 0;
+}
+
+void Host::playFilledAudioBuffer(){
+}
+
+bool Host::shouldRunMainLoop(){
+ if (shouldQuit()){
+ return false;
+ }
+
+ return true;
+}
+
+vector<string> Host::listcarts(){
+ vector<string> carts;
+
+ DIR *dir;
+ struct dirent *ent;
+ if ((dir = opendir (_cartDirectory.c_str())) != NULL) {
+ /* print all the files and directories within directory */
+ while ((ent = readdir (dir)) != NULL) {
+ carts.push_back(_cartDirectory + "/" + ent->d_name);
+ }
+ closedir (dir);
+ } else {
+ /* could not open directory */
+ perror ("");
+ }
+
+
+ return carts;
+}
+
+const char* Host::logFilePrefix() {
+ return "";
+}
+
+std::string Host::customBiosLua() {
+ return "cartpath = \"roms/pico-8/\"\n"
+ "selectbtn = \"z\"\n"
+ "pausebtn = \"esc\""
+ "exitbtn = \"close window\""
+ "sizebtn = \"\"";
+}
+
+std::string Host::getCartDirectory() {
+ return _cartDirectory;
+}