# 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 mask
from sprite cimport Sprite
from texture cimport Texture

cdef class Mask:
    #TODO: fix this so that kwargs can be used!
    def __init__(self, item, xoffset = None, yoffset = None):
        assert self.handle is NULL
        
        if isinstance(item, Sprite):
            self.handle = sgMaskCreateSprite((<Sprite>item).handle)
        elif isinstance(item, Texture):
            if xoffset is None or yoffset is None:
                self.handle = sgMaskCreateTexture((<Texture>item).handle)
            else:
                self.handle = sgMaskCreateTexture2i((<Texture>item).handle, xoffset, yoffset)
        else:
            if xoffset is None or yoffset is None:
                self.handle = sgMaskCreateFile(item)
            else:
                self.handle = sgMaskCreateFile2i(item, xoffset, yoffset)
        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
        
        sgMaskDestroy(self.handle)
        self.handle = NULL
    
    cpdef checkCollision(self, SGint x1, SGint y1, Mask other, SGint x2, SGint y2):
        assert self.handle is not NULL
        
        return bool(sgMaskCheckCollision(self.handle, x1, y1, other.handle, x2, y2))
    
    cpdef getSize(self):
        assert self.handle is not NULL
        
        cdef SGuint width, height
        sgMaskGetSize(self.handle, &width, &height)
        return (width, height)
    cpdef SGuint getWidth(self):
        assert self.handle is not NULL
        
        return sgMaskGetWidth(self.handle)
    cpdef SGuint getHeight(self):
        assert self.handle is not NULL
        
        return sgMaskGetHeight(self.handle)
    
    cpdef drawDBG(self, SGint x, SGint y, SGbool transparent = 1):
        assert self.handle is not NULL
        
        sgMaskDrawDBG(self.handle, x, y, transparent)

