Java的定制油漆实施绩效问题 [英] Java custom Paint implementation performance issue

查看:111
本文介绍了Java的定制油漆实施绩效问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java创建一个游戏,我使用TexturePaint在背景纹理区域。性能优良使用java.awt.TexturePaint,但是,我希望有一个有内在的旋转区域,所以我尝试实现定制喷漆称为OrientedTexturePaint:

I'm using Java to create a game, and I'm using TexturePaint to texture areas in the background. Performance is fine using java.awt.TexturePaint, however, I want to have areas having an inherent rotation, so I tried implementing a custom Paint called OrientedTexturePaint:

public class OrientableTexturePaint implements Paint {

    private TexturePaint texture;
    private float orientation;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
        this.orientation = HelperMethods.clampRadians((float)Math.toRadians(orientation));
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        AffineTransform newTransform = (AffineTransform)xform.clone();
        newTransform.rotate(orientation);
        return texture.createContext(cm, deviceBounds, userBounds, newTransform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }


}

唯一的问题是,有一个巨大的性能损失:帧速率从舒适(上限)60fps的约3FPS下降。此外,如果我实现这个作为一个纯粹的包装器TexturePaint - 无需创建新的变换,简单地传递参数给TexturePaint的方法和返回什么TexturePaint的回报,我得到了相同的结果。

Only problem is, there's a huge performance hit: the frame rate drops from a comfortable (capped) 60fps to about 3fps. Furthermore, if I implement this as a pure wrapper to TexturePaint - without creating the new transform, simply passing the arguments to TexturePaint's methods and returning what TexturePaint returns, I get the same result.

即:

public class MyTexturePaint implements Paint {

    private TexturePaint texture;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        return texture.createContext(cm, deviceBounds, userBounds, xform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }
}

进行大规模逊于TexturePaint一样。怎么来了,有没有什么办法来解决这个问题?

performs massively worse than TexturePaint does. How come, and is there any way to get around this?

推荐答案

我要画的旋转部分为一个BufferedImage,然后油漆背景。然后你只需要更新的BufferedImage时,有一个变化的旋转部分。

I would paint the rotated portion to a BufferedImage and then paint that to the background. Then you only have to update the BufferedImage when there is a change to the rotated portions.

如果你没有创建一个高速缓存这种方法,如果你每次都从头开始做画,你要考虑漆模型的管道,以及如何复杂,是。性能大约减少了瓶颈渲染流水线,并通过一切都通过一个texturepaint过程听起来像一个巨大的瓶颈。纹理是要创建一次,经常使用,不经常创建,使用一次。

If you don't create a cache this way, if you do the painting from scratch each time, you have to consider the pipeline of the paint model and how complex that is. Performance is about reducing the bottlenecks in the rendering pipeline, and passing everything through a texturepaint process sounds like a massive bottleneck. Textures are to be created once, used often, not created often, used once.

这篇关于Java的定制油漆实施绩效问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆