smooth scroll added
This commit is contained in:
@ -47,14 +47,14 @@ void GameRenderer::drawBlockTexture(SDL_Renderer* renderer, SDL_Texture* blocksT
|
||||
SDL_RenderTexture(renderer, blocksTex, &srcRect, &dstRect);
|
||||
}
|
||||
|
||||
void GameRenderer::drawPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, const Game::Piece& piece, float ox, float oy, float tileSize, bool isGhost) {
|
||||
void GameRenderer::drawPiece(SDL_Renderer* renderer, SDL_Texture* blocksTex, const Game::Piece& piece, float ox, float oy, float tileSize, bool isGhost, float pixelOffsetY) {
|
||||
if (piece.type >= PIECE_COUNT) return;
|
||||
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (Game::cellFilled(piece, cx, cy)) {
|
||||
float px = ox + (piece.x + cx) * tileSize;
|
||||
float py = oy + (piece.y + cy) * tileSize;
|
||||
float py = oy + (piece.y + cy) * tileSize + pixelOffsetY;
|
||||
|
||||
if (isGhost) {
|
||||
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
|
||||
@ -239,6 +239,59 @@ void GameRenderer::renderPlayingState(
|
||||
|
||||
bool allowActivePieceRender = true;
|
||||
|
||||
auto computeFallOffset = [&]() -> float {
|
||||
if (game->isPaused()) {
|
||||
return 0.0f;
|
||||
}
|
||||
double gravityMs = game->getGravityMs();
|
||||
if (gravityMs <= 0.0) {
|
||||
return 0.0f;
|
||||
}
|
||||
double effectiveMs = game->isSoftDropping() ? std::max(5.0, gravityMs / 5.0) : gravityMs;
|
||||
double accumulator = std::clamp(game->getFallAccumulator(), 0.0, effectiveMs);
|
||||
if (effectiveMs <= 0.0) {
|
||||
return 0.0f;
|
||||
}
|
||||
float progress = static_cast<float>(accumulator / effectiveMs);
|
||||
progress = std::clamp(progress, 0.0f, 1.0f);
|
||||
return progress * finalBlockSize;
|
||||
};
|
||||
|
||||
float activePieceOffset = (!game->isPaused()) ? computeFallOffset() : 0.0f;
|
||||
if (activePieceOffset > 0.0f) {
|
||||
const auto& boardRef = game->boardRef();
|
||||
const Game::Piece& piece = game->current();
|
||||
float maxAllowed = finalBlockSize;
|
||||
for (int cy = 0; cy < 4; ++cy) {
|
||||
for (int cx = 0; cx < 4; ++cx) {
|
||||
if (!Game::cellFilled(piece, cx, cy)) {
|
||||
continue;
|
||||
}
|
||||
int gx = piece.x + cx;
|
||||
int gy = piece.y + cy;
|
||||
if (gx < 0 || gx >= Game::COLS) {
|
||||
continue;
|
||||
}
|
||||
int testY = gy + 1;
|
||||
int emptyRows = 0;
|
||||
if (testY < 0) {
|
||||
emptyRows -= testY; // number of rows until we reach row 0
|
||||
testY = 0;
|
||||
}
|
||||
while (testY >= 0 && testY < Game::ROWS) {
|
||||
if (boardRef[testY * Game::COLS + gx] != 0) {
|
||||
break;
|
||||
}
|
||||
++emptyRows;
|
||||
++testY;
|
||||
}
|
||||
float cellLimit = (emptyRows > 0) ? finalBlockSize : 0.0f;
|
||||
maxAllowed = std::min(maxAllowed, cellLimit);
|
||||
}
|
||||
}
|
||||
activePieceOffset = std::min(activePieceOffset, maxAllowed);
|
||||
}
|
||||
|
||||
// Draw ghost piece (where current piece will land)
|
||||
if (allowActivePieceRender) {
|
||||
Game::Piece ghostPiece = game->current();
|
||||
@ -274,7 +327,7 @@ void GameRenderer::renderPlayingState(
|
||||
|
||||
// Draw the falling piece
|
||||
if (allowActivePieceRender) {
|
||||
drawPiece(renderer, blocksTex, game->current(), gridX, gridY, finalBlockSize, false);
|
||||
drawPiece(renderer, blocksTex, game->current(), gridX, gridY, finalBlockSize, false, activePieceOffset);
|
||||
}
|
||||
|
||||
// Draw line clearing effects
|
||||
|
||||
Reference in New Issue
Block a user