/*
    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 "draw.h"
#include "texture.h"

static PyObject* pysDrawBegin(PyObject* self, PyObject* args)
{
    SGenum type;
    PysTexture* texture = NULL;
    if(!PyArg_ParseTuple(args, "I|O", &type, &texture))
        return NULL;

    if(texture != NULL)
        sgDrawBeginT(type, texture->texture);
    else
        sgDrawBegin(type);

    Py_RETURN_NONE;
}
static PyObject* pysDrawEnd(PyObject* self)
{
    sgDrawEnd();
    Py_RETURN_NONE;
}
static PyObject* pysDrawColor(PyObject* self, PyObject* args)
{
    float r;
    float g = SG_NAN;
    float b = SG_NAN;
    float a = SG_NAN;
    if(!PyArg_ParseTuple(args, "f|fff", &r, &g, &b, &a))
        return NULL;

    if(g != g) // r
        sgDrawColor1f(r);
    else if(b != b) // r,g
        sgDrawColor2f(r, g);
    else if(a != a) // r,g,b
        sgDrawColor3f(r, g, b);
    else
        sgDrawColor4f(r, g, b, a);
    Py_RETURN_NONE;
}
static PyObject* pysDrawTexCoord(PyObject* self, PyObject* args)
{
    float x,y;
    if(!PyArg_ParseTuple(args, "ff", &x, &y))
        return NULL;

    sgDrawTexCoord2f(x, y);
    Py_RETURN_NONE;
}
static PyObject* pysDrawVertex(PyObject* self, PyObject* args)
{
    float x, y;
    float z = SG_NAN;
    if(!PyArg_ParseTuple(args, "ff|f", &x, &y, &z))
        return NULL;

    if(z != z)
        sgDrawVertex2f(x, y);
    else
        sgDrawVertex3f(x, y, z);
    Py_RETURN_NONE;
}
static PyObject* pysDrawClear(PyObject* self, PyObject* args)
{
    float r = SG_NAN;
    float g = SG_NAN;
    float b = SG_NAN;
    float a = SG_NAN;
    if(!PyArg_ParseTuple(args, "|ffff", &r, &g, &b, &a))
        return NULL;

    if(r != r) // none
        sgDrawClear();
    else if(g != g) // r
        sgDrawClear1f(r);
    else if(b != b) // r,g
        sgDrawClear2f(r, g);
    else if(a != a) // r,g,b
        sgDrawClear3f(r, g, b);
    else
        sgDrawClear4f(r, g, b, a);
    Py_RETURN_NONE;
}

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

    sgDrawPoint(x, y);
    Py_RETURN_NONE;
}
static PyObject* pysDrawPointSetSize(PyObject* self, PyObject* args)
{
    float size;
    if(!PyArg_ParseTuple(args, "f", &size))
        return NULL;

    sgDrawPointSetSize(size);
    Py_RETURN_NONE;
}

static PyObject* pysDrawLine(PyObject* self, PyObject* args)
{
    float x1, y1, x2, y2;
    if(!PyArg_ParseTuple(args, "ffff", &x1, &y1, &x2, &y2))
        return NULL;

    sgDrawLine(x1, y1, x2, y2);
    Py_RETURN_NONE;
}
static PyObject* pysDrawLineSetWidth(PyObject* self, PyObject* args)
{
    float width;
    if(!PyArg_ParseTuple(args, "f", &width))
        return NULL;

    sgDrawLineSetWidth(width);
    Py_RETURN_NONE;
}

static PyObject* pysDrawTriangle(PyObject* self, PyObject* args)
{
    float x1, y1, x2, y2, x3, y3;
    SGbool fill = SG_TRUE;
    if(!PyArg_ParseTuple(args, "ffffff|b", &x1, &y1, &x2, &y2, &x3, &y3, &fill))
        return NULL;

    sgDrawTriangle(x1, y1, x2, y2, x3, y3, fill);
    Py_RETURN_NONE;
}

static PyObject* pysDrawQuad(PyObject* self, PyObject* args)
{
    float x1, y1, x2, y2, x3, y3, x4, y4;
    SGbool fill = SG_TRUE;
    if(!PyArg_ParseTuple(args, "ffffffff|b", &x1, &y1, &x2, &y2, &x3, &y3, &x4, &y4, &fill))
        return NULL;

    sgDrawQuad(x1, y1, x2, y2, x3, y3, x4, y4, fill);
    Py_RETURN_NONE;
}

static PyObject* pysDrawRectangle(PyObject* self, PyObject* args)
{
    float x1, y1, x2, y2;
    SGbool fill = SG_TRUE;
    if(!PyArg_ParseTuple(args, "ffff|b", &x1, &y1, &x2, &y2, &fill))
        return NULL;

    sgDrawRectangle(x1, y1, x2, y2, fill);
    Py_RETURN_NONE;
}

static PyObject* pysDrawEllipse2R(PyObject* self, PyObject* args)
{
    float x, y, rx, ry;
    SGbool fill = SG_TRUE;
    if(!PyArg_ParseTuple(args, "ffff|b", &x, &y, &rx, &ry, &fill))
        return NULL;

    sgDrawEllipse2R(x, y, rx, ry, fill);
    Py_RETURN_NONE;
}

static PyObject* pysDrawCircle(PyObject* self, PyObject* args)
{
    float x, y, radius;
    SGbool fill = SG_TRUE;
    if(!PyArg_ParseTuple(args, "fff|b", &x, &y, &radius, &fill))
        return NULL;

    sgDrawCircle(x, y, radius, fill);
    Py_RETURN_NONE;
}

static PyMethodDef GraphicsDrawMethods[] = {
    {"begin", pysDrawBegin, METH_VARARGS, ""},
    {"end", pysDrawEnd, METH_NOARGS, ""},
    {"color", pysDrawColor, METH_VARARGS, ""},
    {"texCoord", pysDrawTexCoord, METH_VARARGS, ""},
    {"vertex", pysDrawVertex, METH_VARARGS, ""},
    {"clear", pysDrawClear, METH_VARARGS, ""},

    {"point", pysDrawPoint, METH_VARARGS, ""},
    {"pointSetSize", pysDrawPointSetSize, METH_VARARGS, ""},

    {"line", pysDrawLine, METH_VARARGS, ""},
    {"lineSetWidth", pysDrawLineSetWidth, METH_VARARGS, ""},

    {"triangle", pysDrawTriangle, METH_VARARGS, ""},

    {"quad", pysDrawQuad, METH_VARARGS, ""},

    {"rectangle", pysDrawRectangle, METH_VARARGS, ""},

    {"ellipse2R", pysDrawEllipse2R, METH_VARARGS, ""},

    {"circle", pysDrawCircle, METH_VARARGS, ""},
    {NULL, NULL, 0, NULL}
};

void sgpyInitGraphicsDraw(PyObject* parent)
{
    PyObject* draw = Py_InitModule("siege.graphics.draw", GraphicsDrawMethods);
    if(parent != NULL)
        PyModule_AddObject(parent, "draw", draw);
}
