简体   繁体   中英

LibGdx - Android - Sprite allocation during render

I have a routine that draws a Sprite in every loop of the render method. My first approach was to create a Sprite every loop but this leads to garbage and when the gc ocurrs it may affect the game performance (FPS goes from 60 to 5* or worse). So, I removed all the heap allocation during the render loop, but I coudn't find a solution for the sprites. I tried to declare a Sprite object (currentSprite in the code below) in the class and initialize it during the constructor. Then set the TextureRegion for the corresponding image index (animation) in the draw method. It does not work. But if I instantiate currentSprite in the draw method it does (but generates garbage). Weird.... Any thoughts? Thank you in advance

        JP

*private boolean draw(float stateTime) {
    //Sprite sprite;    
    TextureRegion currentFrame;
    float OriginX=0;
    float OriginY=0;
    WorldEntityEstate childEstate;


    WorldEntityEstate currentEstate = this.estateHandler.getCurrentEstate(); 
    if(currentEstate.anim==null) return(true);
    Vector2 spritePos = this.body.getPosition();

    //System.out.println("currentEstate.animFrameIndex = " + currentEstate.animFrameIndex);
    currentFrame =  currentEstate.anim.getKeyFrames()[currentEstate.animFrameIndex];        

    this.currentSprite = new Sprite();          // If I comment this out the Sprite is not drawn
    this.currentSprite.setRegion(currentFrame);
    this.currentSprite.setSize(currentFrame.getRegionWidth(), currentFrame.getRegionHeight());          
    OriginX = this.currentSprite.getWidth()*this.bodyOrigin.x / (this.properties.worldScale * (this.currentSprite.getWidth()/ Definitions.PIXELS_PER_METER));
    OriginY = this.currentSprite.getHeight()*this.bodyOrigin.y / (this.properties.worldScale * (this.currentSprite.getHeight()/ Definitions.PIXELS_PER_METER));                         
    this.currentSprite.setOrigin(OriginX, OriginY);                         
    this.currentSprite.setScale(1 / Definitions.PIXELS_PER_METER * this.properties.worldScale) ;
    this.currentSprite.setRotation(this.body.getAngle()*MathUtils.radiansToDegrees);
    this.currentSprite.translate(-1 * OriginX + spritePos.x + currentEstate.animOffsetX, -1 * OriginY + spritePos.y+currentEstate.animOffsetY);

    this.checkBlink();        
    this.currentSprite.setColor(this.currentColorAmount);
    this.currentSprite.draw(this.spriteBatch);
    return(true);

}*

Why do you need the sprite?

You can use only the TextureRegion to do your rotation and scaling.

Please refer at:

https://github.com/libgdx/libgdx/wiki/2D-Animation

and

https://github.com/libgdx/libgdx/wiki/Spritebatch%2C-Textureregions%2C-and-Sprites

draw(TextureRegion region,
     float x, float y,
     float originX, float originY,
     float width, float height,
     float scaleX, float scaleY,
     float rotation)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM