SFML community forums

Help => General => Topic started by: KyleGBC on January 30, 2019, 04:50:45 am

Title: Storing Textures
Post by: KyleGBC on January 30, 2019, 04:50:45 am
So I have this class and it wants to be able to store the texture of a sprite, change the texture, and then when the object passes out of scope, change the texture back to the original. The problem I'm having is that I can't find a way to store the original texture.
Details
=====
Here is the header file:
#pragma once
#include "game.h"
using namespace sf;
const Vector2f DEFUALT_START_POSISTION = Vector2f(800, 450);
class Battle {
public:
        Battle(Sprite& controlledCharacter, float playerXNew, float playerYNew, float cameraXNew, float cameraYNew, Texture& area);
        ~Battle();
        float m_playerXInit;
        float m_playerYInit;
        float m_cameraXInit;
        float m_cameraYInit;
        Sprite m_controlledCharacter;
        Texture m_area;
        Texture m_backgroundInit;
};
Here is the .cpp:
#include "Battle.h"
using namespace sf;
Battle::Battle(Sprite& controlledCharacter, float playerXNew, float playerYNew, float cameraXNew, float cameraYNew, Texture& area)
        : m_controlledCharacter(controlledCharacter), m_area(area)
{
        m_playerXInit = controlledCharacter.getPosition().x;
        m_playerYInit = controlledCharacter.getPosition().y;
        m_cameraXInit = viewBox.getCenter().x;
        m_cameraYInit = viewBox.getCenter().y;
       
        Texture m_backgroundInit = Background.getTexture();

        controlledCharacter.setPosition(Vector2f(playerXNew, playerYNew));
        viewBox.setCenter(Vector2f(cameraXNew, cameraYNew));
        Background.setTexture(area);
}
Battle::~Battle() {
        m_controlledCharacter.setPosition(Vector2f(m_playerXInit, m_playerYInit));
        viewBox.setCenter(Vector2f(m_cameraXInit, m_cameraYInit));
        Background.setTexture(m_backgroundInit);
}
The line "Texture m_backgroundInit = Background.getTexture();" has the problem:"No suitable constructor exists to convert from 'const sf::Texture' to 'sf::Texture'"
How can I store the texture info?
Title: Re: Storing Textures
Post by: Arcade on January 30, 2019, 04:09:08 pm
sf::Sprite::getTexture() returns a pointer to a texture, not the texture itself. See the  SFML documentation (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Sprite.php#a1a76155146c8ff37c4eb5a306b4e9ebe).

Also, you are masking your m_backgroundInit member variable by creating another variable with the same name in the constructor.

And your cpp file is using several variables that I don't see defined anywhere, like 'Background' and 'viewBox'. Are these defined as global variables elsewhere?
Title: Re: Storing Textures
Post by: KyleGBC on February 01, 2019, 12:01:25 am
viewBox and Background are defined in game.h.
If getTexture returns a pointer, is there a way to accomplish what I'm trying to do?
(Sorry if this is a stupid question, I'm very much a novice)
Title: Re: Storing Textures
Post by: NGM88 on February 11, 2019, 07:35:29 am
1. Decide which class should own the texture.
2. Make sure that the owner doesn't run out of scope if the texture will be in use.
3. Pass pointers of the texture from the owner class to any other class that will use the texture.