Sign in to follow this  

SDL sprite engine error

This topic is 4102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hiya I am trying to make a Sprite Engine for my game and I am almost there. Although I have one problem: (Note: If you want I will post all my code but I know where the error is, so that would be a bit confusing)

//I make a global array of CSprites:

       std::vector<CSprite> SpriteList;

//I then add a CSprite to that array (gaurd is a pointer to CSprite):

       gaurd = new CSprite(new CSpriteBase("Images/Person"), screen);
       SpriteList.push_back(*gaurd);

//Then in my Draw Function I basicly go through the list and update/draw stuff:

       for (int i = 0; 1 < SpriteList.size(); i++)
       {
           SpriteList[i].clearBG();
           SpriteList[i].updateBG();
           SpriteList[i].draw();
       }

//Then in the action loop I update the gaurds position:

       keys = SDL_GetKeyState(NULL);
       if ( keys[SDLK_UP] ) { gaurd->Y() += -1; }
       if ( keys[SDLK_DOWN] ) { gaurd->Y() += 1; }
       if ( keys[SDLK_LEFT] ) { gaurd->X() += -1; }
       if ( keys[SDLK_RIGHT] ) { gaurd->X() +=1 ; }

Now my problem is this - if I run the program with "gaurd" added to the array the screen flickers black and then exits. If I run it with any other CSpite I have made (that I don't edit the position of) it runs fine but then doesn't show the sprite. Any help would be much appreciated. (just ask if you want the full code)

Share this post


Link to post
Share on other sites
for (int i = 0; 1 < SpriteList.size(); i++)

The 1 should be i.
As it stands your for loop will not run at all when it contains one (or less) sprites. When it contains more than one sprite it enters an infinite loop and then you start trying to access data beyond the boundary of the vector, which would explain why your program is bombing.

Share this post


Link to post
Share on other sites
Wow thanks - now I have another problem - the gaurd won't move around the screen. As I believe it I have:

- Made a pointer to a CSprite called gaurd before the "int main()" function.
- Done "gaurd = new CSprite(blah)" in the main function
- Added the CSprite it points to to the vector like this "SpriteList.push_back(*gaurd);"
- Changed its position in the event loop with "gaurd->X() += 1"

Any suggestions?

Share this post


Link to post
Share on other sites
What you need is pointers. At the moment SpriteList is a vector of CSprite objects . When you add a new CSprite object to SpriteList it doesn't add that actual object but a copy of it. Therefore changes made using gaurd won't affect the CSprite object in the vector, and vice versa.

I'm guessing that you have not yet studied pointers, so your CSprite::X() and CSprite::Y() functions probably don't do what you think they do either.

Look in to pointers and a solution should become clear [grin]

Share this post


Link to post
Share on other sites

This topic is 4102 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this