Fix a bug where all new screens would be cycled on start.
[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         
24         for (unsigned i = 0; i < subscreens.size(); ++i) {
25                 if (subscreens[i].screen->check_invalidated())
26                         return true;
27         }
28
29         return false;
30 }
31
32 void RotateScreen::draw(unsigned char *buf)
33 {
34         bool force = false;
35
36         if (subscreens.size() == 0) {
37                 valid = true;
38                 gettimeofday(&last_update, NULL);
39                 return;
40         }
41         
42         // if we're in a fade, complete it before doing anything else
43         if (in_fade) {
44                 struct timeval now;
45                 gettimeofday(&now, NULL);
46
47                 if (!fade_found_start_time) {
48                         fade_found_start_time = true;
49                         fade_started = now;
50                 }
51                 
52                 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
53                         double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
54                 
55                 if (elapsed_fade > 5.5 || (!same_fade && elapsed_fade > 0.5)) {
56                         in_fade = false;
57
58                         // ugly hack here? :-)
59                         subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
60                         
61                         memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
62                 } else {
63                         // find the fade factors
64                         int fr, fg, fb, fa;
65                         if (same_fade) {
66                                 if (elapsed_fade < 0.5) {
67                                         fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
68                                 } else {
69                                         fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
70                                 }
71                         } else {
72                                 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
73                         }
74
75                         unsigned char *sptr1 = fadefrom_buf;
76                         unsigned char *sptr2 = subscreens[current_screen].buf;
77                         unsigned char *dptr = buf;
78
79                         if (same_fade && elapsed_fade >= 0.5) {
80                                 // fade G&B to be = R
81                                 for (unsigned i = 0; i < 800 * 600; ++i) {
82                                         dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
83                                         dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
84                                         dptr[2] = sptr2[2];
85                                         dptr[3] = sptr2[3];
86
87                                         sptr1 += 4, sptr2 += 4, dptr += 4;
88                                 }
89                         } else {
90                                 for (unsigned i = 0; i < 800 * 600; ++i) {
91                                         dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
92                                         dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
93                                         dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
94                                         dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
95
96                                         sptr1 += 4, sptr2 += 4, dptr += 4;
97                                 }
98                         }
99                 }
100         } else {
101                 // determine if we want to switch screens
102         
103                 unsigned old_current_screen = current_screen;
104                 
105                 // push any invalidated screen first (for now)
106                 for (unsigned i = 0; i < subscreens.size(); ++i) {
107                         if (subscreens[i].screen->check_invalidated()) {
108                                 current_screen = i;
109                                 force = true;
110                         }
111                 }
112
113                 // check if we want to go to the next screen
114                 if (valid && !force && needs_update()) {
115                         current_screen = (current_screen + 1) % subscreens.size();
116                         gettimeofday(&last_update, NULL);
117                 }
118
119                 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
120                         // initialize a fade
121                         in_fade = true;
122                         fade_found_start_time = false;
123                         same_fade = (current_screen == old_current_screen);
124                         
125                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
126
127                         if (subscreens[current_screen].screen->check_invalidated())
128                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
129                 }
130         }
131
132         if (!valid) {
133                 valid = true;
134                 gettimeofday(&last_update, NULL);
135         }
136 }
137         
138 // note: makes no sense if valid=false!
139 bool RotateScreen::needs_update()
140 {
141         struct timeval now;
142         gettimeofday(&now, NULL);
143
144         double since = double(now.tv_sec - last_update.tv_sec) +
145                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
146
147         return (since >= 10.0);
148 }
149
150 void RotateScreen::add_screen(GenericScreen *screen)
151 {
152         Subscreen ss;
153         ss.buf = new unsigned char[800 * 600 * 4];
154         ss.screen = screen;
155
156         screen->draw(ss.buf);
157         
158         subscreens.push_back(ss);
159 }
160