defer SDL_Quit()

This commit is contained in:
Jakob Hördt 2024-08-03 22:40:14 +02:00
parent 3c725d105e
commit ae43c1c0ef

177
main.cpp
View file

@ -39,112 +39,119 @@ auto operator==(const SDL_Point& a, const SDL_Point& b) {
constexpr int width = 3440; constexpr int width = 3440;
constexpr int height = 1440; constexpr int height = 1440;
template <auto EF>
struct [[nodiscard("give this a name so SDL_Quit is called at the end"
)]] Defer {
Defer() = default;
Defer(const Defer&) = delete;
Defer(Defer&&) = delete;
auto operator=(Defer&&) = delete;
auto operator=(const Defer&) = delete;
~Defer() { EF(); }
};
int main() { int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) { if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::println("fuck"); std::println("fuck");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
Defer<SDL_Quit> defer_SDL_Quit;
{ window_t window{SDL_CreateWindow(
window_t window{SDL_CreateWindow( "random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, height, SDL_WINDOW_FULLSCREEN
width, height, SDL_WINDOW_FULLSCREEN )};
)}; if (!window) {
std::println(
"Window could not be created! SDL_Error: {}", SDL_GetError()
);
return EXIT_FAILURE;
}
renderer_t renderer{ // owned by window
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED) // auto* window_surface = SDL_GetWindowSurface(window);
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
if (!window) { renderer_t renderer{SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
};
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
auto surface = surface_t{SDL_GetWindowSurface(window)};
std::vector<SDL_Point> points;
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 = 0, .y = -1}, {.x = 0, .y = 1}
};
std::mt19937_64 rne(std::random_device{}());
std::uniform_int_distribution dist(0, 3);
bool continu = true;
const auto start = std::chrono::steady_clock::now();
auto next_frame = start;
long total_points = 0;
while (continu) {
{
SDL_Point newpoint;
do {
newpoint = pos + directions[dist(rne)];
} 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( std::println(
"Window could not be created! SDL_Error: {}", SDL_GetError() "took {} to get to {} random steps",
std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::steady_clock::now() - start
),
goal
); );
return EXIT_FAILURE;
} }
auto surface = surface_t{SDL_GetWindowSurface(window)}; // render present; poll events
auto render_start = std::chrono::steady_clock::now();
if (render_start > next_frame) {
next_frame += std::chrono::milliseconds{200};
std::vector<SDL_Point> points;
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 = 0, .y = -1},
{.x = 0, .y = 1}
};
std::mt19937_64 rne(std::random_device{}());
std::uniform_int_distribution dist(0, 3);
bool continu = true;
const auto start = std::chrono::steady_clock::now();
auto next_frame = start;
long total_points = 0;
while (continu) {
{ {
SDL_Point newpoint; std::ranges::sort(points, std::less<>{});
do { auto [first, last] = std::ranges::unique(points);
newpoint = pos + directions[dist(rne)]; points.resize(std::ranges::distance(points.begin(), first));
} 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; SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
if (total_points >= goal) { SDL_RenderDrawPoints(renderer, points.data(), std::ssize(points));
continu = false; SDL_RenderPresent(renderer);
std::println( SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
"took {} to get to {} random steps", SDL_RenderClear(renderer);
std::chrono::duration_cast<std::chrono::duration<double>>(
std::chrono::steady_clock::now() - start SDL_Event e;
), while (SDL_PollEvent(&e)) {
goal if (e.type == SDL_QUIT) { continu = false; }
);
} }
// render present; poll events auto render_end = std::chrono::steady_clock::now();
auto render_start = std::chrono::steady_clock::now();
if (render_start > next_frame) {
next_frame += std::chrono::milliseconds{200};
{ std::println(
std::ranges::sort(points, std::less<>{}); "drawn {} points in {}", std::ssize(points),
auto [first, last] = std::ranges::unique(points); std::chrono::duration_cast<std::chrono::milliseconds>(
points.resize(std::ranges::distance(points.begin(), first)); render_end - render_start
} )
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); );
SDL_RenderDrawPoints( if (render_end > next_frame) {
renderer, points.data(), std::ssize(points) next_frame = render_end + std::chrono::milliseconds{100};
);
SDL_RenderPresent(renderer);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_Event e;
while (SDL_PollEvent(&e)) {
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_Quit();
// leaking memory, nothing I can do // leaking memory, nothing I can do
} }