diff --git a/include/sims.hpp b/include/sims.hpp index 7d6689f..9d870d3 100644 --- a/include/sims.hpp +++ b/include/sims.hpp @@ -1,6 +1,7 @@ #ifndef SIMS_HPP #define SIMS_HPP +#include #include #include @@ -10,10 +11,11 @@ private: float y; float vx = 1; float vy = 1; - std::pair old_positions[100]; + std::pair old_positions[200]; public: SDL_Renderer* renderer; + SDL_Color color = {255, 255, 255, 255}; // white Body(int x_pos, int y_pos); diff --git a/src/main.cpp b/src/main.cpp index 564e3bc..60b16f2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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; diff --git a/src/sims/two_bodies.cpp b/src/sims/two_bodies.cpp index 0f74020..63b505e 100644 --- a/src/sims/two_bodies.cpp +++ b/src/sims/two_bodies.cpp @@ -5,6 +5,7 @@ #include "../../include/gfx.hpp" #include #include +#include #include #include @@ -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]; }