/* Computer Graphics
 *
 * Filename ........ polys.h
 * Description ..... Functions to manage lists of polygons (header file)
 * Date ............ 19.08.2008
 * Created by ...... Jurgen Sturm
 * Cleaned up by ... Paul Melis
 *
 */

#ifndef POLYS_H
#define POLYS_H

#include <GL/gl.h>

#define MAX_VERTICES    6

typedef struct point
{
    GLdouble x;
    GLdouble y;
    GLdouble z;
}
point;

typedef struct poly
{
    int     points;
    point   pts[MAX_VERTICES];
    point   normal[MAX_VERTICES];   // Vertex normals
    point   pnormal;                // Overall polygon normal
    point   inside;
    GLfloat color[4];
}
poly;


typedef struct polys
{
    /* size of the poly array "items" below */
    int     capacity;

    /* number of poly's stored, also index of the first free position
     * in the "items" array. If this number is equal to "capacity" above,
     * then this list is full */
    int     length;

    /* poly pointer to array of size "capacity" */
    poly    *items;
}
polys;


/* Create a list of polys, and  immediately reserve space for `n' polys */
polys* CreatePolylist(int numpolys);

/* Destroy a list of polygons */
void DestroyPolylist(polys *list);


/* exit() if no memory available, returns 0 if poly* is NULL, 1 otherwise */
int IncreasePolylist(polys *list, int delta);

/* exit() if no memory available, returns 0 if poly* is NULL, 1 otherwise.
 * Decreasing the data segment can, and will, destroy polygons if the new
 * segment cannot contain all current data.
 * The data segment cannot be reduced to a size less than 0 (size is clamped
 * to zero).
 * The new data segment will be a NULL pointer if the new size is 0. */
int DecreasePolylist(polys *list, int delta);

/* poly's are structs of (point) arrays, so they are passed by value instead
 * of by reference (as do arrays)
 * Silently fails if `list' is a NULL pointer.
 * exit()'s if we need more memory but cannot allocate it. */
void AddPolyToPolylist(polys *list, poly p);

/* Copies the list of polys `list', calls AddPolytoPolylist() for each poly
 * in the given list.
 * exit()'s if it cannot allocate memory. */
polys* CopyPolylist(polys *list);

#endif
