Make sure each screen stays on for a minimum of three seconds (eight if it contains...
[ccbs] / bigscreen / rotatescreen.cpp
1 #include <cstring>
2 #include "rotatescreen.h"
3
4 RotateScreen::RotateScreen()
5         : valid(false), current_screen(0), in_fade(false)
6 {
7         fadefrom_buf = new unsigned char[800 * 600 * 4];
8 }
9
10 RotateScreen::~RotateScreen()
11 {
12         delete fadefrom_buf;
13 }
14
15 bool RotateScreen::check_invalidated()
16 {
17         if (!valid)
18                 return true;
19         if (in_fade)
20                 return true;
21         if (needs_update())
22                 return true;
23         if (!can_update())
24                 return false;
25         
26         for (unsigned i = 0; i < subscreens.size(); ++i) {
27                 if (subscreens[i].screen->check_invalidated())
28                         return true;
29         }
30
31         return false;
32 }
33
34 void RotateScreen::draw(unsigned char *buf)
35 {
36         bool force = false;
37
38         if (subscreens.size() == 0) {
39                 valid = true;
40                 gettimeofday(&last_update, NULL);
41                 return;
42         }
43         
44         // if we're in a fade, complete it before doing anything else
45         if (in_fade) {
46                 struct timeval now;
47                 gettimeofday(&now, NULL);
48
49                 if (!fade_found_start_time) {
50                         fade_found_start_time = true;
51                         fade_started = now;
52                 }
53                 
54                 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
55                         double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
56                 
57                 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
58                         in_fade = false;
59
60                         // ugly hack here? :-)
61                         subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
62                         
63                         memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
64                 } else {
65                         // find the fade factors
66                         int fr, fg, fb, fa;
67                         if (fade_to_new_info) {
68                                 if (elapsed_fade < 0.5) {
69                                         fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
70                                 } else {
71                                         fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
72                                 }
73                         } else {
74                                 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
75                         }
76
77                         unsigned char *sptr1 = fadefrom_buf;
78                         unsigned char *sptr2 = subscreens[current_screen].buf;
79                         unsigned char *dptr = buf;
80
81                         if (fade_to_new_info && elapsed_fade >= 0.5) {
82                                 // fade G&B to be = R
83                                 for (unsigned i = 0; i < 800 * 600; ++i) {
84                                         dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
85                                         dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
86                                         dptr[2] = sptr2[2];
87                                         dptr[3] = sptr2[3];
88
89                                         sptr1 += 4, sptr2 += 4, dptr += 4;
90                                 }
91                         } else {
92                                 for (unsigned i = 0; i < 800 * 600; ++i) {
93                                         dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
94                                         dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
95                                         dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
96                                         dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
97
98                                         sptr1 += 4, sptr2 += 4, dptr += 4;
99                                 }
100                         }
101                 }
102         } else {
103                 // determine if we want to switch screens
104         
105                 unsigned old_current_screen = current_screen;
106                 
107                 // push any invalidated screen first (for now)
108                 for (unsigned i = 0; i < subscreens.size(); ++i) {
109                         if (subscreens[i].screen->check_invalidated()) {
110                                 current_screen = i;
111                                 force = true;
112                         }
113                 }
114
115                 // check if we want to go to the next screen
116                 if (valid && !force && needs_update()) {
117                         current_screen = (current_screen + 1) % subscreens.size();
118                         gettimeofday(&last_update, NULL);
119                 }
120
121                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
122                         // initialize a fade
123                         in_fade = true;
124                         fade_found_start_time = false;
125                         fade_to_new_info = force;
126                         
127                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
128
129                         if (subscreens[current_screen].screen->check_invalidated())
130                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
131                 }
132         }
133
134         if (!valid) {
135                 valid = true;
136                 gettimeofday(&last_update, NULL);
137         }
138 }
139         
140 // note: makes no sense if valid=false!
141 bool RotateScreen::needs_update()
142 {
143         struct timeval now;
144         gettimeofday(&now, NULL);
145
146         double since = double(now.tv_sec - last_update.tv_sec) +
147                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
148
149         return (since >= 10.0);
150 }
151
152 // hold all screens for minimum three seconds, hold all screens with new info for at minimum eight seconds
153 bool RotateScreen::can_update()
154 {
155         struct timeval now;
156         gettimeofday(&now, NULL);
157
158         double since = double(now.tv_sec - last_update.tv_sec) +
159                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
160
161         if (since < 3.0)
162                 return false;
163         if (fade_to_new_info && since < 8.0)
164                 return false;
165         return true;
166 }
167
168 void RotateScreen::add_screen(GenericScreen *screen)
169 {
170         Subscreen ss;
171         ss.buf = new unsigned char[800 * 600 * 4];
172         ss.screen = screen;
173
174         screen->draw(ss.buf);
175         
176         subscreens.push_back(ss);
177 }
178