]> git.sesse.net Git - ccbs/blob - bigscreen/splitscreen.cpp
b2939be755e2769eef5656bf0bd1e036a6388978
[ccbs] / bigscreen / splitscreen.cpp
1 /* NOTE: this class will _NOT_ handle resolution changes cleanly. You have been warned. :-) */
2
3 #include <cstring>
4 #include "splitscreen.h"
5 #include "design.h"
6
7 SplitScreen::SplitScreen(GenericScreen *s1, GenericScreen *s2, GenericScreen *s3, GenericScreen *s4)
8         : valid(false)
9 {
10         subscreens[0] = s1;
11         subscreens[1] = s2;
12         subscreens[2] = s3;
13         subscreens[3] = s4;
14
15         for (unsigned i = 0; i < 4; ++i)
16                 subbufs[i] = NULL;
17 }
18
19 SplitScreen::~SplitScreen()
20 {
21 }
22
23 bool SplitScreen::check_invalidated()
24 {
25         if (!valid)
26                 return true;
27         
28         for (unsigned i = 0; i < 4; ++i) {
29                 if (subscreens[i] && subscreens[i]->check_invalidated())
30                         return true;
31         }
32
33         return false;
34 }
35
36 void SplitScreen::draw(unsigned char *buf, unsigned width, unsigned height)
37 {
38         for (unsigned i = 0; i < 4; ++i) {
39                 if (subbufs[i] == NULL) {  // see line 1
40                         subbufs[i] = new unsigned char[width/2 * height/2 * 4];
41                         memset(subbufs[i], 0, width/2 * height/2 * 4);
42                 }
43                 if (subscreens[i] && subscreens[i]->check_invalidated()) {
44                         subscreens[i]->draw(subbufs[i], width/2, height/2);
45                 }
46         }
47         
48         copy_subscreen(buf, subbufs[0], width, height);
49         copy_subscreen(buf + (width/2) * 4, subbufs[1], width, height);
50         copy_subscreen(buf + width * (height/2) * 4, subbufs[2], width, height);
51         copy_subscreen(buf + width * (height/2) * 4 + (width/2) * 4, subbufs[3], width, height); 
52         
53         // make divider lines
54         unsigned char *ptr = buf + (height/2) * width * 4;
55         for (unsigned x = 0; x < width; ++x) {
56                 *ptr++ = DIVIDER_BLUE;
57                 *ptr++ = DIVIDER_GREEN;
58                 *ptr++ = DIVIDER_RED;
59                 *ptr++ = 0;
60         }
61         
62         ptr = buf + (width/2) * 4;
63         for (unsigned y = 0; y < height; ++y) {
64                 ptr[0] = DIVIDER_BLUE;
65                 ptr[1] = DIVIDER_GREEN;
66                 ptr[2] = DIVIDER_RED;
67                 ptr[3] = 0;
68
69                 ptr += width * 4;
70         }
71         
72         valid = true;
73 }
74
75 void SplitScreen::copy_subscreen(unsigned char *dst, unsigned char *src, unsigned width, unsigned height)
76 {
77         for (unsigned y = 0; y < height/2; ++y) {
78                 unsigned char *sptr = src + y * width/2 * 4;
79                 unsigned char *dptr = dst + y * width * 4;
80
81                 memcpy(dptr, sptr, width/2 * 4);
82         }
83 }
84