/*
*     ___                       ___
*    / _ \___ _    _____ ____  / _ \_______ __    __
*   / ___/ _ \ |/|/ / -_) __/ / // / __/ _ `/ |/|/ /
*  /_/   \___/__,__/\__/_/   /____/_/  \_,_/|__,__/
*
*
*   Original Code by John Garrison
*   Written June 11,25 1999
*   To be released under the LGPL
*
*   PowerDraw initialization and misc. routines
*
*   See LICENSE.TXT for more LGPL info
*/

#include <stdlib.h>
#include <string.h> 

#include "SDL/SDL.h"
#include "powerdraw.h"

int BPP,
    SCREENWIDTH,
    SCREENHEIGHT;
TT_Engine truetype;
PD_Surface *Screen;


///////////////////////////////////////////////
//          INITIALIZATION ROUTINES          //
///////////////////////////////////////////////

/* internal - do not use */
int FilterEvents(const SDL_Event *event)
{
  if (event->type==SDL_QUIT) exit(0); else return(1);
}

/* Initialize all our variables and SDL */
int PD_Init()
{
   #ifdef USE_TTF
   /* Initialize the FreeType library */
   if ( TT_Init_FreeType(&truetype) ) {
      fprintf(stderr,"Couldn't init FreeType engine");
      exit(1);
   }
   #endif

   SDL_SetEventFilter(FilterEvents);
   atexit(SDL_Quit);

   return 0;
}



/* De-initialize all */
int PD_Quit()
{
   #ifdef USE_TTF
   TT_Done_FreeType(truetype);
   #endif

   return 0;
}


///////////////////////////////////////////////
//          WINDOW-MANAGER ROUTINES          //
///////////////////////////////////////////////

/* Function to set the window's title */
int PD_SetCaption (const char *Caption)
{
   SDL_WM_SetCaption(Caption, NULL);

   return 0;
}



/* Function to get the window's title */
const char *PD_GetCaption()
{
   char *caption;

   SDL_WM_GetCaption(&caption, NULL);

   return(caption);
}



/* Loads an Icon from an image file. This came straight from the SDL test programs */
PD_Icon PD_LoadIcon (char *File, int Type)
{
   PD_Icon Icon, null;
   SDL_Surface *icon;
   Uint8 *pixels;
   Uint8 *mask;
   int mlen, i;

   null.icon = NULL;
   null.mask = NULL;

   /* Load the icon surface */
   icon = SDL_LoadBMP(File);
   if (icon == NULL){
      fprintf(stderr, "Couldn't load %s: %s\n", File, SDL_GetError());
      return null;
   }

   /* Check width and height */
   if ((icon->w%8) != 0){
      fprintf(stderr, "Icon width must be a multiple of 8!\n");
      SDL_FreeSurface(icon);
      return null;
   }
   if ( icon->format->palette == NULL ) {
      fprintf(stderr, "Icon must have a palette!\n");
      SDL_FreeSurface(icon);
      return null;
   }

   /* Set the colorkey */
   SDL_SetColorKey(icon, SDL_SRCCOLORKEY, *((Uint8 *)icon->pixels));

   /* Create the mask */
   pixels = (Uint8 *)icon->pixels;
   mlen = icon->w*icon->h;
   mask = (Uint8 *)malloc(mlen/8);
   if (mask == NULL){
      fprintf(stderr, "Out of memory!\n");
      SDL_FreeSurface(icon);
      return null;
   }
   memset(mask, 0, mlen/8);
   for (i = 0; i < mlen;) {
      if (pixels[i] != *pixels)
         mask[i/8] |= 0x01;
      ++i;
      if ((i%8) != 0)
         mask[i/8] <<= 1;
   }

   Icon.icon = icon;
   Icon.mask = mask;

   return Icon;
}



/* Set the icon for the window manager */
int PD_SetIcon (PD_Icon *Icon)
{
   SDL_WM_SetIcon(Icon->icon, Icon->mask);

   return 0;
}