SDL sprite engine error

Started by
5 comments, last by silverphyre673 17 years, 7 months ago
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.clearBG();
           SpriteList.updateBG();
           SpriteList.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)
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Advertisement
Could it be that Images/Person does not exist? You have no file type listed. Shouldn't it be Images/Person.bmp or something like that?
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.
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?
Whether you think you can or think you can’t, you’re probably right – Henry Ford
Thanks alot, I suppose those were really stupid mistakes - thanks for helping.
Whether you think you can or think you can’t, you’re probably right – Henry Ford
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]
Also, as your problem seems to have been solved to a reasonable degree, at least for now, please note that the word you are looking for is "Guard," not "Gaurd," unless I am much mistaken. [grin]
my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement