]> git.sesse.net Git - ccbs/blob - bigscreen/rotatescreen.cpp
On the last fade-from-red, just complete the fade instead of doing a re-render. Fixes...
[ccbs] / bigscreen / rotatescreen.cpp
1 /* NOTE: this class will _NOT_ handle resolution changes cleanly. You have been warned. :-) */
2
3 #include <cstdio>
4 #include <cstring>
5 #include "rotatescreen.h"
6
7 RotateScreen::RotateScreen()
8         : fadefrom_buf(NULL), valid(false), current_screen(0), in_fade(false)
9 {
10 }
11
12 RotateScreen::~RotateScreen()
13 {
14         delete[] fadefrom_buf;
15 }
16
17 bool RotateScreen::check_invalidated()
18 {
19         if (!valid)
20                 return true;
21         if (in_fade)
22                 return true;
23         if (needs_update())
24                 return true;
25         if (!can_update())
26                 return false;
27         
28         for (unsigned i = 0; i < subscreens.size(); ++i) {
29                 if (subscreens[i].buf == NULL || subscreens[i].screen->check_invalidated())
30                         return true;
31         }
32
33         return false;
34 }
35
36 void RotateScreen::draw(unsigned char *buf, unsigned width, unsigned height)
37 {
38         // see line 1 for all of this
39         if (fadefrom_buf == NULL) {
40                 fadefrom_buf = new unsigned char[width * height * 4];
41         }
42         for (std::vector<Subscreen>::iterator i = subscreens.begin(); i != subscreens.end(); ++i) {
43                 if (i->buf == NULL) {
44                         i->buf = new unsigned char[width * height * 4];
45                         i->screen->draw(i->buf, width, height);
46                 }
47         }
48         // end of "line 1"-code :-)
49         
50         bool force = false;
51
52         if (subscreens.size() == 0) {
53                 valid = true;
54                 gettimeofday(&last_update, NULL);
55                 return;
56         }
57         
58         // if we're in a fade, complete it before doing anything else
59         if (in_fade) {
60                 struct timeval now;
61                 gettimeofday(&now, NULL);
62
63                 if (!fade_found_start_time) {
64                         fade_found_start_time = true;
65                         fade_started = now;
66                 }
67                 
68                 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
69                         double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
70                 
71                 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
72                         in_fade = false;
73
74                         // set G&B to be = R
75                         unsigned char *ptr = subscreens[current_screen].buf;
76                         for (unsigned i = 0; i < width * height; ++i) {
77                                 ptr[1] = ptr[2] = ptr[3] = ptr[0];
78                                 ptr += 4;
79                         }
80                         
81                         memcpy(buf, subscreens[current_screen].buf, width * height * 4);
82                 } else {
83                         // find the fade f
84                         int f;
85                         if (fade_to_new_info) {
86                                 if (elapsed_fade < 0.5) {
87                                         f = unsigned(elapsed_fade/0.5 * 256.0);
88                                 } else {
89                                         f = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
90                                 }
91                         } else {
92                                 f = unsigned(elapsed_fade/0.5 * 256.0);
93                         }
94
95                         unsigned char *sptr1 = fadefrom_buf;
96                         unsigned char *sptr2 = subscreens[current_screen].buf;
97                         unsigned char *dptr = buf;
98
99                         if (fade_to_new_info && elapsed_fade >= 0.5) {
100                                 // fade G&B to be = R
101                                 for (unsigned i = 0; i < width * height; ++i) {
102                                         dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * f) >> 8);
103                                         dptr[1] = dptr[0];
104                                         dptr[2] = sptr2[2];
105                                         dptr[3] = sptr2[3];
106
107                                         sptr1 += 4, sptr2 += 4, dptr += 4;
108                                 }
109                         } else {
110                                 for (unsigned i = 0; i < width * height; ++i) {
111                                         dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * f) >> 8);
112                                         dptr[1] = dptr[0];
113                                         dptr[2] = dptr[0];
114                                         dptr[3] = dptr[0];
115
116                                         sptr1 += 4, sptr2 += 4, dptr += 4;
117                                 }
118                         }
119                 }
120         } else {
121                 // determine if we want to switch screens
122         
123                 unsigned old_current_screen = current_screen;
124                 int priority = -9999;  // bah :-P
125                 
126                 // push any invalidated screen first (for now)
127                 for (unsigned i = 0; i < subscreens.size(); ++i) {
128                         if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
129                                 current_screen = i;
130                                 force = true;
131                                 priority = subscreens[i].screen->get_priority();
132                         }
133                 }
134
135                 // check if we want to go to the next screen
136                 if (valid && !force && needs_update()) {
137                         current_screen = (current_screen + 1) % subscreens.size();
138                         gettimeofday(&last_update, NULL);
139                 }
140
141                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
142                         // initialize a fade
143                         in_fade = true;
144                         fade_found_start_time = false;
145                         fade_to_new_info = force;
146                         
147                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
148
149                         if (subscreens[current_screen].screen->check_invalidated()) {
150                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
151                         }
152                 }
153         }
154
155         if (!valid) {
156                 valid = true;
157                 gettimeofday(&last_update, NULL);
158         }
159 }
160         
161 // note: makes no sense if valid=false!
162 bool RotateScreen::needs_update()
163 {
164         struct timeval now;
165         gettimeofday(&now, NULL);
166
167         double since = double(now.tv_sec - last_update.tv_sec) +
168                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
169
170         return (since >= 10.0);
171 }
172
173 /*
174  * Hold all screens for minimum three seconds, hold all screens with new info
175  * for at minimum eight seconds. There's a slight hack here; a screen with new
176  * info _might_ already be outdated (for instance, if we do an update to a group
177  * with a mistake, then correct it quick afterwards). If it's already outdated,
178  * we ignore the "eight second" rule; the results will be somewhat ugly, though,
179  * but it's hard to do much better without too much special code.
180  */
181 bool RotateScreen::can_update()
182 {
183         struct timeval now;
184         gettimeofday(&now, NULL);
185
186         double since = double(now.tv_sec - last_update.tv_sec) +
187                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
188
189         if (since < 3.0)
190                 return false;
191         if (fade_to_new_info && subscreens.size() > 0 && !subscreens[current_screen].screen->check_invalidated() && since < 8.0)
192                 return false;
193         return true;
194 }
195
196 void RotateScreen::add_screen(GenericScreen *screen)
197 {
198         Subscreen ss;
199         ss.buf = NULL;
200         ss.screen = screen;
201
202         subscreens.push_back(ss);
203 }
204