Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualServant of the Lord

Posted 15 August 2013 - 10:25 PM

I created a seperate class to handle loading in the sprites, displaying them and then destroying them.

That doesn't sound very efficient! You want to load them, display them many times, and when they are no longer needed, then you destroy them. But if you load, draw, destroy every time you want to draw them, your code will slow to a crawl. Or maybe I'm misunderstanding what you mean! smile.png 
 

This is a line of code I use to load in a sprite 

    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle);

Don't forget that in SFML, the lifetime of the texture must last longer than the lifetime of the sprite that the texture is set to. The sprite doesn't take ownership of the texture (since the same texture can be set to multiple sprites), so you have to handle the lifetime of the texture yourself.

This isn't related to your error message, but I think it's another bug you might have in your program once you resolve your error message. I can't tell for sure until I see the code though.

So if you do this:

{
    sf::Sprite mySprite;
    
    {
        sf::Texture myTexture;
        //...
        
        mySprite.setTexture(myTexture);
        
    } //<--- 'myTexture' gets destroyed here when it goes out of scope, so mySprite now has an invalid texture!
    
}

So in the main program I make a call to that function however when I debug and run the program it says that sPaddle was not declared in this scope.

I think you're using the words 'debug' and 'run' wrong.

First, your code compiles. This is where you are likely getting your error message.
Second, you run your program after it is compiled.
Third, if you are running your program in debug mode and using a debugger, then you are debugging your program.

 

This is just programmer in-speak we use to communicate to other programmers faster, so the meaning of the words matter for clear communication. 
 

I never uploaded code before so if you need to look at it just let me know, Thanks smile.png

Yep, to help further, we will need to see it. This forum's post editor has a button near the type that says 'code'.

htk0.jpg

 

Click that, then copy+paste the code into the box that appears. We need the files (both header and source) of the new helper class you wrote, as well as the function that is giving you the compiler error.

Also, posting the compiler error itself would help - compiler errors, though confusing at first, actually do try to tell you what is wrong, you just need to learn how to read them (which comes overtime).


#2Servant of the Lord

Posted 15 August 2013 - 10:23 PM

I created a seperate class to handle loading in the sprites, displaying them and then destroying them.

That doesn't sound very efficient! You want to load them, display them many times, and when they are no longer needed, then you destroy them. But if you load, draw, destroy every time you want to draw them, you're drawing code will slow to a crawl. Or maybe I'm misunderstanding what you mean! smile.png 
 

This is a line of code I use to load in a sprite 

    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle);

Don't forget that in SFML, the lifetime of the texture must last longer than the lifetime of the sprite that the texture is set to. The sprite doesn't take ownership of the texture (since the same texture can be set to multiple sprites), so you have to handle the lifetime of the texture yourself.

This isn't related to your error message, but I think it's another bug you might have in your program once you resolve your error message. I can't tell for sure until I see the code though.

So if you do this:

{
    sf::Sprite mySprite;
    
    {
        sf::Texture myTexture;
        //...
        
        mySprite.setTexture(myTexture);
        
    } //<--- 'myTexture' gets destroyed here when it goes out of scope, so mySprite now has an invalid texture!
    
}

So in the main program I make a call to that function however when I debug and run the program it says that sPaddle was not declared in this scope.

I think you're using the words 'debug' and 'run' wrong.

First, your code compiles. This is where you are likely getting your error message.
Second, you run your program after it is compiled.
Third, if you are running your program in debug mode and using a debugger, then you are debugging your program.

 

This is just programmer in-speak we use to communicate to other programmers faster, so the meaning of the words matter for clear communication. 
 

I never uploaded code before so if you need to look at it just let me know, Thanks smile.png

Yep, to help further, we will need to see it. This forum's post editor has a button near the type that says 'code'.

htk0.jpg

 

Click that, then copy+paste the code into the box that appears. We need the files (both header and source) of the new helper class you wrote, as well as the function that is giving you the compiler error.

Also, posting the compiler error itself would help - compiler errors, though confusing at first, actually do try to tell you what is wrong, you just need to learn how to read them (which comes overtime).


#1Servant of the Lord

Posted 15 August 2013 - 10:20 PM

I created a seperate class to handle loading in the sprites, displaying them and then destroying them.

That doesn't sound very efficient! You want to load them, display them many times, and when they are no longer needed, then you destroy them. But if you load, draw, destroy every time you want to draw them, you're drawing code will slow to a crawl. Or maybe I'm misunderstanding what you mean! smile.png 
 

This is a line of code I use to load in a sprite 

    if(!paddle.loadFromFile("Paddle.png"))
        std::cout << "Could not load paddle." << std::endl;
    sf::Sprite sPaddle;
    sPaddle.setTexture(paddle);

Don't forget that in SFML, the lifetime of the texture must last longer than the lifetime of the sprite that the texture is set to. The sprite doesn't take ownership of the texture (since the same texture can be set to multiple sprites), so you have to handle the lifetime of the texture yourself.

This isn't related to your error message, but I think it's another bug you might have in your program once you resolve your error message. I can't tell for sure until I see the code though.

So if you do this:

{
    sf::Sprite mySprite;
    
    {
        sf::Texture myTexture;
        //...
        
        mySprite.setTexture(myTexture);
        
    } //<--- 'myTexture' gets destroyed here when it goes out of scope, so mySprite now has an invalid texture!
    
}

So in the main program I make a call to that function however when I debug and run the program it says that sPaddle was not declared in this scope.

I think you're using the words 'debug' and 'run' wrong.

First, your code compiles. This is where you are likely getting your error message.
Second, you run your program after it is compiled.
Third, if you are running your program in debug mode and using a debugger, then you are debugging your program.

 

This is just programmer in-speak we use to communicate to other programmers faster, so the meaning of the words matter for clear communication. 
 

I never uploaded code before so if you need to look at it just let me know, Thanks smile.png

Yep, to help further, we will need to see it. This forum's post editor has a button near the type that says 'code'. Click that, then copy+paste the code into there. We need the files (both header and source) of the new helper class you wrote, as well as the function that is giving you the compiler error.

Also, posting the compiler error itself would help - compiler errors, though confusing at first, actually do try to tell you what is wrong, you just need to learn how to read them (which comes overtime).


PARTNERS