Module: action
Synopsis
#include <core/action.h>
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).
Macros
ACTION_STACK_MAX
Maximum number of action in a unique action_stack.
#define ACTION_STACK_MAX 128
Structs
action
Use this structure to create an action that may handle input, be updated and drawn.
All members can be NULL.
Field | Access | Type |
---|---|---|
data | (+&?) | void * |
handle | (+?) | void (*)(struct action *, const union event *) |
update | (+?) | bool (*)(struct action *, unsigned int) |
draw | (+?) | void (*)(struct action *) |
end | (+?) | void (*)(struct action *) |
finish | (+?) | void (*)(struct action *) |
data
Arbitrary user data.
void *data
handle
Handle an event.
void (*handle)(struct action *self, const union event *ev)
update
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
Draw the action self
.
void (*draw)(struct action *self)
end
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)
finish
Destroy internal resources for the action self
.
Close the action before removal. This function should be used to deallocate memory if necessary.
void (*finish)(struct action *act)
action_stack
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.
Field | Type |
---|---|
actions | struct action *[ACTION_STACK_MAX] |
actions
Non-owning array of actions to manage.
Functions
action_handle
Invoke the act
handle callback with the given event ev
if it is
not NULL.
void
action_handle(struct action *act, const union event *ev)
action_update
Invoke and return the act
update callback with the given event ev
and ticks
since last frame if it is not NULL.
bool
action_update(struct action *act, unsigned int ticks)
action_draw
Invoke the act
draw callback if it is not NULL.
void
action_draw(struct action *act)
action_end
Invoke the act
end callback if it is not NULL.
void
action_end(struct action *act)
action_finish
Invoke the act
finish callback if it is not NULL.
void
action_finish(struct action *act)
action_stack_init
Initalize the action stack st
.
Note
It is unnecessary if the object was zero'ed.
void
action_stack_init(struct action_stack *st)
action_stack_add
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)
action_stack_handle
Handle the event ev
for all actions in the stack st
.
void
action_stack_handle(struct action_stack *st, const union event *ev)
action_stack_update
Update all actions with ticks
since last frame in the stack st
.
bool
action_stack_update(struct action_stack *st, unsigned int ticks)
action_stack_draw
Draw all actions in the stack st
.
void
action_stack_draw(const struct action_stack *st)
action_stack_completed
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)
action_stack_finish
Terminate all actions and clear the stack st
.
void
action_stack_finish(struct action_stack *st)