diff --git a/src/graphics/GameRenderer.cpp b/src/graphics/GameRenderer.cpp index 13243e4..8cb7552 100644 --- a/src/graphics/GameRenderer.cpp +++ b/src/graphics/GameRenderer.cpp @@ -6,6 +6,10 @@ #include #include #include +#include +#include + +namespace { // Color constants (copied from main.cpp) static const SDL_Color COLORS[] = { @@ -217,6 +221,12 @@ void GameRenderer::renderPlayingState( float lineY = gridY + y * finalBlockSize; SDL_RenderLine(renderer, gridX, lineY, gridX + GRID_W, lineY); } + + Uint64 nowTicks = SDL_GetTicks(); + float deltaSeconds = (g_lastSparkTick == 0) ? (1.0f / 60.0f) : static_cast(nowTicks - g_lastSparkTick) / 1000.0f; + g_lastSparkTick = nowTicks; + updateSparks(std::max(0.0f, deltaSeconds)); + drawSparks(renderer, gridX, gridY, finalBlockSize); // Draw block statistics panel border drawRectWithOffset(statsX - 3 - contentOffsetX, statsY - 3 - contentOffsetY, statsW + 6, statsH + 6, {100, 120, 200, 255}); diff --git a/src/graphics/Starfield3D.cpp b/src/graphics/Starfield3D.cpp index 3fe7740..fa038c4 100644 --- a/src/graphics/Starfield3D.cpp +++ b/src/graphics/Starfield3D.cpp @@ -137,16 +137,16 @@ void Starfield3D::update(float deltaTime) { } } -void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, int type) { - const SDL_Color& color = STAR_COLORS[type % COLOR_COUNT]; - SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); +void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, SDL_Color color, float alphaScale) { + Uint8 alpha = static_cast(std::clamp(color.a * alphaScale, 0.0f, 255.0f)); + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha); // Draw star as a small rectangle (1x1 pixel) SDL_FRect rect{x, y, 1.0f, 1.0f}; SDL_RenderFillRect(renderer, &rect); } -void Starfield3D::draw(SDL_Renderer* renderer) { +void Starfield3D::draw(SDL_Renderer* renderer, float offsetX, float offsetY, float alphaScale, bool grayscale) { for (const Star3D& star : stars) { // Calculate perspective projection factor const float k = DEPTH_FACTOR / star.z; @@ -158,7 +158,12 @@ void Starfield3D::draw(SDL_Renderer* renderer) { // Only draw stars that are within the viewport if (px >= 0.0f && px <= static_cast(width) && py >= 0.0f && py <= static_cast(height)) { - drawStar(renderer, px, py, star.type); + SDL_Color baseColor = STAR_COLORS[star.type % COLOR_COUNT]; + if (grayscale) { + Uint8 gray = static_cast(0.299f * baseColor.r + 0.587f * baseColor.g + 0.114f * baseColor.b); + baseColor.r = baseColor.g = baseColor.b = gray; + } + drawStar(renderer, px + offsetX, py + offsetY, baseColor, alphaScale); } } } diff --git a/src/graphics/Starfield3D.h b/src/graphics/Starfield3D.h index 7dfd5c9..3e03189 100644 --- a/src/graphics/Starfield3D.h +++ b/src/graphics/Starfield3D.h @@ -13,7 +13,7 @@ public: void init(int width, int height, int starCount = 160); void update(float deltaTime); - void draw(SDL_Renderer* renderer); + void draw(SDL_Renderer* renderer, float offsetX = 0.0f, float offsetY = 0.0f, float alphaScale = 1.0f, bool grayscale = false); void resize(int width, int height); private: @@ -32,7 +32,7 @@ private: void setRandomDirection(Star3D& star); float randomFloat(float min, float max); int randomRange(int min, int max); - void drawStar(SDL_Renderer* renderer, float x, float y, int type); + void drawStar(SDL_Renderer* renderer, float x, float y, SDL_Color color, float alphaScale); std::vector stars; int width{0}, height{0}; diff --git a/src/graphics/effects/Starfield3D.cpp b/src/graphics/effects/Starfield3D.cpp index 3fe7740..fa038c4 100644 --- a/src/graphics/effects/Starfield3D.cpp +++ b/src/graphics/effects/Starfield3D.cpp @@ -137,16 +137,16 @@ void Starfield3D::update(float deltaTime) { } } -void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, int type) { - const SDL_Color& color = STAR_COLORS[type % COLOR_COUNT]; - SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); +void Starfield3D::drawStar(SDL_Renderer* renderer, float x, float y, SDL_Color color, float alphaScale) { + Uint8 alpha = static_cast(std::clamp(color.a * alphaScale, 0.0f, 255.0f)); + SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, alpha); // Draw star as a small rectangle (1x1 pixel) SDL_FRect rect{x, y, 1.0f, 1.0f}; SDL_RenderFillRect(renderer, &rect); } -void Starfield3D::draw(SDL_Renderer* renderer) { +void Starfield3D::draw(SDL_Renderer* renderer, float offsetX, float offsetY, float alphaScale, bool grayscale) { for (const Star3D& star : stars) { // Calculate perspective projection factor const float k = DEPTH_FACTOR / star.z; @@ -158,7 +158,12 @@ void Starfield3D::draw(SDL_Renderer* renderer) { // Only draw stars that are within the viewport if (px >= 0.0f && px <= static_cast(width) && py >= 0.0f && py <= static_cast(height)) { - drawStar(renderer, px, py, star.type); + SDL_Color baseColor = STAR_COLORS[star.type % COLOR_COUNT]; + if (grayscale) { + Uint8 gray = static_cast(0.299f * baseColor.r + 0.587f * baseColor.g + 0.114f * baseColor.b); + baseColor.r = baseColor.g = baseColor.b = gray; + } + drawStar(renderer, px + offsetX, py + offsetY, baseColor, alphaScale); } } } diff --git a/src/graphics/effects/Starfield3D.h b/src/graphics/effects/Starfield3D.h index 7dfd5c9..3e03189 100644 --- a/src/graphics/effects/Starfield3D.h +++ b/src/graphics/effects/Starfield3D.h @@ -13,7 +13,7 @@ public: void init(int width, int height, int starCount = 160); void update(float deltaTime); - void draw(SDL_Renderer* renderer); + void draw(SDL_Renderer* renderer, float offsetX = 0.0f, float offsetY = 0.0f, float alphaScale = 1.0f, bool grayscale = false); void resize(int width, int height); private: @@ -32,7 +32,7 @@ private: void setRandomDirection(Star3D& star); float randomFloat(float min, float max); int randomRange(int min, int max); - void drawStar(SDL_Renderer* renderer, float x, float y, int type); + void drawStar(SDL_Renderer* renderer, float x, float y, SDL_Color color, float alphaScale); std::vector stars; int width{0}, height{0}; diff --git a/src/graphics/renderers/GameRenderer.cpp b/src/graphics/renderers/GameRenderer.cpp index 52bdd9d..6fb9fb5 100644 --- a/src/graphics/renderers/GameRenderer.cpp +++ b/src/graphics/renderers/GameRenderer.cpp @@ -10,6 +10,7 @@ #include #include #include "../../core/Settings.h" +#include "../../graphics/effects/Starfield3D.h" namespace { struct ImpactSpark { @@ -30,6 +31,9 @@ struct ActivePieceSmoothState { }; ActivePieceSmoothState s_activePieceSmooth; + +Starfield3D s_inGridStarfield; +bool s_starfieldInitialized = false; } // Color constants (copied from main.cpp) @@ -250,6 +254,22 @@ void GameRenderer::renderPlayingState( float lineY = gridY + y * finalBlockSize; SDL_RenderLine(renderer, gridX, lineY, gridX + GRID_W, lineY); } + + if (!s_starfieldInitialized) { + s_inGridStarfield.init(static_cast(GRID_W), static_cast(GRID_H), 180); + s_starfieldInitialized = true; + } else { + s_inGridStarfield.resize(static_cast(GRID_W), static_cast(GRID_H)); + } + + const float deltaSeconds = std::clamp(static_cast(sparkDeltaMs) / 1000.0f, 0.0f, 0.033f); + s_inGridStarfield.update(deltaSeconds); + + SDL_BlendMode oldBlend = SDL_BLENDMODE_NONE; + SDL_GetRenderDrawBlendMode(renderer, &oldBlend); + SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND); + s_inGridStarfield.draw(renderer, gridX, gridY, 0.22f, true); + SDL_SetRenderDrawBlendMode(renderer, oldBlend); // Draw block statistics panel border drawRectWithOffset(statsX - 3 - contentOffsetX, statsY - 3 - contentOffsetY, statsW + 6, statsH + 6, {100, 120, 200, 255});