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

Author Topic: Deleting a sprite  (Read 8191 times)

0 Members and 3 Guests are viewing this topic.

killmichnich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Deleting a sprite
« on: September 26, 2009, 07:56:43 pm »
Hiho Community =)
I've got a problem with deleting sprites. My current programm looks like this:
Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

using namespace std;

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    sf::Image* Image = new sf::Image();
    if(!Image->LoadFromFile("sprite.png"))
    {
        cout<<"Sprite konnte nicht geladen werden"<<endl;
    }
    sf::Sprite* Sprite = new sf::Sprite((*Image));
    Sprite->SetPosition(0, 0);
    while(App.IsOpened())
    {
        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();
            }
            if(Event.Type == sf::Event::KeyPressed && Event.Key.Code==sf::Key::A)
            {
                delete Image;
                Image = NULL;
                delete Sprite;
                Sprite = NULL;
            }
        }

            App.Clear();

            App.Draw((*Sprite));

            App.Display();
    }


    return EXIT_SUCCESS;
}

Now when i press 'A' and want do delete the image and the sprite the programms hanging up. I'm able to destroy the image alone, but when i'm destroying the sprite it's hanging up and i don't know why :(

I'm using code blocks with the ms vc++ 2008 compiler and downloaded the newest version of sfml for vc++ 08, the libraries i added are sfml-graphics.lib and sfml-window.lib, otherway i can't compile. The Build Target is Release.

I hope someone can help me =)

MfG Killmichnich

PS: Sorry for my bad english, i'm not very goot at it =(

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Deleting a sprite
« Reply #1 on: September 26, 2009, 10:29:26 pm »
Why people always use pointer when tey don't need them ? On goo tip : Don't use them and everything will work ( at least here ) .

But let's skip this recurrent speech and answer to this small question : "What are you doing wrong ?"

Short answer : you're using deleted data.
Long answer : when you press A you destroy your two variables so they are in an undefined state. Then you use them ( directly Sprite and indirectly Image because this one is used by the first ) . That is the problem.

PS : never call your vars with its name's type ( for example  don't do "sf::Sprite Sprite" ) . You may have some trouble one day.
SFML / OS X developer

killmichnich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Deleting a sprite
« Reply #2 on: September 27, 2009, 09:02:03 am »
would be brilliant if it was so simple. At the beginning i din't use the Pointer, and every time i closed my application it hang up. So I tried it with that pointer to know if it is the sprites fault. The only way it works is when i'm closing the application without deleting my Sprite :/ ... but thats a bit stupid ^^

And i'm calling my var only Sprite for testing it because i just began with sfml and wanted to draw sth. in my window ^^

Meltra Bour

  • Newbie
  • *
  • Posts: 28
    • View Profile
Deleting a sprite
« Reply #3 on: September 27, 2009, 11:46:28 am »
Like Hiura sais, your app hangs because your calling a NULL pointer. Your main loop will go on after you press A so your computer tries to draw the spirit after it is deleted ...

Code: [Select]

delete Sprite;
....
App.Draw((*Sprite));


To prevent that you could use 'if(pointer != NULL)'
Code: [Select]

delete Sprite;
Sprite = NULL;
....
if (Sprite != NULL)
    App.Draw((*Sprite));


If you don't need the pointer then don't use it, the code you posted doesn't need a pointer. If you really need that pointer then you have 2 options:
- use the if() statement to make sure you don't call a NULL pointer
- restructure your code and make 100% sure you don't call a invalid pointer.

killmichnich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Deleting a sprite
« Reply #4 on: September 27, 2009, 01:02:31 pm »
okay i know what you mean but my problem isn't that pointer, believe me ^^

here the programm without that pointer:
Code: [Select]

#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>

using namespace std;

int main()
{
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    sf::Image Image;
    if(!Image.LoadFromFile("sprite.png"))
    {
        cout<<"Sprite konnte nicht geladen werden"<<endl;
    }
    sf::Sprite Sprite(Image);
    Sprite.SetPosition(0, 0);
    while(App.IsOpened())
    {
        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();
            }
        }

            App.Clear();

            App.Draw(Sprite);

            App.Display();
    }


    return EXIT_SUCCESS;
}

You meant i should do it light this, dint't you?
Now the programms working brilliantly, just when i close it by esp or clicking on the X, it's hanging up. With that pointer i just wanted to test if it's the sprite's fault or not. So I think it's the destructor of the sprite or smilar.
I hope you know now what's my problem ^^

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Deleting a sprite
« Reply #5 on: September 27, 2009, 03:26:11 pm »
okay. Did you try without sprite thing ? Are you sure that the problem comes from its destruction ?

Quote
I'm using code blocks with the ms vc++ 2008 compiler and downloaded the newest version of sfml for vc++ 08, the libraries i added are sfml-graphics.lib and sfml-window.lib, otherway i can't compile. The Build Target is Release.
Which one ? SFML2 ? 1.5 ? svn-1.6 ?
SFML / OS X developer

killmichnich

  • Newbie
  • *
  • Posts: 4
    • View Profile
Deleting a sprite
« Reply #6 on: September 27, 2009, 04:25:12 pm »
i'm relative sure, when i don't set the image the programms running well, and i can close it without a bugmessage, just when i set the image to the sprite it doesn't work.

I'm using version 1.5 for msvc++ 2008, my operating system is windows vista business 64-bit (maybe important).

Meltra Bour

  • Newbie
  • *
  • Posts: 28
    • View Profile
Deleting a sprite
« Reply #7 on: September 27, 2009, 04:26:56 pm »
sorry, wasn't sure you got what he ment and I got fixated on the pointer ... (din't notice that other thingy)

Try something like this ...
Code: [Select]

int main()
{
....
    bool running = true;
    while(running)
    {
        sf::Event Event;
        while(App.GetEvent(Event))
        {
            if(Event.Type == sf::Event::Closed || (Event.Type==sf::Event::KeyPressed && Event.Key.Code == sf::Key::Escape))
            {
                running = false;
            }
        }

        App.Clear();
        App.Draw(Sprite);
        App.Display();
    }

    App.Close();

    return EXIT_SUCCESS;
}


App.Close() makes one/some of those calls behind it invalid, can't remember witch one do.
Someone should adjust the tutorials, some use the boolean others use App.IsOpened() ... confusing.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Deleting a sprite
« Reply #8 on: September 27, 2009, 04:31:17 pm »
Quote
App.Close() makes one/some of those calls behind it invalid, can't remember witch one do.

None :)
If there's one, it is a bug.

Quote
Someone should adjust the tutorials, some use the boolean others use App.IsOpened() ... confusing.

Which ones are still using a boolean?

Quote
my operating system is windows vista business 64-bit (maybe important)

It is! SFML binaries are compiled for 32 bits architectures. You have to recompile SFML.
Laurent Gomila - SFML developer

Meltra Bour

  • Newbie
  • *
  • Posts: 28
    • View Profile
Deleting a sprite
« Reply #9 on: September 27, 2009, 05:15:40 pm »
ugh, just ignore me, a other myth that lived in my head busted  :lol:
tutorial Window - Opening a window, but that one fits in the flow I gues.

 

anything