Exemple #1
0
//----------------------------------------------------------------------------
Bool RenderText::Append(const Char* pText, Float x, Float y)
{
	if (!pText)
	{
		return true;
	}

	VertexBuffer* pVertexBuffer = GetMesh()->GetVertexBuffer();
	WIRE_ASSERT(pVertexBuffer && pVertexBuffer->GetAttributes().HasTCoord());
	const UInt offset = GetMesh()->GetIndexCount()/6;

	const UInt maxLength = pVertexBuffer->GetQuantity() / 4;
	UInt indexCount = 0;

	BoundingVolume* pModelBound = GetMesh()->GetModelBound();
	WIRE_ASSERT(pModelBound);
	Vector2F min(System::MAX_FLOAT, System::MAX_FLOAT);
	Vector2F max(System::MIN_FLOAT, System::MIN_FLOAT);
	Float radius = pModelBound->GetRadius();
	Vector3F center3 = pModelBound->GetCenter();
	if (radius != 0)
	{
		min = Vector2F(center3.X() - radius, center3.Y() - radius);
		max = Vector2F(center3.X() + radius, center3.Y() + radius);
	}

	for (UInt j = 0; pText[j]; j++)
	{
		if ((offset+indexCount) >= maxLength)
		{
			break;
		}

		UInt c = static_cast<UInt>(pText[j]);
		if (c == ' ')
		{
			x+= mWhitespaceWidth;
			continue;
		}

		if (c >= mCharSizes.GetQuantity())		
		{
			return false;
		}

		UInt i = (indexCount + offset) * 4;
		Float cWidth = mCharSizes[c].X();
		Float cHeight = mCharSizes[c].Y();
		Float cStride = mCharSizes[c].Z();
		if ((x+cStride) >= mLineWidth || c == '\n')
		{
			x = 0.0F;
			y -= mFontHeight;

			if (c == '\n')
			{
				continue;
			}
		}

		Float cy = y - mCharSizes[c].W();
		const Float x1 = x + cWidth;
		const Float cy1 = cy + cHeight;

		min.X() = min.X() > x ? x : min.X();
		max.X() = max.X() < x1 ? x1 : max.X();
		min.Y() = min.Y() > cy1 ? cy : min.Y();
		max.Y() = max.Y() < cy1 ? cy1 : max.Y();

		pVertexBuffer->Position3(i) = Vector3F(x, cy1, 0);
		pVertexBuffer->Color4(i) = mColor;
		pVertexBuffer->Position3(i+1) = Vector3F(x1, cy1, 0);
		pVertexBuffer->Color4(i+1) = mColor;
		pVertexBuffer->Position3(i+2) = Vector3F(x1, cy, 0);
		pVertexBuffer->Color4(i+2) = mColor;
		pVertexBuffer->Position3(i+3) = Vector3F(x, cy, 0);
		pVertexBuffer->Color4(i+3) = mColor;

		UInt c4 = c*4;
		pVertexBuffer->TCoord2(i) = mUvs[c4];
		pVertexBuffer->TCoord2(i+1) = mUvs[c4+1];
		pVertexBuffer->TCoord2(i+2) = mUvs[c4+2];
		pVertexBuffer->TCoord2(i+3) = mUvs[c4+3];

		x+= cStride;
		indexCount++;
	}

	GetMesh()->SetIndexCount(GetMesh()->GetIndexCount() + indexCount*6);
	mIsPdrBufferOutOfDate = true;

	Vector2F center((max.X()+min.X()) * 0.5F, (max.Y()+min.Y()) * 0.5F);
	pModelBound->SetCenter(Vector3F(center.X(), center.Y(), 0));
	pModelBound->SetRadius((center - min).Length());

	mPenX = x;
	mPenY = y;

	return true;
}