Hi everybody,
I have some trouble managing resources, which is probably less of a SFML question but rather a general C++ question.
I have written a resource manager which returns a const pointer to a const sf::Texture.
class ResourceManager {
public:
const sf::Texture *getResource(std::string resourceName) const;
private:
std::map<std::string, sf::Texture> resourceMap;
};
This is all working fine and well. Then I have another class which manages drawing the actual display, which has a blit() function, which is supposed to get a Texture from the ResourceManager and draw it using a Sprite.
class DisplayManager {
public:
void drawDisplay(); // calls Clear() at the beginning and Display() at the end
private:
sf::Sprite drawSprite;
void drawMainMenu();
void drawOptionsMenu(); // etc, etc...
void blit(int screenX, int screenY, float scaleX, float scaleY, std::string resourceName);
};
My main() function calls drawDisplay() every frame, which queries the game state and then decides what to draw (main menu, or options menu, etc). It also runs Clear() at the beginning and Display() at the end.
In my example, it decides to call drawMainMenu(), which then calls
blit(0, 0, 1, 1, "MainmenuBackground");
to draw the background of the main menu. The blit() function uses the member Sprite drawSprite everytime it's called and overwrites its configuration. It basically looks like this:
void DisplayManager::blit(int screenX, int screenY, float scaleX, float scaleY, std::string resourceName) {
const sf::Texture* const renderTexture = resourceManager.getResource(resourceName);
drawSprite.SetTexture(*renderTexture);
drawSprite.SetPosition((float) screenX, (float) screenY);
// omitted the scaling code, which is currently commented out anyway
mainWindow.Draw(drawSprite);
};
The code is compiling cleanly, and is also working in some way. I'm getting the resource from the ResourceManager. I can also set it as the sprite's Texture via SetTexture.
Even more, I can access the texture I set in blit() in the drawScreen() function right before the Display() call, can for example use drawSprite.GetTexture()->GetWidth() and GetHeight() with correct results.
But nonetheless, the application only runs for two frames (it's 2 frames every time), where the first frame is actually showing a "raw" RenderWindow (I can see the contents of the window behind it, using SFML on Linux), and the second frame at least applies the Clear() and draws the contents of the window black.
Then the application just closes (no crash).
Through trial and error I found out that if I comment out the mainWindow.Draw(drawSprite) call in blit(), the application runs just fine (only clearing the screen).
I'm probably misunderstanding how the memory management works in this case, but I just can't understand why it's not displaying anything and just closing the application. Manually drawing primitive shapes in the drawDisplay() function works just fine. Also, if I manually create a sf::Texture and sf::Sprite in the drawDisplay() function before the mainWindow.Display(), it can be displayed.
Thanks in advance, and a happy new year everybody. May SFML continue to be a great library in 2012
PS: I'm using a current git checkout of SFML 2.0 on Fedora 16, 64 bit.