/Richter/TexMan.py
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *

from util import *
import config

sprites = {}

def load(file):
	global sprites
	if sprites.has_key(file):
		return sprites[file]

        image = pygame.image.load(file)
        w = image.get_width()
        h = image.get_height()
        tw = n2ceil(w)
        th = n2ceil(h)
	if config.square_textures:
		tw = th = max(tw,th)
        newImage = pygame.Surface((tw,th), SRCALPHA, 32)
        newImage.fill((255,255,255,0))
        newImage.blit(image, (0, th - h))	# We want this in the lower corner

	pixels = pygame.image.tostring(newImage, "RGBA", True)

        # Create Texture
        texture = glGenTextures(1)
	glBindTexture(GL_TEXTURE_2D, texture)
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tw, th, 0, GL_RGBA,
			GL_UNSIGNED_BYTE, pixels)
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE)

	sprites[file] = {'texture':texture,'size':(w,h),'tsize':(tw,th)}
	return sprites[file]