在libgdx中高效加载图像。

huangapple go评论84阅读模式
英文:

load images efficiently in libgdx

问题

以下是您提供的内容的翻译:

我想要加载一个大的颜色字节数组。在OpenGL中,我想要像播放视频一样绘制它(用于闪屏画面)。
目前,我正在像加载GIF一样加载每个像素图,但这占用了太多的内存(RAM)。(我在使用libgdx在Android平台上完成了这个过程)。

public ImageSequences(int width, int height, byte[][] pixmaps) {
    this.width = width;
    this.height = height;
    this.frameCount = pixmaps.length;
    this.data = new Pixmap[frameCount];
    for (int i = 0; i < frameCount; i++) {
        this.data[i] = new Pixmap(width, height, mainFormat);
        Pixmap p = new Pixmap(pixmaps[i], 0, pixmaps[i].length);
        this.data[i].drawPixmap(p, 0, 0, p.getWidth(), p.getHeight(), 0, 0, width, height);
        p.dispose();
    }
    this.base = new Texture(width, height, mainFormat);
}

int lastIndx = -1;
public Texture getKey(float timeState) {
    int i = Math.round(timeState * 60) % frameCount;
    if (lastIndx == i)
        return base;
    lastIndx = i;
    base.draw(data[i], 0, 0);
    return base;
}

以及

int I = 574 + 1; // 574
byte[][] dats = new byte[I];
int width = 1400;
int height = 720;
for (int i = 0; i < I; i++) {
    dats[i] = Gdx.files.internal(String.format("seq/anim_%05d.jpg", i)).readBytes();
}
anim = new ImageSequences(width, height, dats);

使用视频播放器方法看起来更高效。有什么解决方案吗?
我希望它可以在多平台上完成,并且不需要额外的库。

Edit: 有500多张图片,分辨率为1280x720像素或更高。

抱歉英语不太好,谢谢。

英文:

I wanna load a large byte[] of colors. In opengl, i want to draw it like a video (for splashscreen).
For now, I'm load each pixmap like GIF, but it takes too much memory (RAM). (I did it on the Android platform with libgdx).

public ImageSequences(int width, int height, byte[][] pixmaps) {
	this.width = width;
	this.height = height;
	this.frameCount = pixmaps.length;
	this.data = new Pixmap[frameCount];
	for (int i = 0; i &lt; frameCount; i++) {
		this.data[i] = new Pixmap(width, height, mainFormat);
		Pixmap p = new Pixmap(pixmaps[i], 0, pixmaps[i].length);
		this.data[i].drawPixmap(p, 0, 0, p.getWidth(), p.getHeight(), 0, 0, width, height);
		p.dispose();
	}
	this.base = new Texture(width, height, mainFormat);
}

int lastIndx = -1;
public Texture getKey(float timeState) {
	int i = Math.round(timeState * 60) % frameCount;
	if (lastIndx == i)
		return base;
	lastIndx = i;
	base.draw(data[i], 0, 0);
	return base;
}

And

        int I = 574 + 1;// 574
		byte[][] dats = new byte[I][];
		int width = 1400;
		int height = 720;
		for (int i = 0; i &lt; I; i++) {
			dats[i] = Gdx.files.internal(String.format(&quot;seq/anim_%05d.jpg&quot;, i)).readBytes();
		}
		anim = new ImageSequences(width, height, dats);

Using the video player method looks more efficient. Any solution?

I hope, it can be done on multiplatform and without additional libraries.

<bold>Edit:<bold> with 500+ images and 1280x720 pixels or more high.

Sorry for bad english and Thanks.

答案1

得分: 1

你应该尝试使用AssetManager。
AssetManager会为您处理资源(例如,多次使用)。

在使用资源之前,您需要加载它。

this.assetManager.load("您的内部路径", Texture.class);

assetManager.finishLoading(); // 可选,但它只是等待资源加载结束

之后,您就可以获取您的资源了。

资源的加载可以在应用程序或屏幕启动时提前异步完成。

您可以在libGdx的GitHub Wiki中找到更多相关信息。

另外,我认为您应该使用Texture而不是PixMap。

并且,请修改ImageSequence的构造函数:

public ImageSequences(int width, int height, List<Texture> textures) {
    // ...
}

之后,ImageSequence只需更改当前的纹理以进行动画制作。

Texture类将直接为您处理位图。而Texture对象可以直接由assetManager创建:

Texture texture = assetManager.get("您的内部路径", Texture.class);
英文:

You should try to use an AssetManager.
AssetManager will handle the asset for you (Multiple use by example).

You have to load your Asset before using it.

this.assetManager.load(&quot;Your internal Path&quot;, Texture.class);

assetManager.finishLoading();//optional but he just wait the end of assetLoading 

And after that you can get your Asset.

The load of the asset can be done before in advance asynchronously in the startup of the application or the screen.

You can see more information about it in libGdx gitHub wiki

Also I think you should use a Texture instead of PixMap

and

change ImageSequence constructor

public ImageSequences(int width, int height, List&lt;Texture&gt; textures) {
 ....
}

After that imageSequence will just have to change the current texture for making the animation.

Texture class will directly handle the bit for you. And the Texture Object can directly be created by the assetManager

Teture texture = assetManager.get(&quot;Your internal Path&quot;, Texture.class);

huangapple
  • 本文由 发表于 2020年10月21日 11:53:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64456433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定