MyGUI: MyGUI_RenderItem.cpp Source File

MyGUI 3.4.3

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