#! /usr/bin/env python

"""
Copyright 2008 Ben Sarsgard

    This file is part of ZedZed.

    ZedZed 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 3 of the License, or
    (at your option) any later version.

    ZedZed 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 ZedZed.  If not, see <http://www.gnu.org/licenses/>.
"""

import os, sys
import pygame
from pygame.locals import *

stored_images = {}

def load_image(name, colorkey=None, brightness=1.0):
	image = None
	fullname = os.path.join('data', 'images/Iso')
	fullname = os.path.join(fullname, name)
	
	if stored_images.has_key((fullname, colorkey, brightness)):
		image = stored_images[(fullname, colorkey, brightness)]
	
	if image is None:
		try:
			image = pygame.image.load(fullname)
		except pygame.error, message:
			print 'Cannot load image:', fullname
			raise SystemExit, message
		image = image.convert()
		if colorkey is not None:
			if colorkey is -1:
				colorkey = image.get_at((0,0))
			image.set_colorkey(colorkey, RLEACCEL)
		stored_images[(fullname, colorkey, brightness)] = image
	
	return image
	
def load_text(text, font, color, x, y, background=None, alpha=None):
	if background == None:
		the_text = font.render(text, 1, color)
	else:
		the_text = font.render(text, 1, color, background)
	if alpha is not None:
		the_text.set_alpha(alpha)
	rect = the_text.get_rect(x = x, y = y)
	
	return the_text, rect
	
def load_shadow_text(text, font, color, x, y, background=None, alpha=None):
	the_text, rect = load_text(text, font, color, x, y, background, alpha)
	shadow_text, shadow_rect = load_text(text, font, (0,0,0), x+1, y+1, background, alpha)
	
	return the_text, rect, shadow_text, shadow_rect
	
def get_bresen(board, start_x, start_y, end_x, end_y):
	bl = [(start_x, start_y)]
	steep = abs(end_y - start_y) > abs(end_x - start_x)

	if steep:
		# we need to flip the points
		tmp_x = start_x
		tmp_y = start_y
		start_x = tmp_y
		start_y = tmp_x

		tmp_x = end_x
		tmp_y = end_y
		end_x = tmp_y
		end_y = tmp_x

	delta_x = abs(end_x - start_x)
	delta_y = abs(end_y - start_y)
	error = 0
	delta_error = delta_y
	y_step = 0
	x_step = 0
	yy = start_y
	xx = start_x

	if (start_y < end_y):
		y_step = 1
	else:
		y_step = -1

	if (start_x < end_x):
		x_step = 1
	else:
		x_step = -1

	tmp_x = 0
	tmp_y = 0

	while (xx != end_x):
		xx += x_step
		error += delta_error

		# if the error exceeds the X delta then
		# move one along on the Y axis
		if (2 * error) > delta_x:
			yy += y_step
			error -= delta_x

		#flip the coords if they're steep
		if steep:
			tmp_x = yy
			tmp_y = xx
		else:
			tmp_x = xx
			tmp_y = yy

		# check the point generated is legal
		# and if it is add it to the list
		bl.append((tmp_x, tmp_y))
		if check_point(board, tmp_x, tmp_y) == False:
			# a bad point has been found, so return the list thus far
			return bl, False
	
	# we made it, return the line
	return bl, True

def check_point(board, point_x, point_y):
	# check coords are legal
	#print (point_x, point_y)
	if point_x >= 0 and point_x < len(board[0]) and point_y >= 0 and point_y < len(board):
		# if the cell is empty return true
		if (board[point_y][point_x] == 0):
			return True
		else:
			return False
	else:
		# not legal coords
		return False
