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

Author Topic: Text not rendered if font is reloaded and string compares == to last rendered  (Read 1605 times)

0 Members and 1 Guest are viewing this topic.

GeoCompute

  • Newbie
  • *
  • Posts: 3
    • View Profile
Hi,

I'm having an issue and am wondering if it could possibly be a bug in SFML.  I think it might be related to the issue described at this link <https://github.com/SFML/SFML/pull/1345>, though the symptoms I'm experiencing seem to be quite a bit different.

I'm using SFML 2.4.2.  I don't know how to determine the build number, but I downloaded it on 2017-12-17 in case that helps.

My computer is a Windows 10 Home machine, 64-bit, with an i7, 8.00 GB, AMD Radeon HD 5450, and I'm use Microsoft Visual Studio Community 2017.

Here is the minimal code which consistently reproduces the issue:

Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int __stdcall WinMain(long, long, char *, int)
{
    sf::RenderWindow window;
    sf::Font         font  ;
    sf::Text         text  ;
    sf::Event        event ;

    window.create(sf::VideoMode(500, 300), "My Window");

    auto fnDrawText = [&](int x, int y, char const * sz)
    {
        text.setString(sz);
        text.setPosition(x, y);
        window.draw(text);
    };

    while (window.isOpen())
    {
        window.clear();

        font.loadFromFile("SomeFont.TTF");

        text.setFont(font);
        text.setCharacterSize(10);

        fnDrawText(100, 100, "My Text 1");
        fnDrawText(100, 120, "My Text 2");
        fnDrawText(100, 130, "My Text 1");
     // fnDrawText(100, 160, "ABCDEFG"); // Uncomment this line and everything works

        window.display();

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

With that code as-is, you will only get two lines of code output on the screen and it will be "My Text 2" followed by "My Text 1".  (So the first instance of "My Text 1" is missing.)

If you uncomment the last invocation of fnDrawText, then the output changes and all four lines of text will be output, including the initial instance of "My Text 1" which had previously been missing.

Does anybody have any ideas of what could be causing this?  (I'm a newbie to SFML.)

Thank you,
GeoCompute

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Don't load the font inside your main loop.
Not sure what the issue is, might be better to use a text object for each text instead of reusing one for all of them.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Loading the font once outside the main loop will solve the problem, and applying PR 1345 should solve it too -- you can try it, it was merged in master on 2018-01-25.

The problem with your current code is that the sf::Text instance see no change (same text, same font), so it doesn't update its geometry, but the sf::Font object was reloaded and its internal textures were recreated elsewhere in memory. That's precisely what PR 1345 is supposed to solve.
« Last Edit: February 28, 2018, 09:24:59 am by Laurent »
Laurent Gomila - SFML developer

GeoCompute

  • Newbie
  • *
  • Posts: 3
    • View Profile
Thank you both for your responses.

In thinking about your advice and how to apply it to my particular situation, I've realized I had a mistake in the way I was thinking of text output.  I was thinking of it in terms of being a primitive graphics command, like drawing a line or a rectangle.  (That's practically all text output was when I last spent a lot of time working on video games: about 30 years ago!  I guess things have progressed a bit since then.  ;) )  You've helped me realize I instead need to think of it as being more like a bitmap or a sound file.  I know the SFML text and font documentation already says as much, but I think my decades-old conditioning was preventing me from fully realizing it.

Laurent, regarding your statement that PR-1345 "was merged in master on 2018-01-25," what is master?  Is that a new build of SFML I would obtain by downloading version 2.4.2 over again from the main download page at <https://www.sfml-dev.org/download/sfml/2.4.2/>?

Thank you both for your time and help  :)
GeoCompute


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
By master we mean the Git master branch: https://github.com/SFML/SFML
The easiest is to just build it from source with your compiler, alternatively we do have CI builds available for a few compilers: https://www.sfml-dev.org/artifacts/by-branch/master/
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

GeoCompute

  • Newbie
  • *
  • Posts: 3
    • View Profile
Thank you for that, eXpl0it3r.   I may investigate that to better learn how things work with SFML, though at this point the urgency is gone as I've reworked things to move the loading of the font outside the draw routine, as the two of you suggested.

Thanks for the help!  :)
GeoCompute

 

anything