Trouble moving sprites

Started by
21 comments, last by CraazyDave 12 years, 9 months ago
You are passing a lot of things by value. For example in GameEngine::addSprite the sprite is passed by value which means that the sprite you have in addSprite is a copy of the sprite you passed from the main function. You may consider passing by pointer (Sprite* s) or by reference (Sprite& s). When you do spriteField.push_back(s) a copy of s will be stored in spriteField. You may want to store pointers to sprites in spriteField instead (std::vector<Sprite*>). The problem when using copies is that when you change the copy the original object will not be affected. You update the sprites in spriteField but blit the sprites in main which is not the same sprites. Another problem with making copies of large classes like Image is that it is probably slow, unless you have implemented something special.

I also noticed a few other things that is unrelated to the problem you have.

  • checkFPS() always return 33 (if we assume that the two calls to SDL_GetTicks() return the same value). The function looks more complicated so it looks like you trying to do something else.
  • in update() you call both SDL_Flip(screen) and SDL_UpdateRect(screen, 0, 0, 0, 0). These two calls do the same thing so one can be removed.
  • GameEngine::exit() is called from both events() and from main. Why not place the code that is in exit() in the the GameEngine destructor?
Advertisement

You are passing a lot of things by value. For example in GameEngine::addSprite the sprite is passed by value which means that the sprite you have in addSprite is a copy of the sprite you passed from the main function. You may consider passing by pointer (Sprite* s) or by reference (Sprite& s). When you do spriteField.push_back(s) a copy of s will be stored in spriteField. You may want to store pointers to sprites in spriteField instead (std::vector<Sprite*>). The problem when using copies is that when you change the copy the original object will not be affected. You update the sprites in spriteField but blit the sprites in main which is not the same sprites. Another problem with making copies of large classes like Image is that it is probably slow, unless you have implemented something special.

I also noticed a few other things that is unrelated to the problem you have.
  • checkFPS() always return 33 (if we assume that the two calls to SDL_GetTicks() return the same value). The function looks more complicated so it looks like you trying to do something else.
  • in update() you call both SDL_Flip(screen) and SDL_UpdateRect(screen, 0, 0, 0, 0). These two calls do the same thing so one can be removed.
  • GameEngine::exit() is called from both events() and from main. Why not place the code that is in exit() in the the GameEngine destructor?



Thanks for the tip! I am having a hard time figuring out pointers, are so used to coding in Java that I becomes difficult to use pointers for me. But you sure have a good point. I'll try to send the pointer to the things I am sending instead of a copy. And then see what happens.

And to other issues you mention. checkFps() should controll the framerate but if it allways returns 33 then It does not seem to do that.

The update function works, maybe I will change it later if I feel like it but it does not affect the program negatively as far as I know.

Good point about GameEngine::exit(), I'll do that
The update function works, maybe I will change it later if I feel like it but it does not affect the program negatively as far as I know.
Yes, the function works perfectly fine. The thing is that now update() takes about twice as long time to execute than if you removed one of the calls. This might slow your program down. Maybe it's not a problem for a simple game but I still think it's a bit wasteful for no good reason.

[quote name='Wooh' timestamp='1311775331' post='4841098']
You are passing a lot of things by value. For example in GameEngine::addSprite the sprite is passed by value which means that the sprite you have in addSprite is a copy of the sprite you passed from the main function. You may consider passing by pointer (Sprite* s) or by reference (Sprite& s). When you do spriteField.push_back(s) a copy of s will be stored in spriteField. You may want to store pointers to sprites in spriteField instead (std::vector<Sprite*>). The problem when using copies is that when you change the copy the original object will not be affected. You update the sprites in spriteField but blit the sprites in main which is not the same sprites. Another problem with making copies of large classes like Image is that it is probably slow, unless you have implemented something special.

I also noticed a few other things that is unrelated to the problem you have.
  • checkFPS() always return 33 (if we assume that the two calls to SDL_GetTicks() return the same value). The function looks more complicated so it looks like you trying to do something else.
  • in update() you call both SDL_Flip(screen) and SDL_UpdateRect(screen, 0, 0, 0, 0). These two calls do the same thing so one can be removed.
  • GameEngine::exit() is called from both events() and from main. Why not place the code that is in exit() in the the GameEngine destructor?



Thanks for the tip! I am having a hard time figuring out pointers, are so used to coding in Java that I becomes difficult to use pointers for me. But you sure have a good point. I'll try to send the pointer to the things I am sending instead of a copy. And then see what happens.

And to other issues you mention. checkFps() should controll the framerate but if it allways returns 33 then It does not seem to do that.

The update function works, maybe I will change it later if I feel like it but it does not affect the program negatively as far as I know.

Good point about GameEngine::exit(), I'll do that
[/quote]

Ok, I have fixed checkFPS() and done as you suggested with the update() and the deconstructor. It all works perfectly. Have barely started the major changes you suggest but I'm getting there.

[quote name='CraazyDave' timestamp='1312110681' post='4842804']The update function works, maybe I will change it later if I feel like it but it does not affect the program negatively as far as I know.
Yes, the function works perfectly fine. The thing is that now update() takes about twice as long time to execute than if you removed one of the calls. This might slow your program down. Maybe it's not a problem for a simple game but I still think it's a bit wasteful for no good reason.
[/quote]

I have done as you suggested with the update and deconstructor in GameEngine. It all works perfectly.

I Am about to start the major changes that you suggested.
I really wish that the problems would just end by using references instead of sending copies but sadly that is not the case.

Now I have memory issues regarding the vector, I can only guess that I am not removing the pointers correctly from it.
And I still can not get the sprites to work as they should.

This topic is closed to new replies.

Advertisement