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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Silentstrike

Pages: [1]
1
Graphics / Fixed
« on: February 13, 2012, 07:31:14 am »
Making the engine not a pointer solved the problem.  The reason I made it a pointer was because a book I am reading told me to.  I probably did not allocate memory correctly for it which gave me the error.  Anyway thanks for the help.

2
Graphics / References
« on: February 12, 2012, 09:47:53 pm »
I retried references and it still gave me the same error.  Also, when you pass the sprite into the list dont you get rid of the ampersand?  

Anyway, I thought that if you have to pass objects between classes, especially into a data structure, that you have to use pointers since references are not assignable.  I have also been told by a lot of resources online and in a few books that a list of pointers to sprites on the heap is good way of making a sprite handler as long as you delete them from memory properly.

Any other advice would be appreciated.

3
Graphics / Handler
« on: February 12, 2012, 11:41:27 am »
I forgot to paste that line for the sprite initialization.

sprite = new Sprite();

The main problem now is that I get a bad access error in the sprite handler list in the engine class.  I know that std::list<sf::Sprite> is safer, but as I said before I use my own sprite class in the list and it would also make sense to pass in pointers to sprites rather than the sprites themselves.

So here's what i've got,  I don't really know why this code is a problem as I copied it straight out of a book and don't see any problems with it unless it.  The bad access error occurs when i push_back the list and pass in the sprite.

sprite.h

class Sprite
    {
    protected:
        sf::Image image;
        sf::Sprite sprite;
 
    public:
        Sprite();
        ~Sprite();
       
        bool Load(std::string filename);
    };


main.cpp

    Sprite *sprite;
    sprite = new Sprite();
    sprite->Load("plane.png");
    sprite->Set(100,100);
    g_engine->AddSprite(sprite);

engine.h

std::list<Sprite*> sprites;

engine.cpp

void Engine::AddSprite(Sprite *sprite)
    {
        sprites.push_back(sprite); <- EXC_BAD_ACCESS
    }

4
Graphics / Sprite List Problem
« on: February 11, 2012, 10:49:28 am »
I'm having a problem implementing a sprite list for a game engine that I am making.  I have an abstract sprite class that holds a sf::Image and sf::Sprite object.  In my main file I initialize sprite objects as pointers to pass into the list in the engine class.  However, I get an access error when I try to load the  SFML image.  I know that the error comes from the sf::image going out of scope, but I was wondering how I could alter my code without sacrificing the use of the sprite handler using abstract Sprite objects.

Here is some code:

main.cpp

Sprite *sprite;
sprite->Load("plane.png");
sprite->Set(100,100);
g_engine->AddSprite(sprite);

sprite.h

class Sprite
    {
    protected:
sf::Image image;
sf::Sprite sprite;

public:
        Sprite();
        ~Sprite();
       
        bool Load(std::string filename);

        ...
}

sprite.cpp

bool Sprite::Load(std::string filename)
    {
        if(image.LoadFromFile(filename)) <- EXC_BAD_ACCESS
        {
            sprite.SetImage(image);
            SetWidth(image.GetWidth());
            SetHeight(image.GetHeight());
            return true;
        } else {
            return false;
        }
   }

engine.h

std::list<Sprite*> sprites;

Pages: [1]