Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4008274
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:44:04+00:00 2026-05-20T08:44:04+00:00

I’d like to be able to add a little native C code in my

  • 0

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); 

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);

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.

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T08:44:04+00:00Added an answer on May 20, 2026 at 8:44 am

    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.

    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); 
    

    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);
    }
    

    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

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some data like this: 1 2 3 4 5 9 2 6
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have text I am displaying in SIlverlight that is coming from a CMS
I used javascript for loading a picture on my website depending on which small
I have a JSP page retrieving data and when single or double quotes are
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.