Sign in to follow this  
Kenny77

SDL - Refreshing the screen

Recommended Posts

I'm having problems with SDL and trying to refresh the screen. When I run the code below, the ship just moves across the screen, and leaves a trail.
#include "SDL.h"

const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Sprites";

int main(int argc, char **argv)
{
   SDL_Init( SDL_INIT_VIDEO );
   
   SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0, 
                     SDL_HWSURFACE | SDL_DOUBLEBUF );
   
   SDL_WM_SetCaption( WINDOW_TITLE, 0 ); 
   
   SDL_Surface* ship = SDL_LoadBMP("ship.bmp");
   
   SDL_Rect shipPos;
   shipPos.x = 100;
   shipPos.y = 100;
   shipPos.w = 30;
   shipPos.h = 55;
   
   SDL_SetColorKey(screen, SDL_SRCCOLORKEY, SDL_MapRGB(screen->format, 0, 0, 0));
   
   SDL_Event event;
   
   bool gameRunning = true;
   
   while (gameRunning)
   {
         if (SDL_PollEvent(&event))
         {
            if (event.type == SDL_QUIT)
            {
               gameRunning = false;
            }
         }
         SDL_BlitSurface(ship, NULL, screen, &shipPos);
         
         if (shipPos.x < 610)
         {
            shipPos.x += 1;
         }
         SDL_Flip(screen);
      
         SDL_UpdateRect(screen, 0, 0, 0, 0);
   }
   SDL_FreeSurface(ship);

   SDL_Quit();

   return 0;
}

I've tried putting the line: SDL_UpdateRect(screen, 0, 0, 0, 0); in lines in the while loop, but it still has no effect.

Share this post


Link to post
Share on other sites
Clear the screen by filling it white using SDL_FillRect().

Also SDL_Flip() and SDL_UpdateRect() do the same thing in your code so there's no need to call them both.

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