Skip to content

Commit

Permalink
Added support for a ghost block to reduce misdrops
Browse files Browse the repository at this point in the history
  • Loading branch information
JumpmanMV committed Aug 15, 2021
1 parent 5db09ed commit 4aa2b23
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ I do not have a MacOS machine so I cannot test any builds for that OS. However,
## Features to add
- ~~Create sprites for all objects~~
- ~~Add a hold block~~
- Add a ghost block (the outline that shows the position of the piece if dropped)
- ~~Add a ghost block (the outline that shows the position of the piece if dropped)~~
- Add a simple menu
- Add basic sound effects
## Third party tools
- [SDL2 framework](https://www.libsdl.org/) to handle all the low-level stuff
- [SDL_ttf 2.0](https://www.libsdl.org/projects/SDL_ttf/) for rendering fonts
- [SDL_image 2.0](https://www.libsdl.org/projects/SDL_image/) for loading images
- The font I use is [Fira Sans](https://fonts.google.com/specimen/Fira+Sans)
- [CMake scripts](https://github.com/tcbrindle/sdl2-cmake-scripts) to find SDL2, SDL_ttf, and SDL_image

Expand Down
Binary file modified assets/tetrominoSprites.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ namespace config

// Constants relating to Tetris
const int wait_time = 1000; // Time in milliseconds
const bool ghost_piece_enabled = true; // Enables or disables the ghost piece to reduce misdrops

// Visuals
const SDL_Color default_text_color = {0x00, 0x00, 0x00, 0xFF}; // Default color for all text
const Uint8 background_r_light = 0xF9; // Background color for light mode
const Uint8 background_g_light = 0xE6;
const Uint8 background_b_light = 0xCF;
const Uint8 transparency_alpha = 100; // Default alpha for transparency (with 255 being no transparency)
}

#endif // CONFIG_HPP
28 changes: 28 additions & 0 deletions src/gamestate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ void GameState::drawState ()
{
drawBoard();
drawCurrentPiece(currentPiece);
if (!board->isGameOver() && config::ghost_piece_enabled) drawGhostPiece(currentPiece);
if (!hold_block_first_time) drawHoldPiece(holdPiece);
drawNextPiece(nextPiece);
}
Expand Down Expand Up @@ -278,6 +279,33 @@ void GameState::drawCurrentPiece (Piece p)
}
}

// Draws the ghost piece of the piece given
void GameState::drawGhostPiece (Piece p)
{
ghostPiece = p;
while (board->isPositionLegal(ghostPiece))
{
ghostPiece.r++;
}
ghostPiece.r--;

tetrominoSprites.setAlphaMode(config::transparency_alpha); // Change transparency to render the ghost piece

for (int row = 0; row < config::matrix_blocks; row++)
{
for (int col = 0; col < config::matrix_blocks; col++)
{
if (ghostPiece.getBlockType(row, col) != 0)
{
tetrominoSprites.render(config::width_to_playfield + (col+ghostPiece.c) * config::block_size, config::height_to_playfield +
(row+ghostPiece.r-(config::playfield_height-config::true_playfield_height))*config::block_size, &tetrominoSpriteClips[ghostPiece.piece_type]);
}
}
}

tetrominoSprites.setAlphaMode(255); // Don't forget to change it back to normal!
}

void GameState::drawHoldPiece (Piece p)
{
for (int row = 0; row < config::matrix_blocks; row++)
Expand Down
2 changes: 2 additions & 0 deletions src/gamestate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class GameState
bool hold_block_used; // True if the hold block has been used, resets after a block is placed
Board *board;
Piece currentPiece {0, 0};
Piece ghostPiece {0, 0};
Piece holdPiece {0, 0};
Piece nextPiece {0, 0};
std::queue<Piece> nextPieces; // Holds the next 2 pieces
Expand All @@ -34,6 +35,7 @@ class GameState

void drawBoard ();
void drawCurrentPiece (Piece p);
void drawGhostPiece (Piece p);
void drawHoldPiece (Piece p);
void drawNextPiece (Piece p);
int getRandom (int lower_limit, int upper_limit); // Return a random number in this range
Expand Down
5 changes: 5 additions & 0 deletions src/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ void Texture::renderCentered (int x, int y)
SDL_RenderCopy(gRenderer, mTexture, nullptr, &r);
}

void Texture::setAlphaMode (Uint8 alpha)
{
SDL_SetTextureAlphaMod (mTexture, alpha);
}

int Texture::getWidth()
{
return width;
Expand Down
1 change: 1 addition & 0 deletions src/texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Texture
void loadFromText (std::string text, SDL_Color text_color);
void render (int x, int y, SDL_Rect *clip = nullptr);
void renderCentered (int x, int y);
void setAlphaMode (Uint8 alpha);

int getWidth ();
int getHeight ();
Expand Down

0 comments on commit 4aa2b23

Please sign in to comment.