aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Jeffery <harry@exec64.co.uk>2015-11-09 14:03:55 +0000
committerHarry Jeffery <harry@exec64.co.uk>2015-11-09 14:03:55 +0000
commitbd0afa25b00cc42a48cba048b1885e32095e1c41 (patch)
tree9a637f5656f55b9dc8e3c7b5f851fa35f813c1eb
parentcbf161ff5ecb71ee65f295aeb7c7678ee36962ae (diff)
downloadimv-bd0afa25b00cc42a48cba048b1885e32095e1c41.tar.gz
Add regular playback support for animated gifs
-rw-r--r--main.c34
1 files changed, 31 insertions, 3 deletions
diff --git a/main.c b/main.c
index 9300ab2..41483b8 100644
--- a/main.c
+++ b/main.c
@@ -48,8 +48,9 @@ struct {
FIBITMAP *frame;
SDL_Texture *tex;
int width, height;
- int cur_frame, next_frame, num_frames;
-} g_img = {NULL,NULL,NULL,0,0,0,0,0};
+ int cur_frame, next_frame, num_frames, playing;
+ double frame_time;
+} g_img = {NULL,NULL,NULL,0,0,0,0,0,0,0};
void toggle_fullscreen()
{
@@ -165,8 +166,12 @@ void render_image(FIBITMAP *image)
void next_frame()
{
+ if(g_img.num_frames < 2) {
+ return;
+ }
FITAG *tag = NULL;
char disposal_method = 0;
+ int frame_time = 0;
g_img.cur_frame = g_img.next_frame;
g_img.next_frame = (g_img.cur_frame + 1) % g_img.num_frames;
@@ -182,6 +187,13 @@ void next_frame()
}
}
+ FreeImage_GetMetadata(FIMD_ANIMATION, frame, "FrameTime", &tag);
+ if(FreeImage_GetTagValue(tag)) {
+ frame_time = *(int*)FreeImage_GetTagValue(tag);
+ }
+
+ g_img.frame_time += frame_time * 0.001;
+
FreeImage_UnlockPage(g_img.mbmp, frame, 0);
switch(disposal_method) {
@@ -212,7 +224,6 @@ void next_frame()
void load_gif(const char* path)
{
-
FIMULTIBITMAP *gif =
FreeImage_OpenMultiBitmap(FIF_GIF, path,
/* don't create */ 0,
@@ -228,6 +239,8 @@ void load_gif(const char* path)
g_img.num_frames = FreeImage_GetPageCount(gif);
g_img.cur_frame = 0;
g_img.next_frame = 0;
+ g_img.frame_time = 0;
+ g_img.playing = 1;
next_frame();
}
@@ -248,6 +261,8 @@ void load_image(const char* path)
g_img.num_frames = 0;
g_img.cur_frame = 0;
g_img.next_frame = 0;
+ g_img.frame_time = 0;
+ g_img.playing = 0;
FIBITMAP *image = FreeImage_Load(fmt, path, 0);
FIBITMAP *frame = FreeImage_ConvertTo32Bits(image);
@@ -302,8 +317,13 @@ int main(int argc, char** argv)
//Use linear sampling for scaling
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
+ double lastTime = SDL_GetTicks() / 1000.0;
+
int quit = 0;
while(!quit) {
+ double curTime = SDL_GetTicks() / 1000.0;
+ double dt = curTime - lastTime;
+ lastTime = curTime;
SDL_Event e;
while(!quit && SDL_PollEvent(&e)) {
@@ -366,6 +386,14 @@ int main(int argc, char** argv)
}
}
+ if(g_img.playing) {
+ g_img.frame_time -= dt;
+
+ while(g_img.frame_time < 0) {
+ next_frame();
+ }
+ }
+
if(g_view.redraw) {
SDL_RenderClear(g_renderer);