/* -*- C++ -*- */

#ifndef _FIGURE_H_
#define _FIGURE_H_

#include "Draw3D.h"


/** Manages the displaying and manipulating of figures build up of
 * cubes of the size 1,1,1.
 */
class Figure
{
public:
	/** Surrounding border. */
	Figure (const int _sizeX,
		const int _sizeY,
		const char _sizeZ);

	/** Creates a copy of the cubes. */
	Figure (const Figure& fig);

	virtual ~Figure();

	/** Adds a cube with the color color to position pos.
	 * No check is done if there already exists a cube
	 * -  causes memory leak if so -.
	 *
	 * Cubes are oriented from pos to
	 *  pos.getX+1, pos.getY+1, pos.getZ+ 1.
	 *
	 * On clear the color is deleted.
	 */
	void addCube (const Position& pos, const Color* color);

	/** Rotation around the x-axis. direction==1 means clock wise. */
	void rotateX (const int direction);
	/** Rotation around the y-axis. direction==1 means clock wise. */
	void rotateY (const int direction);
	/** Rotation around the z-axis. direction==1 means clock wise. */
	void rotateZ (const int direction);

	/** Returns the count of cubes set in this figure.*/
	int getCountOfCubes() const;

	/** Returns the extend in x direction. */
	int getSizeX() const { return sizeX; };
	/** Returns the extend in y direction. */
	int getSizeY() const { return sizeY; };
	/** Returns the extend in z direction. */
	int getSizeZ() const { return sizeZ; };

	/** Removes and deletes all cubes. */
	virtual void clear();

	/** Moves figure to position newPos. */
	void moveTo (const Position& newPos);
	/** Moves figure relative with rel. */
	void moveRel (const Position& rel);

	/** Returns current position. */
	const Position& getOrigin() const { return origin; };

	/** Checks if any cube of this is on the same absolute position as any of
	 * fig and returns true in this case.
	 */
	bool areCollisions (const Figure& fig);

	/** Removes the cubes from fig and inserts them in this at same
	 * absolute position.
	 *
	 * No check for collisions is done - use checkForCollision before.
	 */
	void insertFigure (Figure& fig1);

	/** Draws figure on Draw3D::globalInstance using a z-buffer for planes. */
	virtual void draw();

	/** Sets the x,y coordinates of the center of the view to determine
	 * if side of cube is visible or hidden - lower left corner is 0,0.
	 */
	static void setLookCenter (double x, double y);

protected:

	/** Return index of pos in linear addressing cubes or planeZBuffer. */
	int get3DIndex (const Position& pos) const { return sizeY*sizeX*pos.getZ()+sizeX*pos.getY()+pos.getX(); };

	/** Return index of pos in linear addressing maxHeight. */
	int get2DIndex (const int x, const int y) const { return sizeX*y+x; };

	/** Calculates the z-Buffer from the cubes buffer. */
	void calculateZBuffer();

	/** Increments the position of x,y,z with order x,y,z. */
	void incrementPosition (int *x, int *y, int *z) const;

	/** The position of this figure relativ to origin of coordinate system
	 * of Draw3D::globalInstance. */
	Position origin;

	/** Extend of figure. */
	int sizeX, sizeY, sizeZ;

	/** sizeX*sizeY*sizeZ */
	int size;

	/** Center of view. */
	static double viewX, viewY;
	
	/** Stores the color of the cubes or null if no cube present at position.
	 * cubes=[z][y][x] with 0<=x<sizeX, 0<=y<sizeY, 0<=z<sizeZ.
	 */
	const Color **cubes;

	/** Z-Buffer for sides of a cube and not only points.
	 *
	 * If a variable is set, the side of the cube is visible and must be
	 * displayed.
	 */

	const int Left=1, Right=2, Front=4, Bottom=8, Top=16;
	
	/** Stores the visibility of the planes of the cubes of the figure.
	 *
	 * planeZBuffer=[z][y][x] with 0<=x<sizeX, 0<=y<sizeY, 0<=z<sizeZ.
	 */
	unsigned char *planeZBuffer;

	/** True in case of new cubes in figure or movement. */
	bool zBufferNeedsUpdate;
};

#endif _FIGURE_H_

Documentation generated by skyhunter@Dagobah on Wed Sep 16 18:39:03 MEST 1998