title: SDL2: How do I move an image along a square-like path?
body: <p>I'm trying to make a program that will run a simulation. I want the simulation to be able to move a small image, or sprite, along a set path, like moving from corner to corner of a square, as if it were drawing it. I would also like the simulation to be triggered on pressing a key on the keyboard, but I'm having some difficulties with refreshing the image after moving it.</p>
  
  <p>For example, I would like the image to move 50 pixels to the left, then 50 up, then 50 right, then 50 down, refreshing the image on the screen after each movement, after just one press of the '1' key on the keyboard.</p>
  
  <p>This is the code I have so far, which is largely pulled from a tutorial, and currently simply moves the image by 4 pixels on each press, and I'm unsure of how to progress from here:</p>
  
  <pre><code>int main(int argc, char ** argv)
  {
  // variables
  
  bool quit = false;
  SDL_Event event;
  int x = 360;
  int y = 240;
  
  // init SDL
  
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window * window = SDL_CreateWindow("SDL2 Keyboard/Mouse events", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, 0);
  SDL_Renderer * renderer = SDL_CreateRenderer(window, -1, 0);
  
  SDL_Surface * image = SDL_LoadBMP("Star.bmp");
  SDL_Texture * texture = SDL_CreateTextureFromSurface(renderer, image);
  SDL_FreeSurface(image);
  
  SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  
  // handle events
  
  while (!quit)
  { 
  
      switch (event.type)
      {
      case SDL_QUIT:
          quit = true;
          break;
  
      case SDL_KEYDOWN:
          switch (event.key.keysym.sym)
          {
  
          case SDLK_1:  
  
              x-=4; 
  
              break;
  
          case SDLK_2: 
  
              x+=4; 
  
              break;
  
          case SDLK_3:
  
              y-=4;
  
              break;
  
          case SDLK_4:
  
              y+=4;
  
              break;
  
          }
          break;
      }
  
     SDL_Rect dstrect = {x, y, 75, 75};
  
     SDL_RenderClear(renderer);
     SDL_RenderCopy(renderer, texture, NULL, &amp;dstrect);
     SDL_RenderPresent(renderer);
  }
  
  // cleanup SDL
  
  SDL_DestroyTexture(texture);
  SDL_DestroyRenderer(renderer);
  SDL_DestroyWindow(window);
  SDL_Quit();
  
  return 0;
  }
  </code></pre>