Hi,

I have a rock class, I use that class to generate multiple rock objects that are then put in a vector.

The problem I'm having is that when a rock is generated the whole game slows down making it laggy/choppy.

Right now my rock is loaded from file: rock.loadFromFile("image/rock.png"); I figures the problem is that I load the image from file everytime a rock is generated. Can you advise me on how to resolve this or is my diagnosis of this is wrong? Note that there are at least 9 rocks generated at any given time.

void SpaceRock::Init()
{
    if (!rock.loadFromFile("image/rock.png"))
    {
    }

    s_Rock.setTexture(rock);
    s_Rock.setTextureRect(sf::IntRect(17, 24, 88, 85));

    s_Rock.setPosition(source.x, source.y); //used sf::vector2f
}

Recommended Answers

All 2 Replies

Member Avatar for Search_not

You could generate 18(more or less depending on the amount of memory you wish to use) different rocks when your game starts and set their individual visibilities to false. Then whenever a rock is supposed to fall, you change the x co-ordinates and set the visibilty to true. Once the rock reaches to floor of the game, you reset the y co-ordinates so that it goes back to where it started i.e the roof of your game. Rocks can be chosen at random. Maybe use a boolean value to determine wether they are in the process of falling or not, so that falling rocks don't get chosen and moved around randomly.

You could also create a singleton class to deal with storage and management of all of the textures for your game.

The singleton should load a single copy of each texture used in the game. Either by loading all textures for the entire game at startup, or do it on a per level basis and load all textures used in the level at the start of each level. Then for your game objects you can simply get pointers to the textures you want to use, as all of the textures will already be available in memory.

That way if you want to create one rock or a hundred, you can use the same texture for all of them. That should remove a lot of unecessary overhead!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.