Problem with SDL animations

My problem is if i press a button to move the image doesnt go away. It leaves like a trail. Also, how do i make it so that if i hold down the button it will keep going. here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void Game::HandleEvents()
{
	while( SDL_PollEvent( &event ) )
	{
		if( event.type == SDL_QUIT )
		{
			nextState = STATE_EXIT;
		}
		if( event.type == SDL_KEYDOWN )
		{
			if( User.yPos > 0 && User.yPos < 480 )
			{
				switch( event.key.keysym.sym )
				{
				case SDLK_UP:
					User.yPos -= 1.f;
					break;
				case SDLK_DOWN:
					User.yPos += 1.f;
					break;
				default:
					break;
				}
				
			}
			ApplySurface( 50, User.yPos, userPaddle, screen );
			SDL_Flip( screen );
		}
	}
}
I tried doing this but it still doesnt work. Now the paddle doesnt move at all.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
void Game::HandleEvents()
{
	while( SDL_PollEvent( &event ) )
	{
		switch( event.type )
		{
		case SDL_QUIT:
			nextState = STATE_EXIT;
			break;
		case SDL_KEYDOWN:
			if( User.yPos > 0 && User.yPos < 480 )
			{
				switch( event.key.keysym.sym )
				{
				case SDLK_UP:
					User.moving = true;
					while( User.moving == true )
					{
						User.yPos -= 5.f;
					}
					break;
				case SDLK_DOWN:
					User.yPos += 5.f;
					break;
				default:
					break;
				}
			}
			break;
		case SDL_KEYUP:
			switch( event.key.keysym.sym )
			{
			case SDLK_UP:
				User.moving = false;
				break;
			case SDLK_DOWN:
				User.moving = false;
				break;
			}

		}
		SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
	}
}
You should not be doing anything drawing related in your event loop. Drawing should all be done separately.

Overall program flow usually goes like this:

1
2
3
4
5
6
7
8
while(game_is_running)
{
  HandleEvents();

  RunGameLogic();

  DrawScene();
}


To prevent "trails" from appearing, you would clear the scene every time you draw:

1
2
3
4
5
6
7
8
9
void DrawScene()
{
   ClearTheScreen();  // I forget the SDL function for this, but basically you wipe it to black
     // or whatever BG color you want

   DrawAllObjects();

   SDL_Flip();
}
Yeah i got that part. Now how do i make it so i can hold down the button instead having to keep pressing it?
Well if you've had a key down event about a particular key but not a key up event about that same key since the last key down event, you know that the key in question is currently held down. So you'll need to store a list of keys and whether they are up or down, then when you get a key up or key down event set that key's value, and every loop check whether or not a key is currently down.
quirky that was very confusing for me could you write that in pseudo code maybe?
Topic archived. No new replies allowed.