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

Author Topic: Noob question about variables in Text()  (Read 2977 times)

0 Members and 2 Guests are viewing this topic.

newbieg

  • Newbie
  • *
  • Posts: 6
    • View Profile
Noob question about variables in Text()
« on: May 14, 2012, 09:47:50 am »
Hi everyone,
I just got SFML installed so I was playing around with the code that the tutorial provided, I was just wondering why this code only outputs variable a and not variables a+b;
#include "SFML/Graphics.hpp"
#include <string>
#include <iostream>
using namespace std;
int main()
{
        string a = "hello", b="again";

    sf::RenderWindow window(sf::VideoMode(600, 200), "SFML works!");
    sf::Text text(a + b);

    while (window.isOpen())
    {
               
                sf::Text text(a);
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}
I was just trying to see if I could add strings together within text(), which I would need to do if I tried to make a game. Is it possible to do calculations inside of a function from SFML, or do I have to do the calculations first and only output the one variable that contains the result?
« Last Edit: May 14, 2012, 09:51:27 am by Laurent »

newbieg

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Noob question about variables in Text()
« Reply #1 on: May 14, 2012, 09:55:54 am »
Woops, solved that on my own, dumb mistake on my part.
I can't remove this post now, there's some sort of error message. sorry.

error message;
You cannot delete your own topics in this board. Check to make sure this topic wasn't just moved to another board.

Well, while I have you here, why isn't rect() working for me?
#include "SFML/Graphics.hpp"
#include <string>
#include <iostream>
using namespace std;
int main()
{
        string a = "hello", b="again";

    sf::RenderWindow window(sf::VideoMode(600, 200), "SFML works!");
    sf::Text text(a + b);


    while (window.isOpen())
    {
               
                sf::Text text(a + b);
                sf::Rect rect(20,30,200,80);
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(text);
        window.display();
    }

    return 0;
}
« Last Edit: May 14, 2012, 10:04:49 am by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Noob question about variables in Text()
« Reply #2 on: May 14, 2012, 10:08:46 am »
Here you're not modifying the text content, you're declaring a second sf::Text variable with the same name, which hides the first one.
To modify the contents of a sf::Text, call its setString function.
text.setString(a + b);

Quote
Well, while I have you here, why isn't rect() working for me?
What do you expect this code to do? You declare a rectangle but you don't do anything with it.

You shouldn't try to do complicated things if you're new to C++. Focus on learning the language first, otherwise we'll see you everyday on this forum for this kind of simple problem which has nothing to do with SFML.
Laurent Gomila - SFML developer

newbieg

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Noob question about variables in Text()
« Reply #3 on: May 14, 2012, 10:25:27 am »
Sorry admin., I tried to delete this topic but got that error message, I'd appreciate it if you would remove it for me, thank you.
« Last Edit: May 14, 2012, 10:37:05 am by newbieg »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Noob question about variables in Text()
« Reply #4 on: May 14, 2012, 10:42:22 am »
sf::Rect doesn't exist, and if you meant "sf::FloatRect" or "sf::IntRect" then it's not a drawable class. You should at least read the documentation and tutorials before trying to invent things.
Laurent Gomila - SFML developer