[go: nahoru, domu]

Skip to content

Commit

Permalink
Merge pull request jtothebell#164 from jtothebell/PrintFixes
Browse files Browse the repository at this point in the history
Print fixes
  • Loading branch information
jtothebell committed Aug 30, 2022
2 parents 0621333 + 8207c73 commit 5342d1f
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 25 deletions.
17 changes: 14 additions & 3 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1299,8 +1299,15 @@ fix32 Graphics::fillp(fix32 pat) {
}


int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
int Graphics::drawCharacter(
uint8_t ch,
int x,
int y,
uint8_t printMode,
int forceCharWidth,
int forceCharHeight) {
int extraCharWidth = 0;

if ((printMode & PRINT_MODE_ON) == PRINT_MODE_ON){
int scrW = 4;
int scrH = 5;
Expand Down Expand Up @@ -1332,10 +1339,14 @@ int Graphics::drawCharacter(uint8_t ch, int x, int y, uint8_t printMode) {
else{
if (ch >= 0x10 && ch < 0x80) {
int index = ch - 0x10;
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, 4, 5, false, false);
int width = forceCharWidth > -1 && forceCharWidth < 4 ? forceCharWidth : 4;
int height = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : 5;
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8, width, height, false, false);
} else if (ch >= 0x80) {
int index = ch - 0x80;
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, 8, 5, false, false);
int width = forceCharWidth > -1 && forceCharWidth < 4 ? (forceCharWidth + 4) : 8;
int height = forceCharHeight > -1 && forceCharHeight < 5 ? forceCharHeight : 5;
copySpriteToScreen(fontSpriteData, x, y, (index % 16) * 8, (index / 16) * 8 + 56, width, height, false, false);
extraCharWidth = 4;
}
}
Expand Down
9 changes: 8 additions & 1 deletion source/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,14 @@ class Graphics {

fix32 fillp(fix32 pat);

int drawCharacter(uint8_t ch, int x, int y, uint8_t printMode = 0);
int drawCharacter(
uint8_t ch,
int x,
int y,
uint8_t printMode = 0,
int forceCharWidth = -1,
int forceCharHeight = -1);

std::tuple<int, int> drawCharacterFromBytes(
uint8_t chBytes[],
int x,
Expand Down
43 changes: 26 additions & 17 deletions source/printHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ int print(std::string str, int x, int y) {
}

int print(std::string str, int x, int y, uint8_t c) {
if (y == 83) {
y +=0;
}
_ph_graphics->color(c);

_ph_mem->drawState.text_x = x;
Expand All @@ -95,7 +98,9 @@ int print(std::string str, int x, int y, uint8_t c) {
int tabStopWidth = 4;
int charWidth = 4;
int charHeight = 6;
int lineHeight = 6;
int lineHeight = 0;
int forceCharWidth = -1;
int forceCharHeight = -1;
uint8_t bgColor = 0;
uint8_t fgColor = _ph_mem->drawState.color;
uint8_t charBytes[8];
Expand Down Expand Up @@ -132,7 +137,8 @@ int print(std::string str, int x, int y, uint8_t c) {

ch = str[++n];
for(int i = 0; i < times; i++) {
x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode);
//TODO: combine with other draw character call - fix missing bg? lineheight?
x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode, forceCharWidth, forceCharHeight);
}
}
else if (ch == 2) { // "\#{p0}" draw text on a solid background color
Expand Down Expand Up @@ -210,15 +216,15 @@ int print(std::string str, int x, int y, uint8_t c) {
}
else if (commandChar == 'x'){
uint8_t charWidthChar = str[++n];
charWidth = p0CharToNum(charWidthChar);
forceCharWidth = p0CharToNum(charWidthChar);
charWidth = forceCharWidth;
}
else if (commandChar == 'y'){
//this behaves in a way I wouldn't expect in pico 8- it seems to apply to the next print statment
//but not if there is a \n in the string. Not sure if this is a bug or not
uint8_t charHeightChar = str[++n];
charHeight = p0CharToNum(charHeightChar);
//lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
lineHeight = charHeight;
forceCharHeight = p0CharToNum(charHeightChar);
charHeight = forceCharHeight;
}
else if (commandChar == 'w'){
printMode |= PRINT_MODE_ON;
Expand All @@ -229,7 +235,6 @@ int print(std::string str, int x, int y, uint8_t c) {
printMode |= PRINT_MODE_ON;
printMode |= PRINT_MODE_TALL;
charHeight = 12;
lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
}
else if (commandChar == '='){
printMode |= PRINT_MODE_ON;
Expand All @@ -256,8 +261,7 @@ int print(std::string str, int x, int y, uint8_t c) {
printMode |= PRINT_MODE_SOLID_BG;
}
else if (commandChar == ':' || commandChar == '.'){
lineHeight = 8;
charHeight = 8;
charHeight = forceCharHeight > 0 ? forceCharHeight : 8;
if (commandChar == ':') {
std::string hexStr = str.substr(n+1, 16);
n+=16;
Expand Down Expand Up @@ -294,7 +298,6 @@ int print(std::string str, int x, int y, uint8_t c) {
else if (turnOffModeChar == 't') {
printMode &= ~(PRINT_MODE_TALL);
charHeight = 6;
lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
}
else if (turnOffModeChar == '=') {
printMode &= ~(PRINT_MODE_STRIPEY);
Expand All @@ -305,7 +308,6 @@ int print(std::string str, int x, int y, uint8_t c) {
printMode &= ~(PRINT_MODE_STRIPEY);
charWidth = 4;
charHeight = 6;
lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
}
else if (turnOffModeChar == 'i') {
printMode &= ~(PRINT_MODE_INVERTED);
Expand All @@ -324,11 +326,14 @@ int print(std::string str, int x, int y, uint8_t c) {
uint8_t fgColChar = str[++n];
fgColor = p0CharToNum(fgColChar);
_ph_graphics->color(fgColor);
_ph_mem->drawState.drawPaletteMap[7] = _ph_graphics->getDrawPalMappedColor(fgColor);
//this is needed for legacy text drawing
_ph_mem->drawState.drawPaletteMap[7] = prevDrawPal[fgColor] & 0x0f;
}
else if (ch == '\n') {
x = _ph_mem->drawState.text_x;
x = homeX;
lineHeight = lineHeight > 0 ? lineHeight : 6;
y += lineHeight;
lineHeight = 0;
}
else if (ch == '\t') {
while (x % (tabStopWidth*4) > 0) {
Expand All @@ -339,15 +344,16 @@ int print(std::string str, int x, int y, uint8_t c) {
x -= charWidth;
}
else if (ch == '\r') {
x = _ph_mem->drawState.text_x;
x = homeX;
}
else if (ch >= 0x10) {
lineHeight = charHeight > lineHeight ? charHeight : lineHeight;
if (bgColor != 0) {
uint8_t prevPenColor = _ph_mem->drawState.color;
_ph_graphics->rectfill(x-1, y-1, x + charWidth-1, y + lineHeight-1, bgColor);
_ph_mem->drawState.color = prevPenColor;
}
x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode);
x += charWidth + _ph_graphics->drawCharacter(ch, x, y, printMode, forceCharWidth, forceCharHeight);
while (framesToPause > 0){
_ph_vm->vm_flip();

Expand All @@ -357,17 +363,20 @@ int print(std::string str, int x, int y, uint8_t c) {

//soft wrap if enabled
if (rhsWrap > 0 && x >= rhsWrap) {
x = _ph_mem->drawState.text_x;
x = homeX;
lineHeight = lineHeight > 0 ? lineHeight : 6;
y += lineHeight;
lineHeight = 0;
}
}

for(int i = 0; i < 16; i++) {
_ph_mem->drawState.drawPaletteMap[i] = prevDrawPal[i];
}

lineHeight = lineHeight > 0 ? lineHeight : 6;
//todo: auto scrolling
_ph_mem->drawState.text_y += lineHeight;
_ph_mem->drawState.text_y = y + lineHeight;

return x;
}
1 change: 0 additions & 1 deletion test/cartloadingtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,6 @@ TEST_CASE("Loading and running carts") {


//TODO: build library of carts to test- set up compiler flag?
//std::string _cartDirectory = "/Users/jon/p8carts/archive/carts";
std::string _cartDirectory = "";

DIR *dir;
Expand Down
71 changes: 68 additions & 3 deletions test/graphicstests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2089,7 +2089,7 @@ TEST_CASE("graphics class behaves as expected") {
checkPoints(graphics, expectedPoints);
}
*/
SUBCASE("Remap spritesheet to screen"){
SUBCASE("Remap spritesheet to screen (after drawing character to screen)"){
graphics->cls();

//emulate print("zo",8,0)
Expand Down Expand Up @@ -2355,11 +2355,76 @@ TEST_CASE("graphics class behaves as expected") {
CHECK_EQ(picoRam.data[0xFFFF], 9);
CHECK_EQ(picoRam.userData[0x7FFF], 9);
}

SUBCASE("drawCharacter with forced width and height for wide character"){
graphics->cls();

//emulate print("\^x0\^y1▤a",10,10,9)
picoRam.drawState.drawPaletteMap[7] = 9;
graphics->drawCharacter(152, 10, 10, 0, 0, 1);
//the a should be invisible (0 width)
graphics->drawCharacter(65, 10, 10, 0, 0, 1);

std::vector<coloredPoint> expectedPoints = {
{9,10,0},
{10,10,9},
{11,10,9},
{12,10,9},
{13,10,9},
{14,10,0},
{9,11,0},
{10,11,0},
{11,11,0},
{12,11,0},
{13,11,0},
{14,11,0},
{9,12,0},
{10,12,0},
{11,12,0},
{12,12,0},
{13,12,0},
{14,12,0},

};

checkPoints(graphics, expectedPoints);
}
SUBCASE("drawCharacter with forced width and height for normal character"){
graphics->cls();


//emulate print("\^x3\^y3a",10,10,9)
picoRam.drawState.drawPaletteMap[7] = 9;
graphics->drawCharacter(97, 10, 10, 0, 3, 3);

std::vector<coloredPoint> expectedPoints = {
{9,10,0},
{10,10,9},
{11,10,9},
{12,10,9},
{13,10,0},
{14,10,0},
{9,11,0},
{10,11,9},
{11,11,0},
{12,11,9},
{13,11,0},
{14,11,0},
{9,12,0},
{10,12,9},
{11,12,9},
{12,12,9},
{13,12,0},
{14,12,0},
{9,14,0},
{10,14,0},
{11,14,0},
{12,14,0},
{13,14,0},
{14,14,0},

};

checkPoints(graphics, expectedPoints);
}

//general teardown
delete graphics;
Expand Down
88 changes: 88 additions & 0 deletions test/printHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ TEST_CASE("Print helper functions") {
CHECK(memory->drawState.text_y == 24);
CHECK(memory->drawState.color == 14);
}
SUBCASE("print({str}, {x}, {y}, {c}) updates text location correct in multiline situation") {
graphics->cls();
memory->drawState.text_x = 3;
memory->drawState.text_y = 4;
memory->drawState.color = 10;

print("doesnt\nmatter\nwell\nkinda\ndoes", 19, 18, 14);

CHECK(memory->drawState.text_x == 19);
CHECK(memory->drawState.text_y == 48);
}
SUBCASE("print({str}) uses pal mapped color") {
graphics->cls();
graphics->color(2);
Expand Down Expand Up @@ -354,6 +365,40 @@ TEST_CASE("Print helper functions") {

checkPoints(graphics, expectedPoints);
}

SUBCASE("p8scii special control code move cursor(\\^j) and update home and color test") {
graphics->cls();
//"\^j87\-f\|a\^h\f7:\^je8other\n:"
/*
--\^j87- move cursor to 32,28
--\-f- move cursor horizontally -1 (31,28)
--\|a- move cursor vertically -6 (31,22)
--\^h --set cursor home to be current location (31,22)
--\f7 change color to 7 (white)
--: print the ":" character
--\^je8- move cursor to 56,32
--print "other\n" followed by a new line
--print one more ":"
*/
// coordinates x=40 ("a" = 10, 10 * 4 = 40), y=48 ("c" = 12, 12 * 4 = 48)
print("\x06""j87\x03""f\x04""a\x06""h\x0c""7:\x06""je8:\n:", 0, 0);

std::vector<coloredPoint> expectedPoints = {
{32, 22, 0},
{32, 23, 7},
{32, 24, 0},
{32, 25, 7},
{32, 26, 0},

{32, 38, 0},
{32, 39, 7},
{32, 40, 0},
{32, 41, 7},
{32, 42, 0},
};

checkPoints(graphics, expectedPoints);
}
SUBCASE("p8scii special control code tab stop width(\\^s) ") {
graphics->cls();

Expand Down Expand Up @@ -649,6 +694,49 @@ TEST_CASE("Print helper functions") {

checkPoints(graphics, expectedPoints);
}
SUBCASE("p8scii special control code for char width(\\^x) and char height (\\^y) limit rendering ") {
graphics->cls();

print("\x06""x2\x06""y3a", 0, 0);

std::vector<coloredPoint> expectedPoints = {
{0, 0, 6},
{1, 0, 6},
{2, 0, 0},
{3, 0, 0},

{0, 1, 6},
{1, 1, 0},
{2, 1, 0},
{3, 1, 0},

{0, 2, 6},
{1, 2, 6},
{2, 2, 0},
{3, 2, 0},

{0, 3, 0},
{1, 3, 0},
{2, 3, 0},
{3, 3, 0},
};

checkPoints(graphics, expectedPoints);
}
SUBCASE("p8scii special control code for char height (\\^y) sets line height correctly when lower") {
graphics->cls();

print("\x06""y3a", 0, 0);

CHECK_EQ(memory->drawState.text_y, 3);
}
SUBCASE("p8scii special control code for char height (\\^y) sets line height correctly when higher") {
graphics->cls();

print("\x06""y9a", 0, 0);

CHECK_EQ(memory->drawState.text_y, 9);
}


delete stubHost;
Expand Down

0 comments on commit 5342d1f

Please sign in to comment.