feat: add trail
This commit is contained in:
@@ -2,8 +2,6 @@
|
||||
#define GFX_UTILS_HPP
|
||||
#include <SDL3/SDL_render.h>
|
||||
|
||||
namespace gfx {
|
||||
void draw_circle(SDL_Renderer *renderer, int center_x, int center_y, int radius, bool fill);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,14 +1,26 @@
|
||||
#ifndef SIMS_HPP
|
||||
#define SIMS_HPP
|
||||
#include <SDL3/SDL_render.h>
|
||||
#define SIMS_HPP
|
||||
|
||||
namespace sims {
|
||||
typedef struct {
|
||||
int x;
|
||||
int y;
|
||||
int r;
|
||||
} body;
|
||||
void two_bodies(SDL_Renderer *r);
|
||||
}
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include <utility>
|
||||
|
||||
class Body {
|
||||
private:
|
||||
float x;
|
||||
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
|
||||
|
||||
@@ -1,39 +1,37 @@
|
||||
#include <SDL3/SDL_render.h>
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
30
src/main.cpp
30
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;
|
||||
|
||||
@@ -1,13 +1,44 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include "../../include/gfx.hpp"
|
||||
#include <SDL3/SDL_log.h>
|
||||
#include <SDL3/SDL_render.h>
|
||||
#include "../../include/sims.hpp"
|
||||
#include "../../include/gfx.hpp"
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <iterator>
|
||||
#include <utility>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user