/*Troy Rosin*/

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <mydebug.h>
#include <game.h>
#include <input.h>
#include <deck.h>
#include <main.h>
#include <overworld.h>
#include <err_check.h>

PLAYER *myplayer, me;

PLAYER *get_this_player()
{
  return &me;
}

#define ow_rows ((DEF_WINDOW_WIDTH / OW_GRIDSZ) + 1)
#define ow_cols ((DEF_WINDOW_HEIGHT / OW_GRIDSZ) + 1)

SDL_Rect ow_rects[ow_rows][ow_cols];


void start_overworld()
{
  SDL_Texture *selected_grid, *player_sprite, *grass;
  
  PRINT_M(STARTING GAME)
    
  IMG_GETANDCHECK(grass, renderer, "assets/ow_grid_grass.png")
  IMG_GETANDCHECK(selected_grid, renderer, "assets/ow_selected_grid.png");
  IMG_GETANDCHECK(player_sprite,  renderer, "assets/player.png");

  myplayer = get_this_player();
  
  int row, col;
  
  //set up rects to render textures into
  for(row = 0; row <= ow_rows; row++)
  {
    for(col = 0; col <= ow_cols; col++)
    {
      ow_rects[row][col].x = row * OW_GRIDSZ;
      ow_rects[row][col].y = col * OW_GRIDSZ;
      ow_rects[row][col].h = OW_GRIDSZ;
      ow_rects[row][col].w = OW_GRIDSZ;
    }      
  }

  myplayer->texture = player_sprite;
  myplayer->pos.x = ow_rows >> 1;
  myplayer->pos.y = ow_cols >> 1;
  

  while(running)
  {
    handle_event(&event);
    
    SDL_RenderClear(renderer);
    //render the ow grid
    for(row = 0; row < ow_rows; row++)
    {
      for(col = 0; col < ow_cols; col++)
      {
        SDL_RenderCopy(renderer, grass, NULL, &(ow_rects[row][col]));
      }
    }
    
      
 
    SDL_RenderCopy(renderer, myplayer->texture, NULL, &(ow_rects[myplayer->pos.x][myplayer->pos.y]));   

    SDL_RenderPresent(renderer);
  }
  



  start_battle();
  return;
}






