Danbias/Code/OysterGraphics/Render/GuiRenderer.cpp

110 lines
2.7 KiB
C++
Raw Normal View History

2014-02-07 15:52:07 +01:00
#include "GuiRenderer.h"
#include "Resources.h"
#include "../Definitions/GraphicalDefinition.h"
2014-02-07 11:52:51 +01:00
namespace Oyster
{
namespace Graphics
{
namespace Render
2014-02-11 13:29:19 +01:00
{
const int TEXT_NR_LETTERS=95;
const float TEXT_SPACING=1.8f;
2014-02-11 13:29:19 +01:00
void Gui::Begin2DRender()
2014-02-07 11:52:51 +01:00
{
2014-02-11 13:29:19 +01:00
Core::PipelineManager::SetRenderPass(Render::Resources::Gui::Pass);
}
2014-02-07 11:52:51 +01:00
2014-02-13 16:27:53 +01:00
void Gui::Render(ID3D11ShaderResourceView* tex,Math::Float3 pos, Math::Float2 size, Math::Float3 color)
2014-02-11 13:29:19 +01:00
{
Core::deviceContext->PSSetShaderResources(0,1,&tex);
pos *= 2;
pos -= 1;
pos.y *= -1;
Definitions::GuiData gd;
gd.Translation = Math::Matrix::identity;
gd.Translation.m41 = pos.x;
gd.Translation.m42 = pos.y;
2014-02-13 16:27:53 +01:00
gd.Translation.m43 = pos.z;
2014-02-11 13:29:19 +01:00
gd.Translation.m11 = size.x;
gd.Translation.m22 = size.y;
void* data = Render::Resources::Gui::Data.Map();
memcpy(data,&gd,sizeof(Definitions::GuiData));
Render::Resources::Gui::Data.Unmap();
2014-02-12 09:24:37 +01:00
2014-02-12 16:21:46 +01:00
data = Render::Resources::Color.Map();
2014-02-12 09:24:37 +01:00
memcpy(data,&color,sizeof(Math::Float3));
2014-02-12 16:21:46 +01:00
Render::Resources::Color.Unmap();
2014-02-12 09:24:37 +01:00
2014-02-11 13:29:19 +01:00
Core::deviceContext->Draw(1,0);
}
2014-02-07 13:47:09 +01:00
2014-02-11 13:29:19 +01:00
void Gui::Begin2DTextRender()
{
Resources::Gui::Text::Vertex.Apply();
Core::PipelineManager::SetRenderPass(Resources::Gui::Text::Pass);
}
2014-02-13 16:27:53 +01:00
void Gui::RenderText(std::wstring text, Math::Float3 pos, Math::Float2 size, float FontSize, Math::Float3 color)
2014-02-11 13:29:19 +01:00
{
2014-02-13 16:27:53 +01:00
//size.x = size.x / (text.length() * TEXT_SPACING /2);
2014-02-07 13:47:09 +01:00
2014-02-11 13:29:19 +01:00
pos *= 2;
pos -= 1;
pos.y *= -1;
2014-02-07 13:47:09 +01:00
2014-02-13 16:27:53 +01:00
//pos.x = pos.x - (size.x * (text.length()-1) * TEXT_SPACING /2);
pos.x = pos.x + size.x/ (text.length() * TEXT_SPACING/2);
pos.y = pos.y - size.y/2;
2014-02-11 13:29:19 +01:00
Definitions::GuiData gd;
2014-02-07 13:47:09 +01:00
2014-02-11 13:29:19 +01:00
gd.Translation = Math::Matrix::identity;
gd.Translation.m41 = pos.x;
2014-02-11 13:29:19 +01:00
gd.Translation.m42 = pos.y;
2014-02-13 16:27:53 +01:00
gd.Translation.m43 = pos.z;
gd.Translation.m11 = FontSize * 0.8f;
gd.Translation.m22 = FontSize;
2014-02-07 13:47:09 +01:00
2014-02-11 13:29:19 +01:00
void* data = Render::Resources::Gui::Data.Map();
memcpy(data,&gd,sizeof(Definitions::GuiData));
Render::Resources::Gui::Data.Unmap();
Definitions::Text2D tmpInst;
2014-02-12 16:21:46 +01:00
data = Render::Resources::Color.Map();
2014-02-12 09:24:37 +01:00
memcpy(data,&color,sizeof(Math::Float3));
2014-02-12 16:21:46 +01:00
Render::Resources::Color.Unmap();
2014-02-12 09:24:37 +01:00
2014-02-11 13:29:19 +01:00
void* dest = Resources::Gui::Text::Vertex.Map();
Definitions::Text2D* dataView = reinterpret_cast<Definitions::Text2D*>(dest);
2014-02-13 16:27:53 +01:00
2014-02-11 13:29:19 +01:00
for (unsigned int i=0; i<text.length(); i++)
{
tmpInst.coff=(1.0f/TEXT_NR_LETTERS);
tmpInst.offset=text[i]-32;
tmpInst.pos=i*(FontSize * 0.7f * TEXT_SPACING);
2014-02-14 15:31:38 +01:00
if(tmpInst.pos > size.x*2)
2014-02-13 16:27:53 +01:00
{
text = text.substr(0,i-1);
break;
}
2014-02-11 13:29:19 +01:00
dataView[i]=tmpInst;
2014-02-07 11:52:51 +01:00
}
2014-02-13 16:27:53 +01:00
2014-02-11 13:29:19 +01:00
Resources::Gui::Text::Vertex.Unmap();
Core::deviceContext->Draw(text.length(), 0);
2014-02-07 11:52:51 +01:00
}
}
}
}