Fix a bug where all new screens would be cycled on start.
[ccbs] / bigscreen / rotatescreen.cpp
index 8e216cf..ef7fba0 100644 (file)
@@ -32,38 +32,69 @@ bool RotateScreen::check_invalidated()
 void RotateScreen::draw(unsigned char *buf)
 {
        bool force = false;
+
+       if (subscreens.size() == 0) {
+               valid = true;
+               gettimeofday(&last_update, NULL);
+               return;
+       }
        
        // if we're in a fade, complete it before doing anything else
        if (in_fade) {
                struct timeval now;
                gettimeofday(&now, NULL);
 
+               if (!fade_found_start_time) {
+                       fade_found_start_time = true;
+                       fade_started = now;
+               }
+               
                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) {
+               if (elapsed_fade > 5.5 || (!same_fade && elapsed_fade > 0.5)) {
                        in_fade = false;
+
+                       // ugly hack here? :-)
+                       subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
+                       
                        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);
+                       int fr, fg, fb, fa;
+                       if (same_fade) {
+                               if (elapsed_fade < 0.5) {
+                                       fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
+                               } else {
+                                       fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
+                               }
+                       } else {
+                               fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
+                       }
 
                        unsigned char *sptr1 = fadefrom_buf;
                        unsigned char *sptr2 = subscreens[current_screen].buf;
                        unsigned char *dptr = 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;
+                       if (same_fade && elapsed_fade >= 0.5) {
+                               // fade G&B to be = R
+                               for (unsigned i = 0; i < 800 * 600; ++i) {
+                                       dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
+                                       dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
+                                       dptr[2] = sptr2[2];
+                                       dptr[3] = sptr2[3];
+
+                                       sptr1 += 4, sptr2 += 4, dptr += 4;
+                               }
+                       } else {
+                               for (unsigned i = 0; i < 800 * 600; ++i) {
+                                       dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
+                                       dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
+                                       dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
+                                       dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
+
+                                       sptr1 += 4, sptr2 += 4, dptr += 4;
+                               }
                        }
                }
        } else {
@@ -85,16 +116,16 @@ void RotateScreen::draw(unsigned char *buf)
                        gettimeofday(&last_update, NULL);
                }
 
-               if (current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
+               if (!valid || 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");
+                       fade_found_start_time = false;
+                       same_fade = (current_screen == old_current_screen);
                        
+                       memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
+
                        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);
                }
        }
 
@@ -122,6 +153,8 @@ void RotateScreen::add_screen(GenericScreen *screen)
        ss.buf = new unsigned char[800 * 600 * 4];
        ss.screen = screen;
 
+       screen->draw(ss.buf);
+       
        subscreens.push_back(ss);
 }