-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend twin_pixmap_t to Support gif animation
To enable support for GIF animations, I have extended the twin_pixmap_t structure. The twin_pixmap_t now includes a new pointer to the new twin_animation_t structure, which manages multiple frames and their timing information for animated image. To manipulate twin_animation_t, the following APIs have been added. - twin_animation_get_current_delay : Retrieves the display duration of the current frame. - twin_animation_get_current_frame : Obtains the current frame for display. - twin_animation_advance_frame : Advances the animation to the next frame. If the animation is looping, it will return to the first frame after the last one. - twin_animation_destroy : Frees the memory allocated for the animation, including all associated frames. To show the new features, a demo application has been developed. This application supports both animated images and static images, and serves as a practical example of how to utilize the new animation capabilities.
- Loading branch information
1 parent
1ab586c
commit 241ecd1
Showing
12 changed files
with
919 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 = | ||
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 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 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 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 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 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 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.