From afe1e7d2654893fc948e1f9ad961fd69446fab2b Mon Sep 17 00:00:00 2001 From: Christian Kroll Date: Sun, 3 Jan 2010 03:42:35 +0000 Subject: [PATCH] minor optimizations --- games/tetris/bast.c | 6 +++--- games/tetris/playfield.c | 36 +++++++++++++++++++++--------------- games/tetris/playfield.h | 3 ++- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/games/tetris/bast.c b/games/tetris/bast.c index e688089..5d8a01b 100644 --- a/games/tetris/bast.c +++ b/games/tetris/bast.c @@ -125,15 +125,15 @@ int16_t tetris_bastet_evalPos(tetris_bastet_t *pBastet, int8_t nWidth = tetris_playfield_getWidth(pBastet->pPlayfield); int8_t nStartCol = ((nColumn - 1) < 0) ? 0 : nColumn - 1; int8_t nStopCol; - // do we start at the left most position? + // Do we start at the left most position? + // If we do we MUST calculate the heights of ALL columns (initial step) if (nColumn <= -3) { - // in case we start at the left most position we calculate all heights nStopCol = nWidth - 1; // reset all column heights to zero tetris_bastet_clearColHeights(pBastet, 0 , nWidth); } - // if not, only calculate columns which are affected by the piece + // If not, only calculate columns which are affected by the moved piece. else { nStopCol = (nColumn + 3) < nWidth ? nColumn + 3 : nWidth - 1; diff --git a/games/tetris/playfield.c b/games/tetris/playfield.c index e18468d..66516e9 100644 --- a/games/tetris/playfield.c +++ b/games/tetris/playfield.c @@ -198,8 +198,8 @@ uint8_t tetris_playfield_collision(tetris_playfield_t *pPl, assert(pPl != NULL); // only allow coordinates which are within sane ranges - assert((nColumn >= -4) && (nColumn < pPl->nWidth)); - assert((nRow >= -4) && (nRow < pPl->nHeight)); + assert((nColumn > -4) && (nColumn < pPl->nWidth)); + assert((nRow > -4) && (nRow < pPl->nHeight)); // The rows of a piece get compared with the background one by one // until either a collision occures or all rows are compared. Both the @@ -465,6 +465,9 @@ void tetris_playfield_removeCompleteLines(tetris_playfield_t *pPl) // for complete rows, only i gets decremented int8_t nLowestRow = nStartRow; + // save old value for the first dump index with matter + int8_t nFormerFirstMatterRow = pPl->nFirstMatterRow; + // this loop only considers rows which are affected by the piece for (int8_t i = nStartRow; i >= nStopRow; --i) { @@ -493,10 +496,10 @@ void tetris_playfield_removeCompleteLines(tetris_playfield_t *pPl) uint8_t nComplete = nLowestRow - nStopRow + 1; if (nComplete > 0) { - for (int8_t i = nStopRow - 1; nLowestRow >= 0; --i) + for (int8_t i = nStopRow - 1; nLowestRow >= nFormerFirstMatterRow; --i) { // is the row we are copying from below the upper border? - if (i >= 0) + if (i >= nFormerFirstMatterRow) { // just copy from that row pPl->dump[nLowestRow] = pPl->dump[i]; @@ -741,12 +744,19 @@ uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt, pIt->pPlayfield = pPl; pIt->pPiece = pPiece; pIt->nColumn = nColumn; - pIt->nDeepestPieceRow = nRow; pIt->nFullRow = 0xFFFF >> (16 - pPl->nWidth); pIt->nCurrentRow = pPl->nHeight - 1; pIt->nRowBuffer = 0; + + // determine sane start and stop values for the piece's row indices + pIt->nPieceHighestRow = nRow; + pIt->nPieceLowestRow = ((pIt->nPieceHighestRow + 3) < pPl->nHeight) ? + (pIt->nPieceHighestRow + 3) : pPl->nHeight - 1; + + // don't return any trailing rows which are empty, so we look for a stop row pIt->nStopRow = pPl->nFirstMatterRow < nRow ? pPl->nFirstMatterRow : nRow; pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow; + return tetris_playfield_predictNextRow(pIt); } @@ -761,19 +771,14 @@ uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt) { uint16_t nPieceMap = 0; - if ((pIt->nDeepestPieceRow > -4) && (pIt->nCurrentRow >= pIt->nStopRow)) + if ((pIt->nPieceHighestRow > -4) && (pIt->nCurrentRow >= pIt->nStopRow)) { - // determine sane start and stop values for the piece's indices - int8_t nStartRow = - ((pIt->nDeepestPieceRow + 3) < pIt->pPlayfield->nHeight) ? - (pIt->nDeepestPieceRow + 3) : pIt->pPlayfield->nHeight - 1; - uint16_t nPiece = tetris_piece_getBitmap(pIt->pPiece); - if ((pIt->nCurrentRow <= nStartRow) && - (pIt->nCurrentRow >= pIt->nDeepestPieceRow)) + if ((pIt->nCurrentRow <= pIt->nPieceLowestRow) && + (pIt->nCurrentRow >= pIt->nPieceHighestRow)) { - int8_t y = pIt->nCurrentRow - pIt->nDeepestPieceRow; + int8_t y = pIt->nCurrentRow - pIt->nPieceHighestRow; // clear all bits of the piece we are not interested in and // align the rest to LSB @@ -793,9 +798,10 @@ uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt) // don't return full (and therefore removed) rows if (pIt->nRowBuffer == pIt->nFullRow) { - // recursively determine next row + // recursively determine next (?) row instead return tetris_playfield_predictNextRow(pIt); } + // row isn't full else { return &pIt->nRowBuffer; diff --git a/games/tetris/playfield.h b/games/tetris/playfield.h index 8f30f37..a3a7170 100644 --- a/games/tetris/playfield.h +++ b/games/tetris/playfield.h @@ -53,9 +53,10 @@ typedef struct tetris_playfield_iterator_t tetris_playfield_t *pPlayfield; // playfield to be examined tetris_piece_t *pPiece; // piece which should be tested int8_t nColumn; // the column where the piece should be dropped - int8_t nDeepestPieceRow; // the deepest possible row for a piece uint16_t nFullRow; // value of a full row int8_t nCurrentRow; // the actual row in the playfield + int8_t nPieceHighestRow; // the highest row index of the piece + int8_t nPieceLowestRow; // the lowest row index of the piece int8_t nStopRow; // the last row to be examined uint16_t nRowBuffer; // internal buffer for returned row values }