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

Author Topic: Storing Textures  (Read 2269 times)

0 Members and 1 Guest are viewing this topic.

KyleGBC

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Storing Textures
« 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
=====
  • Using dynamic SFML 2.4.2
  • Using Visual Studio 2015 community
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?
« Last Edit: January 30, 2019, 04:53:03 am by KyleGBC »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Storing Textures
« Reply #1 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.

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?
« Last Edit: January 30, 2019, 04:15:28 pm by Arcade »

KyleGBC

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Storing Textures
« Reply #2 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)

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Storing Textures
« Reply #3 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.

 

anything