From ddf078383a6e02ad512e6661bf9608e9965501b5 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Sun, 22 Sep 2013 03:59:33 -0700 Subject: [PATCH] Fix buffer underrun in mode 0 rendering --- src/gba/renderers/video-software.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/gba/renderers/video-software.c b/src/gba/renderers/video-software.c index 29d28afdd..39fea11fa 100644 --- a/src/gba/renderers/video-software.c +++ b/src/gba/renderers/video-software.c @@ -896,23 +896,31 @@ static void _drawBackgroundMode0(struct GBAVideoSoftwareRenderer* renderer, stru tileData = ((uint32_t*)renderer->d.vram)[charBase]; paletteData = mapData.palette << 4; if (!mapData.hflip) { + outX = renderer->end - mod8; + if (outX < 0) { + outX = 0; + } if (!variant) { - for (outX = renderer->end - mod8; outX < renderer->end; ++outX) { + for (; outX < renderer->end; ++outX) { BACKGROUND_DRAW_PIXEL_16_NORMAL; } } else { - for (outX = renderer->end - mod8; outX < renderer->end; ++outX) { + for (; outX < renderer->end; ++outX) { BACKGROUND_DRAW_PIXEL_16_VARIANT; } } } else { tileData >>= 4 * (0x8 - mod8); + int end2 = renderer->end - 8; + if (end2 < -1) { + end2 = -1; + } if (!variant) { - for (outX = renderer->end - 1; outX > renderer->end - 8; --outX) { + for (outX = renderer->end - 1; outX > end2; --outX) { BACKGROUND_DRAW_PIXEL_16_NORMAL; } } else { - for (outX = renderer->end - 1; outX > renderer->end - 8; --outX) { + for (outX = renderer->end - 1; outX > end2; --outX) { BACKGROUND_DRAW_PIXEL_16_VARIANT; } }