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

Author Topic: Trying to make sprites cleaner in code  (Read 775 times)

0 Members and 1 Guest are viewing this topic.

CloakedYoshi

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Trying to make sprites cleaner in code
« 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...

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
Re: Trying to make sprites cleaner in code
« Reply #1 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 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Trying to make sprites cleaner in code
« Reply #2 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.
Laurent Gomila - SFML developer

 

anything