demo: add demo_nanosleep() for HUD

pull/265/head
nick black 4 years ago
parent 1e403123e1
commit bb0cdb2b88
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

@ -124,6 +124,10 @@ int hud_schedule(const char* demoname);
// demo_render(), which will ensure the HUD stays on the top of the z-stack.
int demo_render(struct notcurses* nc);
// if you won't be doing things, and it's a long sleep, consider using
// demo_nanosleep(). it updates the HUD, which looks better to the user.
int demo_nanosleep(struct notcurses* nc, const struct timespec *ts);
int demo_fader(struct notcurses* nc, struct ncplane* ncp, void* curry);
// grab the hud with the mouse

@ -185,6 +185,26 @@ int hud_schedule(const char* demoname){
return 0;
}
// wake up every 100ms and render a frame so the HUD doesn't look locked up
int demo_nanosleep(struct notcurses* nc, const struct timespec *ts){
uint64_t nstotal = timespec_to_ns(ts);
uint64_t deadline;
struct timespec fsleep, now;
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
deadline = timespec_to_ns(&now) + nstotal;
while(deadline - timespec_to_ns(&now) > GIG / 10){
fsleep.tv_sec = 0;
fsleep.tv_nsec = GIG / 10;
nanosleep(&fsleep, NULL);
demo_render(nc);
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
}
ns_to_timespec(deadline - timespec_to_ns(&now), &fsleep);
nanosleep(&fsleep, NULL);
return 0;
}
int demo_render(struct notcurses* nc){
if(hud){
int plen = HUD_COLS - 4 - NSLEN;

@ -94,7 +94,7 @@ int intro(struct notcurses* nc){
if(demo_render(nc)){
return -1;
}
nanosleep(&demodelay, NULL);
demo_nanosleep(nc, &demodelay);
struct timespec fade = demodelay;
ncplane_fadeout(ncp, &fade, demo_fader, NULL);
return 0;

@ -49,7 +49,7 @@ fadethread(void* vnc){
ncplane_erase(ncp);
fade.tv_sec = 2;
fade.tv_nsec = 0;
nanosleep(&fade, NULL);
demo_nanosleep(nc, &fade);
ncplane_destroy(apiap);
return vnc;
}

@ -111,8 +111,8 @@ int view_demo(struct notcurses* nc){
return -1;
}
demo_render(nc);
demo_nanosleep(nc, &demodelay);
ncplane_move_bottom(dsplane);
nanosleep(&demodelay, NULL);
if(ncvisual_render(ncv, 0, 0, 0, 0)){
ncvisual_destroy(ncv);
ncvisual_destroy(ncv2);
@ -123,14 +123,14 @@ int view_demo(struct notcurses* nc){
cell_set_fg_alpha(&b, CELL_ALPHA_TRANSPARENT);
cell_set_bg_alpha(&b, CELL_ALPHA_TRANSPARENT);
ncplane_set_base(ncvisual_plane(ncv2), &b);
demo_render(nc);
demo_nanosleep(nc, &demodelay);
ncvisual_destroy(ncv);
ncvisual_destroy(ncv2);
demo_render(nc);
nanosleep(&demodelay, NULL);
ncplane_move_top(dsplane);
demo_render(nc);
demo_nanosleep(nc, &demodelay);
ncplane_destroy(dsplane);
nanosleep(&demodelay, NULL);
struct ncplane* ncpl = legend(nc, dimy, dimx);
if(ncpl == NULL){
return -1;

Loading…
Cancel
Save