有没有办法将上下文传递到 onDrawFrame(GL10 gl) 中?

发布于 2025-01-01 16:58:35 字数 584 浏览 5 评论 0原文

我正在 Android 上的 Open GLES 中使用 Java 设计一款游戏。

显然我使用了大量的纹理,很快我就会耗尽内存。

我对 Android 和 Java 很陌生,但我认为解决这个问题的最简单方法是销毁带有位图的对象,我目前不需要这些对象,让垃圾收集器回收它们占用的内存,然后在后期当我再次需要各自的纹理时重新创建这些对象。

像这样的事情:

background = null;

然后当我需要背景对象中的纹理时:

background = new Background(context);

看起来非常简单,除了上下文(我的类背景需要它运行,因为它通过 BitmapFactory 从它加载纹理)没有传递到onDrawFrame(GL10 gl) 函数是我的主游戏循环所在以及我需要它的地方!

如果我尝试在 GL10 gl 之后添加上下文作为第二个参数,则会收到错误 - 这似乎不可能。

有谁知道如何将上下文传递到 onDrawFrame 中,或者以其他方式如何从内存中清除上下文对象,然后再次恢复它们?

I am designing a game in Open GLES on Android in Java.

Obviously I am using lots of textures and soon I will run out of memory.

I am quite new to Android and Java, but I figured that the easiest way to go round this problem is to destroy the objects with bitmaps, which I don't need at the moment, let the garbage collector reclaim the memory which they occupied, then re-create those objects at the late stage when I again need the respective textures.

Something like this:

background = null;

then when I need the textures in background object:

background = new Background(context);

seems pretty straighforward, except that is, that the context (which I need for my class background to function, as it loads the textures from it via BitmapFactory) is not passed into onDrawFrame(GL10 gl) function where my main game loop is and where I need it!

If I try to add context as a second parameter after GL10 gl, I get an error - this does not seem to be possible.

Does anybody know how to pass context into onDrawFrame, or other way how to clear the context objects from memory and then reinstate them again?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

倾城泪 2025-01-08 16:58:35

您必须绘制到 SurfaceViewGLSurfaceView...这显然是 View 的一种类型,因此具有 getContext()方法。

You must be drawing to a SurfaceView, or a GLSurfaceView... which is obviously a type of View, and therefore has a getContext() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文