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