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

cdef class Space:
    def __init__(self):
        assert self.handle is NULL
        
        self.handle = sgPhysicsSpaceCreate()
        if self.handle is NULL:
            raise MemoryError()
        sgPhysicsSpaceSetData(self.handle, <void*>self)
    
    def __dealloc__(self):
        if self.handle is not NULL:
            self.destroy()
    
    cpdef destroy(self):
        assert self.handle is not NULL
        
        sgPhysicsSpaceDestroy(self.handle)
        self.handle = NULL
    
    def step(self, dt):
        assert self.handle is not NULL
        
        sgPhysicsSpaceStep(self.handle, dt)
    
    property iterations:
        def __set__(self, i):
            assert self.handle is not NULL
            
            sgPhysicsSpaceSetIterations(self.handle, i)
    property gravity:
        def __set__(self, g):
            assert self.handle is not NULL
        
            sgPhysicsSpaceSetGravity(self.handle, g[0], g[1])
    property damping:
        def __set__(self, d):
            assert self.handle is not NULL
            
            sgPhysicsSpaceSetDamping(self.handle, d)
    
    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
    
    property staticBody:
        def __get__(self):
            assert self.handle is not NULL
    
    @classmethod
    def getDefault(cls):
        return DefaultSpace()

cdef class DefaultSpace(Space):
    def __init__(self):
        assert self.handle is NULL
        
        self.handle = sgPhysicsSpaceGetDefault()
    
    cpdef destroy(self):
        assert self.handle is not NULL
        self.handle = NULL
