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