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:
Denis Kasak 2009-06-12 10:22:43 +00:00
parent 2308748f01
commit 32d12e90e3
2 changed files with 19 additions and 15 deletions

View file

@ -133,7 +133,7 @@ int DraciEngine::go() {
// Draw small string
path = "Small.fon";
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,
(320 - font.getStringWidth(testString, 1)) / 2, 150, 1);
_system->unlockScreen();

View file

@ -117,27 +117,27 @@ uint8 DraciFont::getCharWidth(uint8 chr) const {
void DraciFont::drawChar(Graphics::Surface *dst, uint8 chr, int tx, int ty) const {
assert(dst != NULL);
assert(tx >= 0);
assert(ty >= 0);
byte *ptr = (byte *)dst->getBasePtr(tx, ty);
uint8 charIndex = chr - kCharIndexOffset;
int charOffset = charIndex * _fontHeight * _maxCharWidth;
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
if (ty + y < 0 || ty + y >= dst->h) {
continue;
}
// Determine how many pixels to draw vertically
int ySpaceLeft = dst->h - ty - 1;
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
int curr = ((int)y) * _maxCharWidth + x;
int curr = y * _maxCharWidth + x;
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,
int x, int y, int spacing) const {
assert(dst != NULL);
assert(dst != NULL);
assert(x >= 0);
assert(y >= 0);
int curx = x;
uint len = str.size();
for (unsigned int i = 0; i < len; ++i) {
drawChar(dst, str[i], curx, y);
curx += getCharWidth(str[i]) + spacing;