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

Author Topic: Can't draw text or use text constructor [Solved]  (Read 4090 times)

0 Members and 1 Guest are viewing this topic.

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
Can't draw text or use text constructor [Solved]
« 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?
« Last Edit: January 22, 2013, 08:26:36 pm by bonsairobo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't draw text or use text constructor
« Reply #1 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!
« Last Edit: January 22, 2013, 08:00:18 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't draw text or use text constructor
« Reply #2 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.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: Can't draw text or use text constructor
« Reply #3 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.

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't draw text or use text constructor
« Reply #4 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));
 
« Last Edit: January 22, 2013, 08:27:58 pm by bonsairobo »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Can't draw text or use text constructor
« Reply #5 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. ;)
« Last Edit: January 22, 2013, 08:27:52 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

bonsairobo

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: Can't draw text or use text constructor [Solved]
« Reply #6 on: January 22, 2013, 08:29:32 pm »
Thanks for the help BTW  :)

 

anything