cool fps control but broken

Backbuffer gets invalid after present
This commit is contained in:
Jakob Hördt 2024-08-03 03:54:16 +02:00
parent c718bd5e3f
commit a1d51e54e1

View file

@ -2,6 +2,7 @@
#include <SDL2/SDL.h>
#include <chrono>
#include <memory>
#include <print>
#include <random>
@ -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<int> 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());
}
}
}