moving a image

Started by
23 comments, last by randomZ 19 years, 4 months ago
a thanks.

The reason why I didnt noticed any differences was becuase I first had this code:
 keys = SDL_GetKeyState(NULL);if (event.type == SDL_KEYDOWN) {                if ( keys[SDLK_LEFT] ) {xpos -= 5; }                if ( keys[SDLK_RIGHT] ) {xpos += 5; }                if ( keys[SDLK_DOWN] ) {ypos += 5; }                if ( keys[SDLK_UP] ) {ypos -= 5; }}


And after I noticed that I forgot to remove the:

"
if ( keys[SDLK_LEFT] ) {xpos -= 5; }
if ( keys[SDLK_RIGHT] ) {xpos += 5; }
if ( keys[SDLK_DOWN] ) {ypos += 5; }
if ( keys[SDLK_UP] ) {ypos -= 5; }
"

from the PollEvent loop :D
So now it's working :P (I can now hold a button to move the object)
Advertisement
Ok, bump. I was reading this from the archives, but i have a question. When moving an image, isnt there some more efficient way than wiping the whole dang screen every loop pass? I mean, why not just tell it where you want the image to write over, and do it there instead?

How would you implement this, considering that the place that you want to fill in with black (where the surface 'used' to be) could be in any direction around your current surface.

Any ideas?
gib.son
sorry if I don't understand you're question, but you want to know if there is another way to clean the trail behind the image when it moves than clearing the whole screen and than draw everything again?

Well.. you should make some other variables: oldxpos and oldypos, that holds the coördinates before it's moved. Than you can draw the background on that old coordinates only so you don't need to draw the whole background again.

How it axactly works I dunno but it was somewere explain on cone3d.gamedev.net.


Blitting is very fast. It would actually be slower to redraw the background on where the trails are.
Rob Loach [Website] [Projects] [Contact]
However, if you draw a lot of stuff every frame, only clearing the parts that have changed last frame *might* make sense. This technique is called "dirty rects". You basically collect the rectangles that have been changed ("dirtied") in the last frame and redraw those.

You might want to google for it.

However, I should also note that in a large class of games (those that change their whole screen every frame), implementing this would only cost you lots of time and add overhead. For the rest of games, always updating the whole screen usually suffices, too.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/

This topic is closed to new replies.

Advertisement