/*
 * Stonage: a puzzle game
 * Copyright (C) 2001 Luc Saillard <luc@saillard.org>
 * 
 * Source file to manage square (properties, 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 "square.h"
#include "game.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.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

static SDL_Surface *Square_Load_Real(char *filename);

struct enum_list square_properties_text2enum[] =
{
   {"CAN_MOVE_TO_NORTH", CAN_MOVE_TO_NORTH},
   {"CAN_MOVE_TO_SOUTH", CAN_MOVE_TO_SOUTH},
   {"CAN_MOVE_TO_EAST", CAN_MOVE_TO_EAST},
   {"CAN_MOVE_TO_WEST", CAN_MOVE_TO_WEST},
   {"AFTER_MOVE_CRASH", AFTER_MOVE_CRASH},
   {"START", START},
   {"FINISH", FINISH},
   {"CAN_GO_ABOVE", CAN_GO_ABOVE},
   {"KEY_RED", KEY_RED},	
   {"KEY_BLUE", KEY_BLUE},	
   {"KEY_GREEN", KEY_GREEN},	
   {"NEED_KEY_RED", NEED_KEY_RED},
   {"NEED_KEY_BLUE", NEED_KEY_BLUE},
   {"NEED_KEY_GREEN", NEED_KEY_GREEN},
   {"HOURGLASS", HOURGLASS},
   {"TELEPORT", TELEPORT},
   {0,0 }
};


static SDL_Surface *Square_Load_Real(char *filename)
{
  SDL_Surface *image, *image2;

#if defined(HAVE_SDL_SDL_IMAGE_H)
  image = IMG_Load(filename);
#else
  image = SDL_LoadBMP(filename);
#endif
  if (image==NULL)
    return NULL;
  image2 = SDL_DisplayFormat(image);
  SDL_FreeSurface(image);
  return image2;
}

/**
 * Load a square within a theme
 * @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 *
Square_Load(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;
}
