27
27
public final class ImFontAtlas implements ImGuiDestroyableStruct {
28
28
final long ptr ;
29
29
30
+ private ByteBuffer alpha8pixels = null ;
31
+ private ByteBuffer rgba32pixels = null ;
32
+
30
33
/**
31
34
* This class will create a native structure.
32
35
* Call {@link #destroy()} method to manually free used memory.
@@ -52,11 +55,17 @@ public void destroy() {
52
55
jfieldID imFontAtlasPtrID;
53
56
54
57
#define IM_FONT_ATLAS ((ImFontAtlas*)env->GetLongField(object, imFontAtlasPtrID))
58
+
59
+ jmethodID jImFontAtlasCreateAlpha8PixelsMID;
60
+ jmethodID jImFontAtlasCreateRgba32PixelsMID;
55
61
*/
56
62
57
63
static native void nInit (); /*
58
64
jclass jImFontAtlasClass = env->FindClass("imgui/ImFontAtlas");
59
65
imFontAtlasPtrID = env->GetFieldID(jImFontAtlasClass, "ptr", "J");
66
+
67
+ jImFontAtlasCreateAlpha8PixelsMID = env->GetMethodID(jImFontAtlasClass, "createAlpha8Pixels", "(I)Ljava/nio/ByteBuffer;");
68
+ jImFontAtlasCreateRgba32PixelsMID = env->GetMethodID(jImFontAtlasClass, "createRgba32Pixels", "(I)Ljava/nio/ByteBuffer;");
60
69
*/
61
70
62
71
private native long nCreate (); /*
@@ -313,20 +322,30 @@ public ByteBuffer getTexDataAsAlpha8(final ImInt outWidth, final ImInt outHeight
313
322
* 1 byte-per-pixel
314
323
*/
315
324
public ByteBuffer getTexDataAsAlpha8 (final ImInt outWidth , final ImInt outHeight , final ImInt outBytesPerPixel ) {
316
- final byte [] tmpBuff = getTexDataAsAlpha8 (outWidth .getData (), outHeight .getData (), outBytesPerPixel .getData ());
317
- final ByteBuffer buffer = ByteBuffer .allocateDirect (tmpBuff .length ).order (ByteOrder .nativeOrder ());
318
- buffer .put (tmpBuff );
319
- buffer .flip ();
320
- return buffer ;
325
+ getTexDataAsAlpha8 (outWidth .getData (), outHeight .getData (), outBytesPerPixel .getData ());
326
+ return alpha8pixels ;
327
+ }
328
+
329
+ private ByteBuffer createAlpha8Pixels (final int size ) {
330
+ if (alpha8pixels == null || alpha8pixels .limit () != size ) {
331
+ alpha8pixels = ByteBuffer .allocateDirect (size ).order (ByteOrder .nativeOrder ());
332
+ } else {
333
+ alpha8pixels .clear ();
334
+ }
335
+
336
+ return alpha8pixels ;
321
337
}
322
338
323
- private native byte [] getTexDataAsAlpha8 (int [] outWidth , int [] outHeight , int [] outBytesPerPixel ); /*
339
+ private native void getTexDataAsAlpha8 (int [] outWidth , int [] outHeight , int [] outBytesPerPixel ); /*
324
340
unsigned char* pixels;
325
341
IM_FONT_ATLAS->GetTexDataAsAlpha8(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
342
+
326
343
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
327
- jbyteArray jBytes = env->NewByteArray(size);
328
- env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels);
329
- return jBytes;
344
+
345
+ jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateAlpha8PixelsMID, size);
346
+ char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
347
+
348
+ memcpy(buffer, pixels, size);
330
349
*/
331
350
332
351
/**
@@ -340,20 +359,30 @@ public ByteBuffer getTexDataAsRGBA32(final ImInt outWidth, final ImInt outHeight
340
359
* 4 bytes-per-pixel
341
360
*/
342
361
public ByteBuffer getTexDataAsRGBA32 (final ImInt outWidth , final ImInt outHeight , final ImInt outBytesPerPixel ) {
343
- final byte [] tmpBuff = nGetTexDataAsRGBA32 (outWidth .getData (), outHeight .getData (), outBytesPerPixel .getData ());
344
- final ByteBuffer buffer = ByteBuffer .allocateDirect (tmpBuff .length ).order (ByteOrder .nativeOrder ());
345
- buffer .put (tmpBuff );
346
- buffer .flip ();
347
- return buffer ;
362
+ nGetTexDataAsRGBA32 (outWidth .getData (), outHeight .getData (), outBytesPerPixel .getData ());
363
+ return rgba32pixels ;
364
+ }
365
+
366
+ private ByteBuffer createRgba32Pixels (final int size ) {
367
+ if (rgba32pixels == null || rgba32pixels .limit () != size ) {
368
+ rgba32pixels = ByteBuffer .allocateDirect (size ).order (ByteOrder .nativeOrder ());
369
+ } else {
370
+ rgba32pixels .clear ();
371
+ }
372
+
373
+ return rgba32pixels ;
348
374
}
349
375
350
- private native byte [] nGetTexDataAsRGBA32 (int [] outWidth , int [] outHeight , int [] outBytesPerPixel ); /*
376
+ private native void nGetTexDataAsRGBA32 (int [] outWidth , int [] outHeight , int [] outBytesPerPixel ); /*
351
377
unsigned char* pixels;
352
378
IM_FONT_ATLAS->GetTexDataAsRGBA32(&pixels, &outWidth[0], &outHeight[0], &outBytesPerPixel[0]);
379
+
353
380
int size = outWidth[0] * outHeight[0] * outBytesPerPixel[0];
354
- jbyteArray jBytes = env->NewByteArray(size);
355
- env->SetByteArrayRegion(jBytes, 0, size, (jbyte*)pixels);
356
- return jBytes;
381
+
382
+ jobject jBuffer = env->CallObjectMethod(object, jImFontAtlasCreateRgba32PixelsMID, size);
383
+ char* buffer = (char*)env->GetDirectBufferAddress(jBuffer);
384
+
385
+ memcpy(buffer, pixels, size);
357
386
*/
358
387
359
388
public native boolean isBuilt (); /*
0 commit comments