/** paintMapConverter
  *
  * 2008 blueapple
  * http://blueappledev.wordpress.com
  *
  */

#include <SDL.h>
#include <fstream>
#include <windows.h>

// Define the tile types
enum E_TILE
{
    T_ERROR = -1,
    T_EMPTY,
    T_WATER,
    T_DIRT,
    T_GOAL,
    T_WALL,
};

SDL_Color GetPixel(SDL_Surface* pSurface, int x, int y);

int main( int argc, char** argv )
{
    std::string input;  // image file that "goes in"
    std::string output; // data file that "goes out"
    std::string desc;   // level description

    AllocConsole();    // output to console
    freopen("CONOUT$", "wb", stdout);

    printf("paintMapConverter 0.1\nBy blueapple\n\n");

    if(argc != 4)
    {
        printf("Usage:\n    paint_mapconverter.exe input.bmp output.map \"Description goes here\"\n\n");

        return 0;
    }

    input   = argv[1]; // Get the data
    output  = argv[2];
    desc    = argv[3];

    if(SDL_Init( SDL_INIT_VIDEO ) < 0)
    {
        printf( "Unable to init SDL: %s\n", SDL_GetError() );
        return -1;
    }

    atexit(SDL_Quit);

    SDL_Surface* bmp = SDL_LoadBMP(input.c_str());
    if(!bmp)
    {
        printf("Unable to load bitmap: %s\n", SDL_GetError());
        return -1;
    }

    // Create a temporary tilemap
    E_TILE map[bmp->w][bmp->h];

    // Read from the imagefile
    for(int y = 0; y < bmp->h; y++)
    {
        for(int x = 0; x < bmp->w; x++)
        {
            SDL_Color color = GetPixel(bmp, x, y);

            if(color.r == 0 && color.g == 0 && color.b == 0) // These are for you to define
                map[x][y] = T_WALL;
            else if(color.r == 128 && color.g == 64 && color.b == 0)
                map[x][y] = T_DIRT;
            else if(color.r == 255 && color.g == 255 && color.b == 0)
                map[x][y] = T_GOAL;
            else if(color.r == 0 && color.g == 0 && color.b == 255)
                map[x][y] = T_WATER;
            else
                map[x][y] = T_EMPTY;
        }
    }

    // Output to file
    std::ofstream outfile;
    outfile.open(output.c_str());
    if(!outfile)
    {
        printf("Could not open %s!\n", output.c_str());
        return 0;
    }

    outfile << "# Example Map\n" << "# Do not edit manually\n\n"; // Add a header (could contain version information etc)
    outfile << desc << "\n\n"; //output the description

    for(int y = 0; y < bmp->h; y++) // now output the map
    {
        for(int x = 0; x < bmp->w; x++)
        {
            if(map[x][y] != T_EMPTY) // we do not need the empty tiles
                outfile << x << " " << y << " " << map[x][y] << "\n";
        }
    }

    outfile.close();

    SDL_FreeSurface(bmp);

    printf("%s created successfully!\n", output.c_str());

    fclose(stdout);
    FreeConsole();

    return 0;
}

SDL_Color GetPixel(SDL_Surface* pSurface, int x, int y)
{
  SDL_Color color ;
  Uint32 col = 0 ;

  char* pPosition = ( char* ) pSurface->pixels ;

  pPosition += ( pSurface->pitch * y ) ;
  pPosition += ( pSurface->format->BytesPerPixel * x ) ;

  memcpy ( &col , pPosition , pSurface->format->BytesPerPixel ) ;

  SDL_GetRGB ( col , pSurface->format , &color.r , &color.g , &color.b ) ;
  return ( color ) ;
}
