/*
    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>.
*/

#include "viewport.h"

static void pysViewportDealloc(PysViewport* self)
{
    sgViewportDestroy(self->viewport);
    self->ob_type->tp_free(self);
}
static PyObject* pysViewportNew(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    PysViewport* self = (PysViewport*)type->tp_alloc(type, 0);
    if(self != NULL)
        self->viewport = NULL;
    return (PyObject*)self;
}
static int pysViewportInit(PysViewport* self, PyObject* args, PyObject* kwargs)
{
    SGuint wposx, wposy, wsizex, wsizey;
    SGfloat posx, posy, sizex, sizey;

    size_t size = PyTuple_Size(args);
    if(size == 8)
    {
        if(!PyArg_ParseTuple(args, "IIIIffff", &wposx, &wposy, &wsizex, &wsizey, &posx, &posy, &sizex, &sizey));
            return -1;
        self->viewport = sgViewportCreate4i4f(wposx, wposy, wsizex, wsizey, posx, posy, sizex, sizey);
    }
    else if(size == 4)
    {
        if(!PyArg_ParseTuple(args, "IIII", &wposx, &wposy, &wsizex, &wsizey));
            return -1;
        self->viewport = sgViewportCreate4i(wposx, wposy, wsizex, wsizey);
    }
    else if(size == 0)
        self->viewport = sgViewportCreate();
    else
        return -1;

    return 0;
}

static PyObject* pysViewportSet(PysViewport* self, PyObject* args)
{
    SGuint wposx, wposy, wsizex, wsizey;
    SGfloat posx, posy, sizex, sizey;

    size_t size = PyTuple_Size(args);
    if(size == 8)
    {
        if(!PyArg_ParseTuple(args, "IIIIffff", &wposx, &wposy, &wsizex, &wsizey, &posx, &posy, &sizex, &sizey));
            return NULL;
        sgViewportSet4i4f(self->viewport, wposx, wposy, wsizex, wsizey, posx, posy, sizex, sizey);
    }
    else if(size == 4)
    {
        if(!PyArg_ParseTuple(args, "IIII", &wposx, &wposy, &wsizex, &wsizey));
            return NULL;
        sgViewportSet4i(self->viewport, wposx, wposy, wsizex, wsizey);
    }
    else
        return NULL;
    Py_RETURN_NONE;
}
static PyObject* pysViewportReset(PysViewport* self)
{
    sgViewportReset(self->viewport);
    Py_RETURN_NONE;
}

static PyMethodDef PysViewportMethods[] = {
    {"set", (PyCFunction)pysViewportSet, METH_VARARGS, ""},
    {"reset", (PyCFunction)pysViewportReset, METH_NOARGS, ""},
    {NULL, NULL, 0, NULL}
};

static PyMemberDef PysViewportMembers[] = {
    {NULL, 0, 0, 0, NULL}
};

static PyTypeObject PysViewportType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "siege.graphics.Viewport",   /*tp_name*/
    sizeof(PysViewport),         /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)pysViewportDealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
    0,                         /*tp_call*/
    0,                         /*tp_str*/
    0,                         /*tp_getattro*/
    0,                         /*tp_setattro*/
    0,                         /*tp_as_buffer*/
    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
    "",                        /* tp_doc */
    0,		                   /* tp_traverse */
    0,		                   /* tp_clear */
    0,		                   /* tp_richcompare */
    0,		                   /* tp_weaklistoffset */
    0,		                   /* tp_iter */
    0,		                   /* tp_iternext */
    PysViewportMethods,          /* tp_methods */
    PysViewportMembers,          /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)pysViewportInit,   /* tp_init */
    0,                         /* tp_alloc */
    pysViewportNew,                 /* tp_new */
};

int pysViewportCheck(PyObject* o)
{
    return PyObject_TypeCheck(o, &PysViewportType);
}
void sgpyInitGraphicsViewport(PyObject* parent)
{
    PysViewportType.tp_new = PyType_GenericNew;
    if(PyType_Ready(&PysViewportType) < 0)
        return;

    Py_INCREF(&PysViewportType);
    PyModule_AddObject(parent, "Viewport", (PyObject*)&PysViewportType);
}
