commit 493737c7db1f7da5e8109f678b94a3c9c1cba148
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat May 16 12:25:54 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Sat May 16 12:25:54 2020 -0600
Refactor platform specific code into host class
---
Makefile | 2 +-
source/Input.cpp | 1 +
source/Input.h | 15 --
source/Platform/3ds/3dsHost.cpp | 383 ++++++++++++++++++++++++++++++++++++++++
source/cart.cpp | 8 +-
source/graphics.cpp | 142 ++++-----------
source/graphics.h | 20 +--
source/host.h | 54 ++++++
source/hostVmShared.h | 23 +++
source/main.cpp | 303 +++----------------------------
source/vm.cpp | 36 +---
source/vm.h | 23 +--
12 files changed, 540 insertions(+), 470 deletions(-)
diff --git a/Makefile b/Makefile
index ee7bfad..b7baa25 100644
--- a/Makefile
+++ b/Makefile
@@ -33,7 +33,7 @@ include $(DEVKITARM)/3ds_rules
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
-SOURCES := source source/tests libs/lua-5.3.2/src libs/utf8-util
+SOURCES := source source/Platform/3ds libs/lua-5.3.2/src libs/utf8-util
DATA := data
INCLUDES := include libs/lua-5.3.2/src libs/utf8-util
GRAPHICS := gfx
diff --git a/source/Input.cpp b/source/Input.cpp
index 360ede0..365838a 100644
--- a/source/Input.cpp
+++ b/source/Input.cpp
@@ -1,4 +1,5 @@
#include "input.h"
+#include "hostVmShared.h"
void Input::SetState(uint8_t kdown, uint8_t kheld){
_currentKDown = kdown;
diff --git a/source/Input.h b/source/Input.h
index 3b4c906..bf0f370 100644
--- a/source/Input.h
+++ b/source/Input.h
@@ -2,21 +2,6 @@
#include <stdint.h>
-/// Creates a bitmask from a bit number.
-#define BITMASK(n) (1U<<(n))
-
-enum
-{
- P8_KEY_LEFT = BITMASK(0),
- P8_KEY_RIGHT = BITMASK(1),
- P8_KEY_UP = BITMASK(2),
- P8_KEY_DOWN = BITMASK(3),
- P8_KEY_O = BITMASK(4),
- P8_KEY_X = BITMASK(5),
- P8_KEY_PAUSE = BITMASK(6),
- P8_KEY_7 = BITMASK(7),
-};
-
class Input{
uint8_t _currentKDown;
uint8_t _currentKHeld;
diff --git a/source/Platform/3ds/3dsHost.cpp b/source/Platform/3ds/3dsHost.cpp
new file mode 100644
index 0000000..4ed1779
--- /dev/null
+++ b/source/Platform/3ds/3dsHost.cpp
@@ -0,0 +1,383 @@
+#include <3ds.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "../../host.h"
+#include "../../hostVmShared.h"
+
+#define SCREEN_WIDTH 400;
+#define SCREEN_HEIGHT 240;
+
+#define SCREEN_2_WIDTH 320;
+#define SCREEN_2_HEIGHT 240;
+
+#define SAMPLERATE 22050
+#define SAMPLESPERBUF (SAMPLERATE / 30)
+#define NUM_BUFFERS 2
+
+const int __3ds_TopScreenWidth = SCREEN_WIDTH;
+const int __3ds_TopScreenHeight = SCREEN_HEIGHT;
+
+const int __3ds_BottomScreenWidth = SCREEN_2_WIDTH;
+const int __3ds_BottomScreenHeight = SCREEN_2_HEIGHT;
+
+
+StretchOption stretch;
+u64 last_time;
+u64 now_time;
+u64 frame_time;
+double targetFrameTimeMs;
+
+u32 currKDown;
+u32 currKHeld;
+
+Host::Host() { }
+
+
+int Host::getScreenWidth() {
+ return __3ds_TopScreenWidth;
+}
+
+int Host::getScreenHeight(){
+ return __3ds_TopScreenHeight;
+}
+
+int Host::getSecondScreenWidth(){
+ return __3ds_BottomScreenWidth;
+}
+int Host::getSecondScreenHeight(){
+ return __3ds_BottomScreenHeight;
+}
+
+void Host::oneTimeSetup(){
+ osSetSpeedupEnable(true);
+
+ Host::audioSetup();
+ Host::gfxSetup();
+
+ last_time = 0;
+ now_time = 0;
+ frame_time = 0;
+ targetFrameTimeMs = 0;
+}
+
+void Host::setTargetFps(uint8_t targetFps){
+ targetFrameTimeMs = 1000.0 / (double)targetFps;
+}
+
+void Host::changeStretch(){
+ if (currKDown & KEY_R) {
+ if (stretch == PixelPerfect) {
+ stretch = StretchToFit;
+ }
+ else if (stretch == StretchToFit) {
+ stretch = StretchAndOverflow;
+ }
+ else if (stretch == StretchAndOverflow) {
+ stretch = PixelPerfect;
+ }
+ }
+}
+
+uint8_t ConvertInputToP8(u32 input){
+ uint8_t result = 0;
+ if (input & KEY_LEFT){
+ result |= P8_KEY_LEFT;
+ }
+
+ if (input & KEY_RIGHT){
+ result |= P8_KEY_RIGHT;
+ }
+
+ if (input & KEY_UP){
+ result |= P8_KEY_UP;
+ }
+
+ if (input & KEY_DOWN){
+ result |= P8_KEY_DOWN;
+ }
+
+ if (input & KEY_B){
+ result |= P8_KEY_O;
+ }
+
+ if (input & KEY_A){
+ result |= P8_KEY_X;
+ }
+
+ if (input & KEY_START){
+ result |= P8_KEY_PAUSE;
+ }
+
+ if (input & KEY_SELECT){
+ result |= P8_KEY_7;
+ }
+
+ return result;
+}
+
+void Host::scanInput(){
+ hidScanInput();
+
+ currKDown = hidKeysDown();
+ currKHeld = hidKeysHeld();
+}
+
+uint8_t Host::getKeysDown(){
+ return ConvertInputToP8(currKDown);
+}
+
+uint8_t Host::getKeysHeld(){
+ return ConvertInputToP8(currKHeld);
+}
+
+
+bool Host::shouldQuit() {
+ bool lpressed = currKHeld & KEY_L;
+ bool rpressed = currKDown & KEY_R;
+
+ return lpressed && rpressed;
+}
+
+void Host::gfxSetup(){
+ gfxInitDefault();
+}
+
+void Host::postFlipFunction(){
+ gfxFlushBuffers();
+ gfxSwapBuffers();
+ gspWaitForVBlank();
+}
+
+
+void Host::gfxCleanup(){
+ gfxExit();
+}
+
+//----------------------------------------------------------------------------
+void init_fill_buffer(void *audioBuffer,size_t offset, size_t size) {
+//----------------------------------------------------------------------------
+
+ u32 *dest = (u32*)audioBuffer;
+
+ for (size_t i=0; i<size; i++) {
+ dest[i] = 0;
+ }
+
+ DSP_FlushDataCache(audioBuffer,size);
+
+}
+
+bool audioInitialized = false;
+u32 *audioBuffer;
+u32 audioBufferSize;
+ndspWaveBuf waveBuf[2];
+bool fillBlock = false;
+u32 currPos;
+
+void Host::audioSetup(){
+ if(R_FAILED(ndspInit())) {
+ return;
+ }
+
+ //audio setup
+ audioBufferSize = SAMPLESPERBUF * NUM_BUFFERS * sizeof(u32);
+ audioBuffer = (u32*)linearAlloc(audioBufferSize);
+ if(audioBuffer == nullptr) {
+ Host::audioCleanup();
+ return;
+ }
+
+
+ ndspSetOutputMode(NDSP_OUTPUT_STEREO);
+
+ ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
+ ndspChnSetRate(0, SAMPLERATE);
+ ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16);
+
+ float mix[12];
+ memset(mix, 0, sizeof(mix));
+ mix[0] = 1.0;
+ mix[1] = 1.0;
+ ndspChnSetMix(0, mix);
+
+ memset(waveBuf,0,sizeof(waveBuf));
+ waveBuf[0].data_vaddr = &audioBuffer[0];
+ waveBuf[0].nsamples = SAMPLESPERBUF;
+ waveBuf[1].data_vaddr = &audioBuffer[SAMPLESPERBUF];
+ waveBuf[1].nsamples = SAMPLESPERBUF;
+
+
+ size_t stream_offset = 0;
+
+ //not sure if this is necessary? if it is, memset might be better?
+ init_fill_buffer(audioBuffer,stream_offset, SAMPLESPERBUF * 2);
+
+ stream_offset += SAMPLESPERBUF;
+
+ ndspChnWaveBufAdd(0, &waveBuf[0]);
+ ndspChnWaveBufAdd(0, &waveBuf[1]);
+
+ audioInitialized = true;
+}
+
+void Host::audioCleanup(){
+ audioInitialized = false;
+
+ ndspExit();
+
+ if(audioBuffer != nullptr) {
+ linearFree(audioBuffer);
+ audioBuffer = nullptr;
+ }
+}
+
+
+
+
+void Host::oneTimeCleanup(){
+ Host::audioCleanup();
+
+ Host::gfxCleanup();
+}
+
+void Host::waitForTargetFps(){
+ now_time = svcGetSystemTick();
+ frame_time = now_time - last_time;
+ last_time = now_time;
+
+ double frameTimeMs = frame_time / CPU_TICKS_PER_MSEC;
+
+ //sleep for remainder of time
+ if (frameTimeMs < targetFrameTimeMs) {
+ double msToSleep = targetFrameTimeMs - frameTimeMs;
+
+ svcSleepThread(msToSleep * 1000 * 1000);
+
+ last_time += CPU_TICKS_PER_MSEC * msToSleep;
+ }
+}
+
+const int PicoScreenWidth = 128;
+const int PicoScreenHeight = 128;
+
+void Host::drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors){
+ int bgcolor = 0;
+ uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
+ //clear whole top framebuffer
+ memset(fb, bgcolor, __3ds_TopScreenWidth*__3ds_TopScreenHeight*3);
+
+ //clear top 16 pixels of bottom buffer in case overflow rendering is being used
+ uint8_t* fbb = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
+ memset(fbb, bgcolor, __3ds_BottomScreenWidth*__3ds_BottomScreenHeight*3);
+
+ short x, y;
+
+ if (stretch == PixelPerfect) {
+ short xOffset = __3ds_TopScreenWidth / 2 - PicoScreenWidth / 2;
+ short yOffset = __3ds_TopScreenHeight / 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 = picoFb[x*128 + y];
+ Color col = paletteColors[screenPaletteMap[c]];
+
+ int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
+
+ fb[pixIdx + 0] = col.Blue;
+ fb[pixIdx + 1] = col.Green;
+ fb[pixIdx + 2] = col.Red;
+ }
+ }
+ }
+ else if (stretch == StretchToFit) {
+ double ratio = (double)__3ds_TopScreenHeight / (double)PicoScreenHeight;
+ int stretchedWidth = PicoScreenWidth * ratio;
+
+ short xOffset = __3ds_TopScreenWidth / 2 - stretchedWidth / 2;
+ short yOffset = 0;
+
+ for(x = 0; x < stretchedWidth; x++) {
+ for(y = 0; y < __3ds_TopScreenHeight; y++) {
+ int picoX = (int)(x / ratio);
+ int picoY = (int)(y / ratio);
+ uint8_t c = picoFb[picoX*128 + picoY];
+ Color col = paletteColors[screenPaletteMap[c]];
+
+ int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
+
+ fb[pixIdx + 0] = col.Blue;
+ fb[pixIdx + 1] = col.Green;
+ fb[pixIdx + 2] = col.Red;
+ }
+ }
+ }
+ else if (stretch == StretchAndOverflow) {
+ //assume landscape, hardcoded double for now (3ds)
+ int ratio = 2;
+ int stretchedWidth = PicoScreenWidth * ratio;
+ int stretchedHeight = PicoScreenHeight * ratio;
+
+ short xOffset = __3ds_TopScreenWidth / 2 - stretchedWidth / 2;
+ short yOffset = 0;
+
+ for(x = 0; x < stretchedWidth; x++) {
+ for(y = 0; y < __3ds_TopScreenHeight; y++) {
+ int picoX = (int)(x / ratio);
+ int picoY = (int)(y / ratio);
+ uint8_t c = picoFb[picoX*128 + picoY];
+ Color col = paletteColors[screenPaletteMap[c]];
+
+ int pixIdx = (((x + xOffset)*__3ds_TopScreenHeight)+ ((__3ds_TopScreenHeight - 1) - (y + yOffset)))*3;
+
+ fb[pixIdx + 0] = col.Blue;
+ fb[pixIdx + 1] = col.Green;
+ fb[pixIdx + 2] = col.Red;
+ }
+ }
+
+ int overflowHeight = stretchedHeight - __3ds_TopScreenHeight;
+
+ xOffset = __3ds_BottomScreenWidth / 2 - stretchedWidth / 2;
+ yOffset = 0;
+
+ for(x = 0; x < stretchedWidth; x++) {
+ for(y = 0; y < overflowHeight; y++) {
+ int picoX = (int)(x / ratio);
+ int picoY = (int)((y + __3ds_TopScreenHeight) / ratio);
+ uint8_t c = picoFb[picoX*128 + picoY];
+ Color col = paletteColors[screenPaletteMap[c]];
+
+ int pixIdx = (((x + xOffset)*__3ds_BottomScreenHeight)+ ((__3ds_BottomScreenHeight - 1) - (y + yOffset)))*3;
+
+ fbb[pixIdx + 0] = col.Blue;
+ fbb[pixIdx + 1] = col.Green;
+ fbb[pixIdx + 2] = col.Red;
+ }
+ }
+ }
+}
+
+bool Host::shouldFillAudioBuff(){
+ return waveBuf[fillBlock].status == NDSP_WBUF_DONE;
+}
+
+void* Host::getAudioBufferPointer(){
+ return waveBuf[fillBlock].data_pcm16;
+}
+
+size_t Host::getAudioBufferSize(){
+ return waveBuf[fillBlock].nsamples;
+}
+
+void Host::playFilledAudioBuffer(){
+ DSP_FlushDataCache(waveBuf[fillBlock].data_pcm16, waveBuf[fillBlock].nsamples);
+
+ ndspChnWaveBufAdd(0, &waveBuf[fillBlock]);
+
+ fillBlock = !fillBlock;
+}
+
+bool Host::mainLoop(){
+ return aptMainLoop();
+}
\ No newline at end of file
diff --git a/source/cart.cpp b/source/cart.cpp
index c184926..e644589 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -12,10 +12,10 @@
#include "cartPatcher.h"
#include "utf8-util.h"
-#include "tests/test_base.h"
-#if _TEST
-#include "tests/cart_test.h"
-#endif
+//#include "tests/test_base.h"
+//#if _TEST
+//#include "tests/cart_test.h"
+//#endif
std::map<char32_t, uint8_t> emoji = {
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 12f3840..f8ad025 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -6,6 +6,7 @@
#include <algorithm>
#include "graphics.h"
+#include "hostVmShared.h"
#include "stringToDataHelpers.h"
@@ -16,24 +17,6 @@ const uint8_t PicoScreenHeight = 128;
const Color BgGray = BG_GRAY_COLOR;
-const Color PaletteColors[] = {
- COLOR_00,
- COLOR_01,
- COLOR_02,
- COLOR_03,
- COLOR_04,
- COLOR_05,
- COLOR_06,
- COLOR_07,
- COLOR_08,
- COLOR_09,
- COLOR_10,
- COLOR_11,
- COLOR_12,
- COLOR_13,
- COLOR_14,
- COLOR_15
-};
//call initialize to make sure defaults are correct
Graphics::Graphics(std::string fontdata) {
@@ -44,6 +27,23 @@ Graphics::Graphics(std::string fontdata) {
//make all colors default
this->clip();
this->pal();
+
+ _paletteColors[0] = COLOR_00;
+ _paletteColors[1] = COLOR_01;
+ _paletteColors[2] = COLOR_02;
+ _paletteColors[3] = COLOR_03;
+ _paletteColors[4] = COLOR_04;
+ _paletteColors[5] = COLOR_05;
+ _paletteColors[6] = COLOR_06;
+ _paletteColors[7] = COLOR_07;
+ _paletteColors[8] = COLOR_08;
+ _paletteColors[9] = COLOR_09;
+ _paletteColors[10] = COLOR_10;
+ _paletteColors[11] = COLOR_11;
+ _paletteColors[12] = COLOR_12;
+ _paletteColors[13] = COLOR_13;
+ _paletteColors[14] = COLOR_14;
+ _paletteColors[15] = COLOR_15;
}
void Graphics::setSpriteSheet(std::string spritesheetstring){
@@ -61,6 +61,18 @@ void Graphics::setMapData(std::string mapDataString){
copy_string_to_memory(mapData, mapDataString);
}
+uint8_t* Graphics::GetP8FrameBuffer(){
+ return this->_pico8_fb;
+}
+
+uint8_t* Graphics::GetScreenPaletteMap(){
+ return this->_gfxState_screenPaletteMap;
+}
+
+Color* Graphics::GetPaletteColors(){
+ return this->_paletteColors;
+}
+
//start helper methods
//based on tac08 implementation of blitter()
void Graphics::copySpriteToScreen(
@@ -815,100 +827,6 @@ void Graphics::palt(uint8_t c, bool t){
}
}
-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]];
-
- 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::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::flipBuffer_SAO(
- uint8_t* fb, int width, int height,
- uint8_t* fb_o, int width_o, int height_o) {
- short x, y;
-
- //assume landscape, hardcoded double for now (3ds)
- int ratio = 2;
- int stretchedWidth = PicoScreenWidth * ratio;
- int stretchedHeight = PicoScreenHeight * 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;
- }
- }
-
- int overflowHeight = stretchedHeight - height;
-
- xOffset = width_o / 2 - stretchedWidth / 2;
- yOffset = 0;
-
- for(x = 0; x < stretchedWidth; x++) {
- for(y = 0; y < overflowHeight; y++) {
- int picoX = (int)(x / ratio);
- int picoY = (int)((y + height) / ratio);
- uint8_t c = _pico8_fb[picoX*128 + picoY];
- Color col = PaletteColors[_gfxState_screenPaletteMap[c]];
-
- int pixIdx = (((x + xOffset)*height)+ ((height_o - 1) - (y + yOffset)))*3;
-
- fb_o[pixIdx + 0] = col.Blue;
- fb_o[pixIdx + 1] = col.Green;
- fb_o[pixIdx + 2] = col.Red;
- }
- }
-}
void Graphics::cursor() {
this->cursor(0, 0);
diff --git a/source/graphics.h b/source/graphics.h
index 484dcb2..e8066af 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -1,6 +1,7 @@
#pragma once
#include <string>
+#include "hostVmShared.h"
#define COLOR_00 { 0, 0, 0, 255}
#define COLOR_01 { 29, 43, 83, 255}
@@ -22,13 +23,6 @@
#define BG_GRAY_COLOR {128, 128, 128, 255}
-struct Color {
- char Red;
- char Green;
- char Blue;
- char Alpha;
-};
-
class Graphics {
uint8_t _pico8_fb[128*128];
uint8_t fontSpriteData[128 * 64];
@@ -37,6 +31,8 @@ class Graphics {
uint8_t mapData[128 * 64];
uint8_t spriteFlags[256];
+ Color _paletteColors[16];
+
uint8_t _gfxState_color;
uint8_t _gfxState_bgColor;
@@ -110,6 +106,10 @@ class Graphics {
void setSpriteFlags(std::string spriteFlagsString);
void setMapData(std::string mapString);
+ uint8_t* GetP8FrameBuffer();
+ uint8_t* GetScreenPaletteMap();
+ Color* GetPaletteColors();
+
void cls();
void cls(uint8_t color);
@@ -192,11 +192,5 @@ class Graphics {
void cursor(short x, short y);
void cursor(short x, short y, uint8_t col);
-
- void flipBuffer(uint8_t* fb, int width, int height);
-
- void flipBuffer_STF(uint8_t* fb, int width, int height);
- void flipBuffer_SAO(uint8_t* fb, int width, int height, uint8_t* fb_o, int width_o, int height_o);
-
};
diff --git a/source/host.h b/source/host.h
new file mode 100644
index 0000000..1072513
--- /dev/null
+++ b/source/host.h
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <stdio.h>
+#include "hostVmShared.h"
+
+enum StretchOption {
+ PixelPerfect,
+ StretchToFit,
+ StretchAndOverflow
+};
+
+class Host {
+ public:
+ Host();
+
+ int getScreenWidth();
+ int getScreenHeight();
+
+ int getSecondScreenWidth();
+ int getSecondScreenHeight();
+
+ void setTargetFps(uint8_t targetFps);
+
+ StretchOption getDefaultStretch();
+ void changeStretch();
+
+ void scanInput();
+ uint8_t getKeysDown();
+ uint8_t getKeysHeld();
+
+ bool shouldQuit();
+
+ void gfxSetup();
+ void postFlipFunction();
+ void gfxCleanup();
+
+ void audioSetup();
+ void audioCleanup();
+ bool shouldFillAudioBuff();
+ void* getAudioBufferPointer();
+ size_t getAudioBufferSize();
+ void playFilledAudioBuffer();
+
+ void drawFrame(uint8_t* picoFb, uint8_t* screenPaletteMap, Color* paletteColors);
+
+
+ void oneTimeSetup();
+
+ void oneTimeCleanup();
+
+ void waitForTargetFps();
+
+ bool mainLoop();
+};
\ No newline at end of file
diff --git a/source/hostVmShared.h b/source/hostVmShared.h
new file mode 100644
index 0000000..d1374cd
--- /dev/null
+++ b/source/hostVmShared.h
@@ -0,0 +1,23 @@
+#pragma once
+
+/// Creates a bitmask from a bit number.
+#define BITMASK(n) (1U<<(n))
+
+enum
+{
+ P8_KEY_LEFT = BITMASK(0),
+ P8_KEY_RIGHT = BITMASK(1),
+ P8_KEY_UP = BITMASK(2),
+ P8_KEY_DOWN = BITMASK(3),
+ P8_KEY_O = BITMASK(4),
+ P8_KEY_X = BITMASK(5),
+ P8_KEY_PAUSE = BITMASK(6),
+ P8_KEY_7 = BITMASK(7),
+};
+
+struct Color {
+ char Red;
+ char Green;
+ char Blue;
+ char Alpha;
+};
diff --git a/source/main.cpp b/source/main.cpp
index ad13a6a..f10b0b9 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -1,334 +1,83 @@
-
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <3ds.h>
#include <math.h>
#include <string>
-#include "graphics.h"
#include "vm.h"
#include "logger.h"
+#include "host.h"
+#include "hostVmShared.h"
-#include "input.h"
-
-//this has the macro _TEST defined in it
-#include "tests/test_base.h"
-
-#define SAMPLERATE 22050
-#define SAMPLESPERBUF (SAMPLERATE / 30)
-#define NUM_BUFFERS 2
-
-
-const int ScreenWidth = 400;
-const int ScreenHeight = 240;
-
-const int BottomScreenWidth = 320;
-const int BottomScreenHeight = 240;
-
-StretchOption stretch = PixelPerfect;
-
-void ChangeStretch() {
- if (stretch == PixelPerfect) {
- stretch = StretchToFit;
- }
- else if (stretch == StretchToFit) {
- stretch = StretchAndOverflow;
- }
- else if (stretch == StretchAndOverflow) {
- stretch = PixelPerfect;
- }
-}
-
-//3ds specific helper function
-uint8_t ConvertInputToP8(u32 input){
- uint8_t result = 0;
- if (input & KEY_LEFT){
- result |= P8_KEY_LEFT;
- }
-
- if (input & KEY_RIGHT){
- result |= P8_KEY_RIGHT;
- }
-
- if (input & KEY_UP){
- result |= P8_KEY_UP;
- }
-
- if (input & KEY_DOWN){
- result |= P8_KEY_DOWN;
- }
-
- if (input & KEY_B){
- result |= P8_KEY_O;
- }
-
- if (input & KEY_A){
- result |= P8_KEY_X;
- }
-
- if (input & KEY_START){
- result |= P8_KEY_PAUSE;
- }
-
- if (input & KEY_SELECT){
- result |= P8_KEY_7;
- }
-
- return result;
-}
-
-void clear3dsFrameBuffer() {
- #if _TEST
- int bgcolor = 255;
- #else
- int bgcolor = 0;
- #endif
- uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
- //clear whole top framebuffer
- memset(fb, bgcolor, ScreenHeight*ScreenWidth*3);
-
- //clear top 16 pixels of bottom buffer in case overflow rendering is being used
- uint8_t* fbb = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
- memset(fbb, bgcolor, BottomScreenHeight*BottomScreenWidth*3);
-
-}
-
-//3ds specific helper function
-void postFlip3dsFunction() {
- gfxFlushBuffers();
- gfxSwapBuffers();
- gspWaitForVBlank();
-}
-
-//----------------------------------------------------------------------------
-void init_fill_buffer(void *audioBuffer,size_t offset, size_t size) {
-//----------------------------------------------------------------------------
-
- u32 *dest = (u32*)audioBuffer;
-
- for (size_t i=0; i<size; i++) {
- dest[i] = 0;
- }
-
- DSP_FlushDataCache(audioBuffer,size);
-
-}
-
-bool audioInitialized = false;
-u32 *audioBuffer;
-u32 audioBufferSize;
-ndspWaveBuf waveBuf[2];
-bool fillBlock = false;
-u32 currPos;
-
-void audioCleanup() {
- audioInitialized = false;
-
- ndspExit();
-
- if(audioBuffer != nullptr) {
- linearFree(audioBuffer);
- audioBuffer = nullptr;
- }
-}
-
-void audioSetup() {
- if(R_FAILED(ndspInit())) {
- return;
- }
-
- //audio setup
- audioBufferSize = SAMPLESPERBUF * NUM_BUFFERS * sizeof(u32);
- audioBuffer = (u32*)linearAlloc(audioBufferSize);
- if(audioBuffer == nullptr) {
- audioCleanup();
- return;
- }
-
-
- ndspSetOutputMode(NDSP_OUTPUT_STEREO);
-
- ndspChnSetInterp(0, NDSP_INTERP_LINEAR);
- ndspChnSetRate(0, SAMPLERATE);
- ndspChnSetFormat(0, NDSP_FORMAT_STEREO_PCM16);
-
- float mix[12];
- memset(mix, 0, sizeof(mix));
- mix[0] = 1.0;
- mix[1] = 1.0;
- ndspChnSetMix(0, mix);
-
- memset(waveBuf,0,sizeof(waveBuf));
- waveBuf[0].data_vaddr = &audioBuffer[0];
- waveBuf[0].nsamples = SAMPLESPERBUF;
- waveBuf[1].data_vaddr = &audioBuffer[SAMPLESPERBUF];
- waveBuf[1].nsamples = SAMPLESPERBUF;
-
-
- size_t stream_offset = 0;
-
- //not sure if this is necessary? if it is, memset might be better?
- init_fill_buffer(audioBuffer,stream_offset, SAMPLESPERBUF * 2);
-
- stream_offset += SAMPLESPERBUF;
-
- ndspChnWaveBufAdd(0, &waveBuf[0]);
- ndspChnWaveBufAdd(0, &waveBuf[1]);
-
- audioInitialized = true;
-}
int main(int argc, char* argv[])
{
- u64 last_time = 0, now_time = 0, frame_time = 0;
+ Host *host = new Host();
- audioSetup();
+ host->oneTimeSetup();
Logger::Initialize();
Logger::Write("created Logger\n");
- gfxInitDefault();
- Logger::Write("gfxInitDefault()\n");
- //use new 3ds cpu if we can
- osSetSpeedupEnable(true);
Logger::Write("initializing Vm\n");
Vm *vm = new Vm();
Logger::Write("initialized Vm\n");
- //test or not both hardcoded to loading test cart as of now
- #if _TEST
- consoleInit(GFX_BOTTOM, NULL);
-
Logger::Write("Loading cart\n");
- vm->LoadCart("testcart.p8");
- Logger::Write("Cart Loaded\n");
-
- #else
vm->LoadCart("lilking.p8");
- #endif
+ Logger::Write("Cart Loaded\n");
// Main loop
Logger::Write("Starting main loop\n");
uint8_t targetFps = vm->GetTargetFps();
- double targetFrametimeMs = 1000.0 / (double)targetFps;
- std::function<void()> clearFb = clear3dsFrameBuffer;
- std::function<void()> postFlip = postFlip3dsFunction;
+ host->setTargetFps(targetFps);
+
- while (aptMainLoop())
+ while (host->mainLoop())
{
- //Scan all the inputs. This should be done once for each frame
- hidScanInput();
-
- now_time = svcGetSystemTick();
- frame_time = now_time - last_time;
- last_time = now_time;
-
- double frameTimeMs = frame_time / CPU_TICKS_PER_MSEC;
+ host->waitForTargetFps();
- consoleClear();
- #if _TEST
+ host->scanInput();
- printf("\n"); //make space for overflow if needed
- printf("svcGetSystemTick(): %lld \n", now_time);
- printf("frame time (ticks): %lld \n", frame_time);
- printf("frame time (ms): %f \n", frameTimeMs);
- printf("target fps: %d \n", targetFps);
- printf("target frame time (ms): %f \n", targetFrametimeMs);
- #endif
+ if (host->shouldQuit()) break; // break in order to return to hbmenu
- //sleep for remainder of time
- if (frameTimeMs < targetFrametimeMs) {
- double msToSleep = targetFrametimeMs - frameTimeMs;
-
- #if _TEST
- printf("sleeping for : %f ms\n", msToSleep);
- #endif
+ host->changeStretch();
- svcSleepThread(msToSleep * 1000 * 1000);
+ uint8_t p8kDown = host->getKeysDown();
+ uint8_t p8kHeld = host->getKeysHeld();
- last_time += CPU_TICKS_PER_MSEC * msToSleep;
- }
-
-
- //hidKeysDown returns information about which buttons have been just pressed (and they weren't in the previous frame)
- u32 kDown = hidKeysDown();
- u32 kHeld = hidKeysHeld();
-
- bool lpressed = kHeld & KEY_L;
- bool rpressed = kHeld & KEY_R;
-
- if (lpressed && rpressed) break; // break in order to return to hbmenu
-
- if (kDown & KEY_R) {
- ChangeStretch();
- }
-
- uint8_t p8kDown = ConvertInputToP8(kDown);
- uint8_t p8kHeld = ConvertInputToP8(kHeld);
-
-
- size_t stream_offset = 0;
-
- //_update();
-
+ vm->UpdateAndDraw(p8kDown, p8kHeld);
- //uint8_t* fb_b = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
- //clear whole framebuffer
- //memset(fb_b, bgcolor, 240*320*3);
-
- //cart draw
- //_draw();
-
- vm->UpdateAndDraw(frame_time, clear3dsFrameBuffer, p8kDown, p8kHeld);
+ uint8_t* picoFb = vm->GetPicoInteralFb();
+ uint8_t* screenPaletteMap = vm->GetScreenPaletteMap();
+ Color* paletteColors = vm->GetPaletteColors();
//send pico 8 screen to framebuffer, then call the function to flush and swap buffers, and wait for vblank
- uint8_t* fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, NULL, NULL);
+ host->drawFrame(picoFb, screenPaletteMap, paletteColors);
+ host->postFlipFunction();
- if (stretch == PixelPerfect) {
- vm->FlipBuffer_PP(fb, ScreenWidth, ScreenHeight, postFlip);
- }
- else if (stretch == StretchToFit) {
- vm->FlipBuffer_STF(fb, ScreenWidth, ScreenHeight, postFlip);
- }
- else if (stretch == StretchAndOverflow) {
- uint8_t* fbb = gfxGetFramebuffer(GFX_BOTTOM, GFX_LEFT, NULL, NULL);
+ if (host->shouldFillAudioBuff()) {
+ vm->FillAudioBuffer(host->getAudioBufferPointer(), 0, host->getAudioBufferSize());
- vm->FlipBuffer_SAO(
- fb, ScreenWidth, ScreenHeight,
- fbb, BottomScreenWidth, BottomScreenHeight,
- postFlip);
+ host->playFilledAudioBuffer();
}
-
- if (waveBuf[fillBlock].status == NDSP_WBUF_DONE) {
-
- vm->FillAudioBuffer(waveBuf[fillBlock].data_pcm16, stream_offset, waveBuf[fillBlock].nsamples);
-
- DSP_FlushDataCache(waveBuf[fillBlock].data_pcm16, waveBuf[fillBlock].nsamples);
-
- ndspChnWaveBufAdd(0, &waveBuf[fillBlock]);
- stream_offset += waveBuf[fillBlock].nsamples;
-
- fillBlock = !fillBlock;
- }
-
}
Logger::Write("Turning off vm and exiting logger\n");
vm->TurnOff();
delete vm;
+
Logger::Exit();
- audioCleanup();
- gfxExit();
+ host->oneTimeCleanup();
+ delete host;
return 0;
}
diff --git a/source/vm.cpp b/source/vm.cpp
index f072a30..5538804 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -10,6 +10,7 @@
#include "logger.h"
#include "Input.h"
#include "p8GlobalLuaFunctions.h"
+#include "hostVmShared.h"
extern "C" {
#include <lua.h>
@@ -184,15 +185,9 @@ void Vm::LoadCart(std::string filename){
//how to call lua from c: https://www.cs.usfca.edu/~galles/cs420/lecture/LuaLectures/LuaAndC.html
void Vm::UpdateAndDraw(
- uint64_t ticksSinceLastCall,
- std::function<void()> clearFbFunction,
uint8_t kdown,
uint8_t kheld)
{
- if (clearFbFunction) {
- clearFbFunction();
- }
-
_input->SetState(kdown, kheld);
if (_hasUpdate){
@@ -222,31 +217,16 @@ void Vm::UpdateAndDraw(
_picoFrameCount++;
}
-void Vm::FlipBuffer_PP(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction){
- _graphics->flipBuffer(fb, width, height);
-
- if (postFlipFunction) {
- postFlipFunction();
- }
+uint8_t* Vm::GetPicoInteralFb(){
+ return _graphics->GetP8FrameBuffer();
}
-void Vm::FlipBuffer_STF(uint8_t* fb, int width, int height, std::function<void()> postFlipFunction){
- _graphics->flipBuffer_STF(fb, width, height);
-
- if (postFlipFunction) {
- postFlipFunction();
- }
+uint8_t* Vm::GetScreenPaletteMap(){
+ return _graphics->GetScreenPaletteMap();
}
-void Vm::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_SAO(fb, width, height, fb_o, width_o, height_o);
-
- if (postFlipFunction) {
- postFlipFunction();
- }
+Color* Vm::GetPaletteColors(){
+ return _graphics->GetPaletteColors();
}
@@ -254,8 +234,6 @@ void Vm::FillAudioBuffer(void *audioBuffer, size_t offset, size_t size){
_audio->FillAudioBuffer(audioBuffer, offset, size);
}
-
-
void Vm::TurnOff() {
lua_close(_luaState);
}
diff --git a/source/vm.h b/source/vm.h
index 99fa760..fb8cb85 100644
--- a/source/vm.h
+++ b/source/vm.h
@@ -1,7 +1,5 @@
#pragma once
-#include <functional>
-
#include "cart.h"
#include "Input.h"
#include "Audio.h"
@@ -12,12 +10,6 @@ extern "C" {
#include <lauxlib.h>
}
-enum StretchOption {
- PixelPerfect,
- StretchToFit,
- StretchAndOverflow
-};
-
class Vm {
Cart* _loadedCart;
Graphics* _graphics;
@@ -38,21 +30,14 @@ class Vm {
void LoadCart(std::string filename);
void UpdateAndDraw(
- uint64_t ticksSinceLastCall,
- std::function<void()> clearFbFunction,
uint8_t kdown,
uint8_t kheld);
- 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);
+ uint8_t* GetPicoInteralFb();
+ uint8_t* GetScreenPaletteMap();
+ Color* GetPaletteColors();
- void FillAudioBuffer(void *audioBuffer,size_t offset, size_t size);
+ void FillAudioBuffer(void *audioBuffer, size_t offset, size_t size);
void TurnOff();