SFML community forums

Help => Graphics => Topic started by: bonsairobo on January 22, 2013, 07:55:38 pm

Title: Can't draw text or use text constructor [Solved]
Post by: bonsairobo on January 22, 2013, 07:55:38 pm
#include <SFML\Graphics.hpp>
#include <iostream>
using namespace std;

#define RENDERHEIGHT 800
#define RENDERWIDTH 400

int main()
{
        sf::RenderWindow tetris(sf::VideoMode(RENDERWIDTH, RENDERHEIGHT, 32), "TETRIS");
        tetris.setFramerateLimit(60);
        tetris.setMouseCursorVisible(false);

        sf::Text text;
        text.setString("hello");
        text.setPosition(0, 0);
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setColor(sf::Color::Red);

        while(tetris.isOpen())
        {
                sf::Event event;
                while(tetris.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed || event.key.code == sf::Keyboard::Escape)
                                tetris.close();
                }

                tetris.draw(text);
                tetris.display();
        }

        return 0;
}
 

I'm using SFML 2.0.  That is my exact code, which compiles, but no text is visible.  I've tried setting different positions, loading my own font (which gave me a runtime error), and on top of that, the text constructor won't accept string arguments.

sf::Text text("hello"); does not compile as VS doesn't think the constructor takes string arguments.

Any help?
Title: Re: Can't draw text or use text constructor
Post by: eXpl0it3r on January 22, 2013, 07:58:44 pm
What exact version of SFML are you using or where did you get it from?

I'm using SFML 2.0.  That is my exact code, which compiles, but no text is visible.
You need to set a font.

sf::Text text("hello"); does not compile as VS doesn't think the constructor takes string arguments.
The default font got removed a few month ago, thus there's only the default constructor or a constructor that takes a string, a font and an optional font size.

Note: You have to call window.clear() every frame iteration!
Title: Re: Can't draw text or use text constructor
Post by: bonsairobo on January 22, 2013, 08:09:39 pm
I compiled SFML with cmake and nmake from the "SFML 2.0 Snapshot" on the downloads page of this site.

I wasn't aware the default font was removed.  The documentation here (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Text.php) suggests you can pass only a string to the constructor.

Clearing the window shouldn't be necessary for the code I've written since I'm not moving any draw-able objects.

I will try setting a font and come back with my results.
Title: Re: Can't draw text or use text constructor
Post by: victorlevasseur on January 22, 2013, 08:15:28 pm
Hello,

You have to call clear() anyway. The era when game refresh only the necessary belongs to the past.
Title: Re: Can't draw text or use text constructor
Post by: bonsairobo on January 22, 2013, 08:26:02 pm
OK loading a font from a ttf file worked.  I did NOT need to clear the screen to see the text, but adding tetris.clear() made the text less "fuzzy".  Obviously I will clear the screen once I start making my game.

This code worked for me:

sf::Font arial;
arial.loadFromFile("arial.ttf");
sf::Text text("hello", arial, 30);
text.setPosition(0, 0);
text.setStyle(sf::Text::Bold);
text.setColor(sf::Color(255, 0 , 255));
 
Title: Re: Can't draw text or use text constructor
Post by: eXpl0it3r on January 22, 2013, 08:26:19 pm
The documentation here (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Text.php) suggests you can pass only a string to the constructor.
I think someone should update the documentation for sf::Text I linked to earlier, as it gives an example of code that would not compile with the changes made to the text constructor.
The documentation matches the official RC build. If you want a current documentation, you can build it on your own with Doxygen. ;)

Clearing the window shouldn't be necessary for the code I've written since I'm not moving any draw-able objects.
Doesn't matter. If you call display you also have to call clear. ;)

OK loading a font from a ttf file worked.  I did NOT need to clear the screen to see the text, but adding tetris.clear() made the text less "fuzzy".  Obviously I will clear the screen once I start making my game.
I mean you can do whatever you want, but don't complain about strange behavior, when you don't follow the way SFML works. ;)
Title: Re: Can't draw text or use text constructor [Solved]
Post by: bonsairobo on January 22, 2013, 08:29:32 pm
Thanks for the help BTW  :)