Exemplo n.º 1
0
void Sprite::Render(RenderTarget&, Renderer& renderer) const
{
    // Get the sprite size
    float width  = static_cast<float>(mySubRect.Width);
    float height = static_cast<float>(mySubRect.Height);

    // Check if the texture is valid, and calculate the texture coordinates
    FloatRect coords;
    if (myTexture)
        coords = myTexture->GetTexCoords(mySubRect);

    // Compute the texture coordinates
    float left   = coords.Left;
    float top    = coords.Top;
    float right  = coords.Left + coords.Width;
    float bottom = coords.Top + coords.Height;
    if (myIsFlippedX) std::swap(left, right);
    if (myIsFlippedY) std::swap(top, bottom);

    // Bind the texture
    renderer.SetTexture(myTexture);

    // Draw the sprite's geometry
    renderer.Begin(Renderer::TriangleStrip);
        renderer.AddVertex(0,     0,      left,  top);
        renderer.AddVertex(width, 0,      right, top);
        renderer.AddVertex(0,     height, left,  bottom);
        renderer.AddVertex(width, height, right, bottom);
    renderer.End();
}
Exemplo n.º 2
0
void AnimatedSprite::Render(RenderTarget&, Renderer& renderer) const
{
	// Get the sprite size
	float width  = mSubRectSize.x;
	float height = mSubRectSize.y;

	// Check if the image is valid, and calculate the texture coordinates
	FloatRect coords;
	IntRect subrect(mSubRectOffset.x + mSubRectSize.x * mFrameNumber, mSubRectOffset.y, mSubRectSize.x, mSubRectSize.y);
	if (GetImage())
		coords = GetImage()->GetTexCoords(subrect);

	// Compute the texture coordinates
	float left   = coords.Left;
	float top    = coords.Top;
	float right  = coords.Left + coords.Width;
	float bottom = coords.Top + coords.Height;

	// Bind the texture
	renderer.SetTexture(GetImage());

	// Draw the sprite's geometry
	renderer.Begin(Renderer::TriangleStrip);
		renderer.AddVertex(0,     0,      left,  top);
		renderer.AddVertex(width, 0,      right, top);
		renderer.AddVertex(0,     height, left,  bottom);
		renderer.AddVertex(width, height, right, bottom);
	renderer.End();
}
Exemplo n.º 3
0
void Text::Render(RenderTarget&, Renderer& renderer) const
{
    // No text or not font: nothing to render
    if (!myFont || myString.IsEmpty())
        return;

    // Bind the font texture
    const Image& texture = myFont->GetImage(myCharacterSize);
    renderer.SetTexture(&texture);

    // Computes values related to the text style
    bool  bold                = (myStyle & Bold) != 0;
    bool  underlined          = (myStyle & Underlined) != 0;
    float italicCoeff         = (myStyle & Italic) ? 0.208f : 0.f; // 12 degrees
    float underlineOffset     = myCharacterSize * 0.1f;
    float underlineThickness  = myCharacterSize * (bold ? 0.1f : 0.07f);
    FloatRect underlineCoords = texture.GetTexCoords(IntRect(1, 1, 1, 1));
    float underlineLeft       = underlineCoords.Left;
    float underlineTop        = underlineCoords.Top;
    float underlineRight      = underlineCoords.Left + underlineCoords.Width;
    float underlineBottom     = underlineCoords.Top + underlineCoords.Height;

    // Initialize the rendering coordinates
    float space       = static_cast<float>(myFont->GetGlyph(L' ', myCharacterSize, bold).Advance);
    float lineSpacing = static_cast<float>(myFont->GetLineSpacing(myCharacterSize));
    float x = 0.f;
    float y = static_cast<float>(myCharacterSize);

    // Note:
    // Here we use a Begin/End pair for each quad because
    // the font's texture may change in a call to GetGlyph

    // Draw one quad for each character
    Uint32 prevChar = 0;
    for (std::size_t i = 0; i < myString.GetSize(); ++i)
    {
        Uint32 curChar = myString[i];

        // Apply the kerning offset
        x += static_cast<float>(myFont->GetKerning(prevChar, curChar, myCharacterSize));
        prevChar = curChar;

        // If we're using the underlined style and there's a new line, draw a line
        if (underlined && (curChar == L'\n'))
        {
            float top = y + underlineOffset;
            float bottom = top + underlineThickness;

            renderer.Begin(Renderer::QuadList);
                renderer.AddVertex(0, top,    underlineLeft,  underlineTop);
                renderer.AddVertex(x, top,    underlineRight, underlineTop);
                renderer.AddVertex(x, bottom, underlineRight, underlineBottom);
                renderer.AddVertex(0, bottom, underlineLeft,  underlineBottom);
            renderer.End();
        }

        // Handle special characters
        switch (curChar)
        {
            case L' ' :  x += space;              continue;
            case L'\t' : x += space * 4;          continue;
            case L'\n' : y += lineSpacing; x = 0; continue;
            case L'\v' : y += lineSpacing * 4;    continue;
        }

        // Extract the current glyph's description
        const Glyph&     glyph   = myFont->GetGlyph(curChar, myCharacterSize, bold);
        int              advance = glyph.Advance;
        const IntRect&   bounds  = glyph.Bounds;
        const FloatRect& coords  = texture.GetTexCoords(glyph.SubRect);

        int   boundsRight  = bounds.Left + bounds.Width;
        int   boundsBottom = bounds.Top  + bounds.Height;
        float coordsRight  = coords.Left + coords.Width;
        float coordsBottom = coords.Top  + coords.Height;

        // Draw a textured quad for the current character
        renderer.Begin(Renderer::QuadList);
            renderer.AddVertex(x + bounds.Left - italicCoeff * bounds.Top,   y + bounds.Top,   coords.Left, coords.Top);
            renderer.AddVertex(x + boundsRight - italicCoeff * bounds.Top,   y + bounds.Top,   coordsRight, coords.Top);
            renderer.AddVertex(x + boundsRight - italicCoeff * boundsBottom, y + boundsBottom, coordsRight, coordsBottom);
            renderer.AddVertex(x + bounds.Left - italicCoeff * boundsBottom, y + boundsBottom, coords.Left, coordsBottom);
        renderer.End();

        // Advance to the next character
        x += advance;
    }

    // If we're using the underlined style, add the last line
    if (underlined)
    {
        float top = y + underlineOffset;
        float bottom = top + underlineThickness;

        renderer.Begin(Renderer::QuadList);
            renderer.AddVertex(0, top,    underlineLeft,  underlineTop);
            renderer.AddVertex(x, top,    underlineRight, underlineTop);
            renderer.AddVertex(x, bottom, underlineRight, underlineBottom);
            renderer.AddVertex(0, bottom, underlineLeft,  underlineBottom);
        renderer.End();
    }
}