From 6244999b080a68abcf7ee2d54a425eb776580cc9 Mon Sep 17 00:00:00 2001 From: Lauchmelder Date: Sat, 16 Apr 2022 14:00:43 +0200 Subject: [PATCH] implemented poor timing --- Makefile | 4 ++-- src/main.c | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index ab3e48b..2d0ab7d 100644 --- a/Makefile +++ b/Makefile @@ -8,14 +8,14 @@ OBJS := $(patsubst src/%.c, out/%.o, $(SRCS)) ALL: $(TARGET) -$(TARGET): mkdir $(OBJS) +$(TARGET): mkdir $(OBJS) $(CC) -o out/$@ $(filter-out $<, $^) $(LIBRARIES) mkdir: mkdir -p out out/%.o: src/%.c - $(CC) $(CFLAGS) -c $< -o $@ + $(CC) $(CFLAGS) -o $@ -c $< clean: rm -rf $(TARGET) out diff --git a/src/main.c b/src/main.c index 0aeeba7..e7096b9 100644 --- a/src/main.c +++ b/src/main.c @@ -69,11 +69,18 @@ int main(int argc, char** argv) void* advance_counter(void* data) { uint16_t ticks_per_quarter = *((uint32_t*)data); + struct timespec begin, end; while(!playing); + clock_gettime(CLOCK_REALTIME, &begin); for(;;) { - usleep((uint64_t)time_per_quarter / ticks_per_quarter); + clock_gettime(CLOCK_REALTIME, &end); + uint64_t diff = 1000000 * (end.tv_sec - begin.tv_sec) + (end.tv_nsec - begin.tv_nsec) / 1000; + diff -= (uint64_t)time_per_quarter / ticks_per_quarter; + diff *= (diff < (uint64_t)time_per_quarter / ticks_per_quarter); + clock_gettime(CLOCK_REALTIME, &begin); + usleep((uint64_t)time_per_quarter / ticks_per_quarter - diff); current_tick++; } }