#include "imagemanager.h"
#include "SDL_image.h"

void CImageManager::Init()
{
    /*
    SDL_Surface* temp = SDL_LoadBMP("explode_7.bmp");
    m_ImageMap["explosion1"] = SDL_DisplayFormat(temp);
	SDL_FreeSurface(temp);
	*/

	LoadImage("enemyplane1.bmp");
	LoadImage("playerplane.bmp");
	LoadImage("bullets.bmp");
	LoadImage("explode_7.bmp");
}

SDL_Surface* CImageManager::GetImage(std::string name)
{
    m_Itor = m_ImageMap.find(name);
    if (m_Itor != NULL)
        return m_Itor->second;
    return NULL;
}

void CImageManager::LoadImage( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized image that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized image
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old image
        SDL_FreeSurface( loadedImage );

        //If the image was optimized just fine
        if( optimizedImage != NULL )
        {
            //Map the color key
            Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 0xFF, 0, 0xFF );

            //Set all pixels of color R 0, G 0xFF, B 0xFF to be transparent
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
        }
    }

    //Store the optimized image
     m_ImageMap[filename] = optimizedImage;
}
