Hi!
I have a multi-part question regarding the proper way of loading, storing and accessing textures and sprites.
Currently i am working on a 2d side-scrolling brawler. Three major game elements, which use textures and sprites are hero, enemies and background/map. I'd like to implement parallax scrolling later on.
Anyway, i wanted to ask about a proper way to load and store gfx elements for each of those instances.
For hero class, i keep all textures and sprites as members, and load textures in the constructor. Since there is only one hero, and he is loaded with the map, it should just work fine. Here's relevant parts:
Hero::Hero (b2World *aWorld, float posX, float posY) : world(aWorld)
{
hero_run.loadFromFile("HeroTextures/run.png");
hero_turn.loadFromFile("HeroTextures/turn.png");
hero_jump.loadFromFile("HeroTextures/jump.png");
hero_crouch.loadFromFile("HeroTextures/crouch.png");
hero_roll.loadFromFile("HeroTextures/roll.png");
hero_stop.loadFromFile("HeroTextures/stop.png");
hero_stand.loadFromFile("HeroTextures/stand.png");
hero_attack.loadFromFile("HeroTextures/attack.png");
m_sprite.setPosition(posX, posY);
m_sprite.setOrigin(250,250);
frame = 0;
set_b2_body(); // Box2d stuff
}
void Hero::display(State state)
{
switch(state)
{
case running:
if( direction == right)
m_sprite.setScale(1.0f,1.0f);
else
m_sprite.setScale(-1.0f,1.0f);
m_sprite.setTexture(hero_run);
m_sprite.setTextureRect(hero_run_clips[frame]);
frame++;
if (frame > 20 )
frame = 0;
break;
}
That's the gist of it. I imagine, for enemy class, i'll have to load textures somewhere else, because there will be mutliple enemies constructed from the same texture. Other than that, setTexture seems the way to go.
So regarding this part, my question is: is this the optimal way of handling graphics for animated entities, or could i store it, or access it in more optimized way?
Second part of my post pertains to the background. After preparing elements of the map i just set em the standard way:
TestLevel::TestLevel(b2World *aWorld) : world(aWorld)
{
backgTexture1.loadFromFile("background/testlevel1.jpg");
backgTexture2.loadFromFile("background/testlevel2.jpg");
backgTexture3.loadFromFile("background/testlevel3.jpg");
background1.setTexture(backgTexture1);
background2.setTexture(backgTexture2);
background3.setTexture(backgTexture3);
background1.setPosition(0,0);
background2.setPosition(5000,0);
background3.setPosition(10000,0);
set_ground_bodies(); // box2d stuff
}
It runs nice and all, but loading time is now good couple seconds, and it's only a small part of an actual planned level. Those textures are pretty big though (5000x5000).
So my last question is: how to improve loading time?
Edit: only part of my post showed the 1st time, edited for full version.