Hi Folks,
I'm still new to programming and sfml (using sfml2.0). I've noticed that the memory usage goes way up very quickly in my system task manager (5-6GB within 10-15min of play for example).
I'm trying to do a simple RISK type war strategy game where I have a map of the world divided up into specific regions, i.e. Spain, France etc.
So I've got a Region class which will have a jpg image filename that I want to load into the program and display the region. The region color changes depending on which nation controls that particular region.
So nothing too fancy at all. I've essentially followed the tutorial
http://sfml-dev.org/tutorials/2.0/graphics-sprite.php in order to determine how to load in images via SFML.
So I've created a region class, which has a texture, sprite, filename and a position. See below.
class Region
{
public:
Region();
struct Position
{
int xPos;
int yPos;
};
string getRegionImageFileName();
Position getImagePosition();
sf::Texture getRegionImageTexture();
sf::Sprite getRegionImageFileSprite();
private:
string regionImageFileName;
Position imagePosition;
sf::Texture regionImageFileTexture;
sf::Sprite regionImageFileSprite;
};
So in the main game loop create a vector of this Region class which defines all of these parameters. (I actually read it in from a text file but that is really relevant for my question). I also have a drawEngine class which has pointer to the Region vector that contains all of the regions. It also has a Sprite, sprite pointer and texture pointer defined as well as the renderwindow.
class drawEngine
{
public:
drawEngine();
int drawAllRegions(); //this draws out the list of regions to the screen.
void drawRegion(Region passed_Region);
sf::RenderWindow gameWindow;
private:
vector<Region> *regionListPtr;
void initSpriteTextureData(Region passed_Region);
sf::Sprite * spritePtr;
sf::Sprite sprite;
sf::Texture * texture;
};
So during each turn, I need to go through this vector and display each region's image file at the correct position.I have a function drawAllRegions() that goes through the vector and calls the drawRegion function.
int drawEngine::drawAllRegions()
{
gameWindow.clear(sf::Color::Black);
for (int i = 0; i < regionListPtr->size(); i++)
{
drawRegion(regionListPtr->at(i));
}
mapBorder.setFillColor(sf::Color::Transparent);
return 0;
}
The drawRegion function inturn calls the initSpriteTextureData function to define the drawEngine's sprite. Once defined, this function draws the sprite.
void drawEngine::drawRegion(Region passed_Region)
{
initSpriteTextureData(passed_Region);
gameWindow.draw(sprite);
}
As you can see the initSpriteTextureData function, gets the data defined in the Region class and then defines the drawEngine's sprite.
void drawEngine::initSpriteTextureData(Region passed_Region)
{
spritePtr = &passed_Region.getRegionImageFileSprite();
texture = &passed_Region.getRegionImageTexture();
texture->loadFromFile(passed_Region.getRegionImageFileName());
spritePtr->setTexture(*texture);
spritePtr->setPosition(passed_Region.getImagePosition().xPos,passed_Region.getImagePosition().yPos);
// Add set color according to nation color
Nation::nationRGB tempNatColour = passed_Region.RegionNationOwnerPtr->getNationColour();
spritePtr->setColor(sf::Color(tempNatColour.red,tempNatColour.green,tempNatColour.blue));
sprite=*spritePtr;
}
Now I've confirmed that this is the part of the code that is eating up all of the memory as I commented out the line below and the game went through fine without using up hardly any memory at all.
texture->loadFromFile(passed_Region.getRegionImageFileName());
Clearly the way I am doing it is not the most efficient way, i.e. I am loading from file each time initSpriteTextureData is called. This load image file can get called a few hundred of times in a single iteration of the game loop. Obviously I'm being dumb here as I literally just followed how to load an image from the tutorial and didn't concern myself with memory issues.
My question, is there a most efficient memory-usage wise way to load in the images once from file into memory and then use the memory copy instead? Please bear in mind that I am still trying to learn SFML so appreciate this might be a silly question.
I've read the below "
http://sfml-dev.org/documentation/2.0/classsf_1_1Texture.php", and I've seen the loadfromMemory function. The name of the function suggests it but, does this allow me to re-use a image file that has already been loaded from a file? I assume that this should stop the memory usage increasing?
I am very confused by the parameters below:-
data Pointer to the file data in memory
size Size of the data to load, in bytes
How can I keep track of where the image file data is stored in memory? Also is the size parameter the file-size or is that defined somewhere else?
Many thanks in advance.