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

Author Topic: SFML2 rendering sprites fail.  (Read 1949 times)

0 Members and 1 Guest are viewing this topic.

grandmasterhack

  • Newbie
  • *
  • Posts: 14
    • View Profile
SFML2 rendering sprites fail.
« on: June 22, 2011, 07:49:19 pm »
honestly, I get this issue all the damn time. what in the hell do i have to do to get a sprite on the screen every single time! i just get a white block of whats supposed to be the image. yes i've used sfml before and this issue is random. some projects i have them working fine. this they dont.

heres the code:

Code: [Select]


#include <SFML/Graphics.hpp>
#include"Background.hpp"

int main()
{
    // Create the main window-width, hieght
    sf::RenderWindow window(sf::VideoMode(800, 600), "Connect 4");

    window.SetFramerateLimit(15);
    //load images
    sf::Sprite background(Background::Load("Data/background.bmp"));
    background.Move(200,200);
    // Start the game loop
    while (window.IsOpened())
    {
        // Process events
        sf::Event event;
        while (window.PollEvent(event))
        {
            // Close window : exit
            if (event.Type == sf::Event::Closed)
                window.Close();
        }

        // Clear screen
        window.Clear();

        window.Draw(background);
        // Update the window
        window.Display();
    }

    return EXIT_SUCCESS;
}


all background does is return the sprite loaded.

Code: [Select]

sf::Image Background::Load(string filename)
{
    sf::Image image;

    if( !image.LoadFromFile(filename) )
    {
        //fail
    }
    else
    {
        return image;
    }
}

"In Miyamoto We Trust"

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
SFML2 rendering sprites fail.
« Reply #1 on: June 22, 2011, 08:59:57 pm »
You get a white sprite because the associate sf::Image is no more available.

Your Background::Load function is good as it returns a copy of the image but then on the line 'sf::Sprite background(Background::Load("Data/background.bmp"));' the returned image is not stored anywhere and is destroyed on the same line.

You can fix this with this 'naive' solution (using an image manager is way better as soon as you have multiple image) : repalce the above line by these two :
Code: [Select]
sf::Image background_image = Background::Load("Data/background.bmp");
sf::Sprite background(background_image);
SFML / OS X developer

grandmasterhack

  • Newbie
  • *
  • Posts: 14
    • View Profile
SFML2 rendering sprites fail.
« Reply #2 on: June 23, 2011, 08:12:37 pm »
Quote from: "Hiura"
You get a white sprite because the associate sf::Image is no more available.

Your Background::Load function is good as it returns a copy of the image but then on the line 'sf::Sprite background(Background::Load("Data/background.bmp"));' the returned image is not stored anywhere and is destroyed on the same line.

You can fix this with this "great" solution (using an image manager is way better as soon as you have multiple image) : repalce the above line by these two :
Code: [Select]
sf::Image background_image = Background::Load("Data/background.bmp");
sf::Sprite background(background_image);


then what would be the point (of using the background loader)? and it stills loads white blocks!

other engines use the same technique and work: http://code.google.com/p/dangerwave/
"In Miyamoto We Trust"

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
SFML2 rendering sprites fail.
« Reply #3 on: June 23, 2011, 08:23:52 pm »
Hum... I haven't took the 'background' name in consideration – I just considered it as 'someRandomNamespaceName' which has a loader  function.(stupid me)

Then, yes, it's pointless to return something that will be destroyed. You can do something else to fix this. While keeping the original code, change only the load function to :
Code: [Select]
sf::Image const& Background::Load(string filename) // 1st modification : const&
{
    static sf::Image image; // 2nd modification : static

    if( !image.LoadFromFile(filename) )
    {
        //fail
    }

    return image;
}


But I still think using a (image) manager is better. In fact your solution is also a manager but you have to create a new function for each image, so it's too static and don't respect DRY idiom (among other defaults).
SFML / OS X developer

grandmasterhack

  • Newbie
  • *
  • Posts: 14
    • View Profile
[Solved]
« Reply #4 on: June 26, 2011, 06:45:02 pm »
thanks man. i looked back at the code for our project and that was also there   :oops:
"In Miyamoto We Trust"