SFML community forums

Help => General => Topic started by: CloakedYoshi on April 23, 2013, 09:40:43 am

Title: Trying to make sprites cleaner in code
Post by: CloakedYoshi on April 23, 2013, 09:40:43 am
**Before ask for help, im a huge c++ noob, I just started taking classes for it, and figured i should start getting my feet wet by having some fun.**

So my issue is that if i want to make a 20+ sprites there is gonna be a very long list of my using

sf::Texture tex1;
sf::Texture tex2;
sf::Texture texN;

if (!tex1.loadFromFile("tex1.png"))
      return 1;
if (!tex2.loadFromFile("tex2.png"))
      return 1;
if (!texN.loadFromFile("texN.png"))

sf::Sprite spr1;
sf::Sprite spr2;
sf::Sprite sprN;

sprN.setTexture(texN);
ect...

since this action became so repetitive i figured their must be a way to make some kind of function... so i came up with this... which did not work because the i was declaring the textures and strings and sprites in the parameters... heres the fuction

void makeSprite(sf::Texture textureName, std::string fileName, sf::Sprite spriteName)
{
   sf::Texture(textureName);
   if (!textureName.loadFromFile(fileName))
      return;
   sf::Sprite spriteName;
   spriteName.setTexture(textureName);
}

am i on the right track, and just need to change something, or am I a total c++ newbie and i need to go a different direction/ i shouldnt even be doing this >.> thanks for the help! :P I hope i dont get raged at for being such a noob, my first post on the forum. This is more of a c++ language question then a SFML question but i think thats ok...
Title: Re: Trying to make sprites cleaner in code
Post by: eXpl0it3r on April 23, 2013, 09:54:57 am
Well you've already answered your question: You don't have enough knowledge on C++. ;)
C++ is not an easy language with which you can just start and figure things out, but it's extremely complex and you should read some good books (http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) on C++, before diving into SFML.

For the problem at hand you can for instance use a std::map and you can also use a function to load things, but make sure to pass data by reference rather than by value, to prevent copies and to make sure that the texture is correctly created.
Title: Re: Trying to make sprites cleaner in code
Post by: Laurent on April 23, 2013, 09:55:54 am
I think you should rather take a good C++ book, and take the time to learn the basics of the language with simple examples. Rushing to make video games while you don't even know how to manipulate variables and arguments... seems like too optimistic ;)

And learning C++ on a forum is not a good idea either. Forums are good for learning tips, good practices and advanced usage, but not for what you're doing. So, take your book and learn before even trying to use a library.