2 #include "rotatescreen.h"
4 RotateScreen::RotateScreen()
5 : valid(false), current_screen(0), in_fade(false)
7 fadefrom_buf = new unsigned char[800 * 600 * 4];
10 RotateScreen::~RotateScreen()
15 bool RotateScreen::check_invalidated()
24 for (unsigned i = 0; i < subscreens.size(); ++i) {
25 if (subscreens[i].screen->check_invalidated())
32 void RotateScreen::draw(unsigned char *buf)
36 // if we're in a fade, complete it before doing anything else
39 gettimeofday(&now, NULL);
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);
45 if (elapsed_fade > 5.0 || (!same_fade && elapsed_fade > 0.5)) {
47 memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
48 printf("fade done\n");
50 // find the fade factors
53 fr = unsigned(elapsed_fade/0.5 * 256.0);
54 if (elapsed_fade < 0.5) {
58 fg = fb = fa = unsigned((elapsed_fade-0.5)/4.5 * 256.0);
61 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
63 printf("%u %u %u %u\n", fr, fg, fb, fa);
65 unsigned char *sptr1 = fadefrom_buf;
66 unsigned char *sptr2 = subscreens[current_screen].buf;
67 unsigned char *dptr = buf;
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);
76 if (dptr[0] > dptr[2])
78 if (dptr[1] > dptr[2])
81 sptr1 += 4, sptr2 += 4, dptr += 4;
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);
90 sptr1 += 4, sptr2 += 4, dptr += 4;
95 // determine if we want to switch screens
97 unsigned old_current_screen = current_screen;
99 // push any invalidated screen first (for now)
100 for (unsigned i = 0; i < subscreens.size(); ++i) {
101 if (subscreens[i].screen->check_invalidated()) {
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);
113 if (current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
116 same_fade = (current_screen == old_current_screen);
117 gettimeofday(&fade_started, NULL);
118 printf("starting fade (same=%u)\n", same_fade);
120 memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
122 if (subscreens[current_screen].screen->check_invalidated())
123 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
129 gettimeofday(&last_update, NULL);
133 // note: makes no sense if valid=false!
134 bool RotateScreen::needs_update()
137 gettimeofday(&now, NULL);
139 double since = double(now.tv_sec - last_update.tv_sec) +
140 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
142 return (since >= 10.0);
145 void RotateScreen::add_screen(GenericScreen *screen)
148 ss.buf = new unsigned char[800 * 600 * 4];
151 subscreens.push_back(ss);