Jo Engine  2020.06.22
Jo Sega Saturn Engine
storyboard.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
35 #ifndef __JO_STORYBOARD_H__
36 # define __JO_STORYBOARD_H__
37 
38 #ifdef JO_COMPILE_WITH_STORYBOARD_SUPPORT
39 
43 # define JO_STORYBOARD_INFINITE_DURATION (0)
44 
48 typedef struct
49 {
51  int dx;
52  int dy;
53  short current_cos;
54  short current_sin;
56 
58 typedef struct
59 {
60  bool disabled;
61  unsigned int count;
62  unsigned short frame;
63  unsigned char frame_skip;
64  unsigned int limit;
66  short cos_radius;
67  short sin_radius;
68  short direction;
71 } jo_animation;
72 
76 typedef struct
77 {
80  bool repeat;
81  bool pause;
83 
84 /*
85 ** STORYBOARD AND ANIMATION FACTORY
86 */
87 
95 jo_storyboard *jo_storyboard_create(const bool autoplay, const bool repeat);
96 
101 void jo_storyboard_destroy(jo_storyboard *const storyboard);
102 
111 jo_animation *jo_storyboard_create_animation(jo_storyboard * const storyboard, const unsigned char frame_skip, const unsigned int duration);
112 
118 static __jo_force_inline void jo_storyboard_destroy_animation(jo_storyboard * const storyboard, const jo_animation * const animation)
119 {
120  jo_list_remove_first_value(&storyboard->animations, (jo_list_data)(void *)animation);
121 }
122 
123 /*
124 ** STORYBOARD CONTROL
125 */
126 
131 static __jo_force_inline void jo_storyboard_repeat(jo_storyboard * const storyboard, bool repeat)
132 {
133  storyboard->repeat = repeat;
134 }
135 
140 {
141  storyboard->pause = true;
142 }
143 
147 static __jo_force_inline void jo_storyboard_resume(jo_storyboard * const storyboard)
148 {
149  storyboard->pause = false;
150 }
151 
155 static __jo_force_inline void jo_storyboard_play(jo_storyboard * const storyboard)
156 {
157  storyboard->pause = false;
158 }
159 
163 static __jo_force_inline void jo_storyboard_toggle(jo_storyboard * const storyboard)
164 {
165  storyboard->pause ^= true;
166 }
167 
168 /*
169 ** ADD/REMOVE OBJECT IN STORYBOARD
170 */
171 
177 static __jo_force_inline void jo_storyboard_add_object(jo_storyboard *const storyboard, const void * const object)
178 {
180 
181  tmp->user = (jo_3d_object_attributes *)object;
182  tmp->dx = JO_MULT_BY_8(tmp->user->x);
183  tmp->dy = JO_MULT_BY_8(tmp->user->y);
184  JO_ZERO(tmp->current_cos);
185  JO_ZERO(tmp->current_sin);
186  jo_list_add_ptr(&storyboard->objects, tmp);
187 }
188 
193 static __jo_force_inline void jo_storyboard_remove_object(jo_storyboard *const storyboard, const void * const object)
194 {
195  jo_node *tmp;
196  for (tmp = storyboard->objects.first; tmp != JO_NULL; tmp = tmp->next)
197  if (((jo_storyboard_object_cache *)tmp->data.ptr)->user == object)
198  {
199  jo_list_remove(&storyboard->objects, tmp);
200  return ;
201  }
202 }
203 
204 /*
205 ** HELPER
206 */
207 
214 {
215  jo_node *tmp;
216  for (tmp = storyboard->objects.first; tmp != JO_NULL; tmp = tmp->next)
217  if (((jo_storyboard_object_cache *)tmp->data.ptr)->user == object)
218  return ((jo_storyboard_object_cache *)tmp->data.ptr);
219  return (JO_NULL);
220 }
221 
227 static __jo_force_inline void jo_storyboard_reset_object_position(jo_storyboard * const storyboard, const void * const object)
228 {
229  jo_storyboard_object_cache *cache = jo_storyboard_get_object_cache(storyboard, object);
230 
231  if (cache == JO_NULL)
232  return;
233  cache->dx = JO_MULT_BY_8(cache->user->x);
234  cache->dy = JO_MULT_BY_8(cache->user->y);
235 }
236 
244 static __jo_force_inline jo_storyboard *jo_storyboard_create_for_object(const bool autoplay, const bool repeat, const void * const object)
245 {
246  jo_storyboard *storyboard = jo_storyboard_create(autoplay, repeat);
247 
248  jo_storyboard_add_object(storyboard, object);
249  return storyboard;
250 }
251 
252 /*
253 ** QUICK ANIMATION FACTORY
254 */
255 
264 static __jo_force_inline jo_animation *jo_storyboard_create_rotation_animation(jo_storyboard * const storyboard, const char speed, const unsigned int duration)
265 {
266  jo_animation *animation = jo_storyboard_create_animation(storyboard, 0, duration);
267 
268  animation->rotation_speed = speed;
269  return animation;
270 }
271 
279 static __jo_force_inline jo_animation *jo_storyboard_create_translation_animation(jo_storyboard *const storyboard, const short angle, const char speed, const unsigned int duration)
280 {
281  jo_animation *animation = jo_storyboard_create_animation(storyboard, 0, duration);
282 
283  animation->direction = angle;
284  animation->translation_speed_x = speed;
285  animation->translation_speed_y = speed;
286  return (animation);
287 }
288 
296 static __jo_force_inline jo_animation *jo_storyboard_create_translation_animation_using_direction(jo_storyboard * const storyboard, const jo_8_directions direction, const char speed, const unsigned int duration)
297 {
298  return (jo_storyboard_create_translation_animation(storyboard, jo_direction_to_angle(direction), speed, duration));
299 }
300 
308 static __jo_force_inline jo_animation *jo_storyboard_create_circle_animation(jo_storyboard * const storyboard, const short radius, const char speed, const unsigned int duration)
309 {
310  jo_animation *animation = jo_storyboard_create_animation(storyboard, 0, duration);
311 
312  animation->sin_radius = radius;
313  animation->cos_radius = radius;
314  animation->translation_speed_x = speed;
315  animation->translation_speed_y = speed;
316  return (animation);
317 }
318 
319 /*
320 ** QUICK STORYBOARD FACTORY FOR OBJECT
321 */
322 
331 static __jo_force_inline jo_storyboard *jo_storyboard_rotate_object(const void * const object, const char speed, const unsigned int duration)
332 {
333  jo_storyboard *storyboard = jo_storyboard_create_for_object(true, false, object);
334 
335  jo_storyboard_create_rotation_animation(storyboard, speed, duration);
336  return (storyboard);
337 }
338 
346 static __jo_force_inline jo_storyboard *jo_storyboard_move_object(const void * const object, const short angle, const char speed, const unsigned int duration)
347 {
348  jo_storyboard *storyboard = jo_storyboard_create_for_object(true, false, object);
349 
350  jo_storyboard_create_translation_animation(storyboard, angle, speed, duration);
351  return (storyboard);
352 }
353 
361 static __jo_force_inline jo_storyboard *jo_storyboard_move_object_in_direction(const void * const object, const jo_8_directions direction, const char speed, const unsigned int duration)
362 {
363  jo_storyboard *storyboard = jo_storyboard_create_for_object(true, false, object);
364 
365  jo_storyboard_create_translation_animation_using_direction(storyboard, direction, speed, duration);
366  return (storyboard);
367 }
368 
376 static __jo_force_inline jo_storyboard *jo_storyboard_move_object_in_circle(const void * const object, const short radius, const char speed, const unsigned int duration)
377 {
378  jo_storyboard *storyboard = jo_storyboard_create_for_object(true, false, object);
379 
380  jo_storyboard_create_circle_animation(storyboard, radius, speed, duration);
381  return storyboard;
382 }
383 
384 #endif /* !JO_COMPILE_WITH_STORYBOARD_SUPPORT */
385 
386 #endif /* !__JO_STORYBOARD_H__ */
387 
388 /*
389 ** END OF FILE
390 */
jo_storyboard_destroy
void jo_storyboard_destroy(jo_storyboard *const storyboard)
Destroy the storyboard.
jo_storyboard_play
static __jo_force_inline void jo_storyboard_play(jo_storyboard *const storyboard)
Play storyboard animation.
Definition: storyboard.h:155
jo_storyboard_suspend
static __jo_force_inline void jo_storyboard_suspend(jo_storyboard *const storyboard)
Suspend all storyboard animation.
Definition: storyboard.h:139
jo_animation::cos_radius
short cos_radius
Definition: storyboard.h:66
jo_storyboard_move_object_in_circle
static __jo_force_inline jo_storyboard * jo_storyboard_move_object_in_circle(const void *const object, const short radius, const char speed, const unsigned int duration)
Create a circle rotation storyboard for a specific object.
Definition: storyboard.h:376
jo_storyboard_resume
static __jo_force_inline void jo_storyboard_resume(jo_storyboard *const storyboard)
Resume all suspended storyboard animation.
Definition: storyboard.h:147
jo_storyboard_object_cache::current_cos
short current_cos
Definition: storyboard.h:53
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
jo_storyboard_object_cache::dx
int dx
Definition: storyboard.h:51
jo_animation
Object animation definition.
Definition: storyboard.h:59
jo_storyboard::animations
jo_list animations
Definition: storyboard.h:79
jo_storyboard::pause
bool pause
Definition: storyboard.h:81
jo_list_remove_first_value
static __jo_force_inline bool jo_list_remove_first_value(jo_list *const list, jo_list_data data)
Remove the first item on the list that match DATA.
Definition: list.h:252
jo_animation::sin_radius
short sin_radius
Definition: storyboard.h:67
jo_storyboard_create_rotation_animation
static __jo_force_inline jo_animation * jo_storyboard_create_rotation_animation(jo_storyboard *const storyboard, const char speed, const unsigned int duration)
Create a rotation animation.
Definition: storyboard.h:264
jo_storyboard_object_cache
Internal object cache.
Definition: storyboard.h:49
jo_animation::limit
unsigned int limit
Definition: storyboard.h:64
jo_list_remove
void jo_list_remove(jo_list *const list, const jo_node *const node_to_delete)
Remove an item on the list.
jo_animation::disabled
bool disabled
Definition: storyboard.h:60
jo_list_data
Node data (4 bytes)
Definition: list.h:49
jo_direction_to_angle
static __jo_force_inline short jo_direction_to_angle(const jo_8_directions direction)
Convert jo_8_directions to angle in degree.
Definition: math.h:1473
JO_ZERO
#define JO_ZERO(X)
Set a variable to zero.
Definition: math.h:213
jo_storyboard::repeat
bool repeat
Definition: storyboard.h:80
jo_storyboard
Storyboard definition.
Definition: storyboard.h:77
jo_storyboard_create_circle_animation
static __jo_force_inline jo_animation * jo_storyboard_create_circle_animation(jo_storyboard *const storyboard, const short radius, const char speed, const unsigned int duration)
Create a circle rotation animation.
Definition: storyboard.h:308
__jo_node::data
jo_list_data data
Definition: list.h:66
jo_list::first
jo_node * first
Definition: list.h:75
jo_animation::count
unsigned int count
Definition: storyboard.h:61
jo_storyboard_move_object
static __jo_force_inline jo_storyboard * jo_storyboard_move_object(const void *const object, const short angle, const char speed, const unsigned int duration)
Create a translation animation storyboard for a specific object.
Definition: storyboard.h:346
jo_storyboard_object_cache::user
jo_3d_object_attributes * user
Definition: storyboard.h:50
jo_storyboard_get_object_cache
static __jo_force_inline jo_storyboard_object_cache * jo_storyboard_get_object_cache(jo_storyboard *const storyboard, const void *const object)
Get object storyboard cache.
Definition: storyboard.h:213
JO_NULL
#define JO_NULL
NULL implementation.
Definition: tools.h:170
jo_storyboard_object_cache::dy
int dy
Definition: storyboard.h:52
jo_animation::translation_speed_x
char translation_speed_x
Definition: storyboard.h:69
jo_storyboard_toggle
static __jo_force_inline void jo_storyboard_toggle(jo_storyboard *const storyboard)
Switch between resume and suspend.
Definition: storyboard.h:163
jo_storyboard_add_object
static __jo_force_inline void jo_storyboard_add_object(jo_storyboard *const storyboard, const void *const object)
Add object in storyboard.
Definition: storyboard.h:177
jo_list
List struct.
Definition: list.h:73
JO_MULT_BY_8
#define JO_MULT_BY_8(X)
Multiply a variable by 8.
Definition: math.h:78
jo_animation::frame_skip
unsigned char frame_skip
Definition: storyboard.h:63
jo_storyboard_create_translation_animation_using_direction
static __jo_force_inline jo_animation * jo_storyboard_create_translation_animation_using_direction(jo_storyboard *const storyboard, const jo_8_directions direction, const char speed, const unsigned int duration)
Create a translation animation using jo_8_directions enum.
Definition: storyboard.h:296
jo_malloc_with_behaviour
void * jo_malloc_with_behaviour(unsigned int n, const jo_malloc_behaviour behaviour)
dynamic memory allocator with specific behaviour
jo_storyboard_rotate_object
static __jo_force_inline jo_storyboard * jo_storyboard_rotate_object(const void *const object, const char speed, const unsigned int duration)
Create a rotation animation storyboard for a specific object.
Definition: storyboard.h:331
jo_storyboard_object_cache::current_sin
short current_sin
Definition: storyboard.h:54
jo_storyboard_create_translation_animation
static __jo_force_inline jo_animation * jo_storyboard_create_translation_animation(jo_storyboard *const storyboard, const short angle, const char speed, const unsigned int duration)
Create a translation animation.
Definition: storyboard.h:279
jo_list_add_ptr
static __jo_force_inline jo_node * jo_list_add_ptr(jo_list *const list, void *ptr)
Add a pointer on the list.
Definition: list.h:105
jo_storyboard_remove_object
static __jo_force_inline void jo_storyboard_remove_object(jo_storyboard *const storyboard, const void *const object)
Remove object from storyboard.
Definition: storyboard.h:193
jo_animation::frame
unsigned short frame
Definition: storyboard.h:62
__jo_node
Node struct.
Definition: list.h:65
JO_MALLOC_TRY_REUSE_SAME_BLOCK_SIZE
@ JO_MALLOC_TRY_REUSE_SAME_BLOCK_SIZE
A little bit slower than JO_MALLOC_TRY_REUSE_BLOCK but reduce memory usage.
Definition: malloc.h:71
jo_animation::rotation_speed
char rotation_speed
Definition: storyboard.h:65
jo_storyboard_move_object_in_direction
static __jo_force_inline jo_storyboard * jo_storyboard_move_object_in_direction(const void *const object, const jo_8_directions direction, const char speed, const unsigned int duration)
Create a translation animation storyboard for a specific object using jo_8_directions enum.
Definition: storyboard.h:361
jo_8_directions
jo_8_directions
8 directions definition
Definition: types.h:283
jo_storyboard::objects
jo_list objects
Definition: storyboard.h:78
__jo_node::next
jo_node * next
Definition: list.h:68
jo_animation::direction
short direction
Definition: storyboard.h:68
jo_storyboard_reset_object_position
static __jo_force_inline void jo_storyboard_reset_object_position(jo_storyboard *const storyboard, const void *const object)
Reset object position.
Definition: storyboard.h:227
jo_storyboard_create_animation
jo_animation * jo_storyboard_create_animation(jo_storyboard *const storyboard, const unsigned char frame_skip, const unsigned int duration)
Create an empty animation.
jo_storyboard_destroy_animation
static __jo_force_inline void jo_storyboard_destroy_animation(jo_storyboard *const storyboard, const jo_animation *const animation)
Destroy animation.
Definition: storyboard.h:118
jo_animation::translation_speed_y
char translation_speed_y
Definition: storyboard.h:70
jo_storyboard_repeat
static __jo_force_inline void jo_storyboard_repeat(jo_storyboard *const storyboard, bool repeat)
Storyboard repeat control.
Definition: storyboard.h:131
jo_list_data::ptr
void * ptr
Definition: list.h:59
jo_storyboard_create
jo_storyboard * jo_storyboard_create(const bool autoplay, const bool repeat)
Create an empty storyboard.
jo_storyboard_create_for_object
static __jo_force_inline jo_storyboard * jo_storyboard_create_for_object(const bool autoplay, const bool repeat, const void *const object)
Create a new storyboard for the object.
Definition: storyboard.h:244
jo_3d_object_attributes
Object for 3D manipulation (storyboard) using fixed number.
Definition: types.h:121