aboutsummaryrefslogtreecommitdiff
path: root/src/Vsync.cpp
blob: ab882055ec0f1017a0c46dc21c4cbf4c89be521d (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) {
        sf::Int64 sleep_time_micro = target_frame_delta_micro - timer.getElapsedTime().asMicroseconds();
        window.display();
        if(sleep_time_micro > 0) {
            if(usleep(sleep_time_micro) != 0) {
                fprintf(stderr, "failed to sleep!\n");
            }
            timer.restart();
        }
    }
}