]> git.sesse.net Git - ccbs/blob - bigscreen/rotatescreen.cpp
Implement a simple priority system.
[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                 int priority = -9999;  // bah :-P
107                 
108                 // push any invalidated screen first (for now)
109                 for (unsigned i = 0; i < subscreens.size(); ++i) {
110                         if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
111                                 current_screen = i;
112                                 force = true;
113                                 priority = subscreens[i].screen->get_priority();
114                         }
115                 }
116
117                 // check if we want to go to the next screen
118                 if (valid && !force && needs_update()) {
119                         current_screen = (current_screen + 1) % subscreens.size();
120                         gettimeofday(&last_update, NULL);
121                 }
122
123                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
124                         // initialize a fade
125                         in_fade = true;
126                         fade_found_start_time = false;
127                         fade_to_new_info = force;
128                         
129                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
130
131                         if (subscreens[current_screen].screen->check_invalidated())
132                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
133                 }
134         }
135
136         if (!valid) {
137                 valid = true;
138                 gettimeofday(&last_update, NULL);
139         }
140 }
141         
142 // note: makes no sense if valid=false!
143 bool RotateScreen::needs_update()
144 {
145         struct timeval now;
146         gettimeofday(&now, NULL);
147
148         double since = double(now.tv_sec - last_update.tv_sec) +
149                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
150
151         return (since >= 10.0);
152 }
153
154 // hold all screens for minimum three seconds, hold all screens with new info for at minimum eight seconds
155 bool RotateScreen::can_update()
156 {
157         struct timeval now;
158         gettimeofday(&now, NULL);
159
160         double since = double(now.tv_sec - last_update.tv_sec) +
161                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
162
163         if (since < 3.0)
164                 return false;
165         if (fade_to_new_info && since < 8.0)
166                 return false;
167         return true;
168 }
169
170 void RotateScreen::add_screen(GenericScreen *screen)
171 {
172         Subscreen ss;
173         ss.buf = new unsigned char[800 * 600 * 4];
174         ss.screen = screen;
175
176         screen->draw(ss.buf);
177         
178         subscreens.push_back(ss);
179 }
180