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

Author Topic: [FIXED] Sprite not displaying when using it in another class  (Read 1643 times)

0 Members and 1 Guest are viewing this topic.

Lideln

  • Newbie
  • *
  • Posts: 5
    • View Profile
[FIXED] Sprite not displaying when using it in another class
« on: December 20, 2013, 03:59:06 pm »
Hi there!

I'm new to SFML, and I experience my first troubles...

I try to do things well: I created a StateManager, and a simple SplashScreenState. I also have an AssetManager that is a factory and creates me sf::Sprites (or other things) when I need one. It takes care of the textures and memory management.

But I have a problem...

My SplashScreenState creates a simple Sprite with an image inside. Then my main loop calls "currentState->draw()"... But nothing shows on the screen! However, if I get a pointer to that sprite, and call "renderWindow.draw()" directly from my main loop, it works like a charm.

What on earth is happening? I tried to debug references and pointers, but they are all valid and identical. AssetManager works well, because I can display the sprite properly in one case, therefore the sprite is ok.

I don't understand what is going on...

Code: [Select]
// Main loop :
void Game::loop()
{
JLE::StateManager stateManager = JLE::StateManager(*this);
stateManager.push(SplashScreenState::instance());

sf::Sprite* sprite;

while (m_window.isOpen())
{
JLE::AState* currentState = stateManager.current();
if (!currentState)
break;

sprite = currentState->m_splashImg; // I added splashImg in AState for debug purposes
std::cout << sprite << " / " << this << " / " << &this->m_window << std::endl;

m_window.clear();

// I switch between these two options to test...
//currentState->draw(); // This one does not work
m_window.draw(*sprite); // This one works

m_window.display();
}
}

// Init splash screen
void SplashScreenState::init(JLE::AGame& game)
{
m_game = &game;
m_active = true;
m_splashImg = JLE::AssetManager::factorySprite("splash.jpg");
LOG_S() << "splash : " << m_splashImg << std::endl;
}

// Draw splash screen
void SplashScreenState::draw()
{
LOG_S() << "draw..." << m_splashImg << " / " << m_game << " / " << &m_game->m_window << std::endl;
m_game->m_window.draw(*m_splashImg);
}


Please, help me!  :-\

PS : also, do not hesitate to tell me if I do things that are not best practices. I try to do my best, but I haven't touched C++ for like 10 years :) and I was just some beginner at the time

Thanks in advance!
« Last Edit: December 21, 2013, 11:26:40 am by Lideln »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
AW: Sprite not displaying when using it in another class
« Reply #1 on: December 20, 2013, 05:39:35 pm »
The sprite is defined as a pointer, thus when calling it directly, you'll just get the address, but when dereferencing (notice the *), you'll actually access the object.

I'm nof sure how it does even compile with just the address...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lideln

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite not displaying when using it in another class
« Reply #2 on: December 20, 2013, 05:56:52 pm »
Hi! Thanks for your answer.

Yes, the sprite is a pointer. It is created here:
Code: [Select]
sf::Sprite* AssetManager::factorySprite(std::string texName)
{
return new sf::Sprite(*AssetManager::loadTexture("images/" + texName));
}

Therefore, I get a pointer to a sprite object. When I dereference this pointer in the SplashScreenState::draw() function, it does not display anything.

But what is strange, is that if I dereference it in the main game loop function in the exact same way, it works fine.

Code: [Select]
// In SplashScreenState::draw()
m_game->m_window.draw(*m_splashImg); // Doesn't work

// In main game loop
sf::Sprite* sprite = currentState->m_splashImg;
m_window.draw(*sprite); // Works nice

It seems I can't pass a pointer to the WindowRenderer::draw() function.

I read a SFML tutorial (written for a game engine named GQE), and it uses references. But I can't use references member variables in a singleton class.

What should I do to convert my pointer to something that works? Although it still works in the main game loop using a pointer...

I also read in the forum a post by the Thor developer that says that SFML has some known issues with static variables or something. Is it the case here? Or is it a C++ error on my side?

Thank you in advance!

PS : I just bought the SFML book! I'll look into it, but if it is a C++ error that I made, I don't think it'll help

Lideln

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Sprite not displaying when using it in another class
« Reply #3 on: December 21, 2013, 11:26:21 am »
Ok, I was so tired that I changed really everything, and now it works.

I think I was doing something wrong, like using statics, and also instead of using a reference, I copied the reference I was given (not sure if I'm clear). Adding "&" to the variable type helped. I also had this error with my new way of doing things but I could spot it and fix it.

I will post a new topic regarding this matter (using references, or pointers, or std::unique_ptr) :)

Thank you!

 

anything