"""Image/Tile loading and processing utilities."""
"""Adventures on Planet Zephulor by Aaron Hachez. 

   All code, artwork, and sound copyright 2003, Aaron Hachez.

   ----------------------------------------------------------------------------------------

   This file is part of "Adventures on Planet Zephulor".

   A copy of the GNU General Public License should be included with any
   redistribution of this program in a file named "COPYING.txt".

   "Adventures on Planet Zephulor" 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.

   "Adventures on Planet Zephulor" 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 "Adventures on Planet Zephulor"; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

   ----------------------------------------------------------------------------------------

   "Adventures on Planet Zephulor" uses the following libraries:

   PyGame
   SDL

   Both are at the time of this writing, licensed under the GNU LGPL. Please see
   the included file LGPL for details 
"""

import os, pygame
from pygame.locals import *

def loadimage(filename, colorkey=None):
    """Load image from file"""

    # Create string containing full path to file
    fullname = os.path.join('../data', filename)

    # Try to load it
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Cannot load image:', fullname
        raise SystemExit, message

    image = image.convert()

    # Set transparent color
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)

    return image


def loadtiles(filename, numtiles=-1, colorkey=-1, tilewidth=32, tileheight=32, top=0, left=0, bottom=0, right=0, border=1):
    """Load set of tiles of width/height with a border of "border", out of top/left, to bottom/right
    coordinates. If bottom/right not specified, image width/height is assumed.

    loadtiles(filename, numtiles=0, colorkey=-1, tilewidth=32, tileheight=32, top=0, left=0, bottom=0, right=0, border=1)"""

    # Save indent values
    leftindent = left

    # Set start past boarder
    left += border
    top += border

    # init counter
    i = 0

    # Load image and get dimensions
    image = loadimage(filename)
    imagewidth = image.get_width()
    imageheight = image.get_height()

    # If no bottom/height set
    if (bottom == 0):
        bottom = imageheight

    # If no right/width set
    if (right == 0):
        right = imagewidth

    # Create first surface in tile list, and setup
    tile_list = [pygame.Surface((tilewidth, tileheight))]
    tile_list[0].convert()
    tile_list[0].set_colorkey(colorkey, RLEACCEL)

    # While not at total height of image, and not past total number of tiles to load
    while not (top + tileheight + border) > bottom:

        # Blit current tile into current surface
        tile_list[i].blit(image, (0, 0), (left, top, tilewidth, tileheight))

        # If at the end of current row, else increment
        if((left + tilewidth + border) >= right):
            top += tileheight + border
            left = leftindent + border
        else:
            left += tilewidth + border

        # If numtiles was specified, and count is greater than num specified
        if (numtiles >= 0) and (i >= numtiles):
            break

        # Append new SDL surface onto tile list, and setup
        tile_list.append(pygame.Surface((tilewidth, tileheight)))
        tile_list[i + 1].convert()
        tile_list[i + 1].set_colorkey(colorkey, RLEACCEL)
        i += 1

    return tile_list

