]> git.itanic.dy.fi Git - sdl-planets/commitdiff
fade_buf: Optimizations
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 3 Apr 2010 10:02:44 +0000 (13:02 +0300)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Sat, 3 Apr 2010 10:02:44 +0000 (13:02 +0300)
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 <kaapeli@itanic.dy.fi>
main.c

diff --git a/main.c b/main.c
index f779cdad8f29b992b31702c65948c893f36cebfd..dfcc14802b7b3ea7e002d38b953133e51d357957 100644 (file)
--- 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)
 {