Public Group

SDL sprite engine error

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

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&lt;CSprite&gt; 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 &lt; 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-&gt;Y() += -1; }
if ( keys[SDLK_DOWN] ) { gaurd-&gt;Y() += 1; }
if ( keys[SDLK_LEFT] ) { gaurd-&gt;X() += -1; }
if ( keys[SDLK_RIGHT] ) { gaurd-&gt;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 on other sites
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?

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 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 on other sites
Thanks alot, I suppose those were really stupid mistakes - thanks for helping.

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 on other sites
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]

• 16
• 9
• 13
• 41
• 15