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

Author Topic: [Solved] Texts with relative width and resize  (Read 3348 times)

0 Members and 1 Guest are viewing this topic.

Futile

  • Newbie
  • *
  • Posts: 2
    • View Profile
[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]
« Last Edit: March 12, 2013, 08:05:17 pm by Futile »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
AW: Texts with relative width and resize
« Reply #1 on: March 12, 2013, 01:55:32 pm »
You'll have to adjust the sf::View of the window. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Futile

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: AW: Texts with relative width and resize
« Reply #2 on: March 12, 2013, 08:05:00 pm »
You'll have to adjust the sf::View of the window. ;)

if(event.type == sf::Event::Resized)
    window.setView(sf::View(sf::FloatRect(0, 0, event.size.width, event.size.height)));
 

Thanks a lot, that solved the problem! :D

 

anything