SDL Help needed.

Recommended Posts

namingway    114
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 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 on other sites
jesse007    158
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 variableUint32 CurrentTime; //game loop starts herewhile(QUIT == false) {CurrentTime = SDL_GetTicks();           // game logic goes here ...// redraw the background where the turtle was positioned last or // redraw the entire backgroundapply_surface(g_Turtle.GetX(),g_Turtle.GetY(), g_Background.GetSurface(),     window);              // reposition the turtle spriteapply_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