SFML community forums

Help => General => Topic started by: Putarda on July 31, 2016, 02:15:17 am

Title: When i store sprite to map and get it back, texture disappears.
Post by: Putarda on July 31, 2016, 02:15:17 am
So when i store sprite to map and get it back, texture disappears. Can someone tell me why is that happening  :-\.

Sprite.cpp:
#include <SFML\Graphics.hpp>

std::map<std::string, sf::Sprite> sprites;

void addSprite(std::string sprite_name, sf::Texture sprite_texture) {

        sf::Sprite sprite;

        sprite.setTexture(sprite_texture);

        sprites[sprite_name] = sprite;

}

sf::Sprite getSprite(std::string sprite_name) {

        sf::Sprite sprite = sprites[sprite_name];

        return sprite;
}
 

Game.cpp:
#include <iostream>
#include "Game.h"
#include "Window.h"
#include <thread>
#include "Resource.h"
#include "Sprite.h"

bool game_running = STOP;

void createGame() {

        openWindow();

        loadResources();

        startGame();

}

void loadResources() {

        //player

        sf::Texture player_texture = loadTexture("C:/Users/User/Desktop/resources/player_texture.png");

        addSprite("player_sprite", player_texture);

}
 

Window.cpp:
#include <SFML\Graphics.hpp>
#include <thread>
#include "Game.h"
#include "Resource.h"
#include "Sprite.h"

sf::RenderWindow window;

void openWindow() {

        window.create(sf::VideoMode(800, 600), getGameName());
        window.setVerticalSyncEnabled(true);

        sf::Sprite sprite_test = getSprite("player_sprite"); //there is no error here

        while (window.isOpen()) {

                sf::Event event;

                while (window.pollEvent(event)) {

                        switch (event.type) {

                        case sf::Event::Closed:

                                window.close();

                                break;
                        }

                }

                window.clear();
                window.draw(sprite_test); //and when i draw it nothing happens. There is even no white texture.
                window.display();

        }

}
 

I hope someone can help me, because i'm messing with this whole 2 days... :'(
Title: Re: When i store sprite to map and get it back, texture disappears.
Post by: fallahn on July 31, 2016, 09:39:59 am
This is the white square problem (http://www.sfml-dev.org/tutorials/2.3/graphics-sprite.php#the-white-square-problem). addSprite is creating a copy of the texture which is then destroyed when the function exits. Consider using a resource manager for your textures such as the one found in Thor (http://www.bromeon.ch/libraries/thor/documentation/v2.0/namespacethor_1_1_resources.html) or the SFML game development book.
Title: Re: When i store sprite to map and get it back, texture disappears.
Post by: Putarda on July 31, 2016, 03:00:11 pm
But there is no white texture...
Title: Re: When i store sprite to map and get it back, texture disappears.
Post by: korczurekk on July 31, 2016, 03:26:00 pm
It actually doesn't have to be white. Just give reference to texture instead of copying it. Btw texture manager would be the best choice.
Title: Re: When i store sprite to map and get it back, texture disappears.
Post by: Putarda on July 31, 2016, 04:54:47 pm
So i need to create class where is stored sprite and texture? And then make map<std::string, class_name>?
Title: Re: When i store sprite to map and get it back, texture disappears.
Post by: Hiura on August 01, 2016, 07:06:29 am
No, not necessarily, just make sure you use only one texture instance and not a copy. It's essential you understand when, where and how an object is copied to use C++ efficiently and to avoid such pitfalls. Probably a good book on C++ will help here.  ;)