Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Futile

Pages: [1]
1
Graphics / [Solved] Texts with relative width and resize
« on: March 12, 2013, 01:34:12 pm »
Hello forums,

maybe somebody can help me out here. I want to display texts by giving a rectangle and from that calculating character size and scale, and that is working fine so far(code below). Each text receives a width of 1/10th of the total screen, and then ten texts are rendered next to each other, spanning the whole width of the window.

However, as soon as I resize the window, the scaling breaks. See attachments for effects, the first screenshot displays the program before, and the second after resizing.

The sfml version I used is about five months old.

Minimal Example:

#include <string>

#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Graphics/Text.hpp>
#include <iostream>

int main(int argc, char *argv[])
{
    sf::RenderWindow window(sf::VideoMode(640, 480), "Text Test");
    window.setVerticalSyncEnabled(true);

    sf::Font font;
    font.loadFromFile("Font/FreeSans.ttf");

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            if(event.type == sf::Event::Closed) window.close();
        }

        window.clear();

        sf::Text text;
        text.setFont(font);

        const float HEIGHT = 0.025;
        const sf::Vector2u winSize = window.getSize();

        for(int i = 0; i < 10; i++)
        {
            text.setString("TEST" + std::to_string(i));

            // scale text height using font size
            text.setCharacterSize(winSize.y * HEIGHT);
            // position text
            text.setPosition(winSize.x * i * 0.1f, winSize.y * i / (float) 50);
            // scale the width using the total window size, every text gets 1/10th (supposedly)
            text.setScale(winSize.x * 0.1f / text.getLocalBounds().width, 1.f);

            window.draw(text);
        }

        window.display();
    }

    return 0;
}
 

[attachment deleted by admin]

Pages: [1]
anything