# 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 surface

cdef class Surface:
    #TODO: fix this so that kwargs can be used!
    def __init__(self, *args):
        assert self.handle is NULL
        
        cdef void* data
        if len(args) == 1:
            self.handle = sgSurfaceCreateFile(args[0])
        elif len(args) == 4:
            assert 0, 'Creating surface with data is currently unimplemented'
            self.handle = sgSurfaceCreateData(args[0], args[1], args[2], <void*>data)
        elif len(args) == 3:
            self.handle = sgSurfaceCreate(args[0], args[1], args[2])
        else:
            raise Exception('Invalid number of arguments for Surface constructor')
        if self.handle is NULL:
            raise MemoryError()

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

    cpdef destroy(self):
        assert self.handle is not NULL
        
        sgSurfaceDestroy(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:
            sgSurfaceDraw(self.handle)
        else:
            sgSurfaceDrawRads3f2f2f1f(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:
            sgSurfaceDraw(self.handle)
        else:
            sgSurfaceDrawDegs3f2f2f1f(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:
            sgSurfaceDraw(self.handle)
        else:
            sgSurfaceDraw3f2f2f(self.handle, x, y, z, xscale, yscale, xoffset, yoffset)

    def target(self):
        assert self.handle is not NULL
        
        sgSurfaceTarget(self.handle)
    def untarget(self):
        assert self.handle is not NULL
        
        sgSurfaceUntarget(self.handle)

    def clearf(self, r = None, g = None, b = None, a = None):
        assert self.handle is not NULL
        
        if a is None:
            if b is None:
                if g is None:
                    if r is None:
                        sgSurfaceClear(self.handle)
                    else:
                        sgSurfaceClear1f(self.handle, r)
                else:
                    sgSurfaceClear2f(self.handle, r, g)
            else:
                sgSurfaceClear3f(self.handle, r, g, b)
        else:
            sgSurfaceClear4f(self.handle, r, g, b, a)

    def clearub(self, r = None, g = None, b = None, a = None):
        assert self.handle is not NULL
        
        if a is None:
            if b is None:
                if g is None:
                    if r is None:
                        sgSurfaceClear(self.handle)
                    else:
                        sgSurfaceClear1ub(self.handle, r)
                else:
                    sgSurfaceClear2ub(self.handle, r, g)
            else:
                sgSurfaceClear3ub(self.handle, r, g, b)
        else:
            sgSurfaceClear4ub(self.handle, r, g, b, a)

    def clear(self):
        assert self.handle is not NULL
        
        sgSurfaceClear(self.handle)

    property size:
        def __get__(self):
            assert self.handle is not NULL
            
            cdef SGuint width, height
            sgSurfaceGetSize(self.handle, &width, &height)
            return (width, height)
    property width:
        def __get__(self):
            assert self.handle is not NULL
            
            return sgSurfaceGetWidth(self.handle)
    property height:
        def __get__(self):
            assert self.handle is not NULL
            
            return sgSurfaceGetHeight(self.handle)
