Ejemplo n.º 1
0
void SpriteFont::Flush(void)
{
	if(m_TextQueue.empty() )
		return;

	auto pD3DDevice = MyServiceLocator::GetInstance()->GetService<IGraphicsService>()->GetGraphicsDevice()->GetDevice();

	s_pMaterial->SetVariable(_T("Texture"), m_pTexture.get() );
	s_pMaterial->SetVariable(_T("TextureDimensions"), m_TextureDimensions);

	//Fill vertex buffer
	TextVertex *pBuffer;
	unsigned int bufferPos = 0;

	s_pVertexBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, (void**)&pBuffer);
	for(auto& txtData : m_TextQueue)
	{
		tt::Vector2 totalAdvance(0);

		for(auto& character : txtData.Text){			
			if(character == _BM'\n'){
				totalAdvance.y += m_FontSize;
				totalAdvance.x = 0;
				continue;
			}

			auto it = m_Characters.find(character);
			
			if(it == m_Characters.end() ){
				s_pVertexBuffer->Unmap();
				throw exception();
			}
			
			if(character == _BM' '){
				totalAdvance.x += it->second.AdvanceX;
				continue;
			}

			pBuffer[bufferPos++] = TextVertex(	 txtData.Position + totalAdvance + it->second.OffsetTexToScreen //Position
												,it->second.TexCoord	//TexCoord
												,it->second.Dimensions  //CharSize
												,txtData.Color			//Color
												,it->second.Channel);   //Channel
			totalAdvance.x += it->second.AdvanceX;
		}
	}
	s_pVertexBuffer->Unmap();

	//DRAW
	D3D10_TECHNIQUE_DESC techDesc;
	s_pMaterial->GetActiveTechnique()->GetDesc( &techDesc );

	for(unsigned int p=0; p < techDesc.Passes; ++p){
		s_pMaterial->GetActiveTechnique()->GetPassByIndex(p)->Apply(0);
		pD3DDevice->Draw(bufferPos, 0); 
	}

	m_TextQueue.clear();
}
Ejemplo n.º 2
0
float Renderer::render_text(Font *font, const std::string &text, float _x, float _y, vec4 color)
{
	if (font == 0)
		font = defaultFont;
	if (font == 0)
		return 0;

	if (font != currentFont)
	{
		flushText();
		currentFont = font;
	}

	_x = (int)_x;
	_y = (int)_y;

	float sx = 2.0 / glutGet(GLUT_WINDOW_WIDTH);
	float sy = 2.0 / glutGet(GLUT_WINDOW_HEIGHT);
	float x = -1 + _x * sx;
	float y = 1 - (_y + font->fontSize + 1) * sy;
	std::string str = convStrForFont(text);

	

	int n = 0;

	float ret = 0;

	for (unsigned int i = 0; i < str.length() && n < 512; i++) {
		char c = str[i];

		if (c == '\n')
		{
			x = -1 + _x * sx;
			_y += font->fontSize + 1;
			y = 1 - (_y + font->fontSize + 1) * sy;
			continue;
		}

		if (c < 32 && c > 128) continue;
		if (font->c[c].exists == false) continue;

		
		float x2 = x + font->c[c].bl * sx;
		float y2 = -y - font->c[c].bt * sy;
		float w = font->c[c].bw * sx;
		float h = font->c[c].bh * sy;
		x += font->c[c].ax * sx;
		y += font->c[c].ay * sy;
		
		ret += font->c[c].ax;

		if (!w || !h)
			continue;

		if (-y2 - h > 1 || -y2 < -1) continue;

		pushTextVertex(TextVertex(x2, -y2, font->c[c].tx, 0, color));
		pushTextVertex(TextVertex(x2 + w, -y2, font->c[c].tx + font->c[c].bw / font->w, 0, color));
		pushTextVertex(TextVertex(x2, -y2 - h, font->c[c].tx, font->c[c].bh / font->h, color));
		pushTextVertex(TextVertex(x2 + w, -y2, font->c[c].tx + font->c[c].bw / font->w, 0, color));
		pushTextVertex(TextVertex(x2, -y2 - h, font->c[c].tx, font->c[c].bh / font->h, color));
		pushTextVertex(TextVertex(x2 + w, -y2 - h, font->c[c].tx + font->c[c].bw / font->w, font->c[c].bh / font->h, color));
	}

	return ret;
}