cool fps control but broken
Backbuffer gets invalid after present
This commit is contained in:
parent
c718bd5e3f
commit
a1d51e54e1
1 changed files with 19 additions and 11 deletions
30
main.cpp
30
main.cpp
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <print>
|
#include <print>
|
||||||
#include <random>
|
#include <random>
|
||||||
|
@ -35,12 +36,14 @@ int main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
renderer_t renderer;
|
window_t window{SDL_CreateWindow(
|
||||||
window_t window;
|
"random walk", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
SDL_CreateWindowAndRenderer(
|
width, height, SDL_WINDOW_FULLSCREEN
|
||||||
width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN,
|
)};
|
||||||
std::out_ptr(window), std::out_ptr(renderer)
|
|
||||||
);
|
renderer_t renderer{
|
||||||
|
SDL_CreateRenderer(window.get(), -1, SDL_RENDERER_ACCELERATED)
|
||||||
|
};
|
||||||
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 0);
|
SDL_SetRenderDrawColor(renderer.get(), 0, 0, 0, 0);
|
||||||
SDL_RenderClear(renderer.get());
|
SDL_RenderClear(renderer.get());
|
||||||
SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255);
|
SDL_SetRenderDrawColor(renderer.get(), 255, 0, 0, 255);
|
||||||
|
@ -65,12 +68,9 @@ int main() {
|
||||||
std::mt19937_64 rne(std::random_device{}());
|
std::mt19937_64 rne(std::random_device{}());
|
||||||
std::uniform_int_distribution<int> dist(0, 3);
|
std::uniform_int_distribution<int> dist(0, 3);
|
||||||
|
|
||||||
SDL_Event e;
|
|
||||||
bool quit = false;
|
bool quit = false;
|
||||||
|
auto next_frame = std::chrono::steady_clock::now();
|
||||||
while (!quit) {
|
while (!quit) {
|
||||||
while (SDL_PollEvent(&e)) {
|
|
||||||
if (e.type == SDL_QUIT) { quit = true; }
|
|
||||||
}
|
|
||||||
SDL_RenderDrawPoint(renderer.get(), point.x, point.y);
|
SDL_RenderDrawPoint(renderer.get(), point.x, point.y);
|
||||||
Point newpoint;
|
Point newpoint;
|
||||||
do {
|
do {
|
||||||
|
@ -79,7 +79,15 @@ int main() {
|
||||||
newpoint.y >= height);
|
newpoint.y >= height);
|
||||||
point = newpoint;
|
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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue