added stars to gameplay grid

This commit is contained in:
2025-11-30 14:00:32 +01:00
parent 55c40f0516
commit 9ea0183339
6 changed files with 54 additions and 14 deletions

View File

@ -6,6 +6,10 @@
#include <array>
#include <cmath>
#include <cstdio>
#include <random>
#include <vector>
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<float>(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});

View File

@ -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<Uint8>(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<float>(width) &&
py >= 0.0f && py <= static_cast<float>(height)) {
drawStar(renderer, px, py, star.type);
SDL_Color baseColor = STAR_COLORS[star.type % COLOR_COUNT];
if (grayscale) {
Uint8 gray = static_cast<Uint8>(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);
}
}
}

View File

@ -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<Star3D> stars;
int width{0}, height{0};

View File

@ -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<Uint8>(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<float>(width) &&
py >= 0.0f && py <= static_cast<float>(height)) {
drawStar(renderer, px, py, star.type);
SDL_Color baseColor = STAR_COLORS[star.type % COLOR_COUNT];
if (grayscale) {
Uint8 gray = static_cast<Uint8>(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);
}
}
}

View File

@ -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<Star3D> stars;
int width{0}, height{0};

View File

@ -10,6 +10,7 @@
#include <random>
#include <vector>
#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<int>(GRID_W), static_cast<int>(GRID_H), 180);
s_starfieldInitialized = true;
} else {
s_inGridStarfield.resize(static_cast<int>(GRID_W), static_cast<int>(GRID_H));
}
const float deltaSeconds = std::clamp(static_cast<float>(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});