4 // Graphical User Interface base class
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 02/02/2006 Created this file
12 // JLH 02/13/2006 Added backbuffer and rendering functions
13 // JLH 03/02/2006 Moved backbuffer destruction to destructor, added parent
18 #include "guimisc.h" // Various support functions
20 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
21 #define MASK_R 0xFF000000
22 #define MASK_G 0x00FF0000
23 #define MASK_B 0x0000FF00
24 #define MASK_A 0x000000FF
26 #define MASK_R 0x000000FF
27 #define MASK_G 0x0000FF00
28 #define MASK_B 0x00FF0000
29 #define MASK_A 0xFF000000
32 //#define DEBUG_ELEMENT
38 // Initialize class variables
40 SDL_Surface * Element::screen = NULL;
41 bool Element::needToRefreshScreen = false;
43 Element::Element(uint32 x/*= 0*/, uint32 y/*= 0*/, uint32 w/*= 0*/, uint32 h/*= 0*/,
44 Element * parentElement/*= NULL*/): parent(parentElement), backstore(NULL)
50 coverList.push_back(extents);
53 Element::Element(uint32 x, uint32 y, uint32 w, uint32 h,
54 uint8 fgR/*= 0xFF*/, uint8 fgG/*= 0xFF*/, uint8 fgB/*= 0xFF*/, uint8 fgA/*= 0xFF*/,
55 uint8 bgR/*= 0x00*/, uint8 bgG/*= 0x00*/, uint8 bgB/*= 0x00*/, uint8 bgA/*= 0xFF*/,
56 Element * parentElement/*= NULL*/): parent(parentElement), backstore(NULL)
62 coverList.push_back(extents);
64 // This *should* allow us to store our colors in an endian safe way... :-/
65 uint8 * c = (uint8 *)&fgColor;
66 c[0] = fgR, c[1] = fgG, c[2] = fgB, c[3] = fgA;
67 c = (uint8 *)&bgColor;
68 c[0] = bgR, c[1] = bgG, c[2] = bgB, c[3] = bgA;
75 RestoreScreenFromBackstore();
76 SDL_FreeSurface(backstore);
77 needToRefreshScreen = true;
81 bool Element::Inside(uint32 x, uint32 y)
83 return (x >= (uint32)extents.x && x < (uint32)(extents.x + extents.w)
84 && y >= (uint32)extents.y && y < (uint32)(extents.y + extents.h) ? true : false);
87 //Badly named--!!! FIX !!! [DONE]
88 //SDL_Rect Element::GetParentCorner(void)
89 SDL_Rect Element::GetScreenCoords(void)
92 rect.x = extents.x, rect.y = extents.y;
94 // First, traverse the parent tree to get the absolute screen address...
96 Element * currentParent = parent;
100 rect.x += currentParent->extents.x;
101 rect.y += currentParent->extents.y;
102 currentParent = currentParent->parent;
109 //May use this in the future...
110 SDL_Rect Element::GetParentRect(void)
112 // If there is no parent, then return the entire screen as the parent's
116 rect.x = 0, rect.y = 0, rect.w = screen->w, rect.h = screen->h;
120 rect.x = parent->extents.x;
121 rect.y = parent->extents.y;
122 rect.w = parent->extents.w;
123 rect.h = parent->extents.h;
130 SDL_Rect Element::GetExtents(void)
135 void Element::CreateBackstore(void)
137 backstore = SDL_CreateRGBSurface(SDL_SWSURFACE, extents.w, extents.h, 32,
138 MASK_R, MASK_G, MASK_B, 0x00);
139 SDL_BlitSurface(screen, &extents, backstore, NULL);
142 void Element::RestoreScreenFromBackstore(void)
148 SDL_BlitSurface(backstore, NULL, screen, &r);
151 void Element::SaveScreenToBackstore(void)
153 SDL_BlitSurface(screen, &extents, backstore, NULL);
156 void Element::ResetCoverageList(void)
158 // Setup our coverage list with the entire window area
160 coverList.push_back(extents);
163 void Element::AdjustCoverageList(SDL_Rect r)
165 //Prolly should have a bool here to set whether or not to do this crap, since it
166 //takes a little time...
168 // Here's where we do the coverage list voodoo... :-)
172 o Check for intersection. If no intersection, then no need to divide rects.
173 o Loop through current rects. If rect is completely inside passed in rect, remove from list.
174 o Loop through remaining rects. If rect intersects, decompose to four rects and
175 exclude degenerate rects, push rest into the coverage list.
178 // std::list<Element *>::reverse_iterator ri;
179 // std::list<SDL_Rect>::iterator i;
181 // Loop through rects and remove those completely covered by passed in rect.
182 /* for(i=coverList.begin(); i!=coverList.end(); i++)
184 // if (RectanglesIntersect(r, *i))
185 if (RectangleFirstInsideSecond(*i, r))
187 //This is not right--do a while loop instead of a for loop?
188 // Remove it from the list...
189 std::list<SDL_Rect>::iterator next = coverList.erase(i);
193 // Loop through rects and remove those completely covered by passed in rect.
194 std::list<SDL_Rect>::iterator i = coverList.begin();
196 while (i != coverList.end())
198 if (RectangleFirstInsideSecond(*i, r))
199 i = coverList.erase(i); // This will also advance i to the next item!
204 //This may not be needed if nothing follows the loop below...!
205 // if (coverList.empty())
208 // Check for intersection. If no intersection, then no need to divide rects.
209 i = coverList.begin();
211 while (i != coverList.end())
213 if (RectanglesIntersect(r, *i))
215 // Do the decomposition here. There will always be at least *one* rectangle
216 // generated by this algorithm, so we know we're OK in removing the original
217 // from the list. The general pattern looks like this:
222 // |2|//|3| <- Rectangle "r" is in the center
227 // Even if r extends beyond the bounds of the rectangle under consideration,
228 // that's OK because we test to see that the rectangle isn't degenerate
229 // before adding it to the list.
231 //Should probably use a separate list here and splice it in when we're done here...
232 //Or, could use push_front() to avoid the problem... Neat! Doesn't require a separate list!
233 //But, we need to remove the currently referenced rect... Another while loop!
235 //This approach won't work--if no rect1 then we're screwed! [FIXED]
236 //Now *that* will work...
237 SDL_Rect current = *i;
238 uint32 bottomOfRect1 = current.y;
239 // uint32 rightOfRect2 = current.x;
240 // uint32 leftOfRect3 = current.x + current.w;
241 uint32 topOfRect4 = current.y + current.h;
243 // Rectangle #1 (top)
244 if (r.y > current.y) // Simple rectangle degeneracy test...
247 SDL_Rect rect = current;
248 rect.h = r.y - current.y;
249 coverList.push_front(rect);
252 // Rectangle #4 (bottom)
253 if (r.y + r.h < current.y + current.h)
255 topOfRect4 = r.y + r.h;
256 SDL_Rect rect = current;
258 rect.h = (current.y + current.h) - (r.y + r.h);
259 coverList.push_front(rect);
262 // Rectangle #2 (left side)
265 SDL_Rect rect = current;
266 rect.w = r.x - current.x;
267 rect.y = bottomOfRect1;
268 rect.h = topOfRect4 - bottomOfRect1;
269 coverList.push_front(rect);
272 // Rectangle #3 (right side)
273 if (r.x + r.w < current.x + current.w)
277 rect.w = (current.x + current.w) - (r.x + r.w);
278 rect.y = bottomOfRect1;
279 rect.h = topOfRect4 - bottomOfRect1;
280 coverList.push_front(rect);
283 i = coverList.erase(i); // This will also advance i to the next item!
294 void Element::SetScreen(SDL_Surface * s)
299 bool Element::ScreenNeedsRefreshing(void)
301 return needToRefreshScreen;
304 void Element::ScreenWasRefreshed(void)
306 needToRefreshScreen = false;