naive rendering of random walk points
This commit is contained in:
parent
3f3d16c9b2
commit
3c725d105e
1 changed files with 74 additions and 23 deletions
97
main.cpp
97
main.cpp
|
@ -2,10 +2,13 @@
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
template <typename SDLT, auto deletef>
|
template <typename SDLT, auto deletef>
|
||||||
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
||||||
|
@ -19,13 +22,18 @@ using window_t = sdl_wrapper_t<SDL_Window, SDL_DestroyWindow>;
|
||||||
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
|
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
|
||||||
using renderer_t = sdl_wrapper_t<SDL_Renderer, SDL_DestroyRenderer>;
|
using renderer_t = sdl_wrapper_t<SDL_Renderer, SDL_DestroyRenderer>;
|
||||||
|
|
||||||
struct Point {
|
auto operator+(SDL_Point a, SDL_Point b) {
|
||||||
int x;
|
return SDL_Point{.x = a.x + b.x, .y = a.y + b.y};
|
||||||
int y;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
auto operator+(Point a, Point b) {
|
auto operator<=>(const SDL_Point& a, const SDL_Point& b) {
|
||||||
return Point{.x = a.x + b.x, .y = a.y + b.y};
|
auto y_comp = a.x <=> b.x;
|
||||||
|
if (y_comp != std::strong_ordering::equal) { return y_comp; }
|
||||||
|
return a.y <=> b.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto operator==(const SDL_Point& a, const SDL_Point& b) {
|
||||||
|
return a.x == b.x && a.y == b.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr int width = 3440;
|
constexpr int width = 3440;
|
||||||
|
@ -59,8 +67,10 @@ int main() {
|
||||||
|
|
||||||
auto surface = surface_t{SDL_GetWindowSurface(window)};
|
auto surface = surface_t{SDL_GetWindowSurface(window)};
|
||||||
|
|
||||||
auto point = Point{.x = width / 2, .y = height / 2};
|
std::vector<SDL_Point> points;
|
||||||
const Point directions[] = {
|
SDL_Point pos{.x = width / 2, .y = height / 2};
|
||||||
|
points.push_back(pos);
|
||||||
|
const SDL_Point directions[] = {
|
||||||
{.x = -1, .y = 0},
|
{.x = -1, .y = 0},
|
||||||
{.x = 1, .y = 0},
|
{.x = 1, .y = 0},
|
||||||
{.x = 0, .y = -1},
|
{.x = 0, .y = -1},
|
||||||
|
@ -68,28 +78,69 @@ int main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
std::mt19937_64 rne(std::random_device{}());
|
std::mt19937_64 rne(std::random_device{}());
|
||||||
std::uniform_int_distribution<int> dist(0, 3);
|
std::uniform_int_distribution dist(0, 3);
|
||||||
|
|
||||||
bool quit = false;
|
bool continu = true;
|
||||||
auto next_frame = std::chrono::steady_clock::now();
|
const auto start = std::chrono::steady_clock::now();
|
||||||
while (!quit) {
|
auto next_frame = start;
|
||||||
SDL_RenderDrawPoint(renderer, point.x, point.y);
|
long total_points = 0;
|
||||||
Point newpoint;
|
while (continu) {
|
||||||
do {
|
{
|
||||||
newpoint = point + directions[dist(rne)];
|
SDL_Point newpoint;
|
||||||
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
do {
|
||||||
newpoint.y >= height);
|
newpoint = pos + directions[dist(rne)];
|
||||||
point = newpoint;
|
} while (newpoint.x < 0 or newpoint.x >= width or
|
||||||
|
newpoint.y < 0 or newpoint.y >= height);
|
||||||
|
pos = newpoint;
|
||||||
|
points.push_back(newpoint);
|
||||||
|
++total_points;
|
||||||
|
}
|
||||||
|
const auto goal = 200'000'000;
|
||||||
|
if (total_points >= goal) {
|
||||||
|
continu = false;
|
||||||
|
std::println(
|
||||||
|
"took {} to get to {} random steps",
|
||||||
|
std::chrono::duration_cast<std::chrono::duration<double>>(
|
||||||
|
std::chrono::steady_clock::now() - start
|
||||||
|
),
|
||||||
|
goal
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (std::chrono::steady_clock::now() > next_frame) {
|
// render present; poll events
|
||||||
|
auto render_start = std::chrono::steady_clock::now();
|
||||||
|
if (render_start > next_frame) {
|
||||||
next_frame += std::chrono::milliseconds{200};
|
next_frame += std::chrono::milliseconds{200};
|
||||||
|
|
||||||
|
{
|
||||||
|
std::ranges::sort(points, std::less<>{});
|
||||||
|
auto [first, last] = std::ranges::unique(points);
|
||||||
|
points.resize(std::ranges::distance(points.begin(), first));
|
||||||
|
}
|
||||||
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||||
|
SDL_RenderDrawPoints(
|
||||||
|
renderer, points.data(), std::ssize(points)
|
||||||
|
);
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
while (SDL_PollEvent(&e)) {
|
while (SDL_PollEvent(&e)) {
|
||||||
if (e.type == SDL_QUIT) { quit = true; }
|
if (e.type == SDL_QUIT) { continu = false; }
|
||||||
|
}
|
||||||
|
|
||||||
|
auto render_end = std::chrono::steady_clock::now();
|
||||||
|
|
||||||
|
std::println(
|
||||||
|
"drawn {} points in {}", std::ssize(points),
|
||||||
|
std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||||
|
render_end - render_start
|
||||||
|
)
|
||||||
|
);
|
||||||
|
if (render_end > next_frame) {
|
||||||
|
next_frame = render_end + std::chrono::milliseconds{100};
|
||||||
}
|
}
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
// SDL_RenderClear(renderer);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue