From 89fe0e0f650d7a1601467225a416656c0fe3a93f Mon Sep 17 00:00:00 2001 From: Alejandro Laguna Date: Sat, 15 Nov 2025 18:29:46 +0100 Subject: [PATCH] feat: add trail --- include/gfx.hpp | 2 -- include/sims.hpp | 32 +++++++++++++++------- src/gfx/circle.cpp | 60 ++++++++++++++++++++--------------------- src/main.cpp | 30 ++++++++++++--------- src/sims/two_bodies.cpp | 47 ++++++++++++++++++++++++++------ 5 files changed, 107 insertions(+), 64 deletions(-) diff --git a/include/gfx.hpp b/include/gfx.hpp index f835044..fce7df4 100644 --- a/include/gfx.hpp +++ b/include/gfx.hpp @@ -2,8 +2,6 @@ #define GFX_UTILS_HPP #include -namespace gfx { void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill); -} #endif diff --git a/include/sims.hpp b/include/sims.hpp index 53c8cd9..7d6689f 100644 --- a/include/sims.hpp +++ b/include/sims.hpp @@ -1,14 +1,26 @@ #ifndef SIMS_HPP - #define SIMS_HPP - #include +#define SIMS_HPP -namespace sims { - typedef struct { - int x; - int y; - int r; - } body; - void two_bodies(SDL_Renderer *r); -} +#include +#include + +class Body { +private: + float x; + float y; + float vx = 1; + float vy = 1; + std::pair old_positions[100]; + +public: + SDL_Renderer* renderer; + + Body(int x_pos, int y_pos); + + void draw_tail(); + void live(); +}; + +void two_bodies(SDL_Renderer* r, Body &a, Body &b); #endif diff --git a/src/gfx/circle.cpp b/src/gfx/circle.cpp index d6265b5..2647962 100644 --- a/src/gfx/circle.cpp +++ b/src/gfx/circle.cpp @@ -1,39 +1,37 @@ #include -namespace gfx { - void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill) { - int x = radius; - int y = 0; - int decision = 1 - x; +void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill) { + int x = radius; + int y = 0; + int decision = 1 - x; - SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - while (x >= y) { - if (fill) { - // Draw horizontal lines across the circle - SDL_RenderLine(renderer, center_x - x, center_y + y, center_x + x, center_y + y); - SDL_RenderLine(renderer, center_x - x, center_y - y, center_x + x, center_y - y); - SDL_RenderLine(renderer, center_x - y, center_y + x, center_x + y, center_y + x); - SDL_RenderLine(renderer, center_x - y, center_y - x, center_x + y, center_y - x); - } else { - // Just draw points for the outline - SDL_RenderPoint(renderer, center_x + x, center_y + y); - SDL_RenderPoint(renderer, center_x - x, center_y + y); - SDL_RenderPoint(renderer, center_x + x, center_y - y); - SDL_RenderPoint(renderer, center_x - x, center_y - y); - SDL_RenderPoint(renderer, center_x + y, center_y + x); - SDL_RenderPoint(renderer, center_x - y, center_y + x); - SDL_RenderPoint(renderer, center_x + y, center_y - x); - SDL_RenderPoint(renderer, center_x - y, center_y - x); - } + while (x >= y) { + if (fill) { + // Draw horizontal lines across the circle + SDL_RenderLine(renderer, center_x - x, center_y + y, center_x + x, center_y + y); + SDL_RenderLine(renderer, center_x - x, center_y - y, center_x + x, center_y - y); + SDL_RenderLine(renderer, center_x - y, center_y + x, center_x + y, center_y + x); + SDL_RenderLine(renderer, center_x - y, center_y - x, center_x + y, center_y - x); + } else { + // Just draw points for the outline + SDL_RenderPoint(renderer, center_x + x, center_y + y); + SDL_RenderPoint(renderer, center_x - x, center_y + y); + SDL_RenderPoint(renderer, center_x + x, center_y - y); + SDL_RenderPoint(renderer, center_x - x, center_y - y); + SDL_RenderPoint(renderer, center_x + y, center_y + x); + SDL_RenderPoint(renderer, center_x - y, center_y + x); + SDL_RenderPoint(renderer, center_x + y, center_y - x); + SDL_RenderPoint(renderer, center_x - y, center_y - x); + } - y++; - if (decision <= 0) { - decision += 2 * y + 1; - } else { - x--; - decision += 2 * (y - x + 1); - } + y++; + if (decision <= 0) { + decision += 2 * y + 1; + } else { + x--; + decision += 2 * (y - x + 1); } } } diff --git a/src/main.cpp b/src/main.cpp index 41fbbbf..564e3bc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,8 +27,22 @@ bool event_handling(SDL_Event e) { return false; } -void sim(SDL_Renderer *r) { - sims::two_bodies(r); +void sim(SDL_Renderer *r, SDL_Event e) { + bool exit = false; + + Body body1 = Body(50, 50); + body1.renderer = r; + Body body2 = Body(50, 100); + body2.renderer = r; + + while (!exit) { + exit = event_handling(e); + SDL_SetRenderDrawColor(r, 0, 0, 0, 0); + SDL_RenderClear(r); + two_bodies(r, body1, body2); + SDL_RenderPresent(r); + SDL_Delay(16); + } } int main() { @@ -51,17 +65,7 @@ int main() { SDL_Event e; - bool exit = false; - while (!exit) { - exit = event_handling(e); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); - SDL_RenderClear(renderer); - - sim(renderer); - - SDL_RenderPresent(renderer); - SDL_Delay(16); - } + sim(renderer, e); SDL_Quit(); return 0; diff --git a/src/sims/two_bodies.cpp b/src/sims/two_bodies.cpp index 36fc926..0f74020 100644 --- a/src/sims/two_bodies.cpp +++ b/src/sims/two_bodies.cpp @@ -1,13 +1,44 @@ #include -#include "../../include/gfx.hpp" +#include +#include #include "../../include/sims.hpp" +#include "../../include/gfx.hpp" +#include +#include +#include +#include -namespace sims { - void two_bodies(SDL_Renderer *r) { - body body1 = {10, 10, 10}; - body body2 = {250, 250, 10}; +using namespace std; - gfx::draw_circle(r, body1.x, body1.y, body1.r, true); - gfx::draw_circle(r, body2.x, body2.y, body2.r, true); - } +Body::Body(int x_pos, int y_pos) : x(x_pos), y(y_pos) { + fill(begin(old_positions), end(old_positions), make_pair(x, y)); } + +void Body::draw_tail() { + size_t n = std::size(old_positions); + + for (size_t i = 0; i < n; ++i) { + const auto& p = old_positions[i]; + SDL_RenderPoint(renderer, p.first, p.second); + } + + for (size_t i = n - 1; i > 0; --i) { + old_positions[i] = old_positions[i - 1]; + } + + old_positions[0] = {x, y}; +} + + +void Body::live() { + x += vx; + y += sin(x * 0.1); + draw_circle(renderer, x, y, 10, true); + draw_tail(); +} + +void two_bodies(SDL_Renderer* r, Body& a, Body& b) { + a.live(); + b.live(); +} +