Make RotateScreen fade between the individual screens.
[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 > 0.5) {
46                         in_fade = false;
47                         memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
48                         printf("fade done\n");
49                 } else {
50                         // find the fade factors
51                         int fr = unsigned(elapsed_fade/0.5 * 256.0);
52                         int fg = unsigned(elapsed_fade/0.5 * 256.0);
53                         int fb = unsigned(elapsed_fade/0.5 * 256.0);
54                         int fa = unsigned(elapsed_fade/0.5 * 256.0);
55
56                         unsigned char *sptr1 = fadefrom_buf;
57                         unsigned char *sptr2 = subscreens[current_screen].buf;
58                         unsigned char *dptr = buf;
59
60                         for (unsigned i = 0; i < 800 * 600; ++i) {
61                                 dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fr) >> 8);
62                                 dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
63                                 dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fb) >> 8);
64                                 dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8); 
65
66                                 sptr1 += 4, sptr2 += 4, dptr += 4;
67                         }
68                 }
69         } else {
70                 // determine if we want to switch screens
71         
72                 unsigned old_current_screen = current_screen;
73                 
74                 // push any invalidated screen first (for now)
75                 for (unsigned i = 0; i < subscreens.size(); ++i) {
76                         if (subscreens[i].screen->check_invalidated()) {
77                                 current_screen = i;
78                                 force = true;
79                         }
80                 }
81
82                 // check if we want to go to the next screen
83                 if (valid && !force && needs_update()) {
84                         current_screen = (current_screen + 1) % subscreens.size();
85                         gettimeofday(&last_update, NULL);
86                 }
87
88                 if (current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
89                         // initialize a fade
90                         in_fade = true;
91                         gettimeofday(&fade_started, NULL);
92                         printf("starting fade\n");
93                         
94                         if (subscreens[current_screen].screen->check_invalidated())
95                                 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
96                 
97                         memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
98                 }
99         }
100
101         if (!valid) {
102                 valid = true;
103                 gettimeofday(&last_update, NULL);
104         }
105 }
106         
107 // note: makes no sense if valid=false!
108 bool RotateScreen::needs_update()
109 {
110         struct timeval now;
111         gettimeofday(&now, NULL);
112
113         double since = double(now.tv_sec - last_update.tv_sec) +
114                 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
115
116         return (since >= 10.0);
117 }
118
119 void RotateScreen::add_screen(GenericScreen *screen)
120 {
121         Subscreen ss;
122         ss.buf = new unsigned char[800 * 600 * 4];
123         ss.screen = screen;
124
125         subscreens.push_back(ss);
126 }
127