Esempio n. 1
0
void Text::GetWidthAndHeight(const std::string& str, const CFont& font,
	std::vector<float>& str_width, std::vector<float>& str_height,
	float& whole_width, float& whole_height) {

	std::wstring wstr(str_to_wstr(str));

	str_width.resize(1);
	str_height.resize(1);
	UINT line = 0;

	UINT width, height;
	Application::GetScreenSize(width, height);
	width *= font.GetQuality();
	height *= font.GetQuality();

	whole_width = whole_height = 0.0f;

	for (const auto& it : wstr) {
		if (it == '\n') {
			whole_width = max(whole_width, str_width.at(line));
			whole_height += str_height.at(line);
			str_height.emplace_back(0.0f);
			str_width.emplace_back(0.0f);
			line++;
			continue;
		}
		try {
			auto& ch = font.GetData(it);
			str_height.at(line) = max(str_height.at(line), ch.gm.gmptGlyphOrigin.y * 2.0f / (float)height);
			str_width.at(line) += ch.gm.gmCellIncX * 2.0f / (float)width;
		}
		catch (...) {
			continue;
		}
	}
	whole_width = max(whole_width, str_width.at(line));
	whole_height += str_height.at(line);
}
Esempio n. 2
0
void Text::DrawString(float x, float y, float z, const std::string& str,
	const CFont& font, const CPipeline& pipeline, const COLOR& color, float alignX, float alignY) {
	std::wstring wstr(str_to_wstr(str));

	pipeline.BeginStorePipeline();
	pipeline.SetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
	pipeline.SetInputLayout(nullptr);
	pipeline.SetVertexShader(&m_vs);
	pipeline.SetGeometryShader(nullptr);
	pipeline.SetPixelShader(&m_ps);
	pipeline.SetPixelShaderSamplerState(0, &m_sampler);
	pipeline.SetVertexShaderConstantBuffer(0, &m_pos);
	m_color.UpdateBufferValue(color, Application::GetImmediateContext());
	pipeline.SetPixelShaderConstantBuffer(1, &m_color);

	std::vector<float> str_height;
	std::vector<float> str_width;
	float whole_width, whole_height;
	GetWidthAndHeight(str, font, str_width, str_height, whole_width, whole_height);

	UINT line = 0;
	UINT width, height;
	Application::GetScreenSize(width, height);
	float lastPosX = 0.0f;
	float lastPosY = str_height.at(0);

	for (const auto& it : wstr) {
		if (it == '\n') {
			lastPosX = 0.0f;
			line++;
			lastPosY += str_height.at(line);
			continue;
		}

		float sizeX;
		float sizeY;

		try {
			const CFont::char_t& ch = font.GetData(it);

			pipeline.SetPixelShaderResource(0, &ch.tex);

			static int index = 0;

			sizeX = (float)ch.gm.gmBlackBoxX / (float)width;
			sizeY = (float)ch.gm.gmBlackBoxY / (float)height;
			XMFLOAT4X4 mat;
			XMStoreFloat4x4(&mat, XMMatrixTranspose(
				XMMatrixScaling(sizeX, sizeY, 1.0f)
				* XMMatrixTranslation(
					x + lastPosX + sizeX + (float)ch.gm.gmptGlyphOrigin.x / (float)width
					- alignX * str_width.at(line),
					-(y + lastPosY + sizeY - (float)ch.gm.gmptGlyphOrigin.y * 2.0f / (float)height
					- alignY * whole_height),
					z)
			));
			m_pos.UpdateBufferValue(mat, Application::GetImmediateContext());
			lastPosX += (float)ch.gm.gmCellIncX * 2.0f / (float)width;
		} catch (...) {
			continue;
		}

		pipeline.Draw(6);

	}

	pipeline.RestorePipeline();
}