/*
   loadimg - Function to load an image from file using SDL_image
   Copyright (C) 2002 John Ericson
*/

#include "loadimg.h"

/* 
When loading PNG images you dont have to set transparent=true,
If the Image is transparent it will be by default.
*/
SDL_Surface *loadimage(char *file, bool transparent) {

   SDL_Surface *image, *rtnsurface;


   image = IMG_Load(file);
   if (image == NULL) {
      fprintf(stderr, "Error couldn't load image %s: %s\n",
         file, IMG_GetError());
      return NULL;
   }

   if (transparent) {
      /* Assuming 8-bit image */
      /* We use the color of the first pixel (Upper left corner)
         to decide which color to make transparent */
      SDL_SetColorKey(image, (SDL_SRCCOLORKEY|SDL_RLEACCEL),
         *(Uint8 *)image->pixels);
   }

   rtnsurface = SDL_DisplayFormat(image);
   if (rtnsurface == NULL) {
      /* The conversion fails or runs out of memory */
      fprintf(stderr, "Image Conversion failed\n");
      return NULL;
   }

   SDL_FreeSurface(image);

   return rtnsurface;
}