Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Static Variables Overriding for different Textures in SpriteAnimations #5

Open
Pikachuxxxx opened this issue Jul 6, 2020 · 2 comments
Assignees
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@Pikachuxxxx
Copy link
Member

Pikachuxxxx commented Jul 6, 2020

void AnimateSpriteSheetRec(Texture2D spriteSheet, Rectangle *frameRec, int framesSpeed, int frames);

Implementation Code :

void AnimateSpriteSheetRec(Texture2D spriteSheet, Rectangle *frameRec, int framesSpeed, int frames){
  static float framesCounter = 0;
  static int currentFrame = 0;

  *frameRec = (Rectangle){ 0.0f, 0.0f, (float)spriteSheet.width/frames, (float)spriteSheet.height };

  framesCounter +=  GetFrameTime();

   if (framesCounter >= (float)framesSpeed/100)
   {
       framesCounter = 0;
       currentFrame++;

       if (currentFrame > frames - 1) currentFrame = 0;
   }
   frameRec->x = (float)currentFrame*(float)spriteSheet.width/frames;
}

Issue :
This function uses Static variables for frameRateCounter and currentFrame counting which makes this is ideal if is being used to animate a single sprite sheet with the intention of avoiding global variables, but the static variables values get overwritten/ influenced if the function is called multiple times to animate other sprites sheets/ Different Textures making this function Non-Generic, also using global variables for frame and rate counting for 100's of sprites sheets would be absurd.

Desired Solution:
It is necessary to make this function more Generic by making it retain certain values (for now 2 variables for rate and frameCount) separately for each instance call if new Texture is passed to is. So for every new texture how many ever times it may be called it must retain a few variables, i.e must have local static variables for every new texture passed.

Current Workaround:

  • If using this function to animate different instances of same Texture multiply the base frameSpeed by number of instances to get the same effect as if using a single texture.
  • This is same as increasing frame speed delay proportional to increased number of function calls.
@Pikachuxxxx
Copy link
Member Author

Pikachuxxxx commented Jul 6, 2020

Proposed solution 1 :
However the current workaround is not a generic solution because similar textures will function properly for the same frame speed but different textures require different frame speeds, however we can use a algorithm to set the frame rate dynamically for every frame call that can interpolate all the frame rates.

@Pikachuxxxx Pikachuxxxx added bug Something isn't working good first issue Good for newcomers labels Jul 6, 2020
@Pikachuxxxx Pikachuxxxx self-assigned this Jul 6, 2020
@Pikachuxxxx
Copy link
Member Author

Sprite Animation System

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

1 participant