/*
    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 "trail.h"

static void pysTrailDealloc(PysTrail* self)
{
    sgTrailDestroy(self->trail);
    self->ob_type->tp_free(self);
}
static PyObject* pysTrailNew(PyTypeObject *type, PyObject *args, PyObject *kwargs)
{
    PysTrail* self = (PysTrail*)type->tp_alloc(type, 0);
    if(self != NULL)
        self->trail = NULL;
    return (PyObject*)self;
}
static int pysTrailInit(PysTrail* self, PyObject* args, PyObject* kwargs)
{
    SGuint maxpoints;
    if(!PyArg_ParseTuple(args, "I", &maxpoints));
        return -1;

    self->trail = sgTrailCreate(maxpoints);
    return 0;
}

static PyObject* pysTrailAddPoint(PysTrail* self, PyObject* args)
{
    float x, y;
    if(!PyArg_ParseTuple(args, "ff", &x, &y))
        return NULL;

    sgTrailAddPoint2f(self->trail, x, y);
    Py_RETURN_NONE;
}

static PyObject* pysTrailAddBreak(PysTrail* self)
{
    sgTrailAddBreak(self->trail);
    Py_RETURN_NONE;
}

static PyObject* pysTrailDraw(PysTrail* self)
{
    sgTrailDraw(self->trail);
    Py_RETURN_NONE;
}

static PyMethodDef PysTrailMethods[] = {
    {"addPoint", (PyCFunction)pysTrailAddPoint, METH_VARARGS, ""},
    {"addBreak", (PyCFunction)pysTrailAddBreak, METH_NOARGS, ""},
    {"draw", (PyCFunction)pysTrailDraw, METH_NOARGS, ""},
    {NULL, NULL, 0, NULL}
};

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

static PyTypeObject PysTrailType = {
    PyObject_HEAD_INIT(NULL)
    0,                         /*ob_size*/
    "siege.graphics.Trail",   /*tp_name*/
    sizeof(PysTrail),         /*tp_basicsize*/
    0,                         /*tp_itemsize*/
    (destructor)pysTrailDealloc, /*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 */
    PysTrailMethods,          /* tp_methods */
    PysTrailMembers,          /* tp_members */
    0,                         /* tp_getset */
    0,                         /* tp_base */
    0,                         /* tp_dict */
    0,                         /* tp_descr_get */
    0,                         /* tp_descr_set */
    0,                         /* tp_dictoffset */
    (initproc)pysTrailInit,   /* tp_init */
    0,                         /* tp_alloc */
    pysTrailNew,                 /* tp_new */
};

int pysTrailCheck(PyObject* o)
{
    return PyObject_TypeCheck(o, &PysTrailType);
}
void sgpyInitGraphicsTrail(PyObject* parent)
{
    PysTrailType.tp_new = PyType_GenericNew;
    if(PyType_Ready(&PysTrailType) < 0)
        return;

    Py_INCREF(&PysTrailType);
    PyModule_AddObject(parent, "Trail", (PyObject*)&PysTrailType);
}
