-
Notifications
You must be signed in to change notification settings - Fork 19
Support GIF image decoder and animation #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Twin - A Tiny Window System | ||
* Copyright (c) 2024 National Cheng Kung University | ||
* All rights reserved. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
|
||
#include "twin_private.h" | ||
|
||
#include "apps_animation.h" | ||
|
||
#define _apps_animation_pixmap(animation) ((animation)->widget.window->pixmap) | ||
|
||
typedef struct { | ||
twin_widget_t widget; | ||
twin_pixmap_t *pix; | ||
twin_timeout_t *timeout; | ||
} apps_animation_t; | ||
|
||
static void _apps_animation_paint(apps_animation_t *anim) | ||
{ | ||
twin_pixmap_t *current_frame = NULL; | ||
|
||
if (twin_pixmap_is_animated(anim->pix)) { | ||
twin_animation_t *a = anim->pix->animation; | ||
current_frame = twin_animation_get_current_frame(a); | ||
twin_animation_advance_frame(a); | ||
} else { | ||
current_frame = anim->pix; | ||
} | ||
|
||
twin_operand_t srcop = { | ||
.source_kind = TWIN_PIXMAP, | ||
.u.pixmap = current_frame, | ||
}; | ||
twin_composite(_apps_animation_pixmap(anim), 0, 0, &srcop, 0, 0, NULL, 0, 0, | ||
TWIN_SOURCE, current_frame->width, current_frame->height); | ||
} | ||
|
||
static twin_time_t _apps_animation_timeout(twin_time_t maybe_unused now, | ||
void *closure) | ||
{ | ||
apps_animation_t *anim = closure; | ||
_twin_widget_queue_paint(&anim->widget); | ||
twin_animation_t *a = anim->pix->animation; | ||
twin_time_t delay = twin_animation_get_current_delay(a); | ||
return delay; | ||
} | ||
|
||
static twin_dispatch_result_t _apps_animation_dispatch(twin_widget_t *widget, | ||
twin_event_t *event) | ||
{ | ||
apps_animation_t *anim = (apps_animation_t *) widget; | ||
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone) | ||
return TwinDispatchDone; | ||
switch (event->kind) { | ||
case TwinEventPaint: | ||
_apps_animation_paint(anim); | ||
break; | ||
default: | ||
break; | ||
} | ||
return TwinDispatchContinue; | ||
} | ||
|
||
static void _apps_animation_init(apps_animation_t *anim, | ||
twin_box_t *parent, | ||
twin_dispatch_proc_t dispatch) | ||
{ | ||
static const twin_widget_layout_t preferred = {0, 0, 1, 1}; | ||
_twin_widget_init(&anim->widget, parent, 0, preferred, dispatch); | ||
|
||
if (twin_pixmap_is_animated(anim->pix)) { | ||
twin_animation_t *a = anim->pix->animation; | ||
twin_time_t delay = twin_animation_get_current_delay(a); | ||
anim->timeout = twin_set_timeout(_apps_animation_timeout, delay, anim); | ||
} else { | ||
anim->timeout = NULL; | ||
} | ||
} | ||
|
||
static apps_animation_t *apps_animation_create(twin_box_t *parent, | ||
twin_pixmap_t *pix) | ||
{ | ||
apps_animation_t *anim = malloc(sizeof(apps_animation_t)); | ||
anim->pix = pix; | ||
_apps_animation_init(anim, parent, _apps_animation_dispatch); | ||
return anim; | ||
} | ||
|
||
void apps_animation_start(twin_screen_t *screen, | ||
const char *name, | ||
const char *path, | ||
int x, | ||
int y) | ||
{ | ||
twin_pixmap_t *pix = twin_pixmap_from_file(path, TWIN_ARGB32); | ||
twin_toplevel_t *toplevel = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid specifying window title to the full path of given GIF file. Instead, use fixed names such as "Viewer." |
||
twin_toplevel_create(screen, TWIN_ARGB32, TwinWindowApplication, x, y, | ||
pix->width, pix->height, name); | ||
apps_animation_t *anim = apps_animation_create(&toplevel->box, pix); | ||
(void) anim; | ||
twin_toplevel_show(toplevel); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* Twin - A Tiny Window System | ||
* Copyright (c) 2024 National Cheng Kung University | ||
* All rights reserved. | ||
*/ | ||
|
||
#ifndef _APPS_ANIMATION_H_ | ||
#define _APPS_ANIMATION_H_ | ||
|
||
#include <twin.h> | ||
|
||
void apps_animation_start(twin_screen_t *screen, | ||
const char *name, | ||
const char *path, | ||
int x, | ||
int y); | ||
|
||
#endif /* _APPS_ANIMATION_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
CONFIG_BACKEND_SDL=y | ||
CONFIG_LOADER_PNG=y | ||
CONFIG_LOADER_JPEG=y | ||
CONFIG_LOADER_GIF=y | ||
CONFIG_DEMO_APPLICATIONS=y | ||
CONFIG_DEMO_MULTI=y | ||
CONFIG_DEMO_HELLO=y | ||
CONFIG_DEMO_CLOCK=y | ||
CONFIG_DEMO_CALCULATOR=y | ||
CONFIG_DEMO_LINE=y | ||
CONFIG_DEMO_SPLINE=y | ||
CONFIG_DEMO_ANIMATION=y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Twin - A Tiny Window System | ||
* Copyright (c) 2024 National Cheng Kung University, Taiwan | ||
* All rights reserved. | ||
*/ | ||
#include <stdlib.h> | ||
|
||
#include "twin.h" | ||
|
||
twin_time_t twin_animation_get_current_delay(const twin_animation_t *anim) | ||
{ | ||
if (!anim) | ||
return 0; | ||
return anim->iter->current_delay; | ||
} | ||
|
||
twin_pixmap_t *twin_animation_get_current_frame(const twin_animation_t *anim) | ||
{ | ||
if (!anim) | ||
return NULL; | ||
return anim->iter->current_frame; | ||
} | ||
|
||
void twin_animation_advance_frame(twin_animation_t *anim) | ||
{ | ||
if (!anim) | ||
return; | ||
twin_animation_iter_advance(anim->iter); | ||
} | ||
|
||
void twin_animation_destroy(twin_animation_t *anim) | ||
{ | ||
if (!anim) | ||
return; | ||
|
||
free(anim->iter); | ||
for (twin_count_t i = 0; i < anim->n_frames; i++) { | ||
twin_pixmap_destroy(anim->frames[i]); | ||
} | ||
free(anim->frames); | ||
free(anim->frame_delays); | ||
free(anim); | ||
} | ||
|
||
twin_animation_iter_t *twin_animation_iter_init(twin_animation_t *anim) | ||
{ | ||
twin_animation_iter_t *iter = malloc(sizeof(twin_animation_iter_t)); | ||
if (!iter || !anim) | ||
return NULL; | ||
iter->current_index = 0; | ||
iter->current_frame = anim->frames[0]; | ||
iter->current_delay = anim->frame_delays[0]; | ||
anim->iter = iter; | ||
iter->anim = anim; | ||
return iter; | ||
} | ||
|
||
void twin_animation_iter_advance(twin_animation_iter_t *iter) | ||
{ | ||
twin_animation_t *anim = iter->anim; | ||
iter->current_index++; | ||
if (iter->current_index >= anim->n_frames) { | ||
if (anim->loop) { | ||
iter->current_index = 0; | ||
} else { | ||
iter->current_index = anim->n_frames - 1; | ||
} | ||
} | ||
iter->current_frame = anim->frames[iter->current_index]; | ||
iter->current_delay = anim->frame_delays[iter->current_index]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.