#from OpenGL.GL import *
#from OpenGL.GLU import *
#from OpenGL.GLUT import *
import ode, pygame, os
from pygame.locals import *
from math import sqrt
import os.path

def getConfigDir():
	return os.path.join(os.path.expanduser("~"),".kimboot")

def createConfigDir():
	p = os.path.join(os.path.expanduser("~"),".kimboot")
	if not (os.path.exists(p)):
		os.mkdir(p)
	return p

def resetSurface(surface,color):
    surface.fill(color)

def drawText(txt,color,bgcolor,pos):
    if pygame.font:
        font = pygame.font.Font("data/arial.ttf", 36)
        text = font.render(txt, 1, color, bgcolor)
        textpos = text.get_rect(center=pos)
        #surface.blit(text, textpos)
        return text, textpos
    
def drawText2(txt,size,color,bgcolor,pos):
    if pygame.font:
        font = pygame.font.Font("data/arial.ttf", size)
        text = font.render(txt, 1, color, bgcolor)
        textpos = text.get_rect(center=pos)
        #surface.blit(text, textpos)
        return text, textpos

def isDefined(var):
    try:
        var
    except NameError:
        var = None
    if var is None:
        return false
    else:
        return true
    
def load_image(name, colorkey=None):
    fullname = os.path.join('data', name)
    try:
        image = pygame.image.load(fullname)
    except pygame.error, message:
        print 'Nie udalo sie zaladowac obrazka:', 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)
    return image, image.get_rect()

def load_sound(name,opcje):
    class NoneSound:
        def play(self): pass
    if not pygame.mixer or not pygame.mixer.get_init():
        return NoneSound()
    fullname = os.path.join('data', name)
    try:
        sound = pygame.mixer.Sound(fullname)
    except pygame.error, message:
        print 'Nie udalo sie zaladowac dzwieku:', fullname
        raise SystemExit, message
    sound.set_volume((opcje["SoundVolume"])/100.0)
    return sound
    
def normalize(wektor):
    suma = 0
    for elem in wektor:
        suma += elem*elem
    suma = sqrt(suma)
    tmp = []
    for i in range(len(wektor)):
        tmp.append(wektor[i]/suma)
    return tmp
    
def combo(n):
    if (n==1):
        return 1
    else:
        return n+combo(n-1)
    
def wypisz(tekst,kolor,tlo,surface,przesuniecie):
    if pygame.font:
        font = pygame.font.Font("data/arial.ttf", 36)
        text = font.render(tekst, 1, kolor, tlo)
        textpos = text.get_rect(center=(surface.get_width()/2+przesuniecie,30))
        surface.blit(text, textpos)
        return text, textpos
        
def kasujNapis(tekst,tekstpos,surf):
    #tekst = text.get_rect()
    a = pygame.Surface((tekst.get_width(), tekst.get_height()))
    a.fill((250,250,250))
    surf.blit(a,tekstpos)
    
def CollisionCallback(args,geom1,geom2):
    contacts=ode.collide(geom1,geom2)
    world,contactgroup=args
    snd=0
    for c in contacts:
        c.setBounce(0.2)
        c.setMu(5000)
        j=ode.ContactJoint(world,contactgroup,c)
        b1 = geom1.getBody()
        b2 = geom2.getBody()
        if b1==None:
            b1=ode.environment
        if b2==None:
            b2=ode.environment
        j.attach(b1, b2)
        snd+=1
##        x,y,z = b1.getPosition()
##    if (len(contacts)>0):
##        tmpbl = Blood(world,space,x,y,(0,0,0),50)
##    if (snd>8):
##        bounce=pygame.mixer.Sound("sound.wav")
##        bounce.play()