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

Author Topic: Loading sprites to vector.  (Read 2715 times)

0 Members and 1 Guest are viewing this topic.

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Loading sprites to vector.
« on: May 25, 2015, 10:10:04 pm »
Hello.
Lately I've been experiencing some problems with my code. To the thing:

class Textures
{
public:
fstream txtWeapon1h;
vector<string> namWeapon1h;
vector<sf::Texture> texWeapon1h;
vector<sf::Sprite> sprWeapon1h;

void loadItemTex()
{
txtWeapon1h.open("gfx/weapon/list.txt");
while( !txtWeapon1h.eof() )
{
string new_name;
getline( txtWeapon1h, new_name );
namWeapon1h.push_back(new_name);
}
txtWeapon1h.close();

for (int i=0; i<namWeapon1h.size(); i++)
{
sf::Texture new_tex;
new_tex.loadFromFile(namWeapon1h[i])
texWeapon1h.push_back(new_tex);
}

for (int i=0; i<namWeapon1h.size(); i++)
{
sf::Sprite new_block;
new_block.setTexture(texWeapon1h[i] );
sprWeapon1h.push_back(new_block);
}

}
//(...)
};
That's how i load my tex/spr in a Texture class. Now, in the same class I've got this drawing method:
void drawTile(sf::RenderWindow & okno, int tileType, int tileID, float posX, float posY)
{
    if(tileType == 0 )
     {
         sprCobble_walk[tileID].setPosition(posX, posY);
         okno.draw(sprCobble_walk[tileID]);
     }

else if(tileType == 1 )
     {

        sprWeapon1h[tileID].setPosition(posX, posY);
        okno.draw(sprWeapon1h[tileID] );
     }
else { error("unkown tile used" ); }
}
 
The problem is, that outside of this class,
sprWeapon1h.size()
is returning 0. Also, the app crashes when I try to use these textures (and method). I think the problem lays in scopes. However, maybe you have got any ideas to tell me how to load sprites into a vector and then to use them anywhere else?
By the way, in my main() I can freely draw those sprites, by simple
okno.draw(cTex.sprWeapon1h[1]);
I just can't get, how to make a drawing method using vectors.
« Last Edit: May 25, 2015, 10:13:21 pm by noct »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading sprites to vector.
« Reply #1 on: May 25, 2015, 11:06:59 pm »
Is cTex the object that holds the textures and sprites?
Are you passing it by reference whenever you pass it to functions?

By your final line of code, it looks like the sprites etc. are created fine since you can use them but your problem is when you try to access them where - inside a function, passed from main()?

p.s. It's recommended that you store the textures and sprites separately; they're very different. Textures are large resources whereas sprites are relatively lightweight, manipulatable objects.
« Last Edit: May 25, 2015, 11:08:34 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: Loading sprites to vector.
« Reply #2 on: May 25, 2015, 11:54:01 pm »
It is, indeed.

The problem appears when I try to access them in the same class, in
void drawTile()
method, and in
class Equipment : public Textures
class with this
void drawEq(sf::RenderWindow & okno, int eqOriginX, int eqOriginY, float cameraX, float cameraY)
{

for(int i=0; i<4; i++)
    {
        for(int j=0; j<7; j++)
        {
         drawTile(okno, 1, eqContainer[ i ][ j ], cameraX+eqOriginX+i*50, cameraY+eqOriginY+j*50); //(rendered window, tileType, tileID- from array, somewhereX, somewhereY)
        }
    }

}
 
method.

Aren't sprites global objects? If so, how should I pass them by reference to any other function?
I've got them loaded in cTex object of Texture class, so from my main function it would be like this? (passing)
some_function(cTex.sprWeapon1h, cTex.texWeapon1h)
and receiving
some_function(vector<sf::Sprite> & sprites, vector<sf::Texture> & textures)
Is the size of vectors saved anyway? I mean, should I use them as usual after this operation?

Ad ps: It's yet simplified code, but I'll take it into account.
« Last Edit: May 25, 2015, 11:57:13 pm by noct »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading sprites to vector.
« Reply #3 on: May 26, 2015, 12:06:06 am »
Yes, your functions parameters seem to be correct but you could just pass the entire object by reference.
e.g.
calling:
some_function(cTex);
and receiving:
void some_function(Textures& cTex);

You say that you draw call in main allows you draw the individual sprites so outputting the value of sprWeapon1h.size() at that point should give the correct size.

Sprites are not global objects, no. They are just a class like any other so the sprite objects' scopes are within where they are created and passed, as with other standard objects.

Is drawTile() in the public section of the Textures class?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Loading sprites to vector.
« Reply #4 on: May 26, 2015, 12:06:39 am »
Quote
Aren't sprites global objects?
No. They have the storage class that you're giving them: usually automatic or dynamic. Making them global is almost always a bad idea.

Pass textures by const reference to the functions that need them. Don't copy them.
void functionNeedingTexture(const sf::Texture& texture);

class Equipment : public Textures
An equipment is a texture or collection of textures? Are you sure this is a good way of class relationship? See LSP.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

noct

  • Newbie
  • *
  • Posts: 24
    • View Profile
    • nctdev.pl
Re: Loading sprites to vector.
« Reply #5 on: May 26, 2015, 01:23:13 am »
@Hapax
It works as I wanted to.
Quote
Is drawTile() in the public section of the Textures class?
Yes, it's public. Shouldn't it?

@Nexus
I've had an idea to make them global, but it was smelling to easy to be a good solution.
Is const necessary?

Just have read this, good point, Equipment was only for set, get and swap things, plus some calls for cPrimitives, containing shapes, vertexes etc.

By the way, after changing texture loading from "manual" to vectors I've got some additional errors - e.g. some not defined integers were set to random values instead of 0. Anyway, setting them to 0 with all load-kind methods worked.

Thank you very much for your help and kindness.
« Last Edit: May 26, 2015, 05:50:58 pm by noct »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Loading sprites to vector.
« Reply #6 on: May 27, 2015, 12:52:04 am »
It works as I wanted to.
I'm not sure which part you are refering to.

Quote
Is drawTile() in the public section of the Textures class?
Yes, it's public. Shouldn't it?
I was just checking. Being public won't stop it from working.

Is const necessary?
Const is very rarely (if ever) technically "necessary". It usually helps in the long run if you spend a moment to use it.
I'm sure Nexus can clarify, more accurately than if I attempt to, the reasons to use const.

Equipment was only for set, get and swap things, plus some calls for cPrimitives, containing shapes, vertexes etc.
If this is the case, it probably shouldn't inherit from the textures and sprites. Rather, it should act as a container and store them all.

By the way, after changing texture loading from "manual" to vectors I've got some additional errors - e.g. some not defined integers were set to random values instead of 0. Anyway, setting them to 0 with all load-kind methods worked.
Initialising vectors sounds like a good thing  ;)

Thank you very much for your help and kindness.
You're welcome. Hope was of some assistance  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything