]> git.sesse.net Git - ccbs/blob - bigscreen/rotatescreen.cpp
Lots of changes/fixes to the new translation stuff.
[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                         // ugly hack here? :-)
75                         subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
76                         
77                         memcpy(buf, subscreens[current_screen].buf, width * height * 4);
78                 } else {
79                         // find the fade factors
80                         int fr, fg, fb, fa;
81                         if (fade_to_new_info) {
82                                 if (elapsed_fade < 0.5) {
83                                         fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
84                                 } else {
85                                         fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
86                                 }
87                         } else {
88                                 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
89                         }
90
91                         unsigned char *sptr1 = fadefrom_buf;
92                         unsigned char *sptr2 = subscreens[current_screen].buf;
93                         unsigned char *dptr = buf;
94
95                         if (fade_to_new_info && elapsed_fade >= 0.5) {
96                                 // fade G&B to be = R
97                                 for (unsigned i = 0; i < width * height; ++i) {
98                                         dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
99                                         dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
100                                         dptr[2] = sptr2[2];
101                                         dptr[3] = sptr2[3];
102
103                                         sptr1 += 4, sptr2 += 4, dptr += 4;
104                                 }
105                         } else {
106                                 for (unsigned i = 0; i < width * height; ++i) {
107                                         dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
108                                         dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
109                                         dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
110                                         dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
111
112                                         sptr1 += 4, sptr2 += 4, dptr += 4;
113                                 }
114                         }
115                 }
116         } else {
117                 // determine if we want to switch screens
118         
119                 unsigned old_current_screen = current_screen;
120                 int priority = -9999;  // bah :-P
121                 
122                 // push any invalidated screen first (for now)
123                 for (unsigned i = 0; i < subscreens.size(); ++i) {
124                         if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
125                                 current_screen = i;
126                                 force = true;
127                                 priority = subscreens[i].screen->get_priority();
128                         }
129                 }
130
131                 // check if we want to go to the next screen
132                 if (valid && !force && needs_update()) {
133                         current_screen = (current_screen + 1) % subscreens.size();
134                         gettimeofday(&last_update, NULL);
135                 }
136
137                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
138                         // initialize a fade
139                         in_fade = true;
140                         fade_found_start_time = false;
141                         fade_to_new_info = force;
142                         
143                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, width * height * 4);
144
145                         if (subscreens[current_screen].screen->check_invalidated()) {
146                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf, width, height);
147                         }
148                 }
149         }
150
151         if (!valid) {
152                 valid = true;
153                 gettimeofday(&last_update, NULL);
154         }
155 }
156         
157 // note: makes no sense if valid=false!
158 bool RotateScreen::needs_update()
159 {
160         struct timeval now;
161         gettimeofday(&now, NULL);
162
163         double since = double(now.tv_sec - last_update.tv_sec) +
164                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
165
166         return (since >= 10.0);
167 }
168
169 /*
170  * Hold all screens for minimum three seconds, hold all screens with new info
171  * for at minimum eight seconds. There's a slight hack here; a screen with new
172  * info _might_ already be outdated (for instance, if we do an update to a group
173  * with a mistake, then correct it quick afterwards). If it's already outdated,
174  * we ignore the "eight second" rule; the results will be somewhat ugly, though,
175  * but it's hard to do much better without too much special code.
176  */
177 bool RotateScreen::can_update()
178 {
179         struct timeval now;
180         gettimeofday(&now, NULL);
181
182         double since = double(now.tv_sec - last_update.tv_sec) +
183                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
184
185         if (since < 3.0)
186                 return false;
187         if (fade_to_new_info && subscreens.size() > 0 && !subscreens[current_screen].screen->check_invalidated() && since < 8.0)
188                 return false;
189         return true;
190 }
191
192 void RotateScreen::add_screen(GenericScreen *screen)
193 {
194         Subscreen ss;
195         ss.buf = NULL;
196         ss.screen = screen;
197
198         subscreens.push_back(ss);
199 }
200