diff --git a/main.cpp b/main.cpp index b6285a1..a39e826 100644 --- a/main.cpp +++ b/main.cpp @@ -2,10 +2,13 @@ #include +#include #include #include #include +namespace { + template struct sdl_wrapper_t : std::unique_ptr { explicit sdl_wrapper_t(SDLT* p) @@ -47,6 +50,15 @@ struct [[nodiscard("give this a name so SDL_Quit is called at the end" ~Defer() { EF(); } }; +using clock = std::chrono::steady_clock; + +auto poll_events(bool& continu) { + for (SDL_Event e; SDL_PollEvent(&e);) { + if (e.type == SDL_QUIT) { continu = false; } + } +} +} // namespace + int main() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { std::println("fuck"); @@ -87,23 +99,29 @@ int main() { // aarrggbb?? constexpr Uint32 walk_color = 0xff00ff00; + const auto start_time = clock::now(); + auto next_poll_events_time = start_time; for (bool continu = true; continu;) { - 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; + SDL_LockSurface(window_surface); - static_cast(window_surface->pixels - )[pos.x + pos.y * window_surface->h] = walk_color; - SDL_UnlockSurface(window_surface); - - for (SDL_Event e; SDL_PollEvent(&e);) { - if (e.type == SDL_QUIT) { continu = false; } + for (auto batch = 0z; batch < 1000; ++batch) { + 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; + static_cast(window_surface->pixels + )[pos.x + pos.y * window_surface->h] = walk_color; } - + SDL_UnlockSurface(window_surface); SDL_UpdateWindowSurface(window); + + const auto now = clock::now(); + if (now >= next_poll_events_time) { + next_poll_events_time = now + std::chrono::milliseconds{100}; + poll_events(continu); + } } // leaking memory, nothing I can do