[go: nahoru, domu]

Skip to content

Commit

Permalink
Fix a couple obscure print issues
Browse files Browse the repository at this point in the history
*keep track of line height correctly
*honor character size when smaller than default
  • Loading branch information
jtothebell committed Aug 19, 2022
1 parent c961fe1 commit fc9f839
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 19 deletions.
17 changes: 14 additions & 3 deletions source/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,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 @@ -1306,10 +1313,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
32 changes: 20 additions & 12 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 @@ -329,7 +331,9 @@ int print(std::string str, int x, int y, uint8_t c) {
}
else if (ch == '\n') {
x = homeX;
lineHeight = lineHeight > 0 ? lineHeight : 6;
y += lineHeight;
lineHeight = 0;
}
else if (ch == '\t') {
while (x % (tabStopWidth*4) > 0) {
Expand All @@ -343,12 +347,13 @@ int print(std::string str, int x, int y, uint8_t c) {
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 @@ -359,14 +364,17 @@ int print(std::string str, int x, int y, uint8_t c) {
//soft wrap if enabled
if (rhsWrap > 0 && x >= rhsWrap) {
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 = y + lineHeight;

Expand Down
71 changes: 68 additions & 3 deletions test/graphicstests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1975,7 +1975,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 @@ -2241,11 +2241,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
43 changes: 43 additions & 0 deletions test/printHelperTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -694,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 fc9f839

Please sign in to comment.