2012-05-07 06:56:00 +00:00
|
|
|
/**
|
|
|
|
* \addtogroup tetris
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file piece.h
|
|
|
|
* @brief Public interface definitions of the piece module.
|
|
|
|
* @author Christian Kroll
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2008-12-03 05:40:16 +00:00
|
|
|
#ifndef TETRIS_PIECE_H_
|
|
|
|
#define TETRIS_PIECE_H_
|
|
|
|
|
2010-08-24 23:00:40 +00:00
|
|
|
#include <stdint.h>
|
2011-06-02 14:00:51 +00:00
|
|
|
#include <stdlib.h>
|
2010-12-15 02:29:51 +00:00
|
|
|
#include <assert.h>
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*********
|
|
|
|
* types *
|
|
|
|
*********/
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/** shape attributes for a piece */
|
2012-05-07 06:56:00 +00:00
|
|
|
enum tetris_piece_shape_e
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2010-04-01 03:11:59 +00:00
|
|
|
TETRIS_PC_LINE, /**< the I shaped brick */
|
|
|
|
TETRIS_PC_T, /**< the T shaped brick */
|
2010-08-24 23:00:40 +00:00
|
|
|
TETRIS_PC_SQUARE, /**< the square shaped brick */
|
2010-04-01 03:11:59 +00:00
|
|
|
TETRIS_PC_L, /**< the L shaped brick */
|
|
|
|
TETRIS_PC_LBACK, /**< the reverse L shaped brick */
|
|
|
|
TETRIS_PC_S, /**< the S shaped brick */
|
|
|
|
TETRIS_PC_Z /**< the Z shaped brick */
|
2011-02-25 04:31:34 +00:00
|
|
|
};
|
|
|
|
#ifdef NDEBUG
|
|
|
|
typedef uint8_t tetris_piece_shape_t;
|
|
|
|
#else
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef enum tetris_piece_shape_e tetris_piece_shape_t;
|
2011-02-25 04:31:34 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/** possible angles for a brick */
|
2012-05-07 06:56:00 +00:00
|
|
|
enum tetris_piece_angle_e
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2010-04-01 03:11:59 +00:00
|
|
|
TETRIS_PC_ANGLE_0, /**< standard angle */
|
|
|
|
TETRIS_PC_ANGLE_90, /**< rotated by 90 degrees */
|
|
|
|
TETRIS_PC_ANGLE_180, /**< rotated by 180 degrees */
|
|
|
|
TETRIS_PC_ANGLE_270 /**< rotated by 270 degrees */
|
2011-02-25 04:31:34 +00:00
|
|
|
};
|
|
|
|
#ifdef NDEBUG
|
|
|
|
typedef uint8_t tetris_piece_angle_t;
|
|
|
|
#else
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef enum tetris_piece_angle_e tetris_piece_angle_t;
|
2011-02-25 04:31:34 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/** rotation attributes */
|
2012-05-07 06:56:00 +00:00
|
|
|
enum tetris_piece_rotation_e
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2011-03-01 16:17:58 +00:00
|
|
|
TETRIS_PC_ROT_CW = 1, /**< clockwise rotation */
|
|
|
|
TETRIS_PC_ROT_CCW = 3 /**< counter clockwise rotation */
|
2011-02-25 04:31:34 +00:00
|
|
|
};
|
|
|
|
#ifdef NDEBUG
|
|
|
|
typedef uint8_t tetris_piece_rotation_t;
|
|
|
|
#else
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef enum tetris_piece_rotation_e tetris_piece_rotation_t;
|
2011-02-25 04:31:34 +00:00
|
|
|
#endif
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* describes the attributes of a piece
|
|
|
|
* @see tetris_piece_shape_t
|
|
|
|
* @see tetris_piece_angle_t
|
|
|
|
*/
|
2012-05-07 06:56:00 +00:00
|
|
|
typedef struct tetris_piece_s
|
2008-12-03 05:40:16 +00:00
|
|
|
{
|
2010-04-01 03:11:59 +00:00
|
|
|
tetris_piece_shape_t shape; /**< specifies the shape of the piece */
|
|
|
|
tetris_piece_angle_t angle; /**< specifies one of 4 angels */
|
2008-12-03 05:40:16 +00:00
|
|
|
}
|
|
|
|
tetris_piece_t;
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************
|
|
|
|
* construction/destruction *
|
|
|
|
*****************************/
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* constructs a piece with the given attributes
|
|
|
|
* @param s shape of the piece (see tetris_piece_shape_t)
|
|
|
|
* @param a its angle (see tetris_piece_angel_t)
|
|
|
|
* @return pointer to a newly created piece
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
|
|
|
tetris_piece_t *tetris_piece_construct(tetris_piece_shape_t s,
|
|
|
|
tetris_piece_angle_t a);
|
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* destructs a piece
|
|
|
|
* @param pPc pointer to the piece to be destructed
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
2011-02-06 22:56:26 +00:00
|
|
|
inline static void tetris_piece_destruct(tetris_piece_t *pPc)
|
|
|
|
{
|
|
|
|
assert(pPc != NULL);
|
|
|
|
free(pPc);
|
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/***************************
|
|
|
|
* piece related functions *
|
|
|
|
***************************/
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* returns bitfield representation of the piece
|
|
|
|
* @param pPc piece from which the bitfield shuld be retrieved
|
|
|
|
* @return bitfield representation of the piece
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
|
|
|
uint16_t tetris_piece_getBitmap(tetris_piece_t *pPc);
|
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* rotates a piece
|
|
|
|
* @param pPc piece to rotate
|
2010-08-24 23:00:40 +00:00
|
|
|
* @param nRotation type of rotation (see tetris_piece_rotation_t)
|
2008-12-03 05:40:16 +00:00
|
|
|
*/
|
2011-03-01 16:17:58 +00:00
|
|
|
inline static void tetris_piece_rotate(tetris_piece_t *pPc,
|
|
|
|
tetris_piece_rotation_t nRotation)
|
|
|
|
{
|
|
|
|
assert(pPc != NULL);
|
|
|
|
assert(nRotation == TETRIS_PC_ROT_CW || nRotation == TETRIS_PC_ROT_CCW);
|
|
|
|
|
|
|
|
// we just rotate through the available angles in the given direction and
|
|
|
|
// wrap around (via modulo) where appropriate
|
|
|
|
pPc->angle = (pPc->angle + nRotation) % 4;
|
|
|
|
}
|
2008-12-03 05:40:16 +00:00
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* changes the shape of a piece
|
|
|
|
* @param pPc piece to change
|
|
|
|
* @param shape the shape of interest
|
2009-10-31 15:15:00 +00:00
|
|
|
*/
|
2010-12-14 22:58:16 +00:00
|
|
|
inline static void tetris_piece_setShape(tetris_piece_t *pPc,
|
|
|
|
tetris_piece_shape_t shape)
|
|
|
|
{
|
|
|
|
assert(pPc != NULL);
|
2010-12-15 02:29:51 +00:00
|
|
|
assert(shape <= TETRIS_PC_Z);
|
2010-12-14 22:58:16 +00:00
|
|
|
|
|
|
|
pPc->shape = shape;
|
|
|
|
}
|
2009-10-31 15:15:00 +00:00
|
|
|
|
2008-12-03 05:40:16 +00:00
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* changes the angle of a piece
|
|
|
|
* @param pPc piece to change
|
|
|
|
* @param angle the angle of interest
|
2009-11-06 02:22:11 +00:00
|
|
|
*/
|
2010-12-14 22:58:16 +00:00
|
|
|
inline static void tetris_piece_setAngle(tetris_piece_t *pPc,
|
|
|
|
tetris_piece_angle_t angle)
|
|
|
|
{
|
|
|
|
assert(pPc != NULL);
|
2010-12-15 02:29:51 +00:00
|
|
|
assert(angle <= TETRIS_PC_ANGLE_270);
|
2010-12-14 22:58:16 +00:00
|
|
|
|
|
|
|
pPc->angle = angle;
|
|
|
|
}
|
2009-11-06 02:22:11 +00:00
|
|
|
|
|
|
|
|
2010-04-01 03:11:59 +00:00
|
|
|
/**
|
|
|
|
* returns the number of different angles
|
|
|
|
* @param pPc piece whose angle count we want to know
|
|
|
|
* @return number of different angles
|
2009-11-06 02:22:11 +00:00
|
|
|
*/
|
2011-01-30 22:47:44 +00:00
|
|
|
uint8_t tetris_piece_getAngleCount(tetris_piece_t *pPc);
|
2009-11-06 02:22:11 +00:00
|
|
|
|
2010-12-15 02:29:51 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the index of the first filled row of a piece
|
|
|
|
* @param nBitmap the bitmap of the piece of interest
|
|
|
|
* @return index of the first filled row
|
|
|
|
*/
|
|
|
|
inline static int8_t tetris_piece_getTopRow(uint16_t const nBitmap)
|
|
|
|
{
|
|
|
|
if (!(nBitmap & 0x0FFF))
|
|
|
|
{
|
|
|
|
return 3; // first three rows can be skipped
|
|
|
|
}
|
|
|
|
else if (!(nBitmap & 0x00FF))
|
|
|
|
{
|
|
|
|
return 2; // first two rows can be skipped
|
|
|
|
}
|
|
|
|
else if (!(nBitmap & 0x000F))
|
|
|
|
{
|
|
|
|
return 1; // first row can be skipped
|
|
|
|
}
|
|
|
|
return 0; // no row can be skipped
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns the offset to the last filled row of a piece
|
|
|
|
* @param nBitmap the bitmap of the piece of interest
|
|
|
|
* @return offset to the last filled row
|
|
|
|
*/
|
|
|
|
inline static int8_t tetris_piece_getBottomOffset(uint16_t const nBitmap)
|
|
|
|
{
|
|
|
|
if (nBitmap > 0x0FFF)
|
|
|
|
{
|
|
|
|
return 3; // piece spans over 4 rows
|
|
|
|
}
|
|
|
|
else if (nBitmap > 0x00FF)
|
|
|
|
{
|
|
|
|
return 2; // last row of the piece is empty
|
|
|
|
}
|
|
|
|
else if (nBitmap > 0x000F)
|
|
|
|
{
|
|
|
|
return 1; // last two rows of the piece are empty
|
|
|
|
}
|
|
|
|
return 0; // last three rows of the piece are empty
|
|
|
|
}
|
|
|
|
|
2009-11-06 02:22:11 +00:00
|
|
|
#endif /*TETRIS_PIECE_H_*/
|
2012-05-07 06:56:00 +00:00
|
|
|
|
|
|
|
/*@}*/
|