Sign in to follow this  
namingway

SDL Help needed.

Recommended Posts

I'm trying ot make a maze game in SDL and whenever I move a turtle object its previous image remains and doesn't clear. So eventually the whol screen is covered with turtle images. Here's some of the code where I think the problem could be.
g_Turtle.assignSurface(playerA);
    
    
    while ( QUIT == false)
    {
          while(SDL_PollEvent(&event))
          {
              if (event.type == SDL_KEYDOWN)
              {
                  switch(event.key.keysym.sym)
                  {
                      case SDLK_RIGHT:
                          xvel = 1;
                          g_Turtle.assignSurface(playerA);
                          break;
                      case SDLK_LEFT:
                          xvel = -1;
                          g_Turtle.assignSurface(playerC);
                          break;
                      case SDLK_UP:
                          yvel = -1;
                          g_Turtle.assignSurface(playerB);
                          break;
                      case SDLK_DOWN:
                          yvel = 1;
                          g_Turtle.assignSurface(playerD);
                          break;
                  }
              }
              if (event.type == SDL_KEYUP)
              {
                  switch(event.key.keysym.sym)
                  {
                      case SDLK_RIGHT:
                          xvel = 0;
                          break;
                      case SDLK_LEFT:
                          xvel = 0;
                          break;
                      case SDLK_UP:
                          yvel = 0;
                          break;
                      case SDLK_DOWN:
                          yvel = 0;
                          break;
                  }
              }
              if (event.type == SDL_QUIT)
              {
                  QUIT = true;
              }
          }
          apply_surface(g_Turtle.GetX(), g_Turtle.GetY(), g_Turtle.GetSurface(), window);
          g_Turtle.Move(xvel, yvel);
          
          SDL_Flip(window);

Also the turtle moves really fast, I believe its because the fps is reaching 900+ is there a way to restrict the fps?

Share this post


Link to post
Share on other sites
This happens because the pixels in the turtles last position are not being updated. Draw over the section of the window where the turtle was with your background image/background colour, then draw the turtle in the new spot.

Also, to limit the frames per second you can implement a timer and update the screen on certain intervals, or use SDL_Delay()

Share this post


Link to post
Share on other sites
The turtle sprite needs to disappear before drawing the next turtle position otherwise you may get what seem like multiple turtles on the screen at one time. I had this problem in Pong that I'm working on but instead of turtles the paddles get really long when I move them. Like NerdInHisShoe said, apply the background where the turtle was at last or apply the entire background before calling SDL_Flip().

For the FPS problem, use a timer. For a quick and dirty solution, you can use SDL_Delay() and play around with some numbers for the parameter or you can use SDL_GetTicks() and save it in some variable. Check Lazy Foo's timer tutorials for a better solution.


// timer variable
Uint32 CurrentTime;

//game loop starts here
while(QUIT == false) {

CurrentTime = SDL_GetTicks();

// game logic goes here

...

// redraw the background where the turtle was positioned last or
// redraw the entire background
apply_surface(g_Turtle.GetX(),g_Turtle.GetY(), g_Background.GetSurface(),
window);

// reposition the turtle sprite
apply_surface(g_Turtle.GetX(), g_Turtle.GetY(), g_Turtle.GetSurface(), window);
g_Turtle.Move(xvel, yvel);

//update and check for errors
if ( SDL_Flip(window) == -1) return -1;

//delay for 10 ms while the game runs too fast
while(SDL_GetTicks()-CurrentTime <= 10)
{ } //do nothing

} //end game loop









Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this