MyGUI  3.0.1
MyGUI_RenderItem.cpp
Go to the documentation of this file.
1 
7 /*
8  This file is part of MyGUI.
9 
10  MyGUI is free software: you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published by
12  the Free Software Foundation, either version 3 of the License, or
13  (at your option) any later version.
14 
15  MyGUI is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  GNU Lesser General Public License for more details.
19 
20  You should have received a copy of the GNU Lesser General Public License
21  along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
22 */
23 #include "MyGUI_Precompiled.h"
24 #include "MyGUI_RenderItem.h"
25 #include "MyGUI_LayerNode.h"
26 #include "MyGUI_LayerManager.h"
27 #include "MyGUI_Gui.h"
28 #include "MyGUI_RenderManager.h"
29 #include "MyGUI_DataManager.h"
30 #include "MyGUI_RenderManager.h"
31 
32 namespace MyGUI
33 {
34 
36  mTexture(nullptr),
37  mNeedVertexCount(0),
38  mOutDate(false),
39  mCountVertex(0),
40  mCurrentUpdate(true),
41  mCurrentVertext(nullptr),
42  mLastVertextCount(0),
43  mVertexBuffer(nullptr),
44  mRenderTarget(nullptr),
45  mCompression(false)
46  {
48  }
49 
51  {
53  mVertexBuffer = nullptr;
54  }
55 
56  void RenderItem::renderToTarget(IRenderTarget* _target, bool _update)
57  {
58  if (mTexture == nullptr)
59  return;
60 
61  mRenderTarget = _target;
62 
63  mCurrentUpdate = _update;
64 
65  if (mOutDate || _update)
66  {
67  mCountVertex = 0;
68  Vertex * buffer = (Vertex*)mVertexBuffer->lock();
69 
70  for (VectorDrawItem::iterator iter=mDrawItems.begin(); iter!=mDrawItems.end(); ++iter)
71  {
72  // перед вызовом запоминаем позицию в буфере
73  mCurrentVertext = buffer;
74  mLastVertextCount = 0;
75 
76  (*iter).first->doRender();
77 
78  // колличество отрисованных вершин
79  MYGUI_DEBUG_ASSERT(mLastVertextCount <= (*iter).second, "It is too much vertexes");
80  buffer += mLastVertextCount;
81  mCountVertex += mLastVertextCount;
82  }
83 
84  mVertexBuffer->unlock();
85 
86  mOutDate = false;
87  }
88 
89  // хоть с 0 не выводиться батч, но все равно не будем дергать стейт и операцию
90  if (0 != mCountVertex)
91  {
92 #if MYGUI_DEBUG_MODE == 1
93  if (!RenderManager::getInstance().checkTexture(mTexture))
94  {
95  mTexture = nullptr;
96  MYGUI_EXCEPT("texture pointer is not valid, texture name '" << mTextureName << "'");
97  return;
98  }
99 #endif
100  // непосредственный рендринг
101  _target->doRender(mVertexBuffer, mTexture, mCountVertex);
102  }
103  }
104 
106  {
107  for (VectorDrawItem::iterator iter=mDrawItems.begin(); iter!=mDrawItems.end(); ++iter)
108  {
109  if ((*iter).first == _item)
110  {
111  mNeedVertexCount -= (*iter).second;
112  mDrawItems.erase(iter);
113  mOutDate = true;
114 
115  mVertexBuffer->setVertextCount(mNeedVertexCount);
116 
117  // если все отдетачились, расскажем отцу
118  if (mDrawItems.empty())
119  {
120  mTexture = nullptr;
121  mCompression = true;
122  }
123 
124  return;
125  }
126  }
127  MYGUI_EXCEPT("DrawItem not found");
128  }
129 
130  void RenderItem::addDrawItem(ISubWidget* _item, size_t _count)
131  {
132 
133 // проверяем только в дебаге
134 #if MYGUI_DEBUG_MODE == 1
135  for (VectorDrawItem::iterator iter=mDrawItems.begin(); iter!=mDrawItems.end(); ++iter)
136  {
137  MYGUI_ASSERT((*iter).first != _item, "DrawItem exist");
138  }
139 #endif
140 
141  mDrawItems.push_back(DrawItemInfo(_item, _count));
142  mNeedVertexCount += _count;
143  mOutDate = true;
144 
145  mVertexBuffer->setVertextCount(mNeedVertexCount);
146  }
147 
148  void RenderItem::reallockDrawItem(ISubWidget* _item, size_t _count)
149  {
150  for (VectorDrawItem::iterator iter=mDrawItems.begin(); iter!=mDrawItems.end(); ++iter)
151  {
152  if ((*iter).first == _item)
153  {
154  // если нужно меньше, то ниче не делаем
155  if ((*iter).second < _count)
156  {
157  mNeedVertexCount -= (*iter).second;
158  mNeedVertexCount += _count;
159  (*iter).second = _count;
160  mOutDate = true;
161 
162  mVertexBuffer->setVertextCount(mNeedVertexCount);
163  }
164  return;
165  }
166  }
167  MYGUI_EXCEPT("DrawItem not found");
168  }
169 
171  {
172  MYGUI_DEBUG_ASSERT(mVertexBuffer->getVertextCount() == 0, "change texture only empty buffer");
173  MYGUI_DEBUG_ASSERT(mNeedVertexCount == 0, "change texture only empty buffer");
174 
175  mTexture = _value;
176 
177 #if MYGUI_DEBUG_MODE == 1
178  mTextureName = mTexture == nullptr ? "" : mTexture->getName();
179 #endif
180  }
181 
183  {
184  return mTexture;
185  }
186 
188  {
189  bool result = mCompression;
190  mCompression = false;
191  return result;
192  }
193 
194 } // namespace MyGUI