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.
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.
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?
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 (https://en.wikipedia.org/wiki/Liskov_substitution_principle).