minor optimizations
This commit is contained in:
parent
48badfd134
commit
afe1e7d265
|
@ -125,15 +125,15 @@ int16_t tetris_bastet_evalPos(tetris_bastet_t *pBastet,
|
||||||
int8_t nWidth = tetris_playfield_getWidth(pBastet->pPlayfield);
|
int8_t nWidth = tetris_playfield_getWidth(pBastet->pPlayfield);
|
||||||
int8_t nStartCol = ((nColumn - 1) < 0) ? 0 : nColumn - 1;
|
int8_t nStartCol = ((nColumn - 1) < 0) ? 0 : nColumn - 1;
|
||||||
int8_t nStopCol;
|
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)
|
if (nColumn <= -3)
|
||||||
{
|
{
|
||||||
// in case we start at the left most position we calculate all heights
|
|
||||||
nStopCol = nWidth - 1;
|
nStopCol = nWidth - 1;
|
||||||
// reset all column heights to zero
|
// reset all column heights to zero
|
||||||
tetris_bastet_clearColHeights(pBastet, 0 , nWidth);
|
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
|
else
|
||||||
{
|
{
|
||||||
nStopCol = (nColumn + 3) < nWidth ? nColumn + 3 : nWidth - 1;
|
nStopCol = (nColumn + 3) < nWidth ? nColumn + 3 : nWidth - 1;
|
||||||
|
|
|
@ -198,8 +198,8 @@ uint8_t tetris_playfield_collision(tetris_playfield_t *pPl,
|
||||||
assert(pPl != NULL);
|
assert(pPl != NULL);
|
||||||
|
|
||||||
// only allow coordinates which are within sane ranges
|
// only allow coordinates which are within sane ranges
|
||||||
assert((nColumn >= -4) && (nColumn < pPl->nWidth));
|
assert((nColumn > -4) && (nColumn < pPl->nWidth));
|
||||||
assert((nRow >= -4) && (nRow < pPl->nHeight));
|
assert((nRow > -4) && (nRow < pPl->nHeight));
|
||||||
|
|
||||||
// The rows of a piece get compared with the background one by one
|
// 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
|
// 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
|
// for complete rows, only i gets decremented
|
||||||
int8_t nLowestRow = nStartRow;
|
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
|
// this loop only considers rows which are affected by the piece
|
||||||
for (int8_t i = nStartRow; i >= nStopRow; --i)
|
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;
|
uint8_t nComplete = nLowestRow - nStopRow + 1;
|
||||||
if (nComplete > 0)
|
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?
|
// is the row we are copying from below the upper border?
|
||||||
if (i >= 0)
|
if (i >= nFormerFirstMatterRow)
|
||||||
{
|
{
|
||||||
// just copy from that row
|
// just copy from that row
|
||||||
pPl->dump[nLowestRow] = pPl->dump[i];
|
pPl->dump[nLowestRow] = pPl->dump[i];
|
||||||
|
@ -741,12 +744,19 @@ uint16_t* tetris_playfield_predictBottomRow(tetris_playfield_iterator_t *pIt,
|
||||||
pIt->pPlayfield = pPl;
|
pIt->pPlayfield = pPl;
|
||||||
pIt->pPiece = pPiece;
|
pIt->pPiece = pPiece;
|
||||||
pIt->nColumn = nColumn;
|
pIt->nColumn = nColumn;
|
||||||
pIt->nDeepestPieceRow = nRow;
|
|
||||||
pIt->nFullRow = 0xFFFF >> (16 - pPl->nWidth);
|
pIt->nFullRow = 0xFFFF >> (16 - pPl->nWidth);
|
||||||
pIt->nCurrentRow = pPl->nHeight - 1;
|
pIt->nCurrentRow = pPl->nHeight - 1;
|
||||||
pIt->nRowBuffer = 0;
|
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 = pPl->nFirstMatterRow < nRow ? pPl->nFirstMatterRow : nRow;
|
||||||
pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow;
|
pIt->nStopRow = pIt->nStopRow < 0 ? 0 : pIt->nStopRow;
|
||||||
|
|
||||||
return tetris_playfield_predictNextRow(pIt);
|
return tetris_playfield_predictNextRow(pIt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -761,19 +771,14 @@ uint16_t* tetris_playfield_predictNextRow(tetris_playfield_iterator_t *pIt)
|
||||||
{
|
{
|
||||||
uint16_t nPieceMap = 0;
|
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);
|
uint16_t nPiece = tetris_piece_getBitmap(pIt->pPiece);
|
||||||
|
|
||||||
if ((pIt->nCurrentRow <= nStartRow) &&
|
if ((pIt->nCurrentRow <= pIt->nPieceLowestRow) &&
|
||||||
(pIt->nCurrentRow >= pIt->nDeepestPieceRow))
|
(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
|
// clear all bits of the piece we are not interested in and
|
||||||
// align the rest to LSB
|
// 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
|
// don't return full (and therefore removed) rows
|
||||||
if (pIt->nRowBuffer == pIt->nFullRow)
|
if (pIt->nRowBuffer == pIt->nFullRow)
|
||||||
{
|
{
|
||||||
// recursively determine next row
|
// recursively determine next (?) row instead
|
||||||
return tetris_playfield_predictNextRow(pIt);
|
return tetris_playfield_predictNextRow(pIt);
|
||||||
}
|
}
|
||||||
|
// row isn't full
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return &pIt->nRowBuffer;
|
return &pIt->nRowBuffer;
|
||||||
|
|
|
@ -53,9 +53,10 @@ typedef struct tetris_playfield_iterator_t
|
||||||
tetris_playfield_t *pPlayfield; // playfield to be examined
|
tetris_playfield_t *pPlayfield; // playfield to be examined
|
||||||
tetris_piece_t *pPiece; // piece which should be tested
|
tetris_piece_t *pPiece; // piece which should be tested
|
||||||
int8_t nColumn; // the column where the piece should be dropped
|
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
|
uint16_t nFullRow; // value of a full row
|
||||||
int8_t nCurrentRow; // the actual row in the playfield
|
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
|
int8_t nStopRow; // the last row to be examined
|
||||||
uint16_t nRowBuffer; // internal buffer for returned row values
|
uint16_t nRowBuffer; // internal buffer for returned row values
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue