feat: add trail
This commit is contained in:
@@ -2,8 +2,6 @@
|
|||||||
#define GFX_UTILS_HPP
|
#define GFX_UTILS_HPP
|
||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
|
|
||||||
namespace gfx {
|
|
||||||
void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill);
|
void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill);
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,14 +1,26 @@
|
|||||||
#ifndef SIMS_HPP
|
#ifndef SIMS_HPP
|
||||||
#define SIMS_HPP
|
#define SIMS_HPP
|
||||||
#include <SDL3/SDL_render.h>
|
|
||||||
|
|
||||||
namespace sims {
|
#include <SDL3/SDL_render.h>
|
||||||
typedef struct {
|
#include <utility>
|
||||||
int x;
|
|
||||||
int y;
|
class Body {
|
||||||
int r;
|
private:
|
||||||
} body;
|
float x;
|
||||||
void two_bodies(SDL_Renderer *r);
|
float y;
|
||||||
}
|
float vx = 1;
|
||||||
|
float vy = 1;
|
||||||
|
std::pair<float, float> 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
|
#endif
|
||||||
|
|||||||
@@ -1,39 +1,37 @@
|
|||||||
#include <SDL3/SDL_render.h>
|
#include <SDL3/SDL_render.h>
|
||||||
|
|
||||||
namespace gfx {
|
void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill) {
|
||||||
void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill) {
|
int x = radius;
|
||||||
int x = radius;
|
int y = 0;
|
||||||
int y = 0;
|
int decision = 1 - x;
|
||||||
int decision = 1 - x;
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255);
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||||
|
|
||||||
while (x >= y) {
|
while (x >= y) {
|
||||||
if (fill) {
|
if (fill) {
|
||||||
// Draw horizontal lines across the circle
|
// 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 - 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);
|
||||||
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 {
|
} else {
|
||||||
// Just draw points for the outline
|
// 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 + 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);
|
||||||
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++;
|
y++;
|
||||||
if (decision <= 0) {
|
if (decision <= 0) {
|
||||||
decision += 2 * y + 1;
|
decision += 2 * y + 1;
|
||||||
} else {
|
} else {
|
||||||
x--;
|
x--;
|
||||||
decision += 2 * (y - x + 1);
|
decision += 2 * (y - x + 1);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/main.cpp
30
src/main.cpp
@@ -27,8 +27,22 @@ bool event_handling(SDL_Event e) {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void sim(SDL_Renderer *r) {
|
void sim(SDL_Renderer *r, SDL_Event e) {
|
||||||
sims::two_bodies(r);
|
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() {
|
int main() {
|
||||||
@@ -51,17 +65,7 @@ int main() {
|
|||||||
|
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
|
|
||||||
bool exit = false;
|
sim(renderer, e);
|
||||||
while (!exit) {
|
|
||||||
exit = event_handling(e);
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
|
|
||||||
sim(renderer);
|
|
||||||
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
SDL_Delay(16);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,13 +1,44 @@
|
|||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include "../../include/gfx.hpp"
|
#include <SDL3/SDL_log.h>
|
||||||
|
#include <SDL3/SDL_render.h>
|
||||||
#include "../../include/sims.hpp"
|
#include "../../include/sims.hpp"
|
||||||
|
#include "../../include/gfx.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <cmath>
|
||||||
|
#include <iterator>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
namespace sims {
|
using namespace std;
|
||||||
void two_bodies(SDL_Renderer *r) {
|
|
||||||
body body1 = {10, 10, 10};
|
|
||||||
body body2 = {250, 250, 10};
|
|
||||||
|
|
||||||
gfx::draw_circle(r, body1.x, body1.y, body1.r, true);
|
Body::Body(int x_pos, int y_pos) : x(x_pos), y(y_pos) {
|
||||||
gfx::draw_circle(r, body2.x, body2.y, body2.r, true);
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user