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

Author Topic: Text on the screen?  (Read 2549 times)

0 Members and 1 Guest are viewing this topic.

ItsTehShadow

  • Newbie
  • *
  • Posts: 5
    • View Profile
Text on the screen?
« on: July 11, 2013, 12:38:05 pm »
Hi again,

Think is a graphics issue? Actually got some time to start looking more indepth into SFML 2.0, and was running through some guides and tutorials. I ended up merging a few bits I found to try make something without the aid of the guides completely. However this might be where my errors occurred, I'm trying to get a simple bit of text to display in the window. I've searched these forums and Google, to no avail really. The program runs fine, no errors but the text is simply not there? Maybe another simple issue but I'd love a point in the right direction  :D

#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>

using namespace sf;

int main()
{
        VideoMode videoMode(320,240);
        RenderWindow window(videoMode,"KeyEvent Window");

        //Set objects in the window    
        sf::Font font;
        font.loadFromFile("Fonts\BRADHTC.ttf");

        sf::Text text; 
        text.setString("Hello");
        text.setFont(font);
        text.setCharacterSize(2);
        text.setPosition(30, 30);
        text.setColor(sf::Color::Black);

        RectangleShape rectangle;
        rectangle.setPosition(30,30);
        rectangle.setSize(Vector2f(50, 30));

        rectangle.setFillColor(Color::Green);
        rectangle.setOutlineColor(Color::White);
        rectangle.setOutlineThickness(3);      
        //

        while (window.isOpen())
        {
                window.clear(sf::Color::White);
                window.draw(text);
                window.draw(rectangle);        
                window.display();

                Event event;
                while (window.pollEvent(event))
                {
                        if ( (event.type == Event::Closed) ||
                                ((event.type == Event::KeyPressed) && (event.key.code==Keyboard::Escape)) )
                                window.close();
                        else
                        {
                                if (event.type == Event::KeyPressed)
                                {
                                        switch(event.key.code)
                                        {
                                        case Keyboard::Up: rectangle.move(0, -10);
                                                break;
                                        case Keyboard::Down: rectangle.move(0, 10);
                                                break;
                                        case Keyboard::Left: rectangle.move(-10, 0);
                                                break;
                                        case Keyboard::Right: rectangle.move(10, 0);
                                                break;
                                        }
                                }      
                        }
                }
        }

        return EXIT_SUCCESS;
}
 

If any of this code is a bit messy or unneeded, feel free to point that out aswell but I am still grasping this so don't know all the best ways to do things yet  :P

Thanks to anyone who reads this..

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Text on the screen?
« Reply #1 on: July 11, 2013, 12:55:07 pm »
You are first drawing the text and then the rectangle. So even if the text would be drawn, you draw the rectangle over it.

Also check the loadFromFile line.
font.loadFromFile("Fonts\BRADHTC.ttf");
Backslash is an escape character. You need to replace it with '/' or with '\\' to make it work.

Edit: And isn't a character size of 2 pixels a little small for a text?
TGUI: C++ SFML GUI

ItsTehShadow

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Text on the screen?
« Reply #2 on: July 11, 2013, 01:03:04 pm »
You are first drawing the text and then the rectangle. So even if the text would be drawn, you draw the rectangle over it.

Also check the loadFromFile line.
font.loadFromFile("Fonts\BRADHTC.ttf");
Backslash is an escape character. You need to replace it with '/' or with '\\' to make it work.

Edit: And isn't a character size of 2 pixels a little small for a text?

I've tried it both ways and the text doesn't display when its before or after the rectangle. Also its size 2 as I was playing around with the size incase it was outside the window or something. It doesn't appear at any size :( 

I changed the backslash to both / and \\ and it doesn't show either

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Text on the screen?
« Reply #3 on: July 11, 2013, 01:14:12 pm »
After making those 3 changes, the text should be drawn.
It can only be a driver issue if the examples from sfml don't even work.

Are you sure the font is being loaded? Does it print something in the command prompt? Does the loadFromFile function return true?
TGUI: C++ SFML GUI

ItsTehShadow

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Text on the screen?
« Reply #4 on: July 11, 2013, 01:26:09 pm »
After making those 3 changes, the text should be drawn.
It can only be a driver issue if the examples from sfml don't even work.

Are you sure the font is being loaded? Does it print something in the command prompt? Does the loadFromFile function return true?
Strangely, while I was just checking those features I decided to change the font, downloaded another ttf font file and this one worked fine. I'd already tried this with two fonts before though, so not sure why the first 2 files don't work but this one does  :-\

EDIT* Seems all good now, not quite sure what happened there. Guess those two fonts are damaged in some way. Thanks for replying  ;D
« Last Edit: July 11, 2013, 01:28:58 pm by ItsTehShadow »

 

anything