0

I have a texture pack made of 9 images. Out of the 9 images, I am using 3 of them as animation frames. So my animation is made of 3 images. The images have a fixed resolution of 128x128.

TextureRegion sprite1 = atlas.findRegion("sprite1");
TextureRegion sprite2 = atlas.findRegion("sprite2");
TextureRegion sprite3 = atlas.findRegion("sprite3");

...
...

Animation spriteAnimation = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);
// similarly I am creating 4 more animation instance using same 3 regions
Animation spriteAnimation2 = new Animation(.130f,new TextureRegion[sprite1,sprite2,sprite3]);

...
...

Game loop :-

{
 update();
 render();
}

I have following code in update() and render().

update()
{
 ...
 // because images are of fixed resolution 128x128
 // I will calculate the scale factor as per the device screen
 // so for each device width and height will vary..
 // GameUnits is giving scaled width and height need for device

 x = get new x;
 y = get new y;

 width = GameUnits.getWidth();
 height = GameUnits.getHeight();

 // similarly updating 4 more width & height, each of them are different.
 ...
 ...
}

render()
{
 ...
 spriteBatch.begin();
 spriteBatch.draw(spriteAnimation.getKeyFrame(totalTime),x,y,width1,height1);

 // similarly rendering total of 5 animation
 spriteBatch.draw(spriteAnimation2.getKeyFrame(totalTime),x,y,width2,height2);
 ...

 spriteBatch.end();
 ...
}

Now, update() method is taking 0.045ms on average, whereas render() method is taking 16ms on average, giving me FPS of ~60.

At some instant render() method takes 22ms or high or low and hence creating lags. Render method has same work load every frame, i.e to render 5 animation.

I am new to libgdx without any prior experience in gaming.

May I know what mistake I made in implementing Animations?

The update() code is working well for me, but it looks like there are serious holes in render().

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Vipul
  • 35
  • 2
  • 7
  • That sounds pretty average, drawing stuff to the screen takes longer than anything else. – KodyVanRy Mar 27 '14 at 05:43
  • The backends limit the FPS: http://code.google.com/p/libgdx-users/wiki/limitFps So if you are using the lwjgl backend you won't go over 60 FPS as much as i understood – Robert P Mar 27 '14 at 07:02
  • @Springrbua this is not correct. The wiki you posted ist totaly outdate. See this post: http://stackoverflow.com/questions/15097834/is-it-possible-to-disable-frame-limiting-in-libgdx . The backends have the option to disable the max fps. **BUT** the android backend wont let you disable vsync! – bemeyer Mar 27 '14 at 09:59
  • 1
    @BennX it should be correct, as by deffault the limits are turned on as much as i understood. Also the wiki i posted tells you, that you can disable it. It is maybe outdated, but there is no link to the new wikipost to this theme and i don't think that it all totally changed... So he probably has the limitation on. Correct me if i am wrong i am here to learn :) – Robert P Mar 27 '14 at 10:08
  • It looks okay, some variation is normal but performance tuning is hard. First you'd have to show every line of code in render and maybe before. You could be doing something else like calling `new` one two many times and kicking off a GC. Look into Traceview - https://developer.android.com/tools/debugging/debugging-tracing.html – Chase Mar 27 '14 at 20:11

0 Answers0