/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 * 
 * Source file to manage sprites (load, draw, ...)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 * 
 */

#include "stonage.h"
#include "game.h"
#include "level.h"
#include "image.h"
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <SDL.h>
#if defined(HAVE_SDL_SDL_IMAGE_H)
#include <SDL/SDL_image.h>
#endif

/* Please keep in sync with enum sprite_index */
struct enum_list level_sprites[]= {
    {"images/dinosaur.png",0},
    {"images/kinesist.png",0},
    {"images/kinesist2.png",0},
    {"images/key_red.png",KEY_RED},
    {"images/key_blue.png",KEY_BLUE},
    {"images/key_green.png",KEY_GREEN},
    {"images/need_key_red.png",NEED_KEY_RED},
    {"images/need_key_blue.png",NEED_KEY_BLUE},
    {"images/need_key_green.png",NEED_KEY_GREEN},
    {"images/hourglass.png",HOURGLASS},
    {NULL,0}
};

/**
 * Load all sprites
 * @param theme name of theme (which is a folder)
 * @param filename name of the file to load
 * @reurn a SDL_Surface or NULL for an error
 */
int 
Sprite_Load_All(const char *theme, struct t_level *l)
{
  int r=1;


  return r;
}

/**
 * Load sprites for dinosaur
 * @param theme name of theme (which is a folder)
 * @param l struct that will contains sprites of the dinosaur
 * @return 0 if we can't load sprites 
 */
int 
Sprite_Load_Dinosaur(const char *theme, struct t_dinosaur *dinosaur)
{
  int i,j;
  SDL_Surface *sprites_img;
  SDL_Rect r_src,r_dst;
  List *sprites[7];

  /* Load an image that contains all sprites in one line */
  sprites_img=Image_ThemeLoad(theme,"dinosaurs.png");
  if (sprites_img==NULL)
    return 0;
  
  r_src.x=0; r_src.y=0; r_src.w=32; r_src.h=32;
  r_dst.x=0; r_dst.y=0; r_dst.w=32; r_dst.h=32;

  for (j=0;j<7;j++)
   {
     sprites[j]=List_Alloc();
     for (i=0;i<8;i++)
      {
	SDL_Surface *spr = SDL_CreateRGBSurface(
	    SDL_HWSURFACE|SDL_SRCCOLORKEY,32,32,
	    sprites_img->format->BitsPerPixel,
	    sprites_img->format->Rmask, sprites_img->format->Gmask,
	    sprites_img->format->Bmask, sprites_img->format->Amask);
	if (spr==NULL)
	 { /* FIXME: Need to delete previous allocated SDL_Surface */
	   SDL_FreeSurface(sprites_img);
	   return 0;
	 }
	SDL_BlitSurface(sprites_img,&r_src,spr,&r_dst);
	r_src.x+=32;
	r_src.x+=32;
	List_Append(sprites[j],spr);
      }
   }

  dinosaur->walk_right=sprites[0];
  dinosaur->walk_left=sprites[1];
  dinosaur->walk_down=sprites[2];
  dinosaur->walk_up=sprites[3];
  dinosaur->sleeping=sprites[4];
  dinosaur->come_from_lowlevel=sprites[5];
  dinosaur->go_to_uplevel=sprites[6];

  SDL_FreeSurface(sprites_img);
  return 1;
}
  
#if 0

/**
 * Load all sprites
 * @param theme name of theme (which is a folder)
 * @param filename name of the file to load
 * @reurn a SDL_Surface or NULL for an error
 */
SDL_Surface *
Special_Sprite_Load_All(const char *theme, const char *filename)
{
  int i;
  char tmp[1024];
  SDL_Surface *surface=NULL;

  i=0;
  while (datadir_path[i])
   {
     snprintf(tmp,1024,"%s/squares/%s/%s",datadir_path[i],theme,filename);
     trace("Trying to load %s",tmp);
     surface = Square_Load_Real(tmp);
     if (surface)
       break;
     i++;
   }
  if (datadir_path[i]==NULL)
   {
     /* Give another chance, if the theme folder doesn't exist */
     if (strlen(theme)>0)
       return Square_Load("",filename);
     fprintf(stderr,"Can't load this square %s\n",filename);
     return NULL;
   }
  return surface;
}

/* We don't free the structure itself */
void Square_Free(struct t_square *s)
{
  int i;
  if (s!=NULL)
   {
     if (s->images!=NULL)
      {
	for (i=0;i<s->nimages;i++)
	  SDL_FreeSurface(s->images[i]);
	free(s->images);
      }
   }
}


/**
 *
 * Return a index of square who have the same properties.
 * This function parse the list of square to find a square who have the same
 * properties.
 * @param          s the square array
 * @param   nsquares the number of squares in the list
 * @param properties properties we try to find
 * @return returns the position in the list if we found a square, else return
 * -1 when not found.
 *
 */
int
Square_FindProperties(const struct t_square *s, int nsquares, uint32 properties)
{
  int i;

  for (i=0;i<nsquares;i++)
   {
     if (s->properties == properties)
       return i;
     s++;
   }
  return -1;
}




#endif

