#include #include SDL_Surface* buffer = NULL; SDL_Surface* background = NULL; SDL_Surface* dot = NULL; SDL_Event event; SDL_Rect offset; int dx = 0; int dy = 0; int main(int argc, char* args[]) { SDL_Init(SDL_INIT_EVERYTHING); buffer = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE); background = SDL_LoadBMP("background.bmp"); dot = SDL_LoadBMP("dot.bmp"); offset.x = 300; offset.y = 200; int x = 0; int y = 0; // This is our target-position variables. Initialize it to the current location int toX = offset.x; int toY = offset.y; Uint8 *key = SDL_GetKeyState( NULL ); while (event.type != SDL_QUIT) { if( event.type == SDL_MOUSEMOTION ) { //Get the mouse offsets x = event.motion.x; y = event.motion.y; } if( event.type == SDL_MOUSEBUTTONDOWN ) { // The button has been pressed. if( event.button.button == SDL_BUTTON_LEFT ) { // We need to set our target x and y location as the current mouse location. We need to store this as seperate variables. // Think what would happen if we just used the x and y, and we wanted to check if we had reached the target location (which would then keep moving with the mouse) toX = x; toY = Y; dx = toX - offset.x; dy = toY - offset.y; } } // If we are not currently at the target location we need to move towards it if (offset.x != toX || offset.y != toY) { offset.x += dx / 20; offset.y += dy / 20; } SDL_BlitSurface(background, NULL, buffer, NULL); SDL_BlitSurface(dot, NULL, buffer, &offset); SDL_PollEvent(&event); SDL_Flip(buffer); } SDL_FreeSurface(background); SDL_FreeSurface(dot); SDL_Quit(); return 0; }