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

@ -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};