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

Author Topic: Is there a function to sort through pngs?  (Read 2169 times)

0 Members and 1 Guest are viewing this topic.

FrankHamill

  • Newbie
  • *
  • Posts: 3
    • View Profile
Is there a function to sort through pngs?
« on: September 18, 2022, 02:55:57 am »
I plan to have say 100 structs inside a vector, and inside each struct, I would like a unique corresponding png file which I have aptly named 000.png - 100.png. At the moment, I have to manually tell each struct to save a texture (ex: texture.loadFromFile(000.png), texture1.loadFromFile(001.png), texture2.loadFromFile(002.png), etc.).

 Is there a faster, quicker, or automated way to do this such as a 'for' or 'while' function/loop? (I realize that not all files should be loaded at once, and I don't plan to do that, but typing out texture.loadFromFile(000.png) 100 individual times because rather monotonous and seems as though it would have an easier solution.)

Thanks for any help, I'm still new to this!

smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Is there a function to sort through pngs?
« Reply #1 on: September 20, 2022, 08:48:24 pm »

 Is there a faster, quicker, or automated way to do this such as a 'for' or 'while' function/loop?

Yes.
Pseudocode but something like:

for (int i = 0; i< myVector.length; i++)
{
    myVector[i].loadFromFile(i + ".png");
}
 

Side note, this is a very basic "intro to programming" type of question, so I would recommend you forget about SFML for a week and just read a beginners book or several hours of youtube videos on coding first.

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Is there a function to sort through pngs?
« Reply #2 on: September 21, 2022, 08:33:40 am »
myVector.loadFromFile(i + ".png"); is very dangerous in C++, it will start trying to read invalid memory if i is greater than 4.

i + ".png" doesn't turn i into a string and append ".png" to it. Instead i is added to the start address of ".png". So the loop will generate:
".png"
"png"
"ng"
"g"
""
After that it's reading memory that wasn't part of the string.

Here's a working one:
myVector[i].loadFromFile(std::to_string(i) + ".png");
(Although this doesn't have the leading zeros, so might not suit the thread situation exactly)

smurf

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: Is there a function to sort through pngs?
« Reply #3 on: September 22, 2022, 09:29:49 pm »
It was pseudocode and it was actually C#
But good to know.

 

anything