aboutsummaryrefslogtreecommitdiff
path: root/src/Vsync.cpp
blob: d46f9fa29580800f26fd1cad23df1699f7698997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "../include/Vsync.hpp"
#include <time.h>
#include <unistd.h>

namespace QuickMedia {
    VSync::VSync(int framerate) : target_frame_delta_micro(1000.0 / (double)framerate * 1000.0), overflow(0) {
        
    }

    void VSync::display(sf::RenderWindow &window) {
        window.display();
        sf::Int64 sleep_time_micro = target_frame_delta_micro - timer.getElapsedTime().asMicroseconds();
        if(sleep_time_micro > 0) {
            if(usleep(sleep_time_micro) != 0) {
                fprintf(stderr, "failed to sleep!\n");
            }
        }
        timer.restart();
    }
}