Moving an image

I’ve tried evrythhing and it still won’t move.So will someone explain to how
to move an image

Randy wrote:

I’ve tried evrythhing and it still won’t move.So will someone explain to how
to move an image

Basically, in SDL and in games in general, you should remember that you
don’t really move anything. Remember that the game is made up of frames,
each of which is drawn anew to the monitor.

What you think of as movement is actually drawing the image at one
position in the first frame, and at a slightly different position in the
next - like a flip-book. So moving an image in looks something like this:

int x = 100; // Our image’s starting position is 100/100
int y = 100;
bool done = false;

// Every time this while loop is executed, we draw one frame.
while(!done) {
SDL_event event;

// Now we process all events that SDL has pending for us
while(SDL_PollEvent(&event)) {
	switch(event.type) {
		case SDL_KEYDOWN: // A keypress
			switch(event.key.keysym.sym) {
				case SDLK_ESC:
					//Exit the main loop
					done = true;
					break;
				case SDLK_UP:
					// Move the image 1px up
					y -= 1;
					break;
				case SDLK_DOWN:
					y += 1;
					break;
				case SDLK_LEFT:
					x -= 1;
					break;
				case SDLK_RIGHT:
					x += 1;
					break;
			}
		break;
	}
}

SDL_Rect position; // This rect will hold the position of our
		   // image. Only the x and y values are used.
position.x = x;
position.y = y;

SDL_Rect screenrect = screen->clip_rect; // This rect contains
					 // the screen size

// Now we create a black color
Uint32 black = SDL_MapRGB(screen->format, // We need to use the
					  // same format as the
					  // screen
			  0, 0, 0);       // R, G, B

SDL_FillRect(screen, screenrect, black); // Fill the whole
					 // screen black

SDL_BlitSurface(image,  // Of course you need to load it first
		NULL,   // We don't use this
		screen, // You've hopefully initialized screen
		        // with SDL_SetVideoMode before
		&position);
SDL_Flip(screen); // Update the screen

}

// END CODE

This is written off the top of my head, I haven’t tested it. If it
doesn’t work, please complain ^^

The important steps to do every frame are:

  • Calculate image position (done here in the event loop)
  • Clear the screen
  • Draw the image
  • Flip the screen

HTH,
Sebastian

I frogot to mention that I was trying to use time-based movment.

Randy wrote:

I frogot to mention that I was trying to use time-based movment.

There’s not a lot of info to go on here, but I could take a wild stab at
it. There are two things I’ve noticed that cause people problems when
they get started:

One common mistake I find myself repeating with time-based animations is
this: I define my velocities as X per second (pixels per second,
degrees per second, etc). Then when I calculate how far something has
moved, I use something like:

value += velocity * SDL_GetTicks()/1000; // Advance an objects position
based on how many seconds have passed

The problem with that code is that 1000 is an integer, and so is the
return value of SDL_GetTicks(). Dividing two integers in C returns a
rounded integer. So if less than a second has passed (which is usually
the case), say 500 milliseconds, the 500/1000 rounds down to 0. So no
matter how much time passes, you don’t see movement. The way to fix
that is to remeber to force floating-point math when dealing with fractions:

value += velocity * SDL_GetTicks()/1000.0; // / =

That adds (velocity * .5) instead of adding (velocity * 0).

The other common problem is updating your screen. If the screen isn’t
being updated correctly, the code might be executing to draw the object
in new places, but the screen doesn’t show any change because it’s not
being updated with something like SDL_Flip() or SDL_UpdateRects() after
each frame.

Those are the only common things I know of that match up with what
you’ve described so far. If it’s not one of those things, you’ll have
to give a lot more detail about the problem. And it never hurts to send
some sample code that shows what you’re doing.

Ryan

So are you saying that it is better to use frame dependant movement?
Can you direct me to a good tutorial that works?