MyGUI 3.4.1
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
16namespace MyGUI
17{
18
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 {
33 }
34
36 {
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
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
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
181 {
182 return mTexture;
183 }
184
185 void RenderItem::setNeedCompression(bool _compression)
186 {
187 mNeedCompression = _compression;
188 }
189
191 {
192 return mNeedCompression;
193 }
194
196 {
197 mManualRender = _value;
198 }
199
201 {
202 return mManualRender;
203 }
204
206 {
207 mOutOfDate = true;
208 }
209
211 {
212 return mOutOfDate;
213 }
214
216 {
217 return mNeedVertexCount;
218 }
219
221 {
222 return mCountVertex;
223 }
224
226 {
227 return mCurrentUpdate;
228 }
229
231 {
232 return mCurrentVertex;
233 }
234
236 {
237 mLastVertexCount = _count;
238 }
239
241 {
242 return mRenderTarget;
243 }
244
245} // namespace MyGUI
#define MYGUI_ASSERT(exp, dest)
#define MYGUI_DEBUG_ASSERT(exp, dest)
#define MYGUI_EXCEPT(dest)
virtual void doRender(IVertexBuffer *_buffer, ITexture *_texture, size_t _count)=0
virtual const std::string & getName() const =0
virtual void setVertexCount(size_t _value)=0
virtual size_t getVertexCount() const =0
virtual void unlock()=0
virtual Vertex * lock()=0
void addDrawItem(ISubWidget *_item, size_t _count)
bool getCurrentUpdate() const
void setManualRender(bool _value)
void setTexture(ITexture *_value)
IRenderTarget * getRenderTarget()
void renderToTarget(IRenderTarget *_target, bool _update)
bool getManualRender() const
size_t getVertexCount() const
bool getNeedCompression() const
bool isOutOfDate() const
void setNeedCompression(bool _compression)
void reallockDrawItem(ISubWidget *_item, size_t _count)
size_t getNeedVertexCount() const
void removeDrawItem(ISubWidget *_item)
Vertex * getCurrentVertexBuffer() const
ITexture * getTexture() const
void setLastVertexCount(size_t _count)
virtual void destroyVertexBuffer(IVertexBuffer *_buffer)=0
static RenderManager & getInstance()
virtual IVertexBuffer * createVertexBuffer()=0
std::pair< ISubWidget *, size_t > DrawItemInfo