foxygit / fake-08-xbox360 Log in
commit 7476518fbfd3c7bfbeeed87d6ea7b15df34f8728
Author:     Jon Bell <jondbell@protonmail.com>
AuthorDate: Sat Jun 13 22:07:33 2020 -0600
Commit:     Jon Bell <jondbell@protonmail.com>
CommitDate: Sat Jun 13 22:07:33 2020 -0600

    Move text to data conversion to cart class
---
 source/Audio.cpp       |  97 ---------------------
 source/Audio.h         |   3 -
 source/cart.cpp        | 227 ++++++++++++++++++++++++++++++++++++++++++-------
 source/cart.h          |  19 +++++
 source/filehelpers.cpp |  13 ++-
 source/graphics.cpp    |  13 ---
 source/graphics.h      |   4 -
 source/vm.cpp          |  23 +++--
 8 files changed, 243 insertions(+), 156 deletions(-)

diff --git a/source/Audio.cpp b/source/Audio.cpp
index ea0b9e9..09a6e4e 100644
--- a/source/Audio.cpp
+++ b/source/Audio.cpp
@@ -19,103 +19,6 @@ Audio::Audio(PicoRam* memory){
     }
 }

-void Audio::setMusic(std::string musicString){
-    std::istringstream s(musicString);
-    std::string line;
-    char buf[3] = {0};
-    int musicIdx = 0;
-
-    while (std::getline(s, line)) {
-        buf[0] = line[0];
-        buf[1] = line[1];
-        uint8_t flagByte = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[3];
-        buf[1] = line[4];
-        uint8_t channel1byte = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[5];
-        buf[1] = line[6];
-        uint8_t channel2byte = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[7];
-        buf[1] = line[8];
-        uint8_t channel3byte = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[9];
-        buf[1] = line[10];
-        uint8_t channel4byte = (uint8_t)strtol(buf, NULL, 16);
-
-        _memory->_songs[musicIdx++] = {
-            flagByte,
-            channel1byte,
-            channel2byte,
-            channel3byte,
-            channel4byte
-        };
-    }
-
-}
-
-void Audio::setSfx(std::string sfxString) {
-    std::istringstream s(sfxString);
-    std::string line;
-    char buf[3] = {0};
-    int sfxIdx = 0;
-
-    while (std::getline(s, line)) {
-        buf[0] = line[0];
-        buf[1] = line[1];
-        uint8_t editorMode = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[2];
-        buf[1] = line[3];
-        uint8_t noteDuration = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[4];
-        buf[1] = line[5];
-        uint8_t loopRangeStart = (uint8_t)strtol(buf, NULL, 16);
-
-        buf[0] = line[6];
-        buf[1] = line[7];
-        uint8_t loopRangeEnd = (uint8_t)strtol(buf, NULL, 16);
-
-        _memory->_sfx[sfxIdx].editorMode = editorMode;
-        _memory->_sfx[sfxIdx].speed = noteDuration;
-        _memory->_sfx[sfxIdx].loopRangeStart = loopRangeStart;
-        _memory->_sfx[sfxIdx].loopRangeEnd = loopRangeEnd;
-
-        //32 notes, 5 chars each
-        int noteIdx = 0;
-        for (int i = 8; i < 168; i+=5) {
-            buf[0] = line[i];
-            buf[1] = line[i + 1];
-            uint8_t key = (uint8_t)strtol(buf, NULL, 16);
-
-            buf[0] = '0';
-            buf[1] = line[i + 2];
-            uint8_t waveform = (uint8_t)strtol(buf, NULL, 16);
-
-            buf[0] = '0';
-            buf[1] = line[i + 3];
-            uint8_t volume = (uint8_t)strtol(buf, NULL, 16);
-
-            buf[0] = '0';
-            buf[1] = line[i + 4];
-            uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
-
-            _memory->_sfx[sfxIdx].notes[noteIdx++] = {
-                key,
-                waveform,
-                volume,
-                effect
-            };
-        }
-
-        sfxIdx++;
-    }
-}
-
 void Audio::api_sfx(uint8_t sfx, int channel, int offset){

     if (sfx < -2 || sfx > 63 || channel < -1 || channel > 3 || offset > 31) {
diff --git a/source/Audio.h b/source/Audio.h
index e438f44..316b796 100644
--- a/source/Audio.h
+++ b/source/Audio.h
@@ -84,9 +84,6 @@ class Audio {
     public:
     Audio(PicoRam* memory);

-    void setSfx(std::string sfxString);
-    void setMusic(std::string musicString);
-
     void api_sfx(uint8_t sfx, int channel, int offset);
     void api_music(uint8_t pattern, int16_t fade_len, int16_t mask);

diff --git a/source/cart.cpp b/source/cart.cpp
index 3e6c1c9..258dfe6 100644
--- a/source/cart.cpp
+++ b/source/cart.cpp
@@ -5,6 +5,8 @@
 #include "cart.h"
 #include "filehelpers.h"

+#include "stringToDataHelpers.h"
+
 #include "utils.h"

 #include "logger.h"
@@ -86,53 +88,96 @@ std::string convert_emojis(const std::string& lua) {
 	return res;
 }

+bool hasEnding (std::string const &fullString, std::string const &ending) {
+    if (fullString.length() >= ending.length()) {
+        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
+    } else {
+        return false;
+    }
+}
+
 //tac08 based cart parsing and stripping of emoji
 Cart::Cart(std::string filename){
     Filename = filename;\
     Logger::Write("getting file contents\n");
+
+    if (hasEnding(filename, ".p8")){
+        std::string cartStr;

-    std::string cartStr;
-
-    if (filename == "__FAKE08-BIOS.p8") {
-        cartStr = fake08BiosP8;
-    }
-    else {
-        cartStr = get_file_contents(filename.c_str());
-    }
-    Logger::Write("Got file contents... parsing cart\n");
+        if (filename == "__FAKE08-BIOS.p8") {
+            cartStr = fake08BiosP8;
+        }
+        else {
+            cartStr = get_file_contents(filename.c_str());
+        }
+        Logger::Write("Got file contents... parsing cart\n");

-    fullCartText = cartStr;
+        fullCartText = cartStr;

-    std::istringstream s(cartStr);
-    std::string line;
-    std::string currSec = "";
-
-    while (std::getline(s, line)) {
-		line = utils::trimright(line, " \n\r");
-        line = convert_emojis(line);
+        std::istringstream s(cartStr);
+        std::string line;
+        std::string currSec = "";
+
+        while (std::getline(s, line)) {
+            line = utils::trimright(line, " \n\r");
+            line = convert_emojis(line);

-        if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
-            currSec = line;
-        }
-        else if (currSec == "__lua__"){
-            LuaString += line + "\n";
+            if (line.length() > 2 && line[0] == '_' && line[1] == '_') {
+                currSec = line;
+            }
+            else if (currSec == "__lua__"){
+                LuaString += line + "\n";
+            }
+            else if (currSec == "__gfx__"){
+                SpriteSheetString += line + "\n";
+            }
+            else if (currSec == "__gff__"){
+                SpriteFlagsString += line + "\n";
+            }
+            else if (currSec == "__map__"){
+                MapString += line + "\n";
+            }
+            else if (currSec == "__sfx__"){
+                SfxString += line + "\n";
+            }
+            else if (currSec == "__music__"){
+                MusicString += line + "\n";
+            }
         }
-        else if (currSec == "__gfx__"){
-            SpriteSheetString += line + "\n";
+
+        //I don't think this should be necessary, but things seem to be lingering between cart loads
+        //even after destruction and construction of a new cart
+        for(size_t i = 0; i < sizeof(SpriteSheetData); i++) {
+            SpriteSheetData[i] = 0;
         }
-        else if (currSec == "__gff__"){
-            SpriteFlagsString += line + "\n";
+        for(size_t i = 0; i < sizeof(SpriteFlagsData); i++) {
+            SpriteFlagsData[i] = 0;
         }
-        else if (currSec == "__map__"){
-            MapString += line + "\n";
+        for(size_t i = 0; i < sizeof(MapData); i++) {
+            MapData[i] = 0;
         }
-        else if (currSec == "__sfx__"){
-             SfxString += line + "\n";
+        for(size_t i = 0; i < 64; i++) {
+            SfxData[i] = {0};
         }
-        else if (currSec == "__music__"){
-             MusicString += line + "\n";
+        for(size_t i = 0; i < 64; i++) {
+            SongData[i] = {0};
         }
-	}
+
+        Logger::Write("Setting cart graphics rom data from strings\n");
+        setSpriteSheet(SpriteSheetString);
+        setSpriteFlags(SpriteFlagsString);
+        setMapData(MapString);
+
+        Logger::Write("Setting cart audio rom data from strings\n");
+        setSfx(SfxString);
+        setMusic(MusicString);
+    }
+    else if (hasEnding(filename, ".p8.png")) {
+        //parse png cart
+    }
+    else {
+        return;
+    }

     const char * patched = getPatchedLua(LuaString.c_str());

@@ -147,3 +192,119 @@ Cart::Cart(std::string filename){

     #endif
 }
+
+Cart::~Cart(){
+
+}
+
+void Cart::setSpriteSheet(std::string spritesheetstring){
+	Logger::Write("Copying data to spritesheet\n");
+	copy_string_to_sprite_memory(SpriteSheetData, spritesheetstring);
+}
+
+void Cart::setSpriteFlags(std::string spriteFlagsstring){
+	Logger::Write("Copying data to sprite flags\n");
+	copy_string_to_memory(SpriteFlagsData, spriteFlagsstring);
+}
+
+void Cart::setMapData(std::string mapDataString){
+	Logger::Write("Copying data to map data\n");
+	copy_string_to_memory(MapData, mapDataString);
+}
+
+void Cart::setMusic(std::string musicString){
+    std::istringstream s(musicString);
+    std::string line;
+    char buf[3] = {0};
+    int musicIdx = 0;
+
+    while (std::getline(s, line)) {
+        buf[0] = line[0];
+        buf[1] = line[1];
+        uint8_t flagByte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[3];
+        buf[1] = line[4];
+        uint8_t channel1byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[5];
+        buf[1] = line[6];
+        uint8_t channel2byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[7];
+        buf[1] = line[8];
+        uint8_t channel3byte = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[9];
+        buf[1] = line[10];
+        uint8_t channel4byte = (uint8_t)strtol(buf, NULL, 16);
+
+        SongData[musicIdx++] = {
+            flagByte,
+            channel1byte,
+            channel2byte,
+            channel3byte,
+            channel4byte
+        };
+    }
+
+}
+
+void Cart::setSfx(std::string sfxString) {
+    std::istringstream s(sfxString);
+    std::string line;
+    char buf[3] = {0};
+    int sfxIdx = 0;
+
+    while (std::getline(s, line)) {
+        buf[0] = line[0];
+        buf[1] = line[1];
+        uint8_t editorMode = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[2];
+        buf[1] = line[3];
+        uint8_t noteDuration = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[4];
+        buf[1] = line[5];
+        uint8_t loopRangeStart = (uint8_t)strtol(buf, NULL, 16);
+
+        buf[0] = line[6];
+        buf[1] = line[7];
+        uint8_t loopRangeEnd = (uint8_t)strtol(buf, NULL, 16);
+
+        SfxData[sfxIdx].editorMode = editorMode;
+        SfxData[sfxIdx].speed = noteDuration;
+        SfxData[sfxIdx].loopRangeStart = loopRangeStart;
+        SfxData[sfxIdx].loopRangeEnd = loopRangeEnd;
+
+        //32 notes, 5 chars each
+        int noteIdx = 0;
+        for (int i = 8; i < 168; i+=5) {
+            buf[0] = line[i];
+            buf[1] = line[i + 1];
+            uint8_t key = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 2];
+            uint8_t waveform = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 3];
+            uint8_t volume = (uint8_t)strtol(buf, NULL, 16);
+
+            buf[0] = '0';
+            buf[1] = line[i + 4];
+            uint8_t effect = (uint8_t)strtol(buf, NULL, 16);
+
+            SfxData[sfxIdx].notes[noteIdx++] = {
+                key,
+                waveform,
+                volume,
+                effect
+            };
+        }
+
+        sfxIdx++;
+    }
+}
diff --git a/source/cart.h b/source/cart.h
index 57a1938..799b56c 100644
--- a/source/cart.h
+++ b/source/cart.h
@@ -8,15 +8,34 @@
 class Cart {
     std::string fullCartText;

+    void setSpriteSheet(std::string spriteSheetString);
+	void setSpriteFlags(std::string spriteFlagsString);
+	void setMapData(std::string mapString);
+
+    void setSfx(std::string sfxString);
+    void setMusic(std::string musicString);
+
     public:
     Cart (std::string filename);
+    ~Cart();

     std::string Filename;
+
     std::string LuaString;
+
     std::string SpriteSheetString;
+    uint8_t SpriteSheetData[128 * 64];
+
     std::string SpriteFlagsString;
+    uint8_t SpriteFlagsData[256];
+
     std::string MapString;
+    uint8_t MapData[128 * 32];
+
     std::string SfxString;
+    sfx SfxData[64];
+
     std::string MusicString;
+    song SongData[64];

 };
diff --git a/source/filehelpers.cpp b/source/filehelpers.cpp
index 62af738..9e91b86 100644
--- a/source/filehelpers.cpp
+++ b/source/filehelpers.cpp
@@ -1,5 +1,6 @@
 #include <string>
 #include <fstream>
+#include <vector>

 //http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
 std::string get_file_contents(std::string filename){
@@ -16,4 +17,14 @@ std::string get_file_contents(std::string filename){
     }

   return "";
-}
\ No newline at end of file
+}
+
+//https://stackoverflow.com/questions/5420317/reading-and-writing-binary-file
+std::vector<unsigned char> get_file_buffer(std::string filename){
+  std::ifstream in(filename, std::ios::binary);
+
+  std::vector<unsigned char> buffer(std::istreambuf_iterator<char>(in), {});
+
+  return buffer;
+}
+
diff --git a/source/graphics.cpp b/source/graphics.cpp
index e51f779..9cb797d 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -43,20 +43,7 @@ Graphics::Graphics(std::string fontdata, PicoRam* memory) {
 	_paletteColors[15] = COLOR_15;
 }

-void Graphics::setSpriteSheet(std::string spritesheetstring){
-	Logger::Write("Copying data to spritesheet\n");
-	copy_string_to_sprite_memory(_memory->spriteSheetData, spritesheetstring);
-}
-
-void Graphics::setSpriteFlags(std::string spriteFlagsstring){
-	Logger::Write("Copying data to sprite flags\n");
-	copy_string_to_memory(_memory->spriteFlags, spriteFlagsstring);
-}

-void Graphics::setMapData(std::string mapDataString){
-	Logger::Write("Copying data to map data\n");
-	copy_string_to_memory(_memory->mapData, mapDataString);
-}

 uint8_t* Graphics::GetP8FrameBuffer(){
 	return this->_pico8_fb;
diff --git a/source/graphics.h b/source/graphics.h
index 7721ce0..afc03d4 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -76,10 +76,6 @@ class Graphics {
 	public:
 	Graphics(std::string fontdata, PicoRam* memory);

-	void setSpriteSheet(std::string spriteSheetString);
-	void setSpriteFlags(std::string spriteFlagsString);
-	void setMapData(std::string mapString);
-
 	uint8_t* GetP8FrameBuffer();
 	uint8_t* GetScreenPaletteMap();
 	Color* GetPaletteColors();
diff --git a/source/vm.cpp b/source/vm.cpp
index 132594d..de28f58 100644
--- a/source/vm.cpp
+++ b/source/vm.cpp
@@ -2,6 +2,8 @@
 #include <functional>
 #include <math.h>

+#include <string.h>
+
 #include "vm.h"
 #include "graphics.h"
 #include "fontdata.h"
@@ -72,13 +74,24 @@ bool Vm::loadCart(Cart* cart) {
     }
     _memory._musicChannel.pattern = -1;

+    //copy data from cart rom to ram
+    for(size_t i = 0; i < sizeof(_memory.spriteSheetData); i++) {
+        _memory.spriteSheetData[i] = cart->SpriteSheetData[i];
+    }
+    for(size_t i = 0; i < sizeof(_memory.spriteFlags); i++) {
+        _memory.spriteFlags[i] = cart->SpriteFlagsData[i];
+    }
+    for(size_t i = 0; i < sizeof(_memory.mapData); i++) {
+        _memory.mapData[i] = cart->MapData[i];
+    }

-    _graphics->setSpriteSheet(cart->SpriteSheetString);
-    _graphics->setSpriteFlags(cart->SpriteFlagsString);
-    _graphics->setMapData(cart->MapString);
+    for(size_t i = 0; i < 64; i++) {
+        _memory._sfx[i] = cart->SfxData[i];
+    }

-    _audio->setSfx(cart->SfxString);
-    _audio->setMusic(cart->MusicString);
+    for(size_t i = 0; i < 64; i++) {
+        _memory._songs[i] = cart->SongData[i];
+    }

     // initialize Lua interpreter
     _luaState = luaL_newstate();