Generic updatable and drawable actions.
This module help creating user interaction within the gameplay by adding actions. They have the following properties:
- Can handle user input and events,
- Can be updated through the game loop,
- Can be drawn.
Most more high level objects can handle actions to add flexibility (like in battles, maps, etc).
Maximum number of action in a unique action_stack.
#define ACTION_STACK_MAX 128
Use this structure to create an action that may handle input, be updated and drawn.
All members can be NULL.
Arbitrary user data.
Handle an event.
void (*handle)(struct action *self, const union event *ev)
Update the action
self with the
ticks since last frame. The callback should
return true if it is considered complete.
bool (*update)(struct action *self, unsigned int ticks)
Draw the action
void (*draw)(struct action *self)
Called with the action
self when it was completed.
This callback is mostly provided to allow the user doing something else once an action is complete. Predefined actions should not use this callback by themselves.
void (*end)(struct action *self)
Destroy internal resources for the action
Close the action before removal. This function should be used to deallocate memory if necessary.
void (*finish)(struct action *act)
Stack of actions.
The purpose of this structure is to help managing several actions at once. Actions are automatically removed from the stack if the corresponding update member function returns true after completion.
This structure contains pointers to actions that must be kept until the stack is destroyed. User is responsible of deallocating them if they were allocated from the heap.
Non-owning array of actions to manage.
act handle callback with the given event
ev if it is
void action_handle(struct action *act, const union event *ev)
Invoke and return the
act update callback with the given event
ticks since last frame if it is not NULL.
bool action_update(struct action *act, unsigned int ticks)
act draw callback if it is not NULL.
void action_draw(struct action *act)
act end callback if it is not NULL.
void action_end(struct action *act)
act finish callback if it is not NULL.
void action_finish(struct action *act)
Initalize the action stack
It is unnecessary if the object was zero'ed.
void action_stack_init(struct action_stack *st)
Add the action
act to the stack pointed by
st. Returns true if there was
enough room to insert.
bool action_stack_add(struct action_stack *st, struct action *act)
Handle the event
ev for all actions in the stack
void action_stack_handle(struct action_stack *st, const union event *ev)
Update all actions with
ticks since last frame in the stack
bool action_stack_update(struct action_stack *st, unsigned int ticks)
Draw all actions in the stack
void action_stack_draw(const struct action_stack *st)
Tells if there is any pending action in the stack
st. Returns true if there
are no actions or if they have all completed.
bool action_stack_completed(const struct action_stack *st)
Terminate all actions and clear the stack
void action_stack_finish(struct action_stack *st)