text: /*
  ** File name :
  ** game.c
  ** File creator :
  ** Mathilde
  */
  
  #include "taquin.h"
  
  // Game principal loop
  uint8_t gameScene(taquin_t *taquin)
  {
      while (taquin->currentScene_ == GAME)
      {
          // Clear the renderer
          SDL_RenderClear(taquin->renderer_);
          // Catch events
          gameEvent(taquin);
          // Draw the game content
          gameDraw(taquin);
          // Refresh the renderer
          SDL_RenderPresent(taquin->renderer_);
          /*if (isVictory(taquin))
          {
              taquin->currentScene_ = EXIT;
          }*/
          //printf("blank index = %d\n", taquin->game_.board_.blankIndex_);
      }
      return EXIT_SUCCESS;
  }
  
  // Game event loop
  void gameEvent(taquin_t *taquin)
  {
      SDL_Event event;
      static int state = 0;
  
      SDL_PollEvent(&event);
      if(event.type == SDL_QUIT)
      {
          taquin->currentScene_ = EXIT;
      }
      if (event.type == SDL_KEYUP)
          state = 0;
      if (event.type == SDL_KEYDOWN && state == 0)
      {
          switch (event.key.keysym.sym)
          {
              case SDLK_UP:
                  moveTaquin(taquin, UP);
                  break;
              case SDLK_DOWN:
                  moveTaquin(taquin, DOWN);
                  break;
              case SDLK_LEFT:
                  moveTaquin(taquin, LEFT);
                  break;
              case SDLK_RIGHT:
                  moveTaquin(taquin, RIGHT);
                  break;
              default:
                  break;
          }
          state = 1;
      }
  }
  
  // Game draw loop
  void gameDraw(taquin_t *taquin)
  {
      SDL_Rect rect       = {0, 0, 0, 0};
      // Draw the game buttons
      for (uint8_t i = 0; i < NBR_OF_BUTTONS_IN_GAME; i++)
      {
          rect.x = taquin->game_.buttons_[i].x_;
          rect.y = taquin->game_.buttons_[i].y_;
          rect.w = taquin->game_.buttons_[i].width_;
          rect.h = taquin->game_.buttons_[i].height_;
          SDL_RenderCopy(taquin->renderer_, taquin->game_.buttons_[i].texture_, &taquin->game_.board_.images_[i].textureInfo_, &rect);
      }
      // Draw the game grid
      for (uint8_t i = 0; i < NBR_OF_IMAGES_IN_GAME; i++)
      {
          // Set the position of the image
          rect.x = taquin->game_.board_.images_[i].x_;
          rect.y = taquin->game_.board_.images_[i].y_;
          rect.w = taquin->game_.board_.images_[i].width_;
          rect.h = taquin->game_.board_.images_[i].height_;
          if (i != taquin->game_.board_.blankIndex_)
          {
              SDL_RenderCopy(taquin->renderer_, taquin->game_.board_.images_[i].texture_, &taquin->game_.board_.images_[i].textureInfo_, &rect);
          }
      }
  }
  
  // Move the taquin if possible according to the direction
  void moveTaquin(taquin_t *taquin, int direction)
  {
      uint8_t swap = 0;
      image_t *tmpImage = NULL;
  
      if (direction == UP)
      {
          if ((taquin->game_.board_.blankIndex_ - BOARD_SIZE) >= 0)
          {
              tmpImage = &taquin->game_.board_.images_[taquin->game_.board_.blankIndex_ - BOARD_SIZE];
          }
          swap = swapSquare(&taquin->game_.board_.images_[taquin->game_.board_.blankIndex_], tmpImage);
          taquin->game_.board_.blankIndex_ = ((swap) ? (taquin->game_.board_.blankIndex_ - BOARD_SIZE) : taquin->game_.board_.blankIndex_);
      }
      else if (direction == DOWN)
      {
          if (taquin->game_.board_.blankIndex_ + BOARD_SIZE < NBR_OF_IMAGES_IN_GAME)
          {
              tmpImage = &taquin->game_.board_.images_[taquin->game_.board_.blankIndex_ + BOARD_SIZE];
          }
          swap = swapSquare(&taquin->game_.board_.images_[taquin->game_.board_.blankIndex_], tmpImage);
          taquin->game_.board_.blankIndex_ = (swap) ? taquin->game_.board_.blankIndex_ + BOARD_SIZE : taquin->game_.board_.blankIndex_;
      }
      else if (direction == LEFT)
      {
          if (taquin->game_.board_.blankIndex_ % BOARD_SIZE != 0)
          {
              tmpImage = &taquin->game_.board_.images_[taquin->game_.board_.blankIndex_ - 1];
          }
          swap = swapSquare(&taquin->game_.board_.images_[taquin->game_.board_.blankIndex_], tmpImage);
          taquin->game_.board_.blankIndex_ = (swap) ? taquin->game_.board_.blankIndex_ - 1 : taquin->game_.board_.blankIndex_;
      }
      else if (direction == RIGHT)
      {
          if (taquin->game_.board_.blankIndex_ % BOARD_SIZE != BOARD_SIZE - 1)
          {
              tmpImage = &taquin->game_.board_.images_[taquin->game_.board_.blankIndex_ + 1];
          }
          swap = swapSquare(&taquin->game_.board_.images_[taquin->game_.board_.blankIndex_], tmpImage);
          taquin->game_.board_.blankIndex_ = (swap) ? taquin->game_.board_.blankIndex_ + 1 : taquin->game_.board_.blankIndex_;
      }
      else
      {
          return;
      }
  }
  
  uint8_t swapSquare(image_t *image1, image_t *image2)
  {
      SDL_Rect tmp = {0, 0, 0, 0};
  
      if (image1 == NULL || image2 == NULL)
      {
          return 0;
      }
      tmp.x = image1->textureInfo_.x;
      tmp.y = image1->textureInfo_.y;
  
      image1->textureInfo_.x = image2->textureInfo_.x;
      image1->textureInfo_.y = image2->textureInfo_.y;
  
      image2->textureInfo_.x = tmp.x;
      image2->textureInfo_.y = tmp.y;
  
      return 1;
  }
  