From a1ab99a862b7a6f1fc6c3ef279ede1ad0aa1229e Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Wed, 14 Jan 2004 10:34:20 +0000 Subject: [PATCH] * src/event.c: + Make sure the mouse coordinates stay within the screen boundaries even if the window was resized. * src/bitmap.c: + Fixed a buffer underflow in the rendering routine that caused the bottom and rightmost pixels to be missed in certain conditions. + Minor speed optimisation. --- src/bitmap.c | 35 +++++++++++++++++++++++------------ src/event.c | 6 ++++++ 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/src/bitmap.c b/src/bitmap.c index 4ceb093..ef80e40 100644 --- a/src/bitmap.c +++ b/src/bitmap.c @@ -456,7 +456,7 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2, "####" "????"; - int x, y, w, h, pitch; + int x, y, w, h, pitch, deltax, deltay; if(!bitmap || !pixels) return; @@ -475,6 +475,9 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2, int tmp = y2; y2 = y1; y1 = tmp; } + deltax = x2 - x1 + 1; + deltay = y2 - y1 + 1; + switch(_caca_dithering) { case CACA_DITHERING_NONE: @@ -529,14 +532,19 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2, /* First get RGB */ if(_caca_antialiasing == CACA_ANTIALIASING_PREFILTER) { - fromx = ((x - x1) * (w - 1)) / (x2 - x1 + 1); - fromy = ((y - y1) * (h - 1)) / (y2 - y1 + 1); - tox = ((x - x1 + 1) * (w - 1)) / (x2 - x1 + 1); - toy = ((y - y1 + 1) * (h - 1)) / (y2 - y1 + 1); + fromx = (x - x1) * w / deltax; + fromy = (y - y1) * h / deltay; + tox = (x - x1 + 1) * w / deltax; + toy = (y - y1 + 1) * h / deltay; + + /* We want at least one pixel */ + if(tox == fromx) tox++; + if(toy == fromy) toy++; + dots = 0; - for(myx = fromx; myx <= tox; myx++) - for(myy = fromy; myy <= toy; myy++) + for(myx = fromx; myx < tox; myx++) + for(myy = fromy; myy < toy; myy++) { dots++; get_rgba_default(bitmap, pixels, myx, myy, &r, &g, &b, &a); @@ -550,11 +558,14 @@ void caca_draw_bitmap(int x1, int y1, int x2, int y2, } else { - fromx = ((x - x1) * (w - 1)) / (x2 - x1 + 1); - fromy = ((y - y1) * (h - 1)) / (y2 - y1 + 1); - tox = ((x - x1 + 1) * (w - 1)) / (x2 - x1 + 1); - toy = ((y - y1 + 1) * (h - 1)) / (y2 - y1 + 1); - + fromx = (x - x1) * w / deltax; + fromy = (y - y1) * h / deltay; + tox = (x - x1 + 1) * w / deltax; + toy = (y - y1 + 1) * h / deltay; + + /* tox and toy can overflow the screen, but they cannot overflow + * when averaged with fromx and fromy because these are guaranteed + * to be within the pixel boundaries. */ myx = (fromx + tox) / 2; myy = (fromy + toy) / 2; diff --git a/src/event.c b/src/event.c index 4f5b9d4..2894449 100644 --- a/src/event.c +++ b/src/event.c @@ -143,6 +143,9 @@ unsigned int caca_wait_event(unsigned int event_mask) */ unsigned int caca_get_mouse_x(void) { + if(mouse_x >= _caca_width) + mouse_x = _caca_width - 1; + return mouse_x; } @@ -157,6 +160,9 @@ unsigned int caca_get_mouse_x(void) */ unsigned int caca_get_mouse_y(void) { + if(mouse_y >= _caca_height) + mouse_y = _caca_height - 1; + return mouse_y; }