2024-08-03 02:42:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
#include <SDL2/SDL.h>
|
|
|
|
|
2024-08-03 03:54:16 +02:00
|
|
|
#include <chrono>
|
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 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>;
|
|
|
|
|
|
|
|
struct Point {
|
|
|
|
int x;
|
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto operator+(Point a, Point b) {
|
|
|
|
return Point{.x = a.x + b.x, .y = a.y + b.y};
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr int width = 3440;
|
|
|
|
constexpr int height = 1440;
|
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 03:54:16 +02:00
|
|
|
window_t window{SDL_CreateWindow(
|
|
|
|
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
|
|
|
width, height, SDL_WINDOW_FULLSCREEN
|
|
|
|
)};
|
|
|
|
|
|
|
|
renderer_t renderer{
|
2024-08-03 18:55:58 +02:00
|
|
|
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
|
2024-08-03 03:54:16 +02:00
|
|
|
};
|
2024-08-03 18:55:58 +02:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
2024-08-03 02:42:49 +02:00
|
|
|
|
|
|
|
if (!window) {
|
|
|
|
std::println(
|
|
|
|
"Window could not be created! SDL_Error: {}", SDL_GetError()
|
|
|
|
);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2024-08-03 18:55:58 +02:00
|
|
|
auto surface = surface_t{SDL_GetWindowSurface(window)};
|
2024-08-03 02:42:49 +02:00
|
|
|
|
2024-08-03 03:23:16 +02:00
|
|
|
auto point = Point{.x = width / 2, .y = height / 2};
|
|
|
|
const 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<int> dist(0, 3);
|
|
|
|
|
2024-08-03 02:42:49 +02:00
|
|
|
bool quit = false;
|
2024-08-03 03:54:16 +02:00
|
|
|
auto next_frame = std::chrono::steady_clock::now();
|
2024-08-03 02:42:49 +02:00
|
|
|
while (!quit) {
|
2024-08-03 18:55:58 +02:00
|
|
|
SDL_RenderDrawPoint(renderer, point.x, point.y);
|
2024-08-03 03:23:16 +02:00
|
|
|
Point newpoint;
|
|
|
|
do {
|
|
|
|
newpoint = point + directions[dist(rne)];
|
|
|
|
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
|
|
|
newpoint.y >= height);
|
|
|
|
point = newpoint;
|
|
|
|
|
2024-08-03 03:54:16 +02:00
|
|
|
if (std::chrono::steady_clock::now() > next_frame) {
|
|
|
|
next_frame += std::chrono::milliseconds{200};
|
|
|
|
|
|
|
|
SDL_Event e;
|
|
|
|
while (SDL_PollEvent(&e)) {
|
|
|
|
if (e.type == SDL_QUIT) { quit = true; }
|
|
|
|
}
|
2024-08-03 18:55:58 +02:00
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
// SDL_RenderClear(renderer);
|
2024-08-03 03:54:16 +02:00
|
|
|
}
|
2024-08-03 02:42:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
|
|
|
// leaking memory, nothing I can do
|
|
|
|
}
|