feat: trail fades out
This commit is contained in:
@@ -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);
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user