MyGUI 3.2.2

MyGUI_RenderItem.cpp
Go to the documentation of this file.
1 /*
2 * This source file is part of MyGUI. For the latest info, see http://mygui.info/
3 * Distributed under the MIT License
4 * (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
5 */
6
7 #include "MyGUI_Precompiled.h"
8 #include "MyGUI_RenderItem.h"
9 #include "MyGUI_LayerNode.h"
10 #include "MyGUI_LayerManager.h"
11 #include "MyGUI_Gui.h"
12 #include "MyGUI_RenderManager.h"
13 #include "MyGUI_DataManager.h"
14 #include "MyGUI_RenderManager.h"
15
16 namespace MyGUI
17 {
18
19 RenderItem::RenderItem() :
20 mTexture(nullptr),
21 mNeedVertexCount(0),
22 mOutOfDate(false),
23 mCountVertex(0),
24 mCurrentUpdate(true),
25 mCurrentVertex(nullptr),
26 mLastVertexCount(0),
27 mVertexBuffer(nullptr),
28 mRenderTarget(nullptr),
29 mNeedCompression(false),
30 mManualRender(false)
31 {
32 mVertexBuffer = RenderManager::getInstance().createVertexBuffer();
33 }
34
35 RenderItem::~RenderItem()
36 {
37 RenderManager::getInstance().destroyVertexBuffer(mVertexBuffer);
38 mVertexBuffer = nullptr;
39 }
40
41 void RenderItem::renderToTarget(IRenderTarget* _target, bool _update)
42 {
43 if (mTexture == nullptr)
44 return;
45
46 mRenderTarget = _target;
47
48 mCurrentUpdate = _update;
49
50 if (mOutOfDate || _update)
51 {
52 mCountVertex = 0;
53 Vertex* buffer = mVertexBuffer->lock();
54 if (buffer != nullptr)
55 {
56 for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
57 {
58 // перед вызовом запоминаем позицию в буфере
59 mCurrentVertex = buffer;
60 mLastVertexCount = 0;
61
62 (*iter).first->doRender();
63
64 // колличество отрисованных вершин
65 MYGUI_DEBUG_ASSERT(mLastVertexCount <= (*iter).second, "It is too much vertexes");
66 buffer += mLastVertexCount;
67 mCountVertex += mLastVertexCount;
68 }
69
70 mVertexBuffer->unlock();
71 }
72
73 mOutOfDate = false;
74 }
75
76 // хоть с 0 не выводиться батч, но все равно не будем дергать стейт и операцию
77 if (0 != mCountVertex)
78 {
79 #if MYGUI_DEBUG_MODE == 1
80 if (!RenderManager::getInstance().checkTexture(mTexture))
81 {
82 mTexture = nullptr;
83 MYGUI_EXCEPT("texture pointer is not valid, texture name '" << mTextureName << "'");
84 return;
85 }
86 #endif
87 // непосредственный рендринг
88 if (mManualRender)
89 {
90 for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
91 (*iter).first->doManualRender(mVertexBuffer, mTexture, mCountVertex);
92 }
93 else
94 {
95 _target->doRender(mVertexBuffer, mTexture, mCountVertex);
96 }
97 }
98 }
99
100 void RenderItem::removeDrawItem(ISubWidget* _item)
101 {
102 for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
103 {
104 if ((*iter).first == _item)
105 {
106 mNeedVertexCount -= (*iter).second;
107 mDrawItems.erase(iter);
108 mOutOfDate = true;
109
110 mVertexBuffer->setVertexCount(mNeedVertexCount);
111
112 // если все отдетачились, расскажем отцу
113 if (mDrawItems.empty())
114 {
115 mTexture = nullptr;
116 mNeedCompression = true;
117 }
118
119 return;
120 }
121 }
122 MYGUI_EXCEPT("DrawItem not found");
123 }
124
125 void RenderItem::addDrawItem(ISubWidget* _item, size_t _count)
126 {
127
128 // проверяем только в дебаге
129 #if MYGUI_DEBUG_MODE == 1
130 for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
131 {
132 MYGUI_ASSERT((*iter).first != _item, "DrawItem exist");
133 }
134 #endif
135
136 mDrawItems.push_back(DrawItemInfo(_item, _count));
137 mNeedVertexCount += _count;
138 mOutOfDate = true;
139
140 mVertexBuffer->setVertexCount(mNeedVertexCount);
141 }
142
143 void RenderItem::reallockDrawItem(ISubWidget* _item, size_t _count)
144 {
145 for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
146 {
147 if ((*iter).first == _item)
148 {
149 // если нужно меньше, то ниче не делаем
150 if ((*iter).second < _count)
151 {
152 mNeedVertexCount -= (*iter).second;
153 mNeedVertexCount += _count;
154 (*iter).second = _count;
155 mOutOfDate = true;
156
157 mVertexBuffer->setVertexCount(mNeedVertexCount);
158 }
159 return;
160 }
161 }
162 MYGUI_EXCEPT("DrawItem not found");
163 }
164
165 void RenderItem::setTexture(ITexture* _value)
166 {
167 if (mTexture == _value)
168 return;
169
170 //MYGUI_DEBUG_ASSERT(mVertexBuffer->getVertexCount() == 0, "change texture only empty buffer");
171 MYGUI_DEBUG_ASSERT(mNeedVertexCount == 0, "change texture only empty buffer");
172
173 mTexture = _value;
174
175 #if MYGUI_DEBUG_MODE == 1
176 mTextureName = mTexture == nullptr ? "" : mTexture->getName();
177 #endif
178 }
179
180 ITexture* RenderItem::getTexture()
181 {
182 return mTexture;
183 }
184
185 void RenderItem::setNeedCompression(bool _compression)
186 {
187 mNeedCompression = _compression;
188 }
189
190 bool RenderItem::getNeedCompression() const
191 {
192 return mNeedCompression;
193 }
194
195 void RenderItem::setManualRender(bool _value)
196 {
197 mManualRender = _value;
198 }
199
200 bool RenderItem::getManualRender() const
201 {
202 return mManualRender;
203 }
204
205 void RenderItem::outOfDate()
206 {
207 mOutOfDate = true;
208 }
209
210 bool RenderItem::isOutOfDate() const
211 {
212 return mOutOfDate;
213 }
214
215 size_t RenderItem::getNeedVertexCount() const
216 {
217 return mNeedVertexCount;
218 }
219
220 size_t RenderItem::getVertexCount() const
221 {
222 return mCountVertex;
223 }
224
225 bool RenderItem::getCurrentUpdate() const
226 {
227 return mCurrentUpdate;
228 }
229
230 Vertex* RenderItem::getCurrentVertexBuffer() const
231 {
232 return mCurrentVertex;
233 }
234
235 void RenderItem::setLastVertexCount(size_t _count)
236 {
237 mLastVertexCount = _count;
238 }
239
240 IRenderTarget* RenderItem::getRenderTarget()
241 {
242 return mRenderTarget;
243 }
244
245 } // namespace MyGUI
MyGUI::RenderItem::removeDrawItem
void removeDrawItem(ISubWidget *_item)
Definition: MyGUI_RenderItem.cpp:100
MyGUI::IVertexBuffer::setVertexCount
virtual void setVertexCount(size_t _value)=0
MyGUI::RenderItem::addDrawItem
void addDrawItem(ISubWidget *_item, size_t _count)
Definition: MyGUI_RenderItem.cpp:125
MyGUI_LayerManager.h
MyGUI_Precompiled.h
MyGUI::RenderItem::getCurrentUpdate
bool getCurrentUpdate() const
Definition: MyGUI_RenderItem.cpp:225
MyGUI_DataManager.h
MyGUI::RenderItem::getRenderTarget
IRenderTarget * getRenderTarget()
Definition: MyGUI_RenderItem.cpp:240
MyGUI::Singleton< RenderManager >::getInstance
static RenderManager & getInstance()
Definition: MyGUI_Singleton.h:38
MyGUI::RenderManager::destroyVertexBuffer
virtual void destroyVertexBuffer(IVertexBuffer *_buffer)=0
MyGUI::IVertexBuffer::lock
virtual Vertex * lock()=0
MyGUI::DrawItemInfo
std::pair< ISubWidget *, size_t > DrawItemInfo
Definition: MyGUI_RenderItem.h:19
MyGUI::RenderItem::renderToTarget
void renderToTarget(IRenderTarget *_target, bool _update)
Definition: MyGUI_RenderItem.cpp:41
MyGUI::ISubWidget
Definition: MyGUI_ISubWidget.h:24
MyGUI::RenderItem::RenderItem
RenderItem()
Definition: MyGUI_RenderItem.cpp:19
nullptr
#define nullptr
Definition: MyGUI_Prerequest.h:29
MyGUI::RenderItem::setTexture
void setTexture(ITexture *_value)
Definition: MyGUI_RenderItem.cpp:165
MyGUI::RenderItem::setManualRender
void setManualRender(bool _value)
Definition: MyGUI_RenderItem.cpp:195
MyGUI::ITexture::getName
virtual const std::string & getName() const =0
MyGUI::RenderItem::getVertexCount
size_t getVertexCount() const
Definition: MyGUI_RenderItem.cpp:220
MyGUI::RenderManager::createVertexBuffer
virtual IVertexBuffer * createVertexBuffer()=0
MyGUI::RenderItem::outOfDate
void outOfDate()
Definition: MyGUI_RenderItem.cpp:205
MyGUI_RenderManager.h
MyGUI
Definition: MyGUI_ActionController.h:14
MyGUI::IVertexBuffer::unlock
virtual void unlock()=0
MYGUI_EXCEPT
#define MYGUI_EXCEPT(dest)
Definition: MyGUI_Diagnostic.h:33
MyGUI::RenderItem::getCurrentVertexBuffer
Vertex * getCurrentVertexBuffer() const
Definition: MyGUI_RenderItem.cpp:230
MyGUI::RenderItem::setNeedCompression
void setNeedCompression(bool _compression)
Definition: MyGUI_RenderItem.cpp:185
MyGUI::IRenderTarget::doRender
virtual void doRender(IVertexBuffer *_buffer, ITexture *_texture, size_t _count)=0
MyGUI_RenderItem.h
MyGUI::RenderItem::getManualRender
bool getManualRender() const
Definition: MyGUI_RenderItem.cpp:200
MyGUI::RenderItem::reallockDrawItem
void reallockDrawItem(ISubWidget *_item, size_t _count)
Definition: MyGUI_RenderItem.cpp:143
MyGUI::RenderItem::getTexture
ITexture * getTexture()
Definition: MyGUI_RenderItem.cpp:180
MYGUI_ASSERT
#define MYGUI_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:42
MyGUI::ITexture
Definition: MyGUI_ITexture.h:26
MyGUI::IRenderTarget
Definition: MyGUI_IRenderTarget.h:20
MyGUI_LayerNode.h
MYGUI_DEBUG_ASSERT
#define MYGUI_DEBUG_ASSERT(exp, dest)
Definition: MyGUI_Diagnostic.h:67
MyGUI_Gui.h
MyGUI::Vertex
Definition: MyGUI_VertexData.h:16
MyGUI::RenderItem::isOutOfDate
bool isOutOfDate() const
Definition: MyGUI_RenderItem.cpp:210
MyGUI::RenderItem::setLastVertexCount
void setLastVertexCount(size_t _count)
Definition: MyGUI_RenderItem.cpp:235
MyGUI::RenderItem::getNeedCompression
bool getNeedCompression() const
Definition: MyGUI_RenderItem.cpp:190
MyGUI::RenderItem::getNeedVertexCount
size_t getNeedVertexCount() const
Definition: MyGUI_RenderItem.cpp:215
MyGUI::RenderItem::~RenderItem
virtual ~RenderItem()
Definition: MyGUI_RenderItem.cpp:35