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()
26 for (unsigned i = 0; i < subscreens.size(); ++i) {
27 if (subscreens[i].screen->check_invalidated())
34 void RotateScreen::draw(unsigned char *buf)
38 if (subscreens.size() == 0) {
40 gettimeofday(&last_update, NULL);
44 // if we're in a fade, complete it before doing anything else
47 gettimeofday(&now, NULL);
49 if (!fade_found_start_time) {
50 fade_found_start_time = true;
54 double elapsed_fade = double(now.tv_sec - fade_started.tv_sec) +
55 double(now.tv_usec - fade_started.tv_usec) * 1.0e-6;
57 if (elapsed_fade > 5.5 || (!fade_to_new_info && elapsed_fade > 0.5)) {
60 // ugly hack here? :-)
61 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
63 memcpy(buf, subscreens[current_screen].buf, 800 * 600 * 4);
65 // find the fade factors
67 if (fade_to_new_info) {
68 if (elapsed_fade < 0.5) {
69 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
71 fr = fg = fb = fa = unsigned((elapsed_fade-0.5)/5.0 * 256.0);
74 fr = fg = fb = fa = unsigned(elapsed_fade/0.5 * 256.0);
77 unsigned char *sptr1 = fadefrom_buf;
78 unsigned char *sptr2 = subscreens[current_screen].buf;
79 unsigned char *dptr = buf;
81 if (fade_to_new_info && elapsed_fade >= 0.5) {
83 for (unsigned i = 0; i < 800 * 600; ++i) {
84 dptr[0] = sptr2[0] + (((int(sptr2[2]) - int(sptr2[0])) * fb) >> 8);
85 dptr[1] = sptr2[1] + (((int(sptr2[2]) - int(sptr2[1])) * fg) >> 8);
89 sptr1 += 4, sptr2 += 4, dptr += 4;
92 for (unsigned i = 0; i < 800 * 600; ++i) {
93 dptr[0] = sptr1[0] + (((int(sptr2[0]) - int(sptr1[0])) * fb) >> 8);
94 dptr[1] = sptr1[1] + (((int(sptr2[1]) - int(sptr1[1])) * fg) >> 8);
95 dptr[2] = sptr1[2] + (((int(sptr2[2]) - int(sptr1[2])) * fr) >> 8);
96 dptr[3] = sptr1[3] + (((int(sptr2[3]) - int(sptr1[3])) * fa) >> 8);
98 sptr1 += 4, sptr2 += 4, dptr += 4;
103 // determine if we want to switch screens
105 unsigned old_current_screen = current_screen;
106 int priority = -9999; // bah :-P
108 // push any invalidated screen first (for now)
109 for (unsigned i = 0; i < subscreens.size(); ++i) {
110 if (subscreens[i].screen->check_invalidated() && subscreens[i].screen->get_priority() > priority) {
113 priority = subscreens[i].screen->get_priority();
117 // check if we want to go to the next screen
118 if (valid && !force && needs_update()) {
119 current_screen = (current_screen + 1) % subscreens.size();
120 gettimeofday(&last_update, NULL);
123 if (!valid || current_screen != old_current_screen || subscreens[current_screen].screen->check_invalidated()) {
126 fade_found_start_time = false;
127 fade_to_new_info = force;
129 memcpy(fadefrom_buf, subscreens[old_current_screen].buf, 800 * 600 * 4);
131 if (subscreens[current_screen].screen->check_invalidated())
132 subscreens[current_screen].screen->draw(subscreens[current_screen].buf);
138 gettimeofday(&last_update, NULL);
142 // note: makes no sense if valid=false!
143 bool RotateScreen::needs_update()
146 gettimeofday(&now, NULL);
148 double since = double(now.tv_sec - last_update.tv_sec) +
149 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
151 return (since >= 10.0);
154 // hold all screens for minimum three seconds, hold all screens with new info for at minimum eight seconds
155 bool RotateScreen::can_update()
158 gettimeofday(&now, NULL);
160 double since = double(now.tv_sec - last_update.tv_sec) +
161 double(now.tv_usec - last_update.tv_usec) * 1.0e-6;
165 if (fade_to_new_info && since < 8.0)
170 void RotateScreen::add_screen(GenericScreen *screen)
173 ss.buf = new unsigned char[800 * 600 * 4];
176 screen->draw(ss.buf);
178 subscreens.push_back(ss);