Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: When i store sprite to map and get it back, texture disappears.  (Read 2836 times)

0 Members and 1 Guest are viewing this topic.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
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... :'(

fallahn

  • Sr. Member
  • ****
  • Posts: 468
  • Buns.
    • View Profile
    • Trederia
Re: When i store sprite to map and get it back, texture disappears.
« Reply #1 on: July 31, 2016, 09:39:59 am »
This is 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 or the SFML game development book.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: When i store sprite to map and get it back, texture disappears.
« Reply #2 on: July 31, 2016, 03:00:11 pm »
But there is no white texture...

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: When i store sprite to map and get it back, texture disappears.
« Reply #3 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.

Putarda

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: When i store sprite to map and get it back, texture disappears.
« Reply #4 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>?

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: When i store sprite to map and get it back, texture disappears.
« Reply #5 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.  ;)
SFML / OS X developer

 

anything