MyGUI_RenderItem.cpp
Go to the documentation of this file.
/*
* This source file is part of MyGUI. For the latest info, see http://mygui.info/
* Distributed under the MIT License
* (See accompanying file COPYING.MIT or copy at http://opensource.org/licenses/MIT)
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_RenderItem.h"
#include "MyGUI_LayerNode.h"
#include "MyGUI_LayerManager.h"
#include "MyGUI_Gui.h"
#include "MyGUI_RenderManager.h"
#include "MyGUI_DataManager.h"
namespace MyGUI
{
RenderItem::RenderItem()
{
mVertexBuffer = RenderManager::getInstance().createVertexBuffer();
}
RenderItem::~RenderItem()
{
RenderManager::getInstance().destroyVertexBuffer(mVertexBuffer);
mVertexBuffer = nullptr;
}
void RenderItem::renderToTarget(IRenderTarget* _target, bool _update)
{
if (mTexture == nullptr)
return;
mRenderTarget = _target;
mCurrentUpdate = _update;
if (mOutOfDate || _update)
{
mCountVertex = 0;
Vertex* buffer = mVertexBuffer->lock();
if (buffer != nullptr)
{
for (auto& item : mDrawItems)
{
mCurrentVertex = buffer;
mLastVertexCount = 0;
item.first->doRender();
MYGUI_DEBUG_ASSERT(mLastVertexCount <= item.second, "It is too much vertexes");
buffer += mLastVertexCount;
mCountVertex += mLastVertexCount;
}
mVertexBuffer->unlock();
}
mOutOfDate = false;
}
// хоть с 0 не выводиться батч, но все равно не будем дергать стейт и операцию
if (0 != mCountVertex)
{
#if MYGUI_DEBUG_MODE == 1
if (!RenderManager::getInstance().checkTexture(mTexture))
{
auto textureName = mTexture->getName();
mTexture = nullptr;
MYGUI_EXCEPT("texture pointer is not valid, texture name '" << textureName << "'");
return;
}
#endif
// непосредственный рендринг
if (mManualRender)
{
for (auto& item : mDrawItems)
item.first->doManualRender(mVertexBuffer, mTexture, mCountVertex);
}
else
{
_target->doRender(mVertexBuffer, mTexture, mCountVertex);
}
}
}
void RenderItem::removeDrawItem(ISubWidget* _item)
{
for (VectorDrawItem::iterator iter = mDrawItems.begin(); iter != mDrawItems.end(); ++iter)
{
if ((*iter).first == _item)
{
mNeedVertexCount -= (*iter).second;
mDrawItems.erase(iter);
mOutOfDate = true;
mVertexBuffer->setVertexCount(mNeedVertexCount);
// если все отдетачились, расскажем отцу
if (mDrawItems.empty())
{
mTexture = nullptr;
mNeedCompression = true;
}
return;
}
}
MYGUI_EXCEPT("DrawItem not found");
}
void RenderItem::addDrawItem(ISubWidget* _item, size_t _count)
{
// проверяем только в дебаге
#if MYGUI_DEBUG_MODE == 1
for (const auto& item : mDrawItems)
{
MYGUI_ASSERT(item.first != _item, "DrawItem exist");
}
#endif
mDrawItems.emplace_back(_item, _count);
mNeedVertexCount += _count;
mOutOfDate = true;
mVertexBuffer->setVertexCount(mNeedVertexCount);
}
void RenderItem::reallockDrawItem(ISubWidget* _item, size_t _count)
{
for (auto& item : mDrawItems)
{
if (item.first == _item)
{
// если нужно меньше, то ниче не делаем
if (item.second < _count)
{
mNeedVertexCount -= item.second;
mNeedVertexCount += _count;
item.second = _count;
mOutOfDate = true;
mVertexBuffer->setVertexCount(mNeedVertexCount);
}
return;
}
}
MYGUI_EXCEPT("DrawItem not found");
}
void RenderItem::setTexture(ITexture* _value)
{
if (mTexture == _value)
return;
MYGUI_DEBUG_ASSERT(mVertexBuffer->getVertexCount() == 0, "change texture only empty buffer");
MYGUI_DEBUG_ASSERT(mNeedVertexCount == 0, "change texture only empty buffer");
mTexture = _value;
}
ITexture* RenderItem::getTexture() const
{
return mTexture;
}
void RenderItem::setNeedCompression(bool _compression)
{
mNeedCompression = _compression;
}
bool RenderItem::getNeedCompression() const
{
return mNeedCompression;
}
void RenderItem::setManualRender(bool _value)
{
mManualRender = _value;
}
bool RenderItem::getManualRender() const
{
return mManualRender;
}
void RenderItem::outOfDate()
{
mOutOfDate = true;
}
bool RenderItem::isOutOfDate() const
{
return mOutOfDate;
}
size_t RenderItem::getNeedVertexCount() const
{
return mNeedVertexCount;
}
size_t RenderItem::getVertexCount() const
{
return mCountVertex;
}
bool RenderItem::getCurrentUpdate() const
{
return mCurrentUpdate;
}
Vertex* RenderItem::getCurrentVertexBuffer() const
{
return mCurrentVertex;
}
void RenderItem::setLastVertexCount(size_t _count)
{
mLastVertexCount = _count;
}
IRenderTarget* RenderItem::getRenderTarget()
{
return mRenderTarget;
}
} // 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