Author Topic: Set the frame of a sprite animation!  (Read 178 times)

Mr. Potatobadger

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Set the frame of a sprite animation!
« on: August 14, 2017, 01:52:26 am »
Hey all!

I noticed that Jo Engine's sprite animator does not seem to have a function to set the frame of a sprite to whatever frame you like. I went ahead and added that feature, and figured I should share it, as it's fairly commonly needed, and very simple.

I will attach my sprite_animatior.h file for if you want to simply replace the file, but in case you've made additions like I have, here's a simple way to add it:

in "jo_engine_src\jo_engine\jo", you will see "sprite_animator.h". Open this up. I chose to put it under "jo_set_sprite_anim_frame_rate", as it seemed like the best spot.

Here is the code:

Code: [Select]
/** @brief Set animation frame
 *  @param anim_id Id returned by jo_create_sprite_anim()
 *  @param frame Frame of animation to be displayed, where 0 is the first frame.
 */
static __jo_force_inline int jo_set_sprite_anim_frame(int anim_id, unsigned char frame) { return __jo_sprite_anim_tab[anim_id].cur_frame = frame; }

I made sure to keep it consistent with the usual Jo formatting standards   ;D
This seems to be the simplest way to do it. If someone has a better way to do it, please let me know.

Hoping to see this addition in the next official release of Jo!

EDIT:
Here's an example of it in action:
https://i.gyazo.com/7b2a8c29fef8bb04703a09ad9b561edc.mp4

I've set sonic's jumping animation to frame 5 for the sake of example. The code looks like this:
Code: [Select]
jo_set_sprite_anim_frame(sonic.anim_jump_id, 5);
« Last Edit: August 14, 2017, 01:56:32 am by Mr. Potatobadger »

mindslight

  • Administrator
  • Full Member
  • *****
  • Posts: 134
  • Karma: +2/-0
    • View Profile
    • Jo Engine
Re: Set the frame of a sprite animation!
« Reply #1 on: August 14, 2017, 01:16:06 pm »
Hi Mr. Potatobadger :)

I integrate your function on the next release.

I just added some const and removed the return value.

New prototypes:

Code: [Select]
/** @brief Set animation frame
 *  @param anim_id Id returned by jo_create_sprite_anim()
 *  @param frame Frame of animation to be displayed, where 0 is the first frame.
 */
static __jo_force_inline void jo_set_sprite_anim_frame(const int anim_id, const unsigned char frame) { __jo_sprite_anim_tab[anim_id].cur_frame = frame; }

Mr. Potatobadger

  • Newbie
  • *
  • Posts: 14
  • Karma: +0/-0
    • View Profile
Re: Set the frame of a sprite animation!
« Reply #2 on: August 20, 2017, 12:44:29 am »
Awesome!
 ;D

 

Sitemap 1 2 3 4 5 6 7 8 9 10 
SMF spam blocked by CleanTalk