diff options
author | dec05eba <dec05eba@protonmail.com> | 2021-05-15 03:18:19 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2021-05-15 03:18:19 +0200 |
commit | 75bfa35a6d2f4e727f011e178a1d47d43f8ede71 (patch) | |
tree | a0c632c811e2cdf799982c5422a17ff52b53fad5 /src | |
parent | 3023f35077d17b7a35a6453c7ea973a6de9fe759 (diff) |
Render fps independent video fps
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/main.cpp b/src/main.cpp index 78e3221..0359622 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,6 +4,8 @@ #include <deque> int main() { + const int fps = 24; + sf::Texture texture; if(!texture.loadFromFile("flag.png")) return 1; @@ -12,8 +14,8 @@ int main() { sf::Vector2u window_size = texture.getSize(); sf::RenderWindow window(sf::VideoMode(window_size.x, window_size.y), "SFML works!", 0); - window.setVerticalSyncEnabled(true); - window.setFramerateLimit(24); + window.setVerticalSyncEnabled(false); + window.setFramerateLimit(0); sf::Shader shader; if(!shader.loadFromFile("flag.glsl", sf::Shader::Type::Fragment)) @@ -64,8 +66,9 @@ int main() { output_image.create(flag_width, flag_height, sf::Color::Transparent); const float playtime_sec = 2.0f; + const float frame_time = 1000.0 / (double)fps / 1000.0; + double elapsed_frame_time = 0.0; - sf::Clock timer; while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { @@ -73,7 +76,7 @@ int main() { window.close(); } - shader.setUniform("iTime", timer.getElapsedTime().asSeconds()); + shader.setUniform("iTime", (float)elapsed_frame_time); window.clear(sf::Color::Transparent); window.draw(vertex, 4, sf::PrimitiveType::Quads, &shader); window.display(); @@ -85,7 +88,8 @@ int main() { std::lock_guard<std::mutex> lock(images_lock); pending_frames.push_back(output_image); - if(timer.getElapsedTime().asSeconds() >= playtime_sec) + elapsed_frame_time += frame_time; + if(elapsed_frame_time >= playtime_sec) break; } |