"""
load/unload textures
"""

from OpenGL.GL import *
from OpenGL.GLU import *
import pygame, pygame.image
from pygame.locals import *


gTexDict={}

def tex_id(name):
    if name is None:
        return 0
    return gTexDict[name]

def loadTexture(filename, shortname, reload=0):
    if reload==0:
        if shortname in gTexDict:
            return gTexDict[shortname]
        
    texIndex=glGenTextures(1)

    gTexDict[shortname]=texIndex

    textureSurface=pygame.image.load(filename)

    textureData = pygame.image.tostring(textureSurface, "RGBA", 1)
    glBindTexture(GL_TEXTURE_2D, texIndex )
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                 textureSurface.get_width(),
                 textureSurface.get_height(), 0,
                 GL_RGBA, GL_UNSIGNED_BYTE, textureData )

    gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA,
                      textureSurface.get_width(),
                      textureSurface.get_height(),
                      GL_RGBA,
                      GL_UNSIGNED_BYTE,
                      textureData)
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MAG_FILTER,
                    GL_LINEAR)
    glTexParameterf(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR)

    return texIndex


def releaseTexture(texIndex):
    assert(0)
    # should do this sooner or later
    
    
