# Copyright (c) 2007 SIEGE Development Team
# All rights reserved.
#
# This file is part of libSIEGE.
#
# This software is copyrighted work licensed under the terms of the
# 2-clause BSD license. Please consult the file "license.txt" for
# details.
#
# If you did not recieve the file with this program, please email
# Tim Chas <darkuranium@gmail.com>.

from siege.common cimport *
cimport texture
from libc.stdlib cimport *

cdef class Texture:
    #TODO: fix this so that kwargs can be used!
    def __init__(self, *args):
        assert self.handle is NULL
        
        cdef SGubyte* cdata
        if len(args) == 1:
            self.handle = sgTextureCreateFile(args[0])
        elif len(args) == 4:
            cdata = <SGubyte*>malloc(len(args[3]) * sizeof(SGubyte))
            for i in xrange(0, len(args[3])):
                cdata[i] = args[3][i]
            self.handle = sgTextureCreateData(args[0], args[1], args[2], <void*>cdata)
            free(cdata)
        elif len(args) == 3:
            self.handle = sgTextureCreate(args[0], args[1], args[2])
        else:
            raise Exception('Invalid number of arguments for Texture constructor')
        if self.handle is NULL:
            raise MemoryError()

    def __dealloc__(self):
        if self.handle is not NULL:
            self.destroy()

    cpdef destroy(self):
        sgTextureDestroy(self.handle)
        self.handle = NULL

    def drawRads(self, x = None, y = None, z = 0.0, xscale = 1.0, yscale = 1.0, xoffset = 0.0, yoffset = 0.0, angle = 0.0):
        assert self.handle is not NULL
        
        if x is None or y is None:
            sgTextureDraw(self.handle)
        else:
            sgTextureDrawRads3f2f2f1f(self.handle, x, y, z, xscale, yscale, xoffset, yoffset, angle)
    def drawDegs(self, x = None, y = None, z = 0.0, xscale = 1.0, yscale = 1.0, xoffset = 0.0, yoffset = 0.0, angle = 0.0):
        assert self.handle is not NULL
        
        if x is None or y is None:
            sgTextureDraw(self.handle)
        else:
            sgTextureDrawDegs3f2f2f1f(self.handle, x, y, z, xscale, yscale, xoffset, yoffset, angle)

    def draw(self, x = None, y = None, z = 0.0, xscale = 1.0, yscale = 1.0, xoffset = 0.0, yoffset = 0.0):
        assert self.handle is not NULL
        
        if x is None or y is None:
            sgTextureDraw(self.handle)
        else:
            sgTextureDraw3f2f2f(self.handle, x, y, z, xscale, yscale, xoffset, yoffset)

    property size:
        def __get__(self):
            assert self.handle is not NULL
            
            cdef SGuint width, height
            sgTextureGetSize(self.handle, &width, &height)
            return (width, height)
    property width:
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTextureGetWidth(self.handle)
    property height:
        def __get__(self):
            assert self.handle is not NULL
            
            return sgTextureGetHeight(self.handle)
