basic random walk with fancy out_ptr
This commit is contained in:
parent
9f82d9458c
commit
c718bd5e3f
1 changed files with 48 additions and 6 deletions
54
main.cpp
54
main.cpp
|
@ -4,12 +4,29 @@
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
#include <random>
|
||||||
|
|
||||||
template <auto sdl_destroyfunc>
|
template <auto sdl_destroyfunc>
|
||||||
using sdl_deleter_t = decltype([](auto* ptr) { sdl_destroyfunc(ptr); });
|
using sdl_deleter_t = decltype([](auto* ptr) { sdl_destroyfunc(ptr); });
|
||||||
|
|
||||||
using window_t = std::unique_ptr<SDL_Window, sdl_deleter_t<SDL_DestroyWindow>>;
|
template <class SDLT, auto sdl_destroyfunc>
|
||||||
using surface_t = std::unique_ptr<SDL_Surface, sdl_deleter_t<SDL_FreeSurface>>;
|
using sdl_wrapper_t = std::unique_ptr<SDLT, sdl_deleter_t<sdl_destroyfunc>>;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
|
@ -18,10 +35,15 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
auto window = window_t{SDL_CreateWindow(
|
renderer_t renderer;
|
||||||
"SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
window_t window;
|
||||||
1024, 512, SDL_WINDOW_SHOWN
|
SDL_CreateWindowAndRenderer(
|
||||||
)};
|
width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN,
|
||||||
|
std::out_ptr(window), std::out_ptr(renderer)
|
||||||
|
);
|
||||||
|
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 0);
|
||||||
|
SDL_RenderClear(renderer.get());
|
||||||
|
SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255);
|
||||||
|
|
||||||
if (!window) {
|
if (!window) {
|
||||||
std::println(
|
std::println(
|
||||||
|
@ -32,12 +54,32 @@ int main() {
|
||||||
|
|
||||||
auto surface = surface_t{SDL_GetWindowSurface(window.get())};
|
auto surface = surface_t{SDL_GetWindowSurface(window.get())};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
SDL_Event e;
|
SDL_Event e;
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
while (SDL_PollEvent(&e)) {
|
while (SDL_PollEvent(&e)) {
|
||||||
if (e.type == SDL_QUIT) { quit = true; }
|
if (e.type == SDL_QUIT) { quit = true; }
|
||||||
}
|
}
|
||||||
|
SDL_RenderDrawPoint(renderer.get(), point.x, point.y);
|
||||||
|
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;
|
||||||
|
|
||||||
|
SDL_RenderPresent(renderer.get());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue