From b9283bc8e653b0ad9b8d4a4d291be1e656a7521c Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Fri, 17 Dec 2010 23:33:06 +0000 Subject: [PATCH] minor optimizations and cleanups --- games/tetris/bucket.c | 58 +++++++++++++++++------------------ games/tetris/bucket.h | 17 +++++----- games/tetris/variant_bastet.c | 4 +-- games/tetris/view.c | 8 ++--- 4 files changed, 43 insertions(+), 44 deletions(-) diff --git a/games/tetris/bucket.c b/games/tetris/bucket.c index 9a2b78c..de106c3 100644 --- a/games/tetris/bucket.c +++ b/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 - * @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); // if the piece touches the dump we ensure that the status is "gliding" 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 { - return TETRIS_BUS_HOVERING; + pBucket->status = TETRIS_BUS_HOVERING; } } @@ -143,7 +141,7 @@ void tetris_bucket_insertPiece(tetris_bucket_t *pBucket, else { // 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... pBucket->nRow++; // 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; // are we gliding? - pBucket->status = tetris_bucket_hoverStatus(pBucket); + tetris_bucket_hoverStatus(pBucket); return 1; } @@ -309,7 +307,7 @@ uint8_t tetris_bucket_rotatePiece(tetris_bucket_t *pBucket, } // are we gliding? - pBucket->status = tetris_bucket_hoverStatus(pBucket); + tetris_bucket_hoverStatus(pBucket); return 1; } @@ -375,12 +373,12 @@ void tetris_bucket_removeCompleteLines(tetris_bucket_t *pBucket) int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket, tetris_piece_t *pPiece, - int8_t nStartingRow, + int8_t nStartRow, int8_t nColumn) { assert(pBucket != NULL); assert(pPiece != NULL); - assert(nStartingRow >= -1 && nStartingRow < pBucket->nHeight); + assert(nStartRow >= -1 && nStartRow < pBucket->nHeight); assert(nColumn >= -3 && nColumn < pBucket->nWidth); // 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 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 - 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? - 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 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); // determine sane start and stop values for the piece's row indices - pIt->nPieceHighestRow = nRow + tetris_piece_getTopRow(pIt->nPieceMap); - int8_t nBottomOffset = tetris_piece_getBottomOffset(pIt->nPieceMap); - pIt->nPieceLowestRow = (nRow + nBottomOffset) < pBucket->nHeight ? - nRow + nBottomOffset : pBucket->nHeight - 1; + pIt->nPieceTopRow = nRow + tetris_piece_getTopRow(pIt->nPieceMap); + pIt->nPieceBottomRow = nRow + tetris_piece_getBottomOffset(pIt->nPieceMap); + if (pIt->nPieceBottomRow >= pBucket->nHeight) + { + pIt->nPieceBottomRow = pBucket->nHeight - 1; + } // accelerate detection of full rows - pIt->nPieceMap <<= (nRow + 3 - pIt->nPieceLowestRow) * 4; + pIt->nPieceMap <<= (nRow + 3 - pIt->nPieceBottomRow) * 4; pIt->nShift = nColumn - 12; // don't return any trailing rows which are empty, so we look for a stop row - pIt->nStopRow = pBucket->nFirstTaintedRow < pIt->nPieceHighestRow ? - pBucket->nFirstTaintedRow : pIt->nPieceHighestRow; + pIt->nStopRow = pBucket->nFirstTaintedRow < pIt->nPieceTopRow ? + pBucket->nFirstTaintedRow : pIt->nPieceTopRow; pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow; return tetris_bucket_predictNextRow(pIt); @@ -489,12 +489,12 @@ uint16_t* tetris_bucket_predictNextRow(tetris_bucket_iterator_t *pIt) { assert(pIt != NULL); - if ((pIt->nPieceHighestRow > -4) && (pIt->nCurrentRow >= pIt->nStopRow)) + if (pIt->nCurrentRow >= pIt->nStopRow) { uint16_t nTemp = 0; // embed piece if it is there - if ((pIt->nCurrentRow <= pIt->nPieceLowestRow) && - (pIt->nCurrentRow >= pIt->nPieceHighestRow)) + if ((pIt->nCurrentRow <= pIt->nPieceBottomRow) && + (pIt->nCurrentRow >= pIt->nPieceTopRow)) { nTemp = pIt->nPieceMap & 0xF000; nTemp = pIt->nShift >= 0 ? diff --git a/games/tetris/bucket.h b/games/tetris/bucket.h index 40cb8fa..a7a2fe2 100644 --- a/games/tetris/bucket.h +++ b/games/tetris/bucket.h @@ -60,12 +60,12 @@ typedef struct tetris_bucket_iterator_t { tetris_bucket_t *pBucket; /** bucket to be examined */ 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 nPieceHighestRow; /** the highest row index of the piece */ - int8_t nPieceLowestRow; /** the lowest row index of the piece */ + int8_t nPieceTopRow; /** the highest row index of the piece */ + int8_t nPieceBottomRow; /** the lowest row index of the piece */ 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; @@ -166,9 +166,8 @@ void tetris_bucket_advancePiece(tetris_bucket_t *pBucket); * @param direction direction (see tetris_bucket_direction_t) * @return 1 if piece could be moved, 0 otherwise */ -uint8_t -tetris_bucket_movePiece(tetris_bucket_t *pBucket, - tetris_bucket_direction_t direction); +uint8_t tetris_bucket_movePiece(tetris_bucket_t *pBucket, + 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 * @param pBucket the bucket on which we want to test a piece * @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 * @return the row of the piece (bucket compliant coordinates) */ int8_t tetris_bucket_predictDeepestRow(tetris_bucket_t *pBucket, tetris_piece_t *pPiece, - int8_t nStartingRow, + int8_t nStartRow, int8_t nColumn); diff --git a/games/tetris/variant_bastet.c b/games/tetris/variant_bastet.c index a30ddaa..be60408 100644 --- a/games/tetris/variant_bastet.c +++ b/games/tetris/variant_bastet.c @@ -135,8 +135,8 @@ void tetris_bastet_calcPredictedColHeights(tetris_bastet_variant_t *pBastet, * @param pa the first value to compare * @param pb the second value to compare */ -int tetris_bastet_qsortCompare(void const *pa, - void const *pb) +static int tetris_bastet_qsortCompare(void const *pa, + void const *pb) { tetris_bastet_scorepair_t *pScorePairA = (tetris_bastet_scorepair_t *)pa; tetris_bastet_scorepair_t *pScorePairB = (tetris_bastet_scorepair_t *)pb; diff --git a/games/tetris/view.c b/games/tetris/view.c index 521cf77..9b4a97e 100644 --- a/games/tetris/view.c +++ b/games/tetris/view.c @@ -223,6 +223,8 @@ static void tetris_view_drawDump(tetris_view_t *pV) { return; } + tetris_bearing_t const nBearing = + pV->pVariantMethods->getBearing(pV->pVariant); 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 // align the remaining row to LSB 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 nRowMap |= nColumn >= 0 ? nPieceMap << nColumn : nPieceMap >> -nColumn; @@ -252,8 +254,6 @@ static void tetris_view_drawDump(tetris_view_t *pV) { unsigned char nColor = (nRowMap & nElementMask) ? 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_YOFFSET_DUMP + nRow, nColor); nElementMask <<= 1; @@ -498,7 +498,7 @@ static void tetris_view_blinkLines(tetris_view_t *pV) #ifdef TETRIS_VIEW_XOFFSET_COUNTER /** - * displays completed Lines (0-99) + * displays completed Lines (0-399) * @param pV pointer to the view */ static void tetris_view_showLineNumbers(tetris_view_t *pV)