diff --git a/main.cpp b/main.cpp index cfc2bea..89fb39b 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -35,12 +36,14 @@ int main() { } { - renderer_t renderer; - window_t window; - SDL_CreateWindowAndRenderer( - width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN, - std::out_ptr(window), std::out_ptr(renderer) - ); + window_t window{SDL_CreateWindow( + "random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, + width, height, SDL_WINDOW_FULLSCREEN + )}; + + renderer_t renderer{ + SDL_CreateRenderer(window.get(), -1, SDL_RENDERER_ACCELERATED) + }; SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 0); SDL_RenderClear(renderer.get()); SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255); @@ -65,12 +68,9 @@ int main() { std::mt19937_64 rne(std::random_device{}()); std::uniform_int_distribution dist(0, 3); - SDL_Event e; bool quit = false; + auto next_frame = std::chrono::steady_clock::now(); while (!quit) { - while (SDL_PollEvent(&e)) { - if (e.type == SDL_QUIT) { quit = true; } - } SDL_RenderDrawPoint(renderer.get(), point.x, point.y); Point newpoint; do { @@ -79,7 +79,15 @@ int main() { newpoint.y >= height); point = newpoint; - SDL_RenderPresent(renderer.get()); + 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; } + } + SDL_RenderPresent(renderer.get()); + } } }