Jump to content
  • Advertisement
Sign in to follow this  
AlexFZ

Moving a cursor with SDL (Begininner issue)

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, I'm just starting out with SDL. I'm trying to make a clone of a standard RPG battle system just to learn SDL better. I have everything working, except I cannot get the cursor to move up and down when you press the up and down arrow keys. This is my code so far: (only relevant parts)
SDL_Surface* cursor = NULL;
struct CursorPos {
	int x;
	int y;
};

......

cursor = loadImage("cursor.jpg");
applySurface(7, 447, cursor, screen);
CursorPos cursorPos;
cursorPos.x = 7;
cursorPos.y = 447;

.......

// While the user hasn't quit...
	while ( !quit ) {
		// Keep getting event data while there's a queue
		while( SDL_PollEvent( &event ) )  {
			// If a key is pressed
			if (event.type == SDL_KEYDOWN) {
				switch (event.key.keysym.sym) {
				case SDLK_UP:
					// Can we go up?
					if (cursorPos.y != 447) {
						SDL_FreeSurface(cursor);
						cursorPos.y -= 30;
						applySurface(cursorPos.x, cursorPos.y, cursor, screen);
					}
					break;
				case SDLK_DOWN:
					// Can we go up?
					if (cursorPos.y != 327) {
						SDL_FreeSurface(cursor);
						cursorPos.y += 30;
						applySurface(cursorPos.x, cursorPos.y, cursor, screen);
					}
					break;
				}
				SDL_Flip(screen);
			}
			// If the user quits
			else if (event.type == SDL_QUIT) {
				quit = true;
			}

		}
	}
Obviously the problem is that the old cursor surface stays intact when the cursor moves up or down. I tried SDL_FreeSurface to get rid of it, but this causes errors (I'm guessing it deletes the pointer?). Is there a way I can get rid of the old cursor image before creating the new one? Or just move the current one to a new location? Any help would be much appreciated :)

Share this post


Link to post
Share on other sites
Advertisement
Don't free the surface; you don't need a new surface for every time you move a object.

I would recommend you separate your 'events' from your drawing code. It simplifies things a great deal.

You'll want your loop to look more like this: (On small projects, anyway. On larger projects you'd do things differently)
while( !quit ) {

while( SDL_PollEvent( &event ) ) {
//Put event related code here.
}

//Put draw related code here.
}



Now your problem is (as you know), when drawing the cursor, you need to 'undraw' (for lack of a better word) your old cursor from the previous frame.

How can we do that? Well, we can't go back in time and stop it from drawing, so what we need to do, is draw over it, to basically 'erase' it.

We can do that in a couple different ways; but the easiest (and most common) way is to just redraw the entire screen.

How you do this is first you wipe the entire screen clean, erasing everything you previously had drawn. Then you draw everything in their new locations. And finally, you tell your program to update the screen so we can see the changes.

To erase the screen, we use the SDL function called 'SDL_FillRect'. SDL_FillRect draws a rectangle of color on a image. If we use this function on the image called 'screen', we can draw a rectangle of color on the screen. If we make the rectange large enough to cover the entire screen, in basically cleans the screen so we can redraw everything.

SDL_FillRect(screen, NULL, 0x00);

The first parameter is the image we are drawing on (In this case, the screen). The second parameter is a pointer to a SDL_Rect, which describes how large the rectangle of color we want to draw is. By passing 'NULL' to this, SDL_FillRect defaults to draw over the entire image. The third parameter is a hexadecimal number, which is the color we want the rectangle to be. '0x00' is hexadecimal for 0 (zero) which with SDL on Windows is the color black.

while( !quit ) {
while( SDL_PollEvent( &event ) ) {
//Put event related code here.
}

//Erase the screen. (AKA: Draw black over everything)
SDL_FillRect(screen, NULL, 0x00);

//Anything else you are drawing, place here.

//Draw the cursor in it's new location.
applySurface(cursorPos.x, cursorPos.y, cursor, screen);

//Update the screen, so the changes show up in the window.
SDL_Flip(screen);
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!