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

Author Topic: Can't load sf::Sprite from a function  (Read 5557 times)

0 Members and 1 Guest are viewing this topic.

wademcgillis

  • Newbie
  • *
  • Posts: 36
    • View Profile
Can't load sf::Sprite from a function
« on: August 01, 2009, 05:30:13 pm »
Code: [Select]
#include <SFML/Graphics.hpp>
#include <string>
using namespace std;
sf::RenderWindow App;

sf::Sprite load_sprite(string path)
    {
    sf::Sprite test_sprite;
    sf::Image img;
    img.LoadFromFile(path);
    test_sprite.SetImage(img);
    return test_sprite;
    }

//#define fromFunction // Un-comment the #define to make the image load incorrectly.

#ifdef _WIN32
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
#else
int main()
#endif
    {
    App.Create(sf::VideoMode(640, 480, 32), "SFML Window");
    sf::Sprite spr;
    #ifdef fromFunction
        spr = load_sprite("image.bmp");
    #else
        sf::Image bobf;
        bobf.LoadFromFile("image.bmp");
        spr.SetImage(bobf);
    #endif
    spr.SetCenter(120,80);
    spr.SetScale(1,1);
    spr.SetColor(sf::Color(255,255,255));
    spr.SetX(320);
    spr.SetY(240);
    bool loopin = true;
    while(loopin)
        {
        sf::Event Event;
        while (App.GetEvent(Event))
            {
            if ((Event.Type == sf::Event::Closed) || ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape)))
                {
                App.Close();
                loopin = false;
                break;
                }
            }
        App.Draw(spr);
        App.Display();
        sf::Clock clock;
        clock.Reset();
        while(clock.GetElapsedTime() < 1/60)
            {}
        }
return EXIT_SUCCESS;
    }

Output when fromFunction is defined

Output when fromFunction is NOT defined


edit:
fixed something. but the problem still occurs

K-Bal

  • Full Member
  • ***
  • Posts: 104
    • View Profile
    • pencilcase.bandcamp.com
    • Email
Can't load sf::Sprite from a function
« Reply #1 on: August 01, 2009, 06:07:43 pm »
The image is deleted at the end of the functions scope.
Listen to my band: pencilcase.bandcamp.com

e_barroga

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Can't load sf::Sprite from a function
« Reply #2 on: August 01, 2009, 06:13:25 pm »
Create a resource manager that has a container and add/remove methods.

Core Xii

  • Jr. Member
  • **
  • Posts: 54
    • MSN Messenger - corexii@gmail.com
    • AOL Instant Messenger - Core+Xii
    • View Profile
Can't load sf::Sprite from a function
« Reply #3 on: August 02, 2009, 06:15:19 pm »
That's right, it goes out of scope. You need to allocate it with new and return a reference.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Can't load sf::Sprite from a function
« Reply #4 on: August 02, 2009, 06:44:18 pm »
K-Bal +1
e_barroga +1 ( or use one from the wiki )
Core Xii -1 ( new is not required with a manager so its more easy )
SFML / OS X developer

Core Xii

  • Jr. Member
  • **
  • Posts: 54
    • MSN Messenger - corexii@gmail.com
    • AOL Instant Messenger - Core+Xii
    • View Profile
Can't load sf::Sprite from a function
« Reply #5 on: August 03, 2009, 09:03:35 am »
Quote from: "Hiura"

Core Xii -1 ( new is not required with a manager so its more easy )


Well... I didn't say it was the easy way. I prefer to do things the right way.

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
Can't load sf::Sprite from a function
« Reply #6 on: August 03, 2009, 10:29:30 am »
Quote from: "Core Xii"
Quote from: "Hiura"

Core Xii -1 ( new is not required with a manager so its more easy )

Well... I didn't say it was the easy way. I prefer to do things the right way.

It's not really the right way to create an object in a function and pass a reference to it as the return value. Can lead to dramatic memory leaks because it's a fact a developer *has to know* to use your function properly (very error-prone).

klusark

  • Newbie
  • *
  • Posts: 45
    • View Profile
Can't load sf::Sprite from a function
« Reply #7 on: August 03, 2009, 11:07:52 pm »
The right was would be passing some form of smart pointer. It would fix the problem and memory management would not be an issue.

But, a manager should be looked into for any serious project.

forrestcupp

  • Jr. Member
  • **
  • Posts: 57
    • View Profile
Can't load sf::Sprite from a function
« Reply #8 on: August 14, 2009, 09:14:13 pm »
You could just create the sprite in your main() function and pass a reference to it as one of the parameters in your Load function.  Then you don't even have to worry about memory management or leaks.

 

anything