0

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
}
3
Contributors
2
Replies
10
Views
3 Years
Discussion Span
Last Post by JasonHippy
0

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.

0

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!

Edited by JasonHippy

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.