From 26ff8d1fb2ea8b2eea3828616f0175dac02c3307 Mon Sep 17 00:00:00 2001 From: Ian C Date: Sun, 15 Mar 2020 22:42:55 +0000 Subject: Added simple timing to get 50 frames a second, hopefully. --- src/Makefile | 6 +++++- src/czx81.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/Makefile b/src/Makefile index a9a835d..8ed3914 100644 --- a/src/Makefile +++ b/src/Makefile @@ -15,7 +15,11 @@ # along with this program. If not, see . TARGET=czx81 -CURSES=-lcursesw + +# This library may need to be set to one of these: +# cursesw +# ncursesw +CURSES=-lncurses OBJS= czx81.o \ zx81rom.o diff --git a/src/czx81.c b/src/czx81.c index 57d3525..ccdceba 100644 --- a/src/czx81.c +++ b/src/czx81.c @@ -18,17 +18,63 @@ #include "wide_curses.h" #include +#include +#include #include +static long TimeDiff(const struct timespec *start, + const struct timespec *end) +{ + long diff; + + diff = (end->tv_nsec - start->tv_nsec) + + (end->tv_sec - start->tv_sec) * 1000000000L; + + return diff; +} + int main(void) { + struct timespec start; + int quit = FALSE; + setlocale(LC_ALL, ""); + initscr(); - printw("Euro\n"); - printw("\u20ac\n"); - addwstr(L"\u20AC\n"); + raw(); + noecho(); + keypad(stdscr, TRUE); + nodelay(stdscr, TRUE); + + clock_gettime(CLOCK_MONOTONIC, &start); + + while(!quit) + { + struct timespec end; + struct timespec pause = {0}; + int ch; + + clear(); + + ch = getch(); + + if (ch == 3) + { + quit = TRUE; + } + + refresh(); + + clock_gettime(CLOCK_MONOTONIC, &end); + start = end; + pause.tv_nsec = 20000000 - TimeDiff(&start, &end); + + if (pause.tv_nsec > 0) + { + nanosleep(&pause, NULL); + } + } - getch(); endwin(); return EXIT_SUCCESS; -- cgit v1.2.3