The constructor and draw() function

The first section of code defines the constructor of the Piece class and the draw() function:

#include "piece.h"

using namespace Constants;

Piece::Piece(Piece::Kind kind) :
kind_(kind),
column_(BoardColumns / 2 - PieceSize / 2),
row_(0),
angle_(0) {
}

void Piece::draw(SDL_Renderer *renderer) {
switch (kind_) {
case I:
SDL_SetRenderDrawColor(renderer,
/* Cyan: */ 45, 254, 254, 255);
break;
case J:
SDL_SetRenderDrawColor(renderer,
/* Blue: */ 11, 36, 251, 255);
break;
case L:
SDL_SetRenderDrawColor(renderer,
/* Orange: */ 253, 164, 41, 255);
break;
case O:
SDL_SetRenderDrawColor(renderer,
/* Yellow: */ 255, 253, 56, 255);
break;
case S:
SDL_SetRenderDrawColor(renderer,
/* Green: */ 41, 253, 47, 255);
break;
case T:
SDL_SetRenderDrawColor(renderer,
/* Purple: */ 126, 15, 126, 255);
break;
case Z:
SDL_SetRenderDrawColor(renderer,
/* Red: */ 252, 13, 28, 255);
break;
}

for (int column = 0; column < PieceSize; ++column) {
for (int row = 0; row < PieceSize; ++row) {
if (isBlock(column, row)) {
SDL_Rect rect{
(column + column_) * Offset + 1,
(row + row_) * Offset + 1,
Offset - 2,
Offset - 2
};
SDL_RenderFillRect(renderer, &rect);
}
}
}
}

The constructor initializes the class with default values. The BoardColumns and PieceSize values are constants from the constants.h file. BoardColumns represents the amount of columns that can fit on a board, which is 10 in this case. The PieceSize constant represents the area or block that a piece takes up in columns, which is 4. The initial value assigned to the private columns_ variable represents the center of the board.

The draw() function loops through all of the possible rows and columns on the board and fills in any cells that are populated by a piece with the color that corresponds to its kind. The determination for whether a cell is populated by a piece is performed in the isBlock() function, which we'll discuss next.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset