From 70c006184dc5fc7373e0524f2f74ba0336501c16 Mon Sep 17 00:00:00 2001 From: Timo Kokkonen Date: Sat, 3 Apr 2010 13:02:44 +0300 Subject: [PATCH] fade_buf: Optimizations Since most of the buffer is usually black, we can skip the fading entirely on those words that are already null. Only when the buffer contains something to fade, individual color values are being substracted. Signed-off-by: Timo Kokkonen --- main.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/main.c b/main.c index f779cda..dfcc148 100644 --- a/main.c +++ b/main.c @@ -8,14 +8,22 @@ #define MAX_FPS 60 -static void fade_buf(SDL_Surface *screen, double amount) +static void fade_buf(SDL_Surface *screen, int amount) { int i; - unsigned char *buf = screen->pixels; + unsigned int *buf = screen->pixels; + unsigned char *b; - for (i = 0; i < screen->pitch * screen->h; i++) - buf[i] = (buf[i] < amount) ? 0 : buf[i] - amount; -} + for (i = 0; i < screen->pitch * screen->h / sizeof(*buf); i ++) { + if (!buf[i]) + continue; + b = (unsigned char *)&buf[i]; + *b = *b >= amount ? (*b - amount) : *b; + b++; + *b = *b >= amount ? (*b - amount) : *b; + b++; + *b = *b >= amount ? (*b - amount) : *b; + }} static void clear_buf(SDL_Surface *screen) { -- 2.44.0