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