diff options
author | Ian C <ianc@noddybox.co.uk> | 2020-03-15 22:42:55 +0000 |
---|---|---|
committer | Ian C <ianc@noddybox.co.uk> | 2020-03-15 22:42:55 +0000 |
commit | 26ff8d1fb2ea8b2eea3828616f0175dac02c3307 (patch) | |
tree | b3cc8fdc98949930312d04123a7499ae2a8c9f3e | |
parent | e78edf8ab7b236e8ed86318fb57d4c6642602b5e (diff) |
Added simple timing to get 50 frames a second, hopefully.
-rw-r--r-- | src/Makefile | 6 | ||||
-rw-r--r-- | src/czx81.c | 54 |
2 files changed, 55 insertions, 5 deletions
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 <http://www.gnu.org/licenses/>. 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 <stdlib.h> +#include <unistd.h> +#include <time.h> #include <locale.h> +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; |