2024-08-03 02:42:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2024-08-03 22:06:57 +02:00
|
|
|
#include <algorithm>
|
2024-08-03 03:54:16 +02:00
|
|
|
#include <chrono>
|
2024-08-03 22:06:57 +02:00
|
|
|
#include <functional>
|
2024-08-03 02:42:49 +02:00
|
|
|
#include <memory>
|
|
|
|
#include <print>
|
2024-08-03 03:23:16 +02:00
|
|
|
#include <random>
|
2024-08-03 22:06:57 +02:00
|
|
|
#include <vector>
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 18:55:58 +02:00
|
|
|
template <typename SDLT, auto deletef>
|
|
|
|
struct sdl_wrapper_t : std::unique_ptr<SDLT, decltype(deletef)> {
|
|
|
|
explicit sdl_wrapper_t(SDLT* p)
|
|
|
|
: std::unique_ptr<SDLT, decltype(deletef)>{p, deletef} {}
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 18:55:58 +02:00
|
|
|
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
|
|
|
|
};
|
2024-08-03 03:23:16 +02:00
|
|
|
|
|
|
|
using window_t = sdl_wrapper_t<SDL_Window, SDL_DestroyWindow>;
|
|
|
|
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
|
|
|
|
using renderer_t = sdl_wrapper_t<SDL_Renderer, SDL_DestroyRenderer>;
|
|
|
|
|
2024-08-03 22:06:57 +02:00
|
|
|
auto operator+(SDL_Point a, SDL_Point b) {
|
|
|
|
return SDL_Point{.x = a.x + b.x, .y = a.y + b.y};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto operator<=>(const SDL_Point& a, const SDL_Point& b) {
|
|
|
|
auto y_comp = a.x <=> b.x;
|
|
|
|
if (y_comp != std::strong_ordering::equal) { return y_comp; }
|
|
|
|
return a.y <=> b.y;
|
|
|
|
}
|
2024-08-03 03:23:16 +02:00
|
|
|
|
2024-08-03 22:06:57 +02:00
|
|
|
auto operator==(const SDL_Point& a, const SDL_Point& b) {
|
|
|
|
return a.x == b.x && a.y == b.y;
|
2024-08-03 03:23:16 +02:00
|
|
|
}
|
|
|
|
|
2024-08-03 23:06:35 +02:00
|
|
|
constexpr int width = 1000;
|
|
|
|
constexpr int height = 1000;
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 22:40:14 +02:00
|
|
|
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(); }
|
|
|
|
};
|
|
|
|
|
2024-08-03 02:42:49 +02:00
|
|
|
int main() {
|
|
|
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
|
|
|
std::println("fuck");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2024-08-03 22:40:14 +02:00
|
|
|
Defer<SDL_Quit> defer_SDL_Quit;
|
|
|
|
|
|
|
|
window_t window{SDL_CreateWindow(
|
|
|
|
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
|
2024-08-03 23:06:35 +02:00
|
|
|
height, 0
|
2024-08-03 22:40:14 +02:00
|
|
|
)};
|
|
|
|
if (!window) {
|
|
|
|
std::println(
|
|
|
|
"Window could not be created! SDL_Error: {}", SDL_GetError()
|
|
|
|
);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 22:40:14 +02:00
|
|
|
// owned by window
|
2024-08-03 23:06:35 +02:00
|
|
|
auto* window_surface = SDL_GetWindowSurface(window);
|
|
|
|
if (!window_surface) { return EXIT_FAILURE; }
|
|
|
|
if (window_surface->format->palette) { return EXIT_FAILURE; }
|
2024-08-03 22:40:14 +02:00
|
|
|
|
|
|
|
SDL_Point pos{.x = width / 2, .y = height / 2};
|
|
|
|
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);
|
|
|
|
|
2024-08-03 23:06:35 +02:00
|
|
|
for (bool continu = true; continu;) {
|
|
|
|
|
2024-08-03 22:40:14 +02:00
|
|
|
{
|
|
|
|
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;
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
2024-08-03 23:06:35 +02:00
|
|
|
for (SDL_Event e; SDL_PollEvent(&e);) {
|
|
|
|
if (e.type == SDL_QUIT) { continu = false; }
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|
2024-08-03 23:06:35 +02:00
|
|
|
|
|
|
|
SDL_UpdateWindowSurface(window);
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// leaking memory, nothing I can do
|
|
|
|
}
|