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

Author Topic: How do i write variables contents in text?  (Read 14243 times)

0 Members and 1 Guest are viewing this topic.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
How do i write variables contents in text?
« on: October 25, 2012, 01:22:05 am »
For example

int x = 10;

sf::text mytext;
mytext.setfont(myfont);
myfont.setstring("bla bla bla (x)") // i want to write "bla bla bla 10"

mywindow.draw(mytext);

So how do i write my variable x's contents if i had to track my score?

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #1 on: October 25, 2012, 01:27:52 am »
Erm.. This is not a SFML question?
Use stringstream(c++ approach) or sprintf(C approach) to convert and then use that as your text.
Also you don't set string to fonts. :)
You can easily write template function that takes any variable and returns it as string using stringstream.
template <typename T> std::string  EEVarToStr(T targ)
{
        std::stringstream ss;
        ss<<targ;
        return ss.str();
}
Back to C++ gamedev with SFML in May 2023

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #2 on: October 25, 2012, 01:31:31 am »
Erm.. This is not a SFML question?
Use stringstream(c++ approach) or sprintf(C approach) to convert and then use that as your text.
Also you don't set string to fonts. :)
You can easily write template function that takes any variable and returns it as string using stringstream.
template <typename T> std::string  EEVarToStr(T targ)
{
        std::stringstream ss;
        ss<<targ;
        return ss.str();
}

Im not really that advaned yet to use templates.
But yes... it is a sfml question.

Is there a more easy way to do this?
I just made an example to show what i wanted to make.
Siimply, just write my int variable on my screen to show fx the score

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: How do i write variables contents in text?
« Reply #3 on: October 25, 2012, 01:41:15 am »
You don't need to fully understand how the function works to use it. ;)

#include <sstream>
#include <string>
#include <SFML/Graphics>

template <typename T>
std::string toString(T arg)
{
    std::stringstream ss;
    ss << arg;
    return ss.str();
}

int main()
{
    int myInt = 12434;
    sf::Font myFont;
    // load font
    sf::Text myText(toString<int>(myInt), myFont);

    float myFloat = 345.4f
    std::string myString = toString<float>(myFloat);

    // draw sf::Text & do other stuff
}
« Last Edit: October 25, 2012, 01:44:29 am by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #4 on: October 25, 2012, 01:58:07 am »
Templates are nothing compared to polymorphic classes and handling pointers.
exploiter: I think you can ommit the <> block if passed arguments tell the type(ie. here).
Quote
You don't need to fully understand how the function works to use it. ;)
http://foldoc.org/Voodoo
You have no idea what kinds of stuff can happen when you say that..
« Last Edit: October 25, 2012, 02:03:26 am by FRex »
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #5 on: October 25, 2012, 04:33:17 am »
Quote
You have no idea what kinds of stuff can happen when you say that...

It usually leads to forcefully learning through trial and error what could have been read and understood first, but surely it has happened to all of us.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: How do i write variables contents in text?
« Reply #6 on: October 25, 2012, 11:14:51 am »
I think you can omit the <> block if passed arguments tell the type(ie. here).
Yes you can drop it. Depending on the design decisions you make, you might want to emphasize the type, so you'd for instance notice at compile time when you actually passed a float variable instead of integer instead of later seeing the problem occur at runtime. But it's up to you and for the given examples it doesn't really make sense. ;)

Quote
You don't need to fully understand how the function works to use it. ;)
http://foldoc.org/Voodoo
You have no idea what kinds of stuff can happen when you say that..
The idea behind this is abstraction. If I know how to use it and if I know what the final result will be, then it doesn't matter for me what the 'black box' does.
You can argue around as you want, but in the end you're constantly using abstraction and it's (mostly) a good thing. For example you're using SFML without know how every little tiny detail works inside SFML, or you're using the STL without knowing how they really did it, or you're using C++ without even knowing how the compiler creates the machine code for every given compiler flag etc, or you're using the compiled machine code without knowing how your processor actually works inside, etc. etc. ;)

So abstraction is a good thing.

For the function at hand, if we tell him the interface (usage), what the result will be and what can't be done with the function, then there's no need for him to fully understand how it works at this given moment.
Obviously it's essential for progressing with C++ to get to know things like templates but just because you don't understand them (yet) doesn't imply you can't use them and if you do the whole world would explode. :D
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #7 on: October 25, 2012, 08:51:13 pm »
Yes, I probably overstated that. Dealing with lots of voodoo code lately. Sorry. :)
Back to C++ gamedev with SFML in May 2023

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #8 on: October 25, 2012, 10:35:03 pm »
So how do i write my variable x's contents if i had to track my score?

I'm just a beginner C++ and SFML coder myself but maybe this code snippet is what you're looking for:

#include <sstream>
#include <string>

int x = 10;

//----- Concatenate the variable into the text message. -----
std::stringstream ss;

// Concatenates text with variable(s) into one string.
ss << "The value of x = " << x << ".  And I want\n"
"to make this a multi-line text string like this.\n\n";

// Get the entire contents of the stringstream as a single C++ string
std::string myMsg = ss.str();
//---------- End of concatenate code ------------

// You can display myMsg in SFML and get a message that looks like this:
// The value of x = 10.  And I want
// to make this a multi-line text string like this.

 

Hope this helps,
Raptor
« Last Edit: October 25, 2012, 11:54:05 pm by Raptor88 »

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #9 on: October 25, 2012, 10:48:13 pm »
Woaw thanks alot!!!!
this community is so helpful and AWESOME  ;D ;D ;D

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #10 on: October 25, 2012, 11:01:14 pm »
That's exactly the same method exploiter shown.
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #11 on: October 25, 2012, 11:10:31 pm »
Woaw thanks alot!!!!
this community is so helpful and AWESOME  ;D ;D ;D

A small tip. The stringstream object doesn't clean it's buffer on it's own, if you concatenate one thing and need to concatenate something else you'll have to reset the string within. Check the STL documentation for more information (and take an overall look, it will surely help you with learning C++).
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #12 on: October 25, 2012, 11:34:13 pm »
Quote
Im not really that advaned yet to use templates.
Quote
Check the StandardTemplateLibrary documentation for more information
Love this forum sometimes.  ;D
Back to C++ gamedev with SFML in May 2023

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #13 on: October 25, 2012, 11:45:21 pm »
That's exactly the same method exploiter shown.

Hmmm, I didn't see how text could be combined with a variable using exploiter's code as posted.
Maybe obvious to experienced programmers but not so much to us newbies.  ;)

Thanks,
Raptor

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: How do i write variables contents in text?
« Reply #14 on: October 25, 2012, 11:53:23 pm »
Love this forum sometimes.  ;D
And I don't like such 'random' comments... ;)
If you're not aware about this: The acronym STL gets often wrongly used to generally related to the C++ Standard Library and the Standard Template Library. Mostly all C++ developers will understand if you use STL but talk about parts of the C++ Standard Library and many use it because it's short and there's no acronym for C++ Standard Library.

The stringstream object doesn't clean it's buffer on it's own, if you concatenate one thing and need to concatenate something else you'll have to reset the string within.
iirc: ss.str("");

Hmmm, I didn't see how text could be combined with a variable using exploiter's code as posted.
Maybe obvious to experienced programmers but not so much to us newbies.  ;)
sf::Text text;
int a = 45;
float b = 3.4f;
std::string c = "Hello";

text.setString(toString(a) + toString(b) + c);
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything