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

Author Topic: Problem with Texture::create in custom class  (Read 2146 times)

0 Members and 1 Guest are viewing this topic.

Dante12129

  • Newbie
  • *
  • Posts: 24
    • View Profile
Problem with Texture::create in custom class
« on: November 29, 2013, 02:44:44 am »
I am creating a custom class, called TexturedSprite, to ease the process of loading textures/ setting a sprite's texture/ drawing the sprite. It basically holds a Texture pointer and sprite and autmatically takes care of the relations between them. The problem is when I try to load a file into the texture.

Here are the definitions of my loadFromFile function and constructor that takes a string parameter:

bool TexturedSprite::loadFromFile(const std::string& filename, const sf::IntRect& area) { return m_texture->loadFromFile(filename, area); m_sprite.setTexture(*m_texture); }
TexturedSprite::TexturedSprite(const std::string& filename)
    {
        //Load the texture
        if(!m_texture->loadFromFile(filename))
           std::cout << "Failed to load file for textured sprite: " << filename << std::endl;
        m_sprite.setTexture(*m_texture); //Set the sprite to the texture
    }

I ran my program in gdb and the call stack says the problem is at sf::Texture::create, specifically line 125:
m_size.x        = width;
Here is my call stack:
#0 68ED9585     sf::Texture::create(this=0xbaadf00dbaadf00d, width=950, height=600) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:125)
#1 68ED9A2A     sf::Texture::loadFromImage(this=0xbaadf00dbaadf00d, image=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:192)
#2 68ED97F7     sf::Texture::loadFromFile(this=0xbaadf00dbaadf00d, filename=..., area=...) (C:\SFML-2.1\src\SFML\Graphics\Texture.cpp:160)
#3 00402349     whack::TexturedSprite::TexturedSprite(this=0x6f7708, filename=...) (C:\Users\Main\Documents\Programs\WhackGame\src\TexturedSprite.cpp:12)
#4 00401A73     whack::IntroState::IntroState(this=0x6f76f0, window=...) (C:\Users\Main\Documents\Programs\WhackGame\src\IntroState.cpp:12)
#5 004016DA     whack::Game::Game(this=0x22fbf0) (C:\Users\Main\Documents\Programs\WhackGame\src\Game.cpp:8)
#6 00401512     main() (C:\Users\Main\Documents\Programs\WhackGame\main.cpp:5)
 

Here is the code I'm calling it from (it crashes whether I use the initializer list or loadFromFile function:
IntroState::IntroState(sf::RenderWindow& window) : m_window(&window), m_needs_change(false, dte::StateChangeType::Next) //,m_background("Resources/Images/intro_background.png"), m_logo("Resources/Images/logo.png")
    {
        if(!m_background.loadFromFile("Resources/Images/intro_background.png"))
            std::cerr << "Unable to load background texture for intro state." << std::endl;
        if(!m_logo.loadFromFile("Resources/Images/logo.png"))
            std::cerr << "Unable to load background texture for intro state." << std::endl;
    }

It works fine if I do the textures and sprites separately. I am using TDM gcc 4.8.1, Windows 7 64-bit, and CB 12.11. I have included my TexturedSprite files and IntroState files in case you need any more information.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: Problem with Texture::create in custom class
« Reply #1 on: November 29, 2013, 10:35:23 am »
Well the thing is, you declare a pointer to a texture and then you suddenly assume the pointer points to a valid texture. If you use a pointer, you either have to point it at an existing object or you need to allocate new memory on the heap. But since we're in 2013 and C++ has come a long way since then, manual memory management is a bad idea, thus instead of using a raw pointer you should use a smart pointer, e.g. unique_ptr or shared_ptr.
In any case you'll need to create an object first, before you try to access it. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Problem with Texture::create in custom class
« Reply #2 on: November 29, 2013, 10:38:58 am »
Code: [Select]
sf::Texture::loadFromFile(this=0xbaadf00dbaadf00d, filename=..., area=...)
sf::Texture::loadFromImage(this=0xbaadf00dbaadf00d, image=..., area=...)
sf::Texture::create(this=0xbaadf00dbaadf00d, width=950, height=600)
Hmm.... I wonder why it doesn't like the this pointer. 0xbaadf00d might shed some light on this...

No really... this is kind of obvious... you should learn to read the callstack yourself to solve these kinds of problems in the future.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

 

anything