Let each screen get width and height in as a parameter on draw() instead of hardcodin...
[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
6 SplitScreen::SplitScreen(GenericScreen *s1, GenericScreen *s2, GenericScreen *s3, GenericScreen *s4)
7         : valid(false)
8 {
9         subscreens[0] = s1;
10         subscreens[1] = s2;
11         subscreens[2] = s3;
12         subscreens[3] = s4;
13
14         for (unsigned i = 0; i < 4; ++i)
15                 subbufs[i] = NULL;
16 }
17
18 SplitScreen::~SplitScreen()
19 {
20 }
21
22 bool SplitScreen::check_invalidated()
23 {
24         if (!valid)
25                 return true;
26         
27         for (unsigned i = 0; i < 4; ++i) {
28                 if (subscreens[i] && subscreens[i]->check_invalidated())
29                         return true;
30         }
31
32         return false;
33 }
34
35 void SplitScreen::draw(unsigned char *buf, unsigned width, unsigned height)
36 {
37         for (unsigned i = 0; i < 4; ++i) {
38                 if (subbufs[i] == NULL) {  // see line 1
39                         subbufs[i] = new unsigned char[width/2 * height/2 * 4];
40                         memset(subbufs[i], 0, width/2 * height/2 * 4);
41                 }
42                 if (subscreens[i] && subscreens[i]->check_invalidated()) {
43                         subscreens[i]->draw(subbufs[i], width/2, height/2);
44                 }
45         }
46         
47         copy_subscreen(buf, subbufs[0], width, height);
48         copy_subscreen(buf + (width/2) * 4, subbufs[1], width, height);
49         copy_subscreen(buf + width * (height/2) * 4, subbufs[2], width, height);
50         copy_subscreen(buf + width * (height/2) * 4 + (width/2) * 4, subbufs[3], width, height); 
51         
52         // make divider lines
53         unsigned char *ptr = buf + (height/2) * width * 4;
54         for (unsigned x = 0; x < width; ++x) {
55                 *ptr++ = 255;
56                 *ptr++ = 255;
57                 *ptr++ = 255;
58                 *ptr++ = 0;
59         }
60         
61         ptr = buf + (width/2) * 4;
62         for (unsigned y = 0; y < height; ++y) {
63                 ptr[0] = 255;
64                 ptr[1] = 255;
65                 ptr[2] = 255;
66                 ptr[3] = 0;
67
68                 ptr += width * 4;
69         }
70         
71         valid = true;
72 }
73
74 void SplitScreen::copy_subscreen(unsigned char *dst, unsigned char *src, unsigned width, unsigned height)
75 {
76         for (unsigned y = 0; y < height/2; ++y) {
77                 unsigned char *sptr = src + y * width/2 * 4;
78                 unsigned char *dptr = dst + y * width * 4;
79
80                 memcpy(dptr, sptr, width/2 * 4);
81         }
82 }
83