commit 446c209a598e13df44aaf630cf8f6e5870a9f85a
Author: Jon Bell <j.to.the.bell@gmail.com>
AuthorDate: Tue Mar 8 07:44:15 2022 -0700
Commit: Jon Bell <j.to.the.bell@gmail.com>
CommitDate: Tue Mar 8 07:44:15 2022 -0700
Add support for more p8scii control codes
\* repeat char
\# bg color
\- horizontal movement
\| vertical movement
\+ horizontal and vertical movement
\f foreground color
stub out \^ for special command
---
source/graphics.cpp | 89 +++++++++++++++++++++++++++++++++++++++----
source/graphics.h | 2 +
test/graphicstests.cpp | 101 +++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 184 insertions(+), 8 deletions(-)
diff --git a/source/graphics.cpp b/source/graphics.cpp
index 6adf18c..d791280 100644
--- a/source/graphics.cpp
+++ b/source/graphics.cpp
@@ -1257,6 +1257,19 @@ int Graphics::print(std::string str, int x, int y) {
return this->print(str, x, y, _memory->drawState.color);
}
+uint8_t p0CharToNum(uint8_t hexChar){
+ uint8_t num = 0;
+ if (hexChar > 47 && hexChar < 58){
+ num = hexChar - 48;
+ }
+ if (hexChar > 96) {
+ num = hexChar - 87;
+ }
+
+ return num;
+
+}
+
//based on tac08 impl
int Graphics::print(std::string str, int x, int y, uint8_t c) {
color(c);
@@ -1280,7 +1293,59 @@ int Graphics::print(std::string str, int x, int y, uint8_t c) {
for (size_t n = 0; n < str.length(); n++) {
uint8_t ch = str[n];
- if (ch == '\n') {
+ if (ch == 1) { // "\*{p0}" repeat the next character p0 times
+ uint8_t timesChr = str[++n];
+ int times = p0CharToNum(timesChr);
+
+ ch = str[++n];
+ for(int i = 0; i < times; i++) {
+ x = drawCharacter(ch, x, y);
+ }
+ }
+ else if (ch == 2) { // "\#{p0}" draw text on a solid background color
+ uint8_t bgColChar = str[++n];
+ uint8_t bgCol = p0CharToNum(bgColChar);
+
+ _memory->drawState.drawPaletteMap[0] = bgCol;
+ }
+ else if (ch == 3) { // "\-{p0}" move text cursor horizontally by 16-p0 pixels
+ uint8_t pixelCountChar = str[++n];
+ int deltaX = p0CharToNum(pixelCountChar) - 16;
+
+ x += deltaX;
+ //update memory cursor state?
+ }
+ else if (ch == 4) { // "\|{p0}" move text cursor vertically by 16-p0 pixels
+ uint8_t pixelCountChar = str[++n];
+ int deltaY = p0CharToNum(pixelCountChar) - 16;
+
+ y += deltaY;
+ //update memory cursor state?
+ }
+ else if (ch == 5) { // "\+{p0}" move text cursor vertically by 16-p0 pixels
+ uint8_t pixelCountXChar = str[++n];
+ uint8_t pixelCountYChar = str[++n];
+ int deltaX = p0CharToNum(pixelCountXChar) - 16;
+ int deltaY = p0CharToNum(pixelCountYChar) - 16;
+
+ x += deltaX;
+ y += deltaY;
+ //update memory cursor state?
+ }
+ else if (ch == 6) { // "\^" special command
+ uint8_t commandChar = str[++n];
+ if (commandChar > 48 && commandChar < 58){
+ //pause for x num frames
+ }
+
+ }
+ else if (ch == 12) { //"\f{p0}" draw text with this foreground color
+ uint8_t fgColChar = str[++n];
+ uint8_t fgCol = p0CharToNum(fgColChar);
+ color(fgCol);
+ _memory->drawState.drawPaletteMap[7] = getDrawPalMappedColor(fgCol);
+ }
+ else if (ch == '\n') {
x = _memory->drawState.text_x;
y += 6;
}
@@ -1296,13 +1361,7 @@ int Graphics::print(std::string str, int x, int y, uint8_t c) {
x = _memory->drawState.text_x;
}
else if (ch >= 0x10 && ch < 0x80) {
- int index = ch - 0x10;
- copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
- x += 4;
- } else if (ch >= 0x80) {
- int index = ch - 0x80;
- copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
- x += 8;
+ x = drawCharacter(ch, x, y);
}
}
@@ -1316,6 +1375,20 @@ int Graphics::print(std::string str, int x, int y, uint8_t c) {
return x;
}
+int Graphics::drawCharacter(uint8_t ch, int x, int y) {
+ if (ch >= 0x10 && ch < 0x80) {
+ int index = ch - 0x10;
+ copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
+ x += 4;
+ } else if (ch >= 0x80) {
+ int index = ch - 0x80;
+ copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
+ x += 8;
+ }
+
+ return x;
+}
+
void Graphics::spr(
int n,
int x,
diff --git a/source/graphics.h b/source/graphics.h
index 6e5500a..a03f676 100644
--- a/source/graphics.h
+++ b/source/graphics.h
@@ -112,6 +112,8 @@ class Graphics {
int print(std::string str, int x, int y);
int print(std::string str, int x, int y, uint8_t c);
+ int drawCharacter(uint8_t ch, int x, int y);
+
void spr(
int n,
int x,
diff --git a/test/graphicstests.cpp b/test/graphicstests.cpp
index a9b362f..2389d83 100644
--- a/test/graphicstests.cpp
+++ b/test/graphicstests.cpp
@@ -1790,10 +1790,20 @@ TEST_CASE("graphics class behaves as expected") {
}
SUBCASE("palt({c}, {t}) sets transparency value for color") {
graphics->palt(4, true);
+
+ CHECK(graphics->isColorTransparent(4) == true);
+
graphics->palt(4, false);
CHECK(graphics->isColorTransparent(4) == false);
}
+ SUBCASE("palt({c}, {t}) can set color 0 to opaque") {
+ CHECK(graphics->isColorTransparent(0) == true);
+
+ graphics->palt(0, false);
+
+ CHECK(graphics->isColorTransparent(0) == false);
+ }
SUBCASE("pal() resets all palette changes") {
graphics->pal(14, 5, 0);
graphics->pal(13, 4, 1);
@@ -2444,6 +2454,97 @@ TEST_CASE("graphics class behaves as expected") {
checkPoints(graphics, expectedPoints);
}
+ SUBCASE("p8scii repeat character (\\*) draws character x number of times") {
+ graphics->cls();
+
+ graphics->print("\x01""5:", 0, 0);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {17, 0, 0},
+ {17, 1, 6},
+ {17, 2, 0},
+ {17, 3, 6},
+ {17, 4, 0}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("p8scii bg color character (\\#) changes bg color") {
+ graphics->cls();
+
+ graphics->print("\x02""1:", 0, 0);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {1, 0, 1},
+ {1, 1, 6},
+ {1, 2, 1},
+ {1, 3, 6},
+ {1, 4, 1}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("p8scii fg color character (\\f) changes bg color") {
+ graphics->cls();
+
+ graphics->print("\x0c""2:", 0, 0);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {1, 0, 0},
+ {1, 1, 2},
+ {1, 2, 0},
+ {1, 3, 2},
+ {1, 4, 0}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("p8scii bg color character (\\#) changes bg color using hex") {
+ graphics->cls();
+
+ graphics->print("\x02""c:", 0, 0);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {1, 0, 12},
+ {1, 1, 6},
+ {1, 2, 12},
+ {1, 3, 6},
+ {1, 4, 12}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("p8scii horizontal move character (\\-) changes x location") {
+ graphics->cls();
+
+ graphics->print("\x03""a:", 10, 0);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {5, 0, 0},
+ {5, 1, 6},
+ {5, 2, 0},
+ {5, 3, 6},
+ {5, 4, 0}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+ SUBCASE("p8scii vertical move character (\\|) changes y location") {
+ graphics->cls();
+
+ graphics->print("\x05""ab:", 6, 5);
+
+ std::vector<coloredPoint> expectedPoints = {
+ {1, 0, 0},
+ {1, 1, 6},
+ {1, 2, 0},
+ {1, 3, 6},
+ {1, 4, 0}
+ };
+
+ checkPoints(graphics, expectedPoints);
+ }
+