Removed overflow/underflow checks from DraciFont::drawChar(). Instead, we now calculate the number of pixels that can be drawn without overflowing beforehand. Also added asserts to catch any negative value passed for the coordinates.
svn-id: r41471
This commit is contained in:
parent
2308748f01
commit
32d12e90e3
2 changed files with 19 additions and 15 deletions
|
@ -133,7 +133,7 @@ int DraciEngine::go() {
|
||||||
// Draw small string
|
// Draw small string
|
||||||
path = "Small.fon";
|
path = "Small.fon";
|
||||||
font.setFont(path);
|
font.setFont(path);
|
||||||
testString = "I'm smaller than the font above me";
|
testString = "I'm smaller than the font above me.";
|
||||||
font.drawString(surf, testString,
|
font.drawString(surf, testString,
|
||||||
(320 - font.getStringWidth(testString, 1)) / 2, 150, 1);
|
(320 - font.getStringWidth(testString, 1)) / 2, 150, 1);
|
||||||
_system->unlockScreen();
|
_system->unlockScreen();
|
||||||
|
|
|
@ -117,27 +117,27 @@ uint8 DraciFont::getCharWidth(uint8 chr) const {
|
||||||
|
|
||||||
void DraciFont::drawChar(Graphics::Surface *dst, uint8 chr, int tx, int ty) const {
|
void DraciFont::drawChar(Graphics::Surface *dst, uint8 chr, int tx, int ty) const {
|
||||||
assert(dst != NULL);
|
assert(dst != NULL);
|
||||||
|
assert(tx >= 0);
|
||||||
|
assert(ty >= 0);
|
||||||
|
|
||||||
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
|
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
|
||||||
uint8 charIndex = chr - kCharIndexOffset;
|
uint8 charIndex = chr - kCharIndexOffset;
|
||||||
int charOffset = charIndex * _fontHeight * _maxCharWidth;
|
int charOffset = charIndex * _fontHeight * _maxCharWidth;
|
||||||
uint8 currentWidth = _charWidths[charIndex];
|
uint8 currentWidth = _charWidths[charIndex];
|
||||||
|
|
||||||
for (uint8 y = 0; y < _fontHeight; ++y) {
|
// Determine how many pixels to draw horizontally (to prevent overflow)
|
||||||
|
int xSpaceLeft = dst->w - tx - 1;
|
||||||
|
int xPixelsToDraw = (currentWidth < xSpaceLeft) ? currentWidth : xSpaceLeft;
|
||||||
|
|
||||||
// Check for vertical overflow
|
// Determine how many pixels to draw vertically
|
||||||
if (ty + y < 0 || ty + y >= dst->h) {
|
int ySpaceLeft = dst->h - ty - 1;
|
||||||
continue;
|
int yPixelsToDraw = (_fontHeight < ySpaceLeft) ? _fontHeight : ySpaceLeft;
|
||||||
}
|
|
||||||
|
for (int y = 0; y < yPixelsToDraw; ++y) {
|
||||||
|
for (int x = 0; x <= xPixelsToDraw; ++x) {
|
||||||
|
|
||||||
for (uint8 x = 0; x <= currentWidth; ++x) {
|
|
||||||
|
|
||||||
// Check for horizontal overflow
|
|
||||||
if (tx + x < 0 || tx + x >= dst->w) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Paint pixel
|
// Paint pixel
|
||||||
int curr = ((int)y) * _maxCharWidth + x;
|
int curr = y * _maxCharWidth + x;
|
||||||
ptr[x] = _charData[charOffset + curr];
|
ptr[x] = _charData[charOffset + curr];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,9 +158,13 @@ void DraciFont::drawChar(Graphics::Surface *dst, uint8 chr, int tx, int ty) cons
|
||||||
|
|
||||||
void DraciFont::drawString(Graphics::Surface *dst, Common::String str,
|
void DraciFont::drawString(Graphics::Surface *dst, Common::String str,
|
||||||
int x, int y, int spacing) const {
|
int x, int y, int spacing) const {
|
||||||
assert(dst != NULL);
|
assert(dst != NULL);
|
||||||
|
assert(x >= 0);
|
||||||
|
assert(y >= 0);
|
||||||
|
|
||||||
int curx = x;
|
int curx = x;
|
||||||
uint len = str.size();
|
uint len = str.size();
|
||||||
|
|
||||||
for (unsigned int i = 0; i < len; ++i) {
|
for (unsigned int i = 0; i < len; ++i) {
|
||||||
drawChar(dst, str[i], curx, y);
|
drawChar(dst, str[i], curx, y);
|
||||||
curx += getCharWidth(str[i]) + spacing;
|
curx += getCharWidth(str[i]) + spacing;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue