From d5f69f2e830bad9b14a554853daf427b978fee0b Mon Sep 17 00:00:00 2001 From: Harry Jeffery Date: Fri, 24 Nov 2017 16:48:28 +0000 Subject: Sleep for longer when displaying still images --- src/imv.c | 14 ++++++++++++-- src/loader.c | 13 +++++++++++-- src/loader.h | 3 +++ 3 files changed, 26 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/imv.c b/src/imv.c index ff60d6e..5484c89 100644 --- a/src/imv.c +++ b/src/imv.c @@ -599,8 +599,18 @@ int imv_run(struct imv *imv) /* check stdin for any more paths */ check_stdin_for_paths(imv); } else { - /* sleep a little bit so we don't waste CPU time */ - SDL_Delay(10); + /* sleep until we have something to do */ + unsigned int timeout = 1000; /* sleep up to a second */ + + /* if we need to display the next frame of an animation soon we should + * limit our sleep until the next frame is due */ + const double next_frame_in = imv_loader_time_left(imv->loader); + if(next_frame_in > 0.0) { + timeout = (unsigned int)(next_frame_in * 1000.0); + } + + /* go to sleep until an input event, etc. or the timeout expires */ + SDL_WaitEventTimeout(NULL, timeout); } } diff --git a/src/loader.c b/src/loader.c index 4f8550c..cbebf2c 100644 --- a/src/loader.c +++ b/src/loader.c @@ -137,9 +137,11 @@ void imv_loader_time_passed(struct imv_loader *ldr, double dt) pthread_mutex_lock(&ldr->lock); if(ldr->num_frames > 1) { ldr->frame_time -= dt; - if(ldr->frame_time < 0) { + if(ldr->frame_time < 0.0) { get_frame = 1; } + } else { + ldr->frame_time = 0.0; } pthread_mutex_unlock(&ldr->lock); @@ -148,6 +150,11 @@ void imv_loader_time_passed(struct imv_loader *ldr, double dt) } } +double imv_loader_time_left(struct imv_loader *ldr) +{ + return ldr->frame_time; +} + static void *bg_new_img(void *data) { /* so we can poll for it */ @@ -187,7 +194,7 @@ static void *bg_new_img(void *data) FIMULTIBITMAP *mbmp = NULL; FIBITMAP *bmp = NULL; int width, height; - int raw_frame_time = 100; /* default to 100 */ + int raw_frame_time = 0; if(fmt == FIF_GIF) { if(from_stdin) { @@ -219,6 +226,8 @@ static void *bg_new_img(void *data) FreeImage_GetMetadata(FIMD_ANIMATION, frame, "FrameTime", &tag); if(FreeImage_GetTagValue(tag)) { raw_frame_time = *(int*)FreeImage_GetTagValue(tag); + } else { + raw_frame_time = 100; /* default value for gifs */ } FreeImage_UnlockPage(mbmp, frame, 0); diff --git a/src/loader.h b/src/loader.h index ad01cb4..07fe068 100644 --- a/src/loader.h +++ b/src/loader.h @@ -69,6 +69,9 @@ void imv_loader_load_next_frame(struct imv_loader *ldr); * the loader will automatically load the next frame when it is due. */ void imv_loader_time_passed(struct imv_loader *ldr, double dt); +/* Ask the loader how long we can sleep for until the next frame */ +double imv_loader_time_left(struct imv_loader *ldr); + #endif -- cgit v1.2.3