Browse Source

minor optimizations and cleanups

feature/2015
Christian Kroll 14 years ago
parent
commit
b9283bc8e6
  1. 58
      games/tetris/bucket.c
  2. 15
      games/tetris/bucket.h
  3. 2
      games/tetris/variant_bastet.c
  4. 8
      games/tetris/view.c

58
games/tetris/bucket.c

@ -13,23 +13,21 @@
***************************/ ***************************/
/** /**
* determines if piece is either hovering or gliding * determines if piece is either hovering or gliding and sets the bucket's state
* @param pBucket the bucket we want information from * @param pBucket the bucket we want information from
* @return TETRIS_BUS_HOVERING or TETRIS_BUS_GLIDING
*/ */
static tetris_bucket_status_t tetris_bucket_hoverStatus(tetris_bucket_t* pBucket) inline static void tetris_bucket_hoverStatus(tetris_bucket_t *pBucket)
{ {
assert(pBucket != NULL); assert(pBucket != NULL);
// if the piece touches the dump we ensure that the status is "gliding" // if the piece touches the dump we ensure that the status is "gliding"
if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow + 1)) if (tetris_bucket_collision(pBucket, pBucket->nColumn, pBucket->nRow + 1))
{ {
return TETRIS_BUS_GLIDING; pBucket->status = TETRIS_BUS_GLIDING;
} }
// otherwise the status must be "hovering"
else else
{ {
return TETRIS_BUS_HOVERING; pBucket->status = TETRIS_BUS_HOVERING;
} }
} }
@ -143,7 +141,7 @@ void tetris_bucket_insertPiece(tetris_bucket_t *pBucket,
else else
{ {
// bring it on! // bring it on!
pBucket->status = tetris_bucket_hoverStatus(pBucket); tetris_bucket_hoverStatus(pBucket);
} }
} }
@ -259,7 +257,7 @@ void tetris_bucket_advancePiece(tetris_bucket_t *pBucket)
// no collision: piece may continue its travel to the ground... // no collision: piece may continue its travel to the ground...
pBucket->nRow++; pBucket->nRow++;
// are we gliding? // are we gliding?
pBucket->status = tetris_bucket_hoverStatus(pBucket); tetris_bucket_hoverStatus(pBucket);
} }
} }
@ -280,7 +278,7 @@ uint8_t tetris_bucket_movePiece(tetris_bucket_t *pBucket,
pBucket->nColumn += nOffset; pBucket->nColumn += nOffset;
// are we gliding? // are we gliding?
pBucket->status = tetris_bucket_hoverStatus(pBucket); tetris_bucket_hoverStatus(pBucket);
return 1; return 1;
} }
@ -309,7 +307,7 @@ uint8_t tetris_bucket_rotatePiece(tetris_bucket_t *pBucket,
} }
// are we gliding? // are we gliding?
pBucket->status = tetris_bucket_hoverStatus(pBucket); tetris_bucket_hoverStatus(pBucket);
return 1; return 1;
} }
@ -375,12 +373,12 @@ void tetris_bucket_removeCompleteLines(tetris_bucket_t *pBucket)
int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket, int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nStartingRow, int8_t nStartRow,
int8_t nColumn) int8_t nColumn)
{ {
assert(pBucket != NULL); assert(pBucket != NULL);
assert(pPiece != NULL); assert(pPiece != NULL);
assert(nStartingRow >= -1 && nStartingRow < pBucket->nHeight); assert(nStartRow >= -1 && nStartRow < pBucket->nHeight);
assert(nColumn >= -3 && nColumn < pBucket->nWidth); assert(nColumn >= -3 && nColumn < pBucket->nWidth);
// exchange current piece of the bucket (to use its collision detection) // exchange current piece of the bucket (to use its collision detection)
@ -389,26 +387,26 @@ int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket,
// skip empty rows at the bottom of the piece which may overlap the dump // skip empty rows at the bottom of the piece which may overlap the dump
uint16_t nMap = tetris_piece_getBitmap(pPiece); uint16_t nMap = tetris_piece_getBitmap(pPiece);
nStartingRow -= tetris_piece_getBottomOffset(nMap); nStartRow -= tetris_piece_getBottomOffset(nMap);
// check if the piece collides with one of the side borders // check if the piece collides with one of the side borders
if (nStartingRow >= -3) if (nStartRow >= -3)
{ {
while (!tetris_bucket_collision(pBucket, nColumn, nStartingRow + 1)) while (!tetris_bucket_collision(pBucket, nColumn, nStartRow + 1))
{ {
++nStartingRow; ++nStartRow;
} }
// bucket overflow? // bucket overflow?
if (nStartingRow < 0 && ((0xFFFF >> (((4 + nStartingRow) * 4))) & nMap)) if (nStartRow < 0 && ((0xFFFF >> (((4 + nStartRow) * 4))) & nMap))
{ {
nStartingRow = TETRIS_BUCKET_INVALIDROW; nStartRow = TETRIS_BUCKET_INVALIDROW;
} }
} }
// restore actual bucket piece // restore actual bucket piece
pBucket->pPiece = pActualPiece; pBucket->pPiece = pActualPiece;
return nStartingRow; return nStartRow;
} }
@ -468,17 +466,19 @@ uint16_t* tetris_bucket_predictBottomRow(tetris_bucket_iterator_t *pIt,
pIt->nPieceMap = tetris_piece_getBitmap(pPiece); pIt->nPieceMap = tetris_piece_getBitmap(pPiece);
// determine sane start and stop values for the piece's row indices // determine sane start and stop values for the piece's row indices
pIt->nPieceHighestRow = nRow + tetris_piece_getTopRow(pIt->nPieceMap); pIt->nPieceTopRow = nRow + tetris_piece_getTopRow(pIt->nPieceMap);
int8_t nBottomOffset = tetris_piece_getBottomOffset(pIt->nPieceMap); pIt->nPieceBottomRow = nRow + tetris_piece_getBottomOffset(pIt->nPieceMap);
pIt->nPieceLowestRow = (nRow + nBottomOffset) < pBucket->nHeight ? if (pIt->nPieceBottomRow >= pBucket->nHeight)
nRow + nBottomOffset : pBucket->nHeight - 1; {
pIt->nPieceBottomRow = pBucket->nHeight - 1;
}
// accelerate detection of full rows // accelerate detection of full rows
pIt->nPieceMap <<= (nRow + 3 - pIt->nPieceLowestRow) * 4; pIt->nPieceMap <<= (nRow + 3 - pIt->nPieceBottomRow) * 4;
pIt->nShift = nColumn - 12; pIt->nShift = nColumn - 12;
// don't return any trailing rows which are empty, so we look for a stop row // don't return any trailing rows which are empty, so we look for a stop row
pIt->nStopRow = pBucket->nFirstTaintedRow < pIt->nPieceHighestRow ? pIt->nStopRow = pBucket->nFirstTaintedRow < pIt->nPieceTopRow ?
pBucket->nFirstTaintedRow : pIt->nPieceHighestRow; pBucket->nFirstTaintedRow : pIt->nPieceTopRow;
pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow; pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow;
return tetris_bucket_predictNextRow(pIt); return tetris_bucket_predictNextRow(pIt);
@ -489,12 +489,12 @@ uint16_t* tetris_bucket_predictNextRow(tetris_bucket_iterator_t *pIt)
{ {
assert(pIt != NULL); assert(pIt != NULL);
if ((pIt->nPieceHighestRow > -4) && (pIt->nCurrentRow >= pIt->nStopRow)) if (pIt->nCurrentRow >= pIt->nStopRow)
{ {
uint16_t nTemp = 0; uint16_t nTemp = 0;
// embed piece if it is there // embed piece if it is there
if ((pIt->nCurrentRow <= pIt->nPieceLowestRow) && if ((pIt->nCurrentRow <= pIt->nPieceBottomRow) &&
(pIt->nCurrentRow >= pIt->nPieceHighestRow)) (pIt->nCurrentRow >= pIt->nPieceTopRow))
{ {
nTemp = pIt->nPieceMap & 0xF000; nTemp = pIt->nPieceMap & 0xF000;
nTemp = pIt->nShift >= 0 ? nTemp = pIt->nShift >= 0 ?

15
games/tetris/bucket.h

@ -60,12 +60,12 @@ typedef struct tetris_bucket_iterator_t
{ {
tetris_bucket_t *pBucket; /** bucket to be examined */ tetris_bucket_t *pBucket; /** bucket to be examined */
uint16_t nPieceMap; /** piece bitmap */ uint16_t nPieceMap; /** piece bitmap */
int8_t nShift; /** helper variable for piece bitmap shifts */ int8_t nShift; /** helper variable for shifting piece bitmaps */
int8_t nCurrentRow; /** the actual row in the bucket */ int8_t nCurrentRow; /** the actual row in the bucket */
int8_t nPieceHighestRow; /** the highest row index of the piece */ int8_t nPieceTopRow; /** the highest row index of the piece */
int8_t nPieceLowestRow; /** the lowest row index of the piece */ int8_t nPieceBottomRow; /** the lowest row index of the piece */
int8_t nStopRow; /** the last row to be examined */ int8_t nStopRow; /** the last row to be examined */
uint16_t nRowBuffer; /** internal buffer for returned rows */ uint16_t nRowBuffer; /** buffer for returned row */
} }
tetris_bucket_iterator_t; tetris_bucket_iterator_t;
@ -166,8 +166,7 @@ void tetris_bucket_advancePiece(tetris_bucket_t *pBucket);
* @param direction direction (see tetris_bucket_direction_t) * @param direction direction (see tetris_bucket_direction_t)
* @return 1 if piece could be moved, 0 otherwise * @return 1 if piece could be moved, 0 otherwise
*/ */
uint8_t uint8_t tetris_bucket_movePiece(tetris_bucket_t *pBucket,
tetris_bucket_movePiece(tetris_bucket_t *pBucket,
tetris_bucket_direction_t direction); tetris_bucket_direction_t direction);
@ -309,13 +308,13 @@ inline static uint16_t tetris_bucket_getDumpRow(tetris_bucket_t *pBucket,
* returns the deepest possible row for a given piece * returns the deepest possible row for a given piece
* @param pBucket the bucket on which we want to test a piece * @param pBucket the bucket on which we want to test a piece
* @param pPiece the piece which should be tested * @param pPiece the piece which should be tested
* @param nStartingRow the row where the collision detection should start * @param nStartRow the row where the collision detection should start
* @param nColumn the column where the piece should be dropped * @param nColumn the column where the piece should be dropped
* @return the row of the piece (bucket compliant coordinates) * @return the row of the piece (bucket compliant coordinates)
*/ */
int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket, int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket,
tetris_piece_t *pPiece, tetris_piece_t *pPiece,
int8_t nStartingRow, int8_t nStartRow,
int8_t nColumn); int8_t nColumn);

2
games/tetris/variant_bastet.c

@ -135,7 +135,7 @@ void tetris_bastet_calcPredictedColHeights(tetris_bastet_variant_t *pBastet,
* @param pa the first value to compare * @param pa the first value to compare
* @param pb the second value to compare * @param pb the second value to compare
*/ */
int tetris_bastet_qsortCompare(void const *pa, static int tetris_bastet_qsortCompare(void const *pa,
void const *pb) void const *pb)
{ {
tetris_bastet_scorepair_t *pScorePairA = (tetris_bastet_scorepair_t *)pa; tetris_bastet_scorepair_t *pScorePairA = (tetris_bastet_scorepair_t *)pa;

8
games/tetris/view.c

@ -223,6 +223,8 @@ static void tetris_view_drawDump(tetris_view_t *pV)
{ {
return; return;
} }
tetris_bearing_t const nBearing =
pV->pVariantMethods->getBearing(pV->pVariant);
for (int8_t nRow = TETRIS_VIEW_HEIGHT_DUMP - 1; nRow >= 0; --nRow) for (int8_t nRow = TETRIS_VIEW_HEIGHT_DUMP - 1; nRow >= 0; --nRow)
{ {
@ -241,7 +243,7 @@ static void tetris_view_drawDump(tetris_view_t *pV)
// clear all bits of the piece we are not interested in and // clear all bits of the piece we are not interested in and
// align the remaining row to LSB // align the remaining row to LSB
int8_t y = nRow - nPieceRow; int8_t y = nRow - nPieceRow;
nPieceMap = (nPieceMap & (0x000F << (y << 2))) >> (y << 2); nPieceMap = (nPieceMap & (0x000F << (y * 4))) >> (y * 4);
// shift remaining part to current column and embed piece into view // shift remaining part to current column and embed piece into view
nRowMap |= nColumn >= 0 ? nRowMap |= nColumn >= 0 ?
nPieceMap << nColumn : nPieceMap >> -nColumn; nPieceMap << nColumn : nPieceMap >> -nColumn;
@ -252,8 +254,6 @@ static void tetris_view_drawDump(tetris_view_t *pV)
{ {
unsigned char nColor = (nRowMap & nElementMask) ? unsigned char nColor = (nRowMap & nElementMask) ?
tetris_view_getPieceColor(pV) : TETRIS_VIEW_COLORSPACE; tetris_view_getPieceColor(pV) : TETRIS_VIEW_COLORSPACE;
tetris_bearing_t const nBearing =
pV->pVariantMethods->getBearing(pV->pVariant);
tetris_view_setpixel(nBearing, TETRIS_VIEW_XOFFSET_DUMP + x, tetris_view_setpixel(nBearing, TETRIS_VIEW_XOFFSET_DUMP + x,
TETRIS_VIEW_YOFFSET_DUMP + nRow, nColor); TETRIS_VIEW_YOFFSET_DUMP + nRow, nColor);
nElementMask <<= 1; nElementMask <<= 1;
@ -498,7 +498,7 @@ static void tetris_view_blinkLines(tetris_view_t *pV)
#ifdef TETRIS_VIEW_XOFFSET_COUNTER #ifdef TETRIS_VIEW_XOFFSET_COUNTER
/** /**
* displays completed Lines (0-99) * displays completed Lines (0-399)
* @param pV pointer to the view * @param pV pointer to the view
*/ */
static void tetris_view_showLineNumbers(tetris_view_t *pV) static void tetris_view_showLineNumbers(tetris_view_t *pV)

Loading…
Cancel
Save