在Android游戏传递java.nio.IntBuffer中,以C函数 [英] Passing java.nio.IntBuffer to C function in an Android game

查看:137
本文介绍了在Android游戏传递java.nio.IntBuffer中,以C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够加入少许本地C code在我的Andr​​oid应用程序。我有一个IntBuffer,我作为一个参数的OpenGL的glColorPointer方法​​来使用。这是填充/使用这样的事情:

I'd like to be able to add a little native C code in my Android app. I have an IntBuffer which I use as a parameter to OpenGL's glColorPointer method. This is populated/used something like this:

private IntBuffer mColourBuffer;
int[] colours = new int[10 * 4];   // 10 dots, 4 colour components per dot


ByteBuffer vbb3 = ByteBuffer.allocateDirect(10 * 4 * 4);   
vbb3.order(ByteOrder.nativeOrder());
mColourBuffer = vbb3.asIntBuffer();
mColourBuffer.put(colours);
mColourBuffer.position(0);

gl.glColorPointer(4, GL10.GL_FIXED, 0, mColourBuffer); 

我想修改此缓冲区,但它是从Java太慢了,所以我想用本土code做到这一点。我一直在寻找一个简单的例子,但我无法找到任何东西,我可以做的感觉,或者是工作。大量的文档我见过有关传递要素在谈判,但肯​​定我要的是一个指针。传递指针和内存运行它指向应该是非常有效的;由于能够从C上的内存进行操作传递之间的Java和C会杀死可能作出的任何速度提升内存负载

I'd like to make changes to this buffer but it's too slow from Java, so I'd like to do it with native code. I've been looking for a simple example but I can't find anything which I can make sense of, or which works. A lot of the documentation I've seen talks about passing elements over, but surely all I want is a pointer. Passing a pointer and operating on the memory it points to should be extremely efficient; passing loads of memory between Java and C would kill any speed improvements made possibly by being able to operate on the memory from C.

到目前为止,我想出了这一点:

So far I've come up with this:

void Java_com_poldie_myTouch2_myclass_mymethod( JNIEnv* env, jobject thiz, jintArray arr, int n )
{
    jint *c_array;

    c_array = (*env)->GetIntArrayElements(env, arr, NULL);

    (*env)->ReleaseIntArrayElements(env, arr, c_array, 0);


 return;


}

我可以称之为是这样的:

Which I can call like this:

public native void  mymethod(IntBuffer intbuf, int n);

mymethod(mColourBuffer , n);

其中至少运行并似乎做了电话,但任何对c_array操作的企图(无论是作为一个复引用指针或数组),使程序立即退出。

Which at least runs and seems to do the call, but any attempt to operate on c_array (either as a dereferenced pointer or an array) causes the program to exit immediately.

我是在正确的轨道吗?我可以在返回一个指针传递给后面mColourBuffer内存我的C code和对它进行操作,以我的Java code?

Am I on the right track here? Can I pass a pointer to the memory behind mColourBuffer to my C code and operate on it upon return to my Java code?

推荐答案

在这里回答我的问题,看来答案是把你的顶点(你正在操作使用Java每一帧)的阵列,并将其写入到一个直接缓冲区。

Answering my own question here, it appears the answer is to take your array of vertices (which you're manipulating each frame using Java) and write them into a Direct Buffer.

在上面我举的例子,我想以某种方式填充直接缓冲区mColourBuffer每一帧无论是在本地阵列'颜色'。原来你想要的东西,如:

In my example above, I'd want to somehow populate the Direct Buffer mColourBuffer each frame with whatever is in the local array 'colours'. Turns out you'd want something like:

JNIfastPutf(mfColourBuffer, colours, nCount); 

其中NCOUNT是要复制的字节数。在(本机,C)函数JNIfastPutf看起来是这样的:

where nCount is the number of bytes to copy. The (Native, C) function JNIfastPutf looks like this:

void Java_com_a_b_c_JNIfastPutf(JNIEnv* env, jobject thiz, jobject jo, jfloatArray jfa, int n)
{
    float* pDst = (float*) (*env)->GetDirectBufferAddress(env, jo);
    float* pSrc = (float*) (*env)->GetPrimitiveArrayCritical(env, jfa, 0);              
    memcpy( pDst, pSrc, n  );

   (*env)->ReleasePrimitiveArrayCritical(env, jfa, pSrc, 0);
}

我发现下面的链接非常有帮助的(我的功能是一个稍微修改C版本的C ++的例子):

I found the following link very helpful (my function is a slightly modified C version of their C++ example):

http://www.badlogicgames.com/wiki/index.php/Direct_Bulk_FloatBuffer.put_is_slow

他们解释说,你真的需要这种方法,如果你正在使用的花车;使用int恐怕是要快得多,虽然这是我起步的地方,在任何情况下,本民族code显示了在20区 - 在'快'廉政版本> 100%的速度递增,所以就出现有什么理由不这样做!

They explain that you really need this sort of approach if you're working with floats; using ints is supposedly much faster, although that's where I started, and in any event, their native code shows in the region of a 20 -> 100% speed increase over the 'fast' int version, so there would appear to be little reason not to do it!

这篇关于在Android游戏传递java.nio.IntBuffer中,以C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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