sdl wrapper which implicitly decays to ptr

This commit is contained in:
Jakob Hördt 2024-08-03 18:55:58 +02:00
parent a1d51e54e1
commit 3f3d16c9b2

View file

@ -7,11 +7,13 @@
#include <print>
#include <random>
template <auto sdl_destroyfunc>
using sdl_deleter_t = decltype([](auto* ptr) { sdl_destroyfunc(ptr); });
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} {}
template <class SDLT, auto sdl_destroyfunc>
using sdl_wrapper_t = std::unique_ptr<SDLT, sdl_deleter_t<sdl_destroyfunc>>;
operator SDLT*() { return std::unique_ptr<SDLT, decltype(deletef)>::get(); }
};
using window_t = sdl_wrapper_t<SDL_Window, SDL_DestroyWindow>;
using surface_t = sdl_wrapper_t<SDL_Surface, SDL_FreeSurface>;
@ -42,11 +44,11 @@ int main() {
)};
renderer_t renderer{
SDL_CreateRenderer(window.get(), -1, SDL_RENDERER_ACCELERATED)
SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED)
};
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 0);
SDL_RenderClear(renderer.get());
SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
if (!window) {
std::println(
@ -55,7 +57,7 @@ int main() {
return EXIT_FAILURE;
}
auto surface = surface_t{SDL_GetWindowSurface(window.get())};
auto surface = surface_t{SDL_GetWindowSurface(window)};
auto point = Point{.x = width / 2, .y = height / 2};
const Point directions[] = {
@ -71,7 +73,7 @@ int main() {
bool quit = false;
auto next_frame = std::chrono::steady_clock::now();
while (!quit) {
SDL_RenderDrawPoint(renderer.get(), point.x, point.y);
SDL_RenderDrawPoint(renderer, point.x, point.y);
Point newpoint;
do {
newpoint = point + directions[dist(rne)];
@ -86,7 +88,8 @@ int main() {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) { quit = true; }
}
SDL_RenderPresent(renderer.get());
SDL_RenderPresent(renderer);
// SDL_RenderClear(renderer);
}
}
}