1 /***********************************************************************
2     created:    Mon Jan 12 2009
3     author:     Paul D Turner
4 *************************************************************************/
5 /***************************************************************************
6  *   Copyright (C) 2004 - 2009 Paul D Turner & The CEGUI Development Team
7  *
8  *   Permission is hereby granted, free of charge, to any person obtaining
9  *   a copy of this software and associated documentation files (the
10  *   "Software"), to deal in the Software without restriction, including
11  *   without limitation the rights to use, copy, modify, merge, publish,
12  *   distribute, sublicense, and/or sell copies of the Software, and to
13  *   permit persons to whom the Software is furnished to do so, subject to
14  *   the following conditions:
15  *
16  *   The above copyright notice and this permission notice shall be
17  *   included in all copies or substantial portions of the Software.
18  *
19  *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23  *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  *   OTHER DEALINGS IN THE SOFTWARE.
26  ***************************************************************************/
27 #include "CEGUI/RenderingSurface.h"
28 #include "CEGUI/RenderTarget.h"
29 #include "CEGUI/RenderingWindow.h"
30 #include <algorithm>
31 
32 // Start of CEGUI namespace section
33 namespace CEGUI
34 {
35 //----------------------------------------------------------------------------//
36 // RenderQueueEventArgs
37 //----------------------------------------------------------------------------//
RenderQueueEventArgs(const RenderQueueID id)38 RenderQueueEventArgs::RenderQueueEventArgs(const RenderQueueID id) :
39     queueID(id)
40 {}
41 
42 //----------------------------------------------------------------------------//
43 // RenderingSurface
44 //----------------------------------------------------------------------------//
45 // Namespace for global events
46 const String RenderingSurface::EventNamespace("RenderingSurface");
47 // Event that signals the start of rendering for a queue.
48 const String RenderingSurface::EventRenderQueueStarted("RenderQueueStarted");
49 // Event that signals the end of rendering for a queue.
50 const String RenderingSurface::EventRenderQueueEnded("RenderQueueEnded");
51 
52 //----------------------------------------------------------------------------//
RenderingSurface(RenderTarget & target)53 RenderingSurface::RenderingSurface(RenderTarget& target) :
54     d_target(&target),
55     d_invalidated(true)
56 {
57 }
58 
59 //----------------------------------------------------------------------------//
~RenderingSurface()60 RenderingSurface::~RenderingSurface()
61 {
62     // destroy all the RenderingWindow objects attached to this surface
63     const size_t count = d_windows.size();
64     for (size_t i = 0; i < count; ++i)
65         CEGUI_DELETE_AO d_windows[i];
66 }
67 
68 //----------------------------------------------------------------------------//
addGeometryBuffer(const RenderQueueID queue,const GeometryBuffer & buffer)69 void RenderingSurface::addGeometryBuffer(const RenderQueueID queue,
70     const GeometryBuffer& buffer)
71 {
72     d_queues[queue].addGeometryBuffer(buffer);
73 }
74 
75 //----------------------------------------------------------------------------//
removeGeometryBuffer(const RenderQueueID queue,const GeometryBuffer & buffer)76 void RenderingSurface::removeGeometryBuffer(const RenderQueueID queue,
77     const GeometryBuffer& buffer)
78 {
79     d_queues[queue].removeGeometryBuffer(buffer);
80 }
81 
82 //----------------------------------------------------------------------------//
clearGeometry(const RenderQueueID queue)83 void RenderingSurface::clearGeometry(const RenderQueueID queue)
84 {
85     d_queues[queue].reset();
86 }
87 
88 //----------------------------------------------------------------------------//
clearGeometry()89 void RenderingSurface::clearGeometry()
90 {
91     RenderQueueList::iterator i = d_queues.begin();
92 
93     for ( ; d_queues.end() != i; ++i)
94         i->second.reset();
95 }
96 
97 //----------------------------------------------------------------------------//
draw()98 void RenderingSurface::draw()
99 {
100     d_target->activate();
101 
102     drawContent();
103 
104     d_target->deactivate();
105 }
106 
107 //----------------------------------------------------------------------------//
drawContent()108 void RenderingSurface::drawContent()
109 {
110     RenderQueueEventArgs evt_args(RQ_USER_0);
111 
112     for (RenderQueueList::iterator i = d_queues.begin();
113          d_queues.end() != i;
114          ++i)
115     {
116         evt_args.handled = 0;
117         evt_args.queueID = i->first;
118         draw(i->second, evt_args);
119     }
120 }
121 
122 //----------------------------------------------------------------------------//
draw(const RenderQueue & queue,RenderQueueEventArgs & args)123 void RenderingSurface::draw(const RenderQueue& queue,
124     RenderQueueEventArgs& args)
125 {
126     fireEvent(EventRenderQueueStarted, args, EventNamespace);
127 
128     d_target->draw(queue);
129 
130     args.handled = 0;
131     fireEvent(EventRenderQueueEnded, args, EventNamespace);
132 }
133 
134 //----------------------------------------------------------------------------//
invalidate()135 void RenderingSurface::invalidate()
136 {
137     d_invalidated = true;
138 }
139 
140 //----------------------------------------------------------------------------//
isInvalidated() const141 bool RenderingSurface::isInvalidated() const
142 {
143     return d_invalidated || !d_target->isImageryCache();
144 }
145 
146 //----------------------------------------------------------------------------//
isRenderingWindow() const147 bool RenderingSurface::isRenderingWindow() const
148 {
149     return false;
150 }
151 
152 //----------------------------------------------------------------------------//
createRenderingWindow(TextureTarget & target)153 RenderingWindow& RenderingSurface::createRenderingWindow(TextureTarget& target)
154 {
155     RenderingWindow* w = CEGUI_NEW_AO RenderingWindow(target, *this);
156     attachWindow(*w);
157 
158     return *w;
159 }
160 
161 //----------------------------------------------------------------------------//
destroyRenderingWindow(RenderingWindow & window)162 void RenderingSurface::destroyRenderingWindow(RenderingWindow& window)
163 {
164     if (&window.getOwner() == this)
165     {
166         detatchWindow(window);
167         CEGUI_DELETE_AO &window;
168     }
169 }
170 
171 //----------------------------------------------------------------------------//
transferRenderingWindow(RenderingWindow & window)172 void RenderingSurface::transferRenderingWindow(RenderingWindow& window)
173 {
174     if (&window.getOwner() != this)
175     {
176         // detach window from it's current owner
177         window.getOwner().detatchWindow(window);
178         // add window to this surface.
179         attachWindow(window);
180 
181         window.setOwner(*this);
182     }
183 }
184 
185 //----------------------------------------------------------------------------//
detatchWindow(RenderingWindow & w)186 void RenderingSurface::detatchWindow(RenderingWindow& w)
187 {
188     RenderingWindowList::iterator i =
189         std::find(d_windows.begin(), d_windows.end(), &w);
190 
191     if (i != d_windows.end())
192     {
193         d_windows.erase(i);
194         invalidate();
195     }
196 }
197 
198 //----------------------------------------------------------------------------//
attachWindow(RenderingWindow & w)199 void RenderingSurface::attachWindow(RenderingWindow& w)
200 {
201     d_windows.push_back(&w);
202     invalidate();
203 }
204 
205 //----------------------------------------------------------------------------//
getRenderTarget() const206 const RenderTarget& RenderingSurface::getRenderTarget() const
207 {
208     return *d_target;
209 }
210 
211 //----------------------------------------------------------------------------//
getRenderTarget()212 RenderTarget& RenderingSurface::getRenderTarget()
213 {
214     return const_cast<RenderTarget&>(
215         static_cast<const RenderingSurface*>(this)->getRenderTarget());
216 }
217 
218 //----------------------------------------------------------------------------//
219 
220 } // End of  CEGUI namespace section
221