-
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 flag to determine if the image is animated, along with a new structure, twin_animation_t, to manage the frames and timing information. I have also integrated an API with gifdec [1] to load, manage and display the twin_animation_t object. In addition, I have added a new application to demonstrate the capabilities of twin_pixmap_t, supporting both GIF animations and static images. [1] https://github.com/lecram/gifdec
- Loading branch information
1 parent
1ab586c
commit c8a0535
Showing
11 changed files
with
891 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,104 @@ | ||
/* | ||
* Twin - A Tiny Window System | ||
* Copyright (c) 2024 National Cheng Kung University | ||
* All rights reserved. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <sys/time.h> | ||
#include <time.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 *animation) | ||
{ | ||
twin_pixmap_t *current_frame = NULL; | ||
|
||
if (animation->pix->animated) { | ||
twin_animation_t *gif = animation->pix->animation; | ||
current_frame = twin_animation_get_current_frame(gif); | ||
twin_animation_advance_frame(gif); | ||
} else { | ||
current_frame = animation->pix; | ||
} | ||
|
||
twin_operand_t srcop = { | ||
.source_kind = TWIN_PIXMAP, | ||
.u.pixmap = current_frame, | ||
}; | ||
twin_composite(_apps_animation_pixmap(animation), 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 *animation = closure; | ||
_twin_widget_queue_paint(&animation->widget); | ||
twin_animation_t *gif = animation->pix->animation; | ||
twin_time_t delay = gif->frame_delays[gif->current_frame]; | ||
return delay; | ||
} | ||
|
||
static twin_dispatch_result_t _apps_animation_dispatch(twin_widget_t *widget, | ||
twin_event_t *event) | ||
{ | ||
apps_animation_t *animation = (apps_animation_t *) widget; | ||
if (_twin_widget_dispatch(widget, event) == TwinDispatchDone) | ||
return TwinDispatchDone; | ||
switch (event->kind) { | ||
case TwinEventPaint: | ||
_apps_animation_paint(animation); | ||
break; | ||
default: | ||
break; | ||
} | ||
return TwinDispatchContinue; | ||
} | ||
|
||
static void _apps_animation_init(apps_animation_t *animation, | ||
twin_box_t *parent, | ||
twin_dispatch_proc_t dispatch) | ||
{ | ||
static const twin_widget_layout_t preferred = {0, 0, 1, 1}; | ||
_twin_widget_init(&animation->widget, parent, 0, preferred, dispatch); | ||
|
||
if (animation->pix->animated) { | ||
twin_animation_t *gif = animation->pix->animation; | ||
twin_time_t delay = gif->frame_delays[gif->current_frame]; | ||
animation->timeout = twin_set_timeout(_apps_animation_timeout, delay, animation); | ||
} else { | ||
animation->timeout = NULL; | ||
} | ||
} | ||
|
||
static apps_animation_t *apps_animation_create(twin_box_t *parent, twin_pixmap_t *pix) | ||
{ | ||
apps_animation_t *animation = malloc(sizeof(apps_animation_t)); | ||
animation->pix = pix; | ||
_apps_animation_init(animation, parent, _apps_animation_dispatch); | ||
return animation; | ||
} | ||
|
||
void apps_animation_start(twin_screen_t *screen, 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, path); | ||
apps_animation_t *animation = apps_animation_create(&toplevel->box, pix); | ||
(void) animation; | ||
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,14 @@ | ||
/* | ||
* 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, 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
Oops, something went wrong.