Ejemplo n.º 1
0
void CTerritoryTexture::RecomputeTexture(int unit)
{
	// If the map was resized, delete and regenerate the texture
	if (m_Texture)
	{
		CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
		if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
			DeleteTexture();
	}

	if (!m_Texture)
		ConstructTexture(unit);

	PROFILE("recompute territory texture");

	std::vector<u8> bitmap;
	bitmap.resize(m_MapSize * m_MapSize * 4);

	CmpPtr<ICmpTerritoryManager> cmpTerritoryManager(m_Simulation, SYSTEM_ENTITY);
	if (!cmpTerritoryManager)
		return;

	const Grid<u8> territories = cmpTerritoryManager->GetTerritoryGrid();

	GenerateBitmap(territories, &bitmap[0], m_MapSize, m_MapSize);

	g_Renderer.BindTexture(unit, m_Texture);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_MapSize, m_MapSize, GL_RGBA, GL_UNSIGNED_BYTE, &bitmap[0]);
}
Ejemplo n.º 2
0
void CLOSTexture::RecomputeTexture(int unit)
{
	// If the map was resized, delete and regenerate the texture
	if (m_Texture)
	{
		CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY);
		if (cmpTerrain && m_MapSize != (ssize_t)cmpTerrain->GetVerticesPerSide())
			DeleteTexture();
	}

	bool recreated = false;
	if (!m_Texture)
	{
		ConstructTexture(unit);
		recreated = true;
	}

	PROFILE("recompute LOS texture");

	std::vector<u8> losData;
	losData.resize(GetBitmapSize(m_MapSize, m_MapSize));

	CmpPtr<ICmpRangeManager> cmpRangeManager(m_Simulation, SYSTEM_ENTITY);
	if (!cmpRangeManager)
		return;

	ICmpRangeManager::CLosQuerier los(cmpRangeManager->GetLosQuerier(g_Game->GetPlayerID()));

	GenerateBitmap(los, &losData[0], m_MapSize, m_MapSize);

	if (CRenderer::IsInitialised() && g_Renderer.m_Options.m_SmoothLOS && recreated)
	{
		g_Renderer.BindTexture(unit, m_TextureSmooth1);		
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_MapSize + g_BlurSize - 1, m_MapSize + g_BlurSize - 1, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
		g_Renderer.BindTexture(unit, m_TextureSmooth2);		
		glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_MapSize + g_BlurSize - 1, m_MapSize + g_BlurSize - 1, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
	}

	g_Renderer.BindTexture(unit, m_Texture);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, m_MapSize + g_BlurSize - 1, m_MapSize + g_BlurSize - 1, GL_ALPHA, GL_UNSIGNED_BYTE, &losData[0]);
}
Ejemplo n.º 3
0
/**
* Returns the width of the string 'str' in pixels (at subpixel accuracy)
* had the string been rendered in this face.  this is the same logic as in
* DrawString() without issuing the GL commands to actually draw to the screen
*/
float
STFont::ComputeWidth(const std::string& str)
{
    if (str.length() == 0 || mImpl->ftFace == NULL)
        return 0.0f;

    float totalAdvance = 0.0f;
    unsigned int useKerning = FT_HAS_KERNING(mImpl->ftFace);

    int strLength = (int)str.length();
    for (int i=0; i<strLength; i++) {

        unsigned int ch = str[i];

        int bitmapIndex = GetBitmapIndex( ch );

        if (bitmapIndex == -1) {
            if (ch == ' ') {
                totalAdvance += (float)mImpl->size / 4.0f;
                continue;
            } 
            else {
                bitmapIndex = GenerateBitmap( ch );
            }
        }

        // Get the kerning offset for the next character.
        FT_Vector kerning = {0,0};
        if( useKerning && (i < strLength-1) ) {
            FT_Get_Kerning(mImpl->ftFace,
                           FT_Get_Char_Index(mImpl->ftFace, str[i] ),
                           FT_Get_Char_Index(mImpl->ftFace, str[i+1] ),
                           0, &kerning);
        }

        totalAdvance += mImpl->glyphBitmaps[bitmapIndex].advanceX + kerning.x/64.0f;
    }

    return totalAdvance;
}
Ejemplo n.º 4
0
/**
* Renders a string of text using this face
* Returns the width of the rendered text in pixels (at subpixel accuracy)
*/
float
STFont::DrawString(const std::string& str, const STColor4f& color)
{
    if (str.length() == 0 || mImpl->ftFace == NULL)
        return 0.0f;

    //
    // Set up OpenGL state for rendering text, and push
    // attributes so that we can restore when done.
    //
    glPushAttrib( GL_ENABLE_BIT | GL_PIXEL_MODE_BIT | GL_COLOR_BUFFER_BIT);
    glPushClientAttrib( GL_CLIENT_PIXEL_STORE_BIT);

    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glDisable(GL_TEXTURE_2D);

    glPixelTransferf(GL_RED_SCALE, color.r);
    glPixelTransferf(GL_GREEN_SCALE, color.g);
    glPixelTransferf(GL_BLUE_SCALE, color.b);
    glPixelTransferf(GL_ALPHA_SCALE, color.a);

    glRasterPos2f(0.0f, 0.0f);

    //
    // Draw each character in order.
    //
    float totalAdvance = 0.0f;
    unsigned int useKerning = FT_HAS_KERNING(mImpl->ftFace);

    // Render glyph for each character in the string
    int strLength = (int)str.length();
    for (int i=0; i<strLength; i++) {

        unsigned int ch = str[i];

        int bitmapIndex = GetBitmapIndex( ch );

        // Pixmap for this character hasn't been generated.  Do that now.
        if (bitmapIndex == -1) {
            if (ch == ' ') {
                glBitmap( 0, 0, 0.0f, 0.0f, (float) mImpl->size / 4.0f, 0.0f, (const GLubyte*)0 );
                totalAdvance += (float) mImpl->size / 4.0f;
                continue;
            } 
            else {
                bitmapIndex = GenerateBitmap( ch );
            }
        }

        STBitmapGlyph& bitmapGlyph = mImpl->glyphBitmaps[bitmapIndex];
        
        // Move the raster position by (offsetX, offsetY)
        glBitmap(0, 0, 0.0f, 0.0f, (float) bitmapGlyph.offsetX,
                 (float) bitmapGlyph.offsetY, NULL);

        // Render the glyph to the framebuffer
        glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
        glPixelStorei(GL_UNPACK_ALIGNMENT, 2);
        glDrawPixels(bitmapGlyph.width, bitmapGlyph.height,
                     GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE,
                     (const GLvoid*) bitmapGlyph.data);
        
        // Restore raster position
        glBitmap(0, 0, 0.0f, 0.0f,
                 (float) -bitmapGlyph.offsetX, (float) -bitmapGlyph.offsetY,
                 NULL);
        
        // Get the kerning offset for the next character.
        FT_Vector kerning = {0,0};
        if( useKerning && (i < strLength-1) ) {
            FT_Get_Kerning(mImpl->ftFace,
                           FT_Get_Char_Index(mImpl->ftFace, str[i] ),
                           FT_Get_Char_Index(mImpl->ftFace, str[i+1] ),
                           0, &kerning );
        }

        // Move the raster position to where it should be for the next character.
        glBitmap(0, 0, 0.0f, 0.0f,
                 (float) (bitmapGlyph.advanceX + kerning.x/64.0f), 0.0f, NULL);

        totalAdvance += bitmapGlyph.advanceX + kerning.x/64.0f;
    }

    //
    // Restore OpenGL state.
    //
    glPopClientAttrib();
    glPopAttrib();

    return totalAdvance;
}