import os.path
import pygame
from xml.dom.minidom import Node

from pygame.locals import *

def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print "Couldn't load image: %s" % (name)
        raise SystemExit, message

    #image = image.convert()
    image.convert_alpha()
    if colorkey is not None:
        if colorkey is -1:
            colorkey = image.get_at((0,0))
        image.set_colorkey(colorkey, RLEACCEL)
    return image, image.get_rect()

def load_font(name, size=13):
    fullname = os.path.join('data', name)
    try:
        font = pygame.font.Font(fullname, size)
    except pygame.error, message:
        print "Couldn't load font: %s" % (name)
        raise SystemExit, message
    return font

# XML Helper functions
# ----------------------------------------------------------------------------
def fetchTextFromNode(node):
    data = ""
    for subTag in node.childNodes:
        if subTag.nodeType == Node.TEXT_NODE:
            data += subTag.data
    return data

def fetchTextFromElementByTagName(element, tag):
    data = ""
    subElements = element.getElementsByTagName(tag)
    for node in subElements:
        data += fetchTextFromNode(node)

    return data

def fetchTextFromFirstElementByTagName(element, tag):
    subElements = element.getElementsByTagName(tag)
    if subElements:
        return fetchTextFromNode(subElements[0])
    else:
        return ''
# ----------------------------------------------------------------------------

