mirror of
https://github.com/dankamongmen/notcurses
synced 2025-03-10 01:29:05 -04:00
Process menu/HUD input in demo_nanosleep() #509
This commit is contained in:
parent
60e3e604a8
commit
67100dc5e9
@ -152,7 +152,9 @@ int demo_render(struct notcurses* nc);
|
||||
#define DEMO_RENDER(nc) { int demo_render_err = demo_render(nc); if(demo_render_err){ return demo_render_err; }}
|
||||
|
||||
// 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.
|
||||
// demo_nanosleep(). it updates the HUD, which looks better to the user, and
|
||||
// dispatches input to the menu/HUD. don't use it if you have other threads
|
||||
// rendering or otherwise manipulating state, as it calls notcurses_render().
|
||||
int demo_nanosleep(struct notcurses* nc, const struct timespec *ts);
|
||||
|
||||
int demo_fader(struct notcurses* nc, struct ncplane* ncp, void* curry);
|
||||
|
@ -190,7 +190,7 @@ eagles(struct notcurses* nc){
|
||||
ncplane_move_yx(e[i].n, e[i].yoff, e[i].xoff);
|
||||
++eaglesmoved;
|
||||
}
|
||||
nanosleep(&flapiter, NULL);
|
||||
demo_nanosleep(nc, &flapiter);
|
||||
}while(eaglesmoved);
|
||||
for(size_t i = 0 ; i < sizeof(e) / sizeof(*e) ; ++i){
|
||||
ncplane_destroy(e[i].n);
|
||||
|
@ -65,7 +65,7 @@ drop_bricks(struct notcurses* nc, struct ncplane** arr, int arrcount){
|
||||
ranges += i;
|
||||
i = 0;
|
||||
}
|
||||
nanosleep(&iterdelay, NULL);
|
||||
demo_nanosleep(nc, &iterdelay);
|
||||
}
|
||||
}while(rangee - ranges + 1 >= FALLINGMAX);
|
||||
}
|
||||
|
@ -392,25 +392,31 @@ int hud_schedule(const char* demoname){
|
||||
// wake up every 100ms and render a frame so the HUD doesn't appear locked up
|
||||
int demo_nanosleep(struct notcurses* nc, const struct timespec *ts){
|
||||
struct timespec fsleep;
|
||||
if(hud){
|
||||
uint64_t nstotal = timespec_to_ns(ts);
|
||||
uint64_t deadline;
|
||||
struct timespec now;
|
||||
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
deadline = timespec_to_ns(&now) + nstotal;
|
||||
while(deadline - timespec_to_ns(&now) > GIG / 10){
|
||||
do{
|
||||
fsleep.tv_sec = 0;
|
||||
fsleep.tv_nsec = GIG / 10;
|
||||
nanosleep(&fsleep, NULL);
|
||||
if(deadline - timespec_to_ns(&now) < GIG / 10){
|
||||
fsleep.tv_nsec = deadline - timespec_to_ns(&now);
|
||||
}
|
||||
ncinput ni;
|
||||
// throw away any input we receive. if it was for the menu or HUD, it was
|
||||
// already dispatched internally to demo_getc().
|
||||
char32_t id;
|
||||
if((id = demo_getc(nc, &fsleep, &ni)) > 0){
|
||||
return -1;
|
||||
}
|
||||
if(hud){
|
||||
demo_render(nc);
|
||||
clock_gettime(CLOCK_MONOTONIC_RAW, &now);
|
||||
}
|
||||
clock_gettime(CLOCK_MONOTONIC, &now);
|
||||
}while(deadline > timespec_to_ns(&now));
|
||||
ns_to_timespec(deadline - timespec_to_ns(&now), &fsleep);
|
||||
}else{
|
||||
fsleep = *ts;
|
||||
}
|
||||
nanosleep(&fsleep, NULL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -26662,7 +26662,7 @@ int jungle_demo(struct notcurses* nc){
|
||||
if(nsrunning < iter * iterns){
|
||||
struct timespec sleepts;
|
||||
ns_to_timespec(iter * iterns - nsrunning, &sleepts);
|
||||
nanosleep(&sleepts, NULL);
|
||||
demo_nanosleep(nc, &sleepts);
|
||||
}
|
||||
cycle_palettes(nc, pal);
|
||||
}while(nsrunning > 0 && (uint64_t)nsrunning < 5 * timespec_to_ns(&demodelay));
|
||||
|
@ -219,7 +219,7 @@ int luigi_demo(struct notcurses* nc){
|
||||
int dimy = ncplane_dim_y(ncvisual_plane(wmncv));
|
||||
ncplane_move_yx(ncvisual_plane(wmncv), rows * 4 / 5 - dimy + 1 + (i % 2), i - 60);
|
||||
DEMO_RENDER(nc);
|
||||
nanosleep(&stepdelay, NULL);
|
||||
demo_nanosleep(nc, &stepdelay);
|
||||
}
|
||||
for(i = 0 ; i < 3 ; ++i){
|
||||
ncplane_destroy(lns[i]);
|
||||
|
@ -47,8 +47,6 @@ fadethread(void* vnc){
|
||||
int canceled = ncvisual_stream(nc, ncv, &averr, delaymultiplier, perframe, &three);
|
||||
ncvisual_destroy(ncv);
|
||||
ncplane_erase(ncp);
|
||||
timespec_mul(&demodelay, 2, &fade);
|
||||
demo_nanosleep(nc, &fade);
|
||||
ncplane_destroy(apiap);
|
||||
if(canceled == 1){
|
||||
return PTHREAD_CANCELED;
|
||||
|
@ -32,7 +32,7 @@ move_square(struct notcurses* nc, struct ncplane* chunk, int* holey, int* holex,
|
||||
targx += deltax;
|
||||
ncplane_move_yx(chunk, targy, targx);
|
||||
DEMO_RENDER(nc);
|
||||
nanosleep(&movetime, NULL);
|
||||
demo_nanosleep(nc, &movetime);
|
||||
}
|
||||
*holey = newholey;
|
||||
*holex = newholex;
|
||||
|
@ -80,7 +80,7 @@ slideitslideit(struct notcurses* nc, struct ncplane* n, uint64_t deadline,
|
||||
*vely = -*vely;
|
||||
}
|
||||
ncplane_move_yx(n, yoff, xoff);
|
||||
nanosleep(&iterdelay, NULL);
|
||||
demo_nanosleep(nc, &iterdelay);
|
||||
clock_gettime(CLOCK_MONOTONIC, &cur);
|
||||
}while(timespec_to_ns(&cur) < deadline);
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user