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

cdef class Viewport:
    cdef SGViewport* handle
    
    def __init__(self, wposx = None, wposy = None, wsizex = None, wsizey = None, posx = None, posy = None, sizex = None, sizey = None):
        assert self.handle is NULL
        
        if self.posx is None or self.posy is None or self.sizex is None or self.sizey is None:
            if self.wposx is None or self.wposy is None or self.wsizex is None or self.wsizey is None:
                self.handle = sgViewportCreate()
            else:
                self.handle = sgViewportCreate4i(wposx, wposy, wsizex, wsizey)
        else:
            self.handle = sgViewportCreate4i4f(wposx, wposy, wsizex, wsizey, posx, posy, sizex, sizey)
        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
        
        sgViewportDestroy(self.handle)
        self.handle = NULL
    
    def set(self, wposx = None, wposy = None, wsizex = None, wsizey = None, posx = None, posy = None, sizex = None, sizey = None):
        assert self.handle is not NULL
        
        if self.posx is None or self.posy is None or self.sizex is None or self.sizey is None:
            if self.wposx is None or self.wposy is None or self.wsizex is None or self.wsizey is None:
                sgViewportReset(self.handle)
            else:
                sgViewportSet4i(self.handle, wposx, wposy, wsizex, wsizey)
        else:
            sgViewportSet4i4f(self.handle, wposx, wposy, wsizex, wsizey, posx, posy, sizex, sizey)
    
    def reset(self):
        assert self.handle is not NULL
        
        sgViewportReset(self.handle)

