Skip to content

Commit 169b710

Browse files
committed
Fix bad custom animation example
The custom animation example suggests that a stack-allocated AnimationImplementation variable can be used, but this is incorrect. The variable is not copied and so must remain valid while the animation runs. But also, the animation variable should not be stack-allocated, as this makes it difficult to destroy it later. Rework the example to use global variables for the animation and implementation. Signed-off-by: Lincoln Ramsay <[email protected]>
1 parent 97a39c6 commit 169b710

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

source/_guides/graphics-and-animations/animations.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -175,27 +175,39 @@ static void implementation_update(Animation *animation,
175175
static void implementation_teardown(Animation *animation) {
176176
APP_LOG(APP_LOG_LEVEL_INFO, "Animation finished!");
177177
}
178+
179+
// This needs to exist while the event loop runs
180+
static const AnimationImplementation s_implementation = {
181+
.setup = implementation_setup,
182+
.update = implementation_update,
183+
.teardown = implementation_teardown
184+
};
185+
186+
// This needs to be preserved so it can be cleaned up
187+
static Animation *s_animation;
178188
```
179189
180190
Once these are in place, create a new ``Animation`` , specifying the new custom
181191
implementation as a `const` object pointer at the appropriate time:
182192
183193
```c
184194
// Create a new Animation
185-
Animation *animation = animation_create();
186-
animation_set_delay(animation, 1000);
187-
animation_set_duration(animation, 1000);
195+
s_animation = animation_create();
196+
animation_set_delay(s_animation, 1000);
197+
animation_set_duration(s_animation, 1000);
188198
189199
// Create the AnimationImplementation
190-
const AnimationImplementation implementation = {
191-
.setup = implementation_setup,
192-
.update = implementation_update,
193-
.teardown = implementation_teardown
194-
};
195-
animation_set_implementation(animation, &implementation);
200+
animation_set_implementation(s_animation, &s_implementation);
201+
202+
...
196203
197204
// Play the Animation
198-
animation_schedule(animation);
205+
animation_schedule(s_animation);
206+
207+
...
208+
209+
// Destroy the animation
210+
animation_destroy(s_animation);
199211
```
200212

201213
The output of the example above will look like the snippet shown below (edited

0 commit comments

Comments
 (0)