# 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 shape
from body cimport Body
from libc.stdlib cimport *

cdef class Shape:        
    def __init__(self, Body body, SGenum type):
        assert self.handle is NULL
        
        self.handle = sgPhysicsShapeCreate(body.handle, type)
        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
        
        sgPhysicsShapeDestroy(self.handle)
        self.handle = NULL
    
    property group:
        def __set__(self, group):
            assert self.handle is not NULL
            
            sgPhysicsShapeSetGroup(self.handle, group)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgPhysicsShapeGetGroup(self.handle)
        def __del__(self):
            self.group = 0
    property friction:
        def __set__(self, friction):
            assert self.handle is not NULL
            
            sgPhysicsShapeSetFriction(self.handle, friction)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgPhysicsShapeGetFriction(self.handle)
        def __del__(self):
            self.friction = 0.0
    property restitution:
        def __set__(self, restitution):
            assert self.handle is not NULL
            
            sgPhysicsShapeSetRestitution(self.handle, restitution)
        def __get__(self):
            assert self.handle is not NULL
            
            return sgPhysicsShapeGetRestitution(self.handle)
        def __del__(self):
            self.restitution = 0.0
    property data:
        def __set__(self, data):
            assert self.handle is not NULL
            
            self.pydata = data
        def __get__(self):
            assert self.handle is not NULL
            
            return self.pydata
        def __del__(self):
            self.data = None
    
    def getAreaS(self):
        assert self.handle is not NULL
        
        return sgPhysicsShapeGetAreaS(self.handle)
    def getAreaU(self):
        assert self.handle is not NULL
        
        return sgPhysicsShapeGetAreaU(self.handle)
    
    def getMass(self, density):
        assert self.handle is not NULL
        
        return sgPhysicsShapeGetMass(self.handle, density)
    def getMomentMass(self, mass):
        assert self.handle is not NULL
        
        return sgPhysicsShapeGetMomentMass(self.handle, mass)
    def getMomentDensity(self, density):
        assert self.handle is not NULL
        
        return sgPhysicsShapeGetMomentDensity(self.handle, density)
    
    def getBBox(self):
        assert self.handle is not NULL
        
        cdef float t, l, b, r
        sgPhysicsShapeGetBBox(self.handle, &t, &l, &b, &r)
        return (t, l, b, r)
    
    def drawDBG(self):
        assert self.handle is not NULL
        
        sgPhysicsShapeDrawDBG(self.handle)

cdef class SegmentShape(Shape):
    def __init__(self, Body body, x1, y1, x2, y2, width):
        assert self.handle is NULL
        
        self.handle = sgPhysicsShapeCreateSegment(body.handle, x1, y1, x2, y2, width)
        if self.handle is NULL:
            raise MemoryError()

cdef class PolyShape(Shape):
    def __init__(self, Body body, x, y, verts):
        assert self.handle is NULL
        
        cdef float* cverts = <float*>malloc(len(verts) * sizeof(float))
        for i in xrange(0, len(verts)):
            cverts[i] = verts[i]
        self.handle = sgPhysicsShapeCreatePoly(body.handle, x, y, cverts, len(verts)/2)
        free(cverts)
        if self.handle is NULL:
            raise MemoryError()

cdef class CircleShape(Shape):
    def __init__(self, Body body, x, y, r1, r2):
        assert self.handle is NULL
        
        self.handle = sgPhysicsShapeCreateCircle(body.handle, x, y, r1, r2)
        if self.handle is NULL:
            raise MemoryError()

