feat: trail fades out

This commit is contained in:
2025-11-18 19:22:01 +01:00
parent 89fe0e0f65
commit a5add0765f
3 changed files with 17 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
#ifndef SIMS_HPP
#define SIMS_HPP
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_render.h>
#include <utility>
@@ -10,10 +11,11 @@ private:
float y;
float vx = 1;
float vy = 1;
std::pair<float, float> old_positions[100];
std::pair<float, float> old_positions[200];
public:
SDL_Renderer* renderer;
SDL_Color color = {255, 255, 255, 255}; // white
Body(int x_pos, int y_pos);

View File

@@ -62,6 +62,8 @@ int main() {
if (renderer == NULL) {
std::printf("SDL_CreateRenderer() failed");
}
// enables alpha
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_Event e;

View File

@@ -5,6 +5,7 @@
#include "../../include/gfx.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iterator>
#include <utility>
@@ -17,11 +18,20 @@ Body::Body(int x_pos, int y_pos) : x(x_pos), y(y_pos) {
void Body::draw_tail() {
size_t n = std::size(old_positions);
uint8_t initial_alpha = color.a;
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
for (size_t i = 0; i < n; ++i) {
const auto& p = old_positions[i];
SDL_RenderPoint(renderer, p.first, p.second);
float f = 1.0f;
if (i > 100) {
// linear interpolation
f = 1.0f - float(i - 100) / float(n - 100);
}
uint8_t a = uint8_t(color.a * std::clamp(f, 0.0f, 1.0f));
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, a);
SDL_RenderPoint(renderer, old_positions[i].first, old_positions[i].second);
}
for (size_t i = n - 1; i > 0; --i) {
old_positions[i] = old_positions[i - 1];
}