diff --git a/src/demo/boxdemo.c b/src/demo/boxdemo.c index 3f92d740b..7f67735e5 100644 --- a/src/demo/boxdemo.c +++ b/src/demo/boxdemo.c @@ -2,9 +2,12 @@ #include #include "demo.h" +#define ITERATIONS 10 + int box_demo(struct notcurses* nc){ - int ylen, xlen; - notcurses_term_dim_yx(nc, &ylen, &xlen); + const int64_t totalns = timespec_to_ns(&demodelay); + struct timespec iterdelay; + timespec_div(&demodelay, ITERATIONS, &iterdelay); struct ncplane* n = notcurses_stdplane(nc); ncplane_erase(n); cell ul = CELL_TRIVIAL_INITIALIZER, ll = CELL_TRIVIAL_INITIALIZER; @@ -15,40 +18,53 @@ int box_demo(struct notcurses* nc){ if(cells_double_box(n, 0, 0, &ul, &ur, &ll, &lr, &hl, &vl)){ return -1; } - int y = 0, x = 0; - while(ylen - y - 1 > 2 && xlen - x - 1 > 2){ - cell_set_fg(&ul, 107 - (y * 2), 40, 107 + (y * 2)); - cell_set_bg(&ul, 20 + y, 20 + y, 20 + y); - cell_set_fg(&ur, 107 - (y * 2), 40, 107 + (y * 2)); - cell_set_bg(&ur, 20 + y, 20 + y, 20 + y); - cell_set_fg(&hl, 107 - (y * 2), 40, 107 + (y * 2)); - cell_set_bg(&hl, 20, 20, 20); - cell_set_fg(&ll, 107 - (y * 2), 40, 107 + (y * 2)); - cell_set_bg(&ll, 20 + y, 20 + y, 20 + y); - cell_set_fg(&lr, 107 - (y * 2), 40, 107 + (y * 2)); - cell_set_bg(&lr, 20 + y, 20 + y, 20 + y); - cell_set_fg(&vl, 20, 20, 20); - cell_set_bg(&vl, 107 - (y * 2), 40, 107 + (y * 2)); - if(ncplane_cursor_move_yx(n, y, x)){ + struct timespec start, now; + clock_gettime(CLOCK_MONOTONIC_RAW, &start); + int zbonus = 40; + int zbonusdelta = 20; + do{ + int ylen, xlen; + ncplane_dim_yx(n, &ylen, &xlen); + int y = 0, x = 0; + while(ylen - y - 1 > 2 && xlen - x - 1 > 2){ + cell_set_fg(&ul, 107 - (y * 2), zbonus, 107 + (y * 2)); + cell_set_bg(&ul, zbonus, 20 + y, 20 + y); + cell_set_fg(&ur, 107 - (y * 2), zbonus, 107 + (y * 2)); + cell_set_bg(&ur, zbonus, 20 + y, 20 + y); + cell_set_fg(&hl, 107 - (y * 2), zbonus, 107 + (y * 2)); + cell_set_bg(&hl, 20, zbonus, 20); + cell_set_fg(&ll, 107 - (y * 2), zbonus, 107 + (y * 2)); + cell_set_bg(&ll, zbonus, 20 + y, 20 + y); + cell_set_fg(&lr, 107 - (y * 2), zbonus, 107 + (y * 2)); + cell_set_bg(&lr, zbonus, 20 + y, 20 + y); + cell_set_fg(&vl, 20, zbonus, 20); + cell_set_bg(&vl, 107 - (y * 2), zbonus, 107 + (y * 2)); + if(ncplane_cursor_move_yx(n, y, x)){ + return -1; + } + if(ncplane_box_sized(n, &ul, &ur, &ll, &lr, &hl, &vl, ylen, xlen, 0)){ + return -1; + } + ylen -= 2; + xlen -= 2; + ++y; + ++x; + } + if(notcurses_render(nc)){ return -1; } - if(ncplane_box_sized(n, &ul, &ur, &ll, &lr, &hl, &vl, ylen, xlen, 0)){ - return -1; + nanosleep(&iterdelay, NULL); + clock_gettime(CLOCK_MONOTONIC_RAW, &now); + if((zbonus += zbonusdelta > 255) || zbonus < 0){ + zbonusdelta = -zbonusdelta; + zbonus += zbonusdelta; } - ylen -= 2; - xlen -= 2; - ++y; - ++x; - } + }while(timespec_subtract_ns(&now, &start) <= totalns); cell_release(n, &ul); cell_release(n, &ur); cell_release(n, &ll); cell_release(n, &lr); cell_release(n, &hl); cell_release(n, &vl); - if(notcurses_render(nc)){ - return -1; - } - nanosleep(&demodelay, NULL); return 0; } diff --git a/src/demo/demo.c b/src/demo/demo.c index 10c3504a2..c9f1ede6c 100644 --- a/src/demo/demo.c +++ b/src/demo/demo.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -8,7 +9,22 @@ #include #include "demo.h" -static const unsigned long GIG = 1000000000; +int timespec_subtract(struct timespec *result, const struct timespec *time0, + struct timespec *time1){ + if(time0->tv_nsec < time1->tv_nsec){ + int nsec = (time1->tv_nsec - time0->tv_nsec) / 1000000000 + 1; + time1->tv_nsec -= 1000000000 * nsec; + time1->tv_sec += nsec; + } + if(time0->tv_nsec - time1->tv_nsec > 1000000000){ + int nsec = (time0->tv_nsec - time1->tv_nsec) / 1000000000; + time1->tv_nsec += 1000000000 * nsec; + time1->tv_sec -= nsec; + } + result->tv_sec = time0->tv_sec - time1->tv_sec; + result->tv_nsec = time0->tv_nsec - time1->tv_nsec; + return time0->tv_sec < time1->tv_sec; +} struct timespec demodelay = { .tv_sec = 1, @@ -37,6 +53,52 @@ usage(const char* exe, int status){ exit(status); } +static int +outro_message(struct notcurses* nc, int rows, int cols){ + const char str0[] = " ATL, baby! ATL! "; + const char str1[] = "much, much more is coming"; + const char str2[] = " hack on! —dank❤ "; + struct ncplane* on = notcurses_newplane(nc, 5, strlen(str1) + 6, rows - 6, + (cols - (strlen(str1) + 6)) / 2, NULL); + if(on == NULL){ + return -1; + } + cell bgcell = CELL_TRIVIAL_INITIALIZER; + notcurses_bg_prep(&bgcell.channels, 0x58, 0x36, 0x58); + ncplane_set_background(on, &bgcell); + ncplane_dim_yx(on, &rows, &cols); + int ybase = 0; + if(ncplane_fg_rgb8(on, 0, 0, 0)){ + return -1; + } + if(ncplane_bg_rgb8(on, 0, 180, 180)){ + return -1; + } + if(ncplane_cursor_move_yx(on, ++ybase, (cols - strlen(str0)) / 2)){ + return -1; + } + if(ncplane_putstr(on, str0) < 0){ + return -1; + } + if(ncplane_cursor_move_yx(on, ++ybase, (cols - strlen(str1)) / 2)){ + return -1; + } + if(ncplane_putstr(on, str1) < 0){ + return -1; + } + if(ncplane_cursor_move_yx(on, ++ybase, (cols - (strlen(str2) - 4)) / 2)){ + return -1; + } + if(ncplane_putstr(on, str2) < 0){ + return -1; + } + if(notcurses_render(nc)){ + return -1; + } + cell_release(on, &bgcell); + return 0; +} + static int outro(struct notcurses* nc){ struct ncplane* ncp; @@ -59,54 +121,13 @@ outro(struct notcurses* nc){ ncvisual_destroy(ncv); return -1; } - const char str0[] = " ATL, baby! ATL! "; - const char str1[] = " much, much more is coming "; - const char str2[] = " hack on! —dank❤ "; - int ybase = rows - 5; - if(ncplane_fg_rgb8(ncp, 0, 0, 0)){ - return -1; + int ret = outro_message(nc, rows, cols); + if(ret == 0){ + struct timespec fade = { .tv_sec = 5, .tv_nsec = 0, }; + ret |= ncplane_fadeout(ncp, &fade); } - if(ncplane_bg_rgb8(ncp, 0, 180, 180)){ - return -1; - } - if(ncplane_cursor_move_yx(ncp, ybase, (cols - strlen(str0) + 4) / 2 - 1)){ - return -1; - } - if(ncplane_printf(ncp, "%*s", strlen(str0) + 2, "") < 0){ - return -1; - } - if(ncplane_cursor_move_yx(ncp, ++ybase, (cols - strlen(str0) + 4) / 2)){ - return -1; - } - if(ncplane_putstr(ncp, str0) != (int)strlen(str0)){ - return -1; - } - if(ncplane_cursor_move_yx(ncp, ++ybase, (cols - strlen(str1) + 4) / 2)){ - return -1; - } - if(ncplane_putstr(ncp, str1) != (int)strlen(str1)){ - return -1; - } - if(ncplane_cursor_move_yx(ncp, ++ybase, (cols - strlen(str2) + 8) / 2)){ - return -1; - } - if(ncplane_putstr(ncp, str2) != (int)strlen(str2)){ - return -1; - } - if(ncplane_cursor_move_yx(ncp, ++ybase, (cols - strlen(str2) + 4) / 2)){ - return -1; - } - if(ncplane_printf(ncp, "%*s", strlen(str2), "") < 0){ - return -1; - } - if(notcurses_render(nc)){ - ncvisual_destroy(ncv); - return -1; - } - nanosleep(&demodelay, NULL); - struct timespec fade = { .tv_sec = 5, .tv_nsec = 0, }; - ncplane_fadeout(ncp, &fade); - return 0; + ncvisual_destroy(ncv); + return ret; } static int diff --git a/src/demo/demo.h b/src/demo/demo.h index aaf6a9dcc..a723dcb78 100644 --- a/src/demo/demo.h +++ b/src/demo/demo.h @@ -20,6 +20,32 @@ int sliding_puzzle_demo(struct notcurses* nc); int view_demo(struct notcurses* nc); int panelreel_demo(struct notcurses* nc); +int timespec_subtract(struct timespec *result, const struct timespec *time1, + struct timespec *time0); + +#define GIG 1000000000ul + +static inline uint64_t +timespec_to_ns(const struct timespec* ts){ + return ts->tv_sec * GIG + ts->tv_nsec; +} + +static inline int64_t +timespec_subtract_ns(const struct timespec* time1, const struct timespec* time0){ + int64_t ns = timespec_to_ns(time1); + ns -= timespec_to_ns(time0); + return ns; +} + +// divide the provided timespec 'ts' by 'divisor' into 'quots' +static inline void +timespec_div(const struct timespec* ts, unsigned divisor, struct timespec* quots){ + uint64_t ns = timespec_to_ns(ts); + ns /= divisor; + quots->tv_nsec = ns % GIG; + quots->tv_sec = ns / GIG; +} + #ifdef __cplusplus } #endif diff --git a/src/lib/notcurses.c b/src/lib/notcurses.c index f8933c907..6544cd8d6 100644 --- a/src/lib/notcurses.c +++ b/src/lib/notcurses.c @@ -21,7 +21,6 @@ #include #include "notcurses.h" #include "internal.h" -#include "timespec.h" #include "version.h" #include "egcpool.h" @@ -113,10 +112,15 @@ drop_signals(notcurses* nc){ return 0; } +static inline uint64_t +timespec_to_ns(const struct timespec* t){ + return t->tv_sec * NANOSECS_IN_SEC + t->tv_nsec; +} + static void update_render_stats(const struct timespec* time1, const struct timespec* time0, ncstats* stats){ - int64_t elapsed = timespec_subtract_ns(time1, time0); + int64_t elapsed = timespec_to_ns(time1) - timespec_to_ns(time0); //fprintf(stderr, "Rendering took %ld.%03lds\n", elapsed / NANOSECS_IN_SEC, // (elapsed % NANOSECS_IN_SEC) / 1000000); if(elapsed > 0){ // don't count clearly incorrect information, egads diff --git a/src/lib/timespec.c b/src/lib/timespec.c deleted file mode 100644 index f89e91f85..000000000 --- a/src/lib/timespec.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include "timespec.h" - -int timespec_subtract(struct timespec *result, const struct timespec *time0, - struct timespec *time1){ - if(time0->tv_nsec < time1->tv_nsec){ - int nsec = (time1->tv_nsec - time0->tv_nsec) / 1000000000 + 1; - time1->tv_nsec -= 1000000000 * nsec; - time1->tv_sec += nsec; - } - if(time0->tv_nsec - time1->tv_nsec > 1000000000){ - int nsec = (time0->tv_nsec - time1->tv_nsec) / 1000000000; - time1->tv_nsec += 1000000000 * nsec; - time1->tv_sec -= nsec; - } - result->tv_sec = time0->tv_sec - time1->tv_sec; - result->tv_nsec = time0->tv_nsec - time1->tv_nsec; - return time0->tv_sec < time1->tv_sec; -} diff --git a/src/lib/timespec.h b/src/lib/timespec.h deleted file mode 100644 index 0d8b0b366..000000000 --- a/src/lib/timespec.h +++ /dev/null @@ -1,25 +0,0 @@ -#ifndef NOTCURSES_TIMESPEC -#define NOTCURSES_TIMESPEC - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int timespec_subtract(struct timespec *result, const struct timespec *time1, - struct timespec *time0); - -static inline int64_t -timespec_subtract_ns(const struct timespec* time1, const struct timespec* time0){ - int64_t ns = time1->tv_sec * 1000000000 + time1->tv_nsec; - ns -= time0->tv_sec * 1000000000 + time0->tv_nsec; - return ns; -} - -#ifdef __cplusplus -} -#endif - -#endif