]> git.sesse.net Git - ccbs/blobdiff - bigscreen/rotatescreen.cpp
Let each screen get width and height in as a parameter on draw() instead of hardcodin...
[ccbs] / bigscreen / rotatescreen.cpp
index 47f90956fd9a5eceebaea443ec121c322fe047f1..4fc9fa0903ff50c7247cd02ecf686908698fc1e1 100644 (file)
@@ -1,10 +1,12 @@
+/* NOTE: this class will _NOT_ handle resolution changes cleanly. You have been warned. :-) */
+
+#include <cstdio>
 #include <cstring>
 #include "rotatescreen.h"
 
 RotateScreen::RotateScreen()
 #include <cstring>
 #include "rotatescreen.h"
 
 RotateScreen::RotateScreen()
-       : valid(false), current_screen(0), in_fade(false)
+       : fadefrom_buf(NULL), valid(false), current_screen(0), in_fade(false)
 {
 {
-       fadefrom_buf = new unsigned char[800 * 600 * 4];
 }
 
 RotateScreen::~RotateScreen()
 }
 
 RotateScreen::~RotateScreen()
@@ -20,68 +22,88 @@ bool RotateScreen::check_invalidated()
                return true;
        if (needs_update())
                return true;
                return true;
        if (needs_update())
                return true;
+       if (!can_update())
+               return false;
        
        for (unsigned i = 0; i < subscreens.size(); ++i) {
        
        for (unsigned i = 0; i < subscreens.size(); ++i) {
-               if (subscreens[i].screen->check_invalidated())
+               if (subscreens[i].buf == NULL || subscreens[i].screen->check_invalidated())
                        return true;
        }
 
        return false;
 }
 
                        return true;
        }
 
        return false;
 }
 
-void RotateScreen::draw(unsigned char *buf)
+void RotateScreen::draw(unsigned char *buf, unsigned width, unsigned height)
 {
 {
+       // see line 1 for all of this
+       if (fadefrom_buf == NULL) {
+               fadefrom_buf = new unsigned char[width * height * 4];
+       }
+       for (std::vector<Subscreen>::iterator i = subscreens.begin(); i != subscreens.end(); ++i) {
+               if (i->buf == NULL) {
+                       i->buf = new unsigned char[width * height * 4];
+                       i->screen->draw(i->buf, width, height);
+               }
+       }
+       // end of "line 1"-code :-)
+       
        bool force = false;
        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 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;
                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 > 5.0 || (!same_fade && elapsed_fade > 0.5)) {
+               if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
                        in_fade = false;
                        in_fade = false;
-                       memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
-                       printf("fade done\n");
+
+                       // ugly hack here? :-)
+                       subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
+                       
+                       memcpy(buf, subscreens[current_screen].buf, width * height * 4);
                } else {
                        // find the fade factors
                        int fr, fg, fb, fa;
                } else {
                        // find the fade factors
                        int fr, fg, fb, fa;
-                       if (same_fade) {
-                               fr = unsigned(elapsed_fade/0.5 * 256.0);
+                       if (fade_to_new_info) {
                                if (elapsed_fade < 0.5) {
                                if (elapsed_fade < 0.5) {
-                                       fg = fb = fa = 0;
+                                       fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
                                } else {
                                } else {
-                                       fr = 256;
-                                       fg = fb = fa = unsigned((elapsed_fade-0.5)/4.5 * 256.0);
+                                       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);
                        }
                                }
                        } else {
                                fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
                        }
-                       printf("%u %u %u %u\n", fr, fg, fb, fa);
 
                        unsigned char *sptr1 = fadefrom_buf;
                        unsigned char *sptr2 = subscreens[current_screen].buf;
                        unsigned char *dptr = buf;
 
 
                        unsigned char *sptr1 = fadefrom_buf;
                        unsigned char *sptr2 = subscreens[current_screen].buf;
                        unsigned char *dptr = buf;
 
-                       if (same_fade) {
-                               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);
-
-                                       if (dptr[0] > dptr[2])
-                                               dptr[0] = dptr[2];
-                                       if (dptr[1] > dptr[2])
-                                               dptr[1] = dptr[2];
+                       if (fade_to_new_info && elapsed_fade >= 0.5) {
+                               // fade G&B to be = R
+                               for (unsigned i = 0; i < width * height; ++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 {
 
                                        sptr1 += 4, sptr2 += 4, dptr += 4;
                                }
                        } else {
-                               for (unsigned i = 0; i < 800 * 600; ++i) {
+                               for (unsigned i = 0; i < width * height; ++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[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);
@@ -95,12 +117,14 @@ void RotateScreen::draw(unsigned char *buf)
                // determine if we want to switch screens
        
                unsigned old_current_screen = current_screen;
                // determine if we want to switch screens
        
                unsigned old_current_screen = current_screen;
+               int priority = -9999;  // bah :-P
                
                // push any invalidated screen first (for now)
                for (unsigned i = 0; i < subscreens.size(); ++i) {
                
                // push any invalidated screen first (for now)
                for (unsigned i = 0; i < subscreens.size(); ++i) {
-                       if (subscreens[i].screen->check_invalidated()) {
+                       if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
                                current_screen = i;
                                force = true;
                                current_screen = i;
                                force = true;
+                               priority = subscreens[i].screen->get_priority();
                        }
                }
 
                        }
                }
 
@@ -110,17 +134,17 @@ void RotateScreen::draw(unsigned char *buf)
                        gettimeofday(&last_update, NULL);
                }
 
                        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;
                        // initialize a fade
                        in_fade = true;
-                       same_fade = (current_screen == old_current_screen);
-                       gettimeofday(&fade_started, NULL);
-                       printf("starting fade (same=%u)\n", same_fade);
+                       fade_found_start_time = false;
+                       fade_to_new_info = force;
                        
                        
-                       memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
+                       memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
 
 
-                       if (subscreens[current_screen].screen->check_invalidated())
-                               subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
+                       if (subscreens[current_screen].screen->check_invalidated()) {
+                               subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
+                       }
                }
        }
 
                }
        }
 
@@ -142,10 +166,26 @@ bool RotateScreen::needs_update()
        return (since >= 10.0);
 }
 
        return (since >= 10.0);
 }
 
+// hold all screens for minimum three seconds, hold all screens with new info for at minimum eight seconds
+bool RotateScreen::can_update()
+{
+       struct timeval now;
+       gettimeofday(&now, NULL);
+
+       double since = double(now.tv_sec - last_update.tv_sec) +
+               double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
+
+       if (since < 3.0)
+               return false;
+       if (fade_to_new_info && since < 8.0)
+               return false;
+       return true;
+}
+
 void RotateScreen::add_screen(GenericScreen *screen)
 {
        Subscreen ss;
 void RotateScreen::add_screen(GenericScreen *screen)
 {
        Subscreen ss;
-       ss.buf = new unsigned char[800 * 600 * 4];
+       ss.buf = NULL;
        ss.screen = screen;
 
        subscreens.push_back(ss);
        ss.screen = screen;
 
        subscreens.push_back(ss);