commit fd1d93f59b2c47fd723ff63643ca6e98dfae3c56
Author: Jon Bell <jondbell@protonmail.com>
AuthorDate: Wed Apr 22 13:32:12 2020 -0600
Commit: Jon Bell <jondbell@protonmail.com>
CommitDate: Wed Apr 22 13:32:12 2020 -0600
Make sprite sheet memory behave like pico to prep for map
---
source/graphics.cpp | 65 +++++++++++++--------------------
source/graphics.h | 7 ++--
source/main.cpp | 3 ++
source/stringToDataHelpers.cpp | 44 ++++++++++++++++++++++
source/stringToDataHelpers.h | 7 ++++
source/tests/stringToDataHelpers_test.h | 15 ++++++++
6 files changed, 98 insertions(+), 43 deletions(-)
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 18312e9..2bd7342 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -6,6 +6,8 @@
#include "graphics.h"
+#include "stringToDataHelpers.h"
+
#include "logger.h"
const Color BgGray = BG_GRAY_COLOR;
@@ -30,41 +32,6 @@ const Color PaletteColors[] = {
};
-
-void copy_data_to_sprites(uint8_t sprite_data[128 * 128], std::string data) {
- uint16_t i = 0;
-
- for (size_t n = 0; n < data.length(); n++) {
- char buf[3] = {0};
-
- if (data[n] > ' ') {
- buf[0] = data[n++];
- buf[1] = data[n];
- uint8_t val = (uint8_t)strtol(buf, NULL, 16);
-
- sprite_data[i++] = val >> 4;
- sprite_data[i++] = val & 0x0f;
- }
- }
-}
-
-void copy_data_to_sprite_flags(uint8_t sprite_flag_data[256], std::string data) {
- uint16_t i = 0;
-
- for (size_t n = 0; n < data.length(); n++) {
- char buf[3] = {0};
-
- if (data[n] > ' ') {
- buf[0] = data[n++];
- buf[1] = data[n];
- uint8_t val = (uint8_t)strtol(buf, NULL, 16);
-
- sprite_flag_data[i++] = val;
- }
- }
-}
-
-
//call initialize to make sure defaults are correct
Graphics::Graphics(std::string fontdata) {
this->_gfxState_color = 7;
@@ -103,10 +70,16 @@ void Graphics::copySpriteToScreen(
short dy = 1;
for (short y = 0; y < scr_h; y++) {
- uint8_t* spr = spritebuffer + ((spr_y + y * dy) & 0x7f) * 128;
+ uint8_t* spr = spritebuffer + ((spr_y + y * dy) & 0x7f) * 64;
for (short x = 0; x < scr_w; x++) {
- uint8_t c = spr[(spr_x + x) & 0x7f];
+ short combinedPixIdx = spr_x / 2 + x / 2;
+ uint8_t bothPix = spr[combinedPixIdx];
+
+ uint8_t c = x % 2 == 0
+ ? bothPix & 0x0f //just first 4 bits
+ : bothPix >> 4; //just last 4 bits
+
if (c != 0) { //if not transparent. Come back later to add palt() support by checking tranparency palette
pset(scr_x + x, scr_y + y, c); //set color on framebuffer. Come back later and add pal() by translating color
}
@@ -150,18 +123,30 @@ void Graphics::copyStretchSpriteToScreen(
}
for (int y = 0; y < scr_h; y++) {
- uint8_t* spr = spritebuffer + (((spr_y + y * dy) >> 16) & 0x7f) * 128;
+ uint8_t* spr = spritebuffer + (((spr_y + y * dy) >> 16) & 0x7f) * 64;
if (!flip_x) {
for (int x = 0; x < scr_w; x++) {
- uint8_t c = spr[((spr_x + x * dx) >> 16) & 0x7f];
+ int pixIndex = (spr_x + x * dx);
+ int combinedPixIdx = ((pixIndex / 2) >> 16) & 0x7f;
+ uint8_t bothPix = spr[combinedPixIdx];
+
+ uint8_t c = (pixIndex >> 16) % 2 == 0
+ ? bothPix & 0x0f //just first 4 bits
+ : bothPix >> 4; //just last 4 bits
if (c != 0) {
pset(scr_x + x, scr_y + y, c);
}
}
} else {
for (int x = 0; x < scr_w; x++) {
- uint8_t c = spr[((spr_x + spr_w - (x + 1) * dx) >> 16) & 0x7f];
+ int pixIndex = (spr_x + spr_w - (x + 1) * dx);
+ int combinedPixIdx = ((pixIndex / 2) >> 16) & 0x7f;
+ uint8_t bothPix = spr[combinedPixIdx];
+
+ uint8_t c = (pixIndex >> 16) % 2 == 0
+ ? bothPix & 0x0f //just first 4 bits
+ : bothPix >> 4; //just last 4 bits
if (c != 0) {
pset(scr_x + x, scr_y + y, c);
}
diff --git a/source/graphics.h b/source/graphics.h
index 5be1cb1..3a99dac 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -31,8 +31,10 @@ struct Color {
class Graphics {
uint8_t _pico8_fb[128*128];
- uint8_t fontSpriteData[128 * 128];
- uint8_t spriteSheetData[128 * 128];
+ uint8_t fontSpriteData[128 * 64];
+
+ uint8_t spriteSheetData[128 * 64];
+ uint8_t mapData[128 * 64];
uint8_t spriteFlags[256];
uint8_t _gfxState_color;
@@ -117,6 +119,5 @@ class Graphics {
void flipBuffer(uint8_t* fb);
-
};
diff --git a/source/main.cpp b/source/main.cpp
index 360091b..5b0e9a1 100644
--- a/source/main.cpp
+++ b/source/main.cpp
@@ -16,6 +16,8 @@
//this has the macro _TEST defined in it
#include "tests/test_base.h"
+
+//3ds specific helper function
uint8_t ConvertInputToP8(u32 input){
uint8_t result = 0;
if (input & KEY_LEFT){
@@ -53,6 +55,7 @@ uint8_t ConvertInputToP8(u32 input){
return result;
}
+//3ds specific helper function
void postFlip3dsFunction() {
gfxFlushBuffers();
gfxSwapBuffers();
diff --git a/source/stringToDataHelpers.cpp b/source/stringToDataHelpers.cpp
new file mode 100644
index 0000000..5f315fa
--- /dev/null
+++ b/source/stringToDataHelpers.cpp
@@ -0,0 +1,44 @@
+#include <string>
+
+#include "stringToDataHelpers.h"
+
+void copy_data_to_sprites(uint8_t sprite_data[128 * 64], std::string data) {
+ uint16_t i = 0;
+
+ for (size_t n = 0; n < data.length(); n++) {
+ char buf[3] = {0};
+
+ //https://pico-8.fandom.com/wiki/Memory
+ // "An 8-bit byte represents two pixels, horizontally adjacent, where the most significant
+ // (leftmost) 4 bits is the right pixel of the pair, and the least significant 4 bits is
+ // the left pixel.
+ if (data[n] > ' ') {
+ //buf[0] = data[n++];
+ //buf[1] = data[n];
+ buf[1] = data[n++]; //left pixel
+ buf[0] = data[n];
+ uint8_t val = (uint8_t)strtol(buf, NULL, 16);
+
+ //this was the impl before combining nibbles into a single byte
+ //sprite_data[i++] = val >> 4;
+ //sprite_data[i++] = val & 0x0f;
+ sprite_data[i++] = val;
+ }
+ }
+}
+
+void copy_data_to_sprite_flags(uint8_t sprite_flag_data[256], std::string data) {
+ uint16_t i = 0;
+
+ for (size_t n = 0; n < data.length(); n++) {
+ char buf[3] = {0};
+
+ if (data[n] > ' ') {
+ buf[0] = data[n++];
+ buf[1] = data[n];
+ uint8_t val = (uint8_t)strtol(buf, NULL, 16);
+
+ sprite_flag_data[i++] = val;
+ }
+ }
+}
\ No newline at end of file
diff --git a/source/stringToDataHelpers.h b/source/stringToDataHelpers.h
new file mode 100644
index 0000000..dbe498f
--- /dev/null
+++ b/source/stringToDataHelpers.h
@@ -0,0 +1,7 @@
+#pragma once
+
+#include <string>
+
+void copy_data_to_sprites(uint8_t sprite_data[128 * 64], std::string data);
+
+void copy_data_to_sprite_flags(uint8_t sprite_flag_data[256], std::string data);
\ No newline at end of file
diff --git a/source/tests/stringToDataHelpers_test.h b/source/tests/stringToDataHelpers_test.h
new file mode 100644
index 0000000..9a387a9
--- /dev/null
+++ b/source/tests/stringToDataHelpers_test.h
@@ -0,0 +1,15 @@
+#include "test_base.h"
+
+#if _TEST
+
+#pragma once
+
+#include <string>
+
+#include "../stringToDataHelpers.h"
+
+//TODO: add tests here
+//bool verifyCopySpriteStringToData();
+
+
+#endif