Make RotateScreen fade between the individual screens.
authorSteinar H. Gunderson <sesse@samfundet.no>
Sun, 20 Feb 2005 13:42:10 +0000 (13:42 +0000)
committerSteinar H. Gunderson <sesse@samfundet.no>
Sun, 20 Feb 2005 13:42:10 +0000 (13:42 +0000)
bigscreen/rotatescreen.cpp
bigscreen/rotatescreen.h

index 0997e5c..8e216cf 100644 (file)
@@ -2,19 +2,22 @@
 #include "rotatescreen.h"
 
 RotateScreen::RotateScreen()
-       : valid(false), current_screen(0)
+       : valid(false), current_screen(0), in_fade(false)
 {
+       fadefrom_buf = new unsigned char[800 * 600 * 4];
 }
 
 RotateScreen::~RotateScreen()
 {
+       delete fadefrom_buf;
 }
 
 bool RotateScreen::check_invalidated()
 {
        if (!valid)
                return true;
-
+       if (in_fade)
+               return true;
        if (needs_update())
                return true;
        
@@ -30,24 +33,70 @@ void RotateScreen::draw(unsigned char *buf)
 {
        bool force = false;
        
-       // push any invalidated screen first (for now)
-       for (unsigned i = 0; i < subscreens.size(); ++i) {
-               if (subscreens[i].screen->check_invalidated()) {
-                       current_screen = i;
-                       force = true;
-               }
-       }
+       // if we're in a fade, complete it before doing anything else
+       if (in_fade) {
+               struct timeval now;
+               gettimeofday(&now, NULL);
 
-       // check if we want to go to the next screen
-       if (valid && !force && needs_update()) {
-               current_screen = (current_screen + 1) % subscreens.size();
-               gettimeofday(&last_update, NULL);
-       }
+               double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
+                       double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
+               printf("fade: %f\n", elapsed_fade);
+               
+               if (elapsed_fade > 0.5) {
+                       in_fade = false;
+                       memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
+                       printf("fade done\n");
+               } else {
+                       // find the fade factors
+                       int fr = unsigned(elapsed_fade/0.5 * 256.0);
+                       int fg = unsigned(elapsed_fade/0.5 * 256.0);
+                       int fb = unsigned(elapsed_fade/0.5 * 256.0);
+                       int fa = unsigned(elapsed_fade/0.5 * 256.0);
+
+                       unsigned char *sptr1 = fadefrom_buf;
+                       unsigned char *sptr2 = subscreens[current_screen].buf;
+                       unsigned char *dptr = buf;
 
-       if (subscreens[current_screen].screen->check_invalidated())
-               subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
+                       for (unsigned i = 0; i < 800 * 600; ++i) {
+                               dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fr) >> 8);
+                               dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
+                               dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fb) >> 8);
+                               dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
+
+                               sptr1 += 4, sptr2 += 4, dptr += 4;
+                       }
+               }
+       } else {
+               // determine if we want to switch screens
        
-       memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
+               unsigned old_current_screen = current_screen;
+               
+               // push any invalidated screen first (for now)
+               for (unsigned i = 0; i < subscreens.size(); ++i) {
+                       if (subscreens[i].screen->check_invalidated()) {
+                               current_screen = i;
+                               force = true;
+                       }
+               }
+
+               // check if we want to go to the next screen
+               if (valid && !force && needs_update()) {
+                       current_screen = (current_screen + 1) % subscreens.size();
+                       gettimeofday(&last_update, NULL);
+               }
+
+               if (current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
+                       // initialize a fade
+                       in_fade = true;
+                       gettimeofday(&fade_started, NULL);
+                       printf("starting fade\n");
+                       
+                       if (subscreens[current_screen].screen->check_invalidated())
+                               subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
+               
+                       memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
+               }
+       }
 
        if (!valid) {
                valid = true;
index 0883ca2..35931b5 100644 (file)
@@ -17,11 +17,13 @@ struct Subscreen {
 
 class RotateScreen : public GenericScreen {
 private:
+       unsigned char *fadefrom_buf;
        bool valid;
        std::vector<Subscreen> subscreens;
        unsigned current_screen;
 
-       struct timeval last_update;
+       struct timeval last_update, fade_started;
+       bool in_fade;
 
        bool needs_update();