defer SDL_Quit()
This commit is contained in:
parent
3c725d105e
commit
ae43c1c0ef
1 changed files with 92 additions and 85 deletions
51
main.cpp
51
main.cpp
|
@ -39,25 +39,29 @@ auto operator==(const SDL_Point& a, const SDL_Point& b) {
|
|||
constexpr int width = 3440;
|
||||
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() {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
std::println("fuck");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
Defer<SDL_Quit> defer_SDL_Quit;
|
||||
|
||||
{
|
||||
window_t window{SDL_CreateWindow(
|
||||
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
width, height, SDL_WINDOW_FULLSCREEN
|
||||
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width,
|
||||
height, SDL_WINDOW_FULLSCREEN
|
||||
)};
|
||||
|
||||
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);
|
||||
|
||||
if (!window) {
|
||||
std::println(
|
||||
"Window could not be created! SDL_Error: {}", SDL_GetError()
|
||||
|
@ -65,16 +69,23 @@ int main() {
|
|||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// owned by window
|
||||
// auto* window_surface = SDL_GetWindowSurface(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}
|
||||
{.x = -1, .y = 0}, {.x = 1, .y = 0}, {.x = 0, .y = -1}, {.x = 0, .y = 1}
|
||||
};
|
||||
|
||||
std::mt19937_64 rne(std::random_device{}());
|
||||
|
@ -89,8 +100,8 @@ int main() {
|
|||
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);
|
||||
} while (newpoint.x < 0 or newpoint.x >= width or newpoint.y < 0 or
|
||||
newpoint.y >= height);
|
||||
pos = newpoint;
|
||||
points.push_back(newpoint);
|
||||
++total_points;
|
||||
|
@ -118,9 +129,7 @@ int main() {
|
|||
points.resize(std::ranges::distance(points.begin(), first));
|
||||
}
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
SDL_RenderDrawPoints(
|
||||
renderer, points.data(), std::ssize(points)
|
||||
);
|
||||
SDL_RenderDrawPoints(renderer, points.data(), std::ssize(points));
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
|
||||
SDL_RenderClear(renderer);
|
||||
|
@ -143,8 +152,6 @@ int main() {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
// leaking memory, nothing I can do
|
||||
}
|
Loading…
Reference in a new issue