aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2017-11-24 16:48:28 +0000
committerHarry Jeffery <harry@exec64.co.uk>2017-11-24 16:48:32 +0000
commitd5f69f2e830bad9b14a554853daf427b978fee0b (patch)
tree35d5f2fed89743e855b225a2adddd537fafbc149 /src
parentbf702c3f433ca7a78d5790bb4b239f5692cfc070 (diff)
downloadimv-d5f69f2e830bad9b14a554853daf427b978fee0b.tar.gz
Sleep for longer when displaying still images
Diffstat (limited to 'src')
-rw-r--r--src/imv.c14
-rw-r--r--src/loader.c13
-rw-r--r--src/loader.h3
3 files changed, 26 insertions, 4 deletions
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