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

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

0 Members and 1 Guest are viewing this topic.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: How do i write variables contents in text?
« Reply #15 on: October 26, 2012, 12:18:48 am »
Quote
Im not really that advaned yet to use templates.
Quote
Check the StandardTemplateLibrary documentation for more information
Love this forum sometimes.  ;D
The best part about the Standard Template Library is that you can know next to nothing about templates and still be able to use it effectively.
I use the latest build of SFML2

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #16 on: October 26, 2012, 12:24:14 am »
Template is just glorified copy/find&replace/paste that will generate the class code for the template parameters you somewhere instanced one with.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: How do i write variables contents in text?
« Reply #17 on: October 26, 2012, 12:28:54 am »
Template is just glorified copy/find&replace/paste that will generate the class code for the template parameters you somewhere instanced one with.
Very wrong. ;)
Templates themselves are Turing complete, which means you could essentially solve any problem just with templates. :D
Beside that do template not just replace things in general, that's what evil macros did, but you can use templates in many different ways which won't be copy/find & replace/paste.
« Last Edit: October 26, 2012, 12:31:07 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/

Raptor88

  • Full Member
  • ***
  • Posts: 111
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #18 on: October 26, 2012, 12:30:14 am »
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.

Thanks masskiller.  Although directed to Assassinbeast, I didn't know that either.

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);

Very neat and tidy.  Thanks for posting that code and clarifying eXpl0it3r.

Raptor

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #19 on: October 27, 2012, 01:19:21 am »
i got it to work :D

#include <SFML\Graphics.hpp>
#include <sstream>
#include <string>

sf::RenderWindow mywindow(sf::VideoMode(800,600,32),"terdas");

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

int main()
{
        mywindow.setVerticalSyncEnabled(true); // 60 fps
        int score = 200;
        std::string scoretext = "Score= ";

        sf::Text mytext;
       

        while(mywindow.isOpen())
        {
                mytext.setString(toString(scoretext) + toString(score));

                mywindow.clear(sf::Color(0,200,0));
                score++;
                mywindow.draw(mytext);
                mywindow.display();
        }
}
 

So is this how you make a score update properly?
I just want to make sure its not heavy for the computer  :D

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do i write variables contents in text?
« Reply #20 on: October 27, 2012, 01:26:40 am »
You should not use toString on a string, it's pointless..
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 #21 on: October 27, 2012, 04:52:01 am »
Quote
So is this how you make a score update properly?
I just want to make sure its not heavy for the computer  :D

Normally yes, it shouldn't be heavy at all, but ideally it should still go in another thread with the rest of the logic for some order within the loop (mainly because all point calculations and stuff should go there).
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!

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: How do i write variables contents in text?
« Reply #22 on: October 28, 2012, 07:26:46 pm »
been looking for other ways of changing things to strings.

Works fine for basic types like int, float, bool, ext.......

But what about changing Objects to strings?

Like (and yes this code is going to look bad),
class ObjectA
{
public:
//Some methods
private:
//Some variables
protected:
//Some other codes
};
/*
Methods codes from class
*/


int main()
{
ObjectA obja;

cout << tostring(obja) << endl;

return 0;
}
 

Just wondering about this that's all.
I have many ideas but need the help of others to find way to make use of them.

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 #23 on: October 28, 2012, 08:06:12 pm »
For that you have to implement the ">>" operator, so that it takes out what you want to take out from the given object. Then you just use it with your stringstream object to make it concatenate whatever you may want for that object type.
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!