好的,我有这个游戏,我写了一个从dll读取缓冲区的JNI类。所以我将一个byteBuffer或intBuffer传递给DLL,并用像素(RGB数组)填充它。
我这样使用它:
GLDXLoader GLDXPlugin = new GLDXLoader(null, 0, 0);
if (GLDXLoader.OpenGLLoaded) {
GLDXPlugin.setMode(OperatingMode.OpenGL);
GLDXPlugin.GetBuffer(gameBuffer); //GetBuffer called..
}
但是当我打电话给它时,它会崩溃..但是,如果我这样做:
GLDXLoader GLDXPlugin = new GLDXLoader(null, 0, 0);
if (GLDXLoader.OpenGLLoaded) {
GLDXPlugin.setMode(OperatingMode.OpenGL);
GLDXLoader.GetOpenGLBuffer(gameBuffer); //GetOpenGLBuffer called..
}
它工作得很好..因此,第一次调用我唯一看错的是我的IntBuffer被传递给一个函数,该函数将它传递给本机函数,而中间人使它崩溃了我的JVM?
我的课程如下:
package JNI;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;
import smart.OperatingMode;
public class GLDXLoader {
private int ByteSize = 0;
private IntBuffer IBuffer = null;
private ByteBuffer BBuffer = null;
private OperatingMode Mode = null;
private BufferedImage Image = null;
public static boolean OpenGLLoaded = false;
public static boolean DirectXLoaded = false;
//ByteSize = ((ImageWidth * BitsPerPixel + 31) / 32) * 4 * ImageHeight;
public static native void GetOpenGLBuffer(IntBuffer Buffer);
private static native void GetDirectXBuffer(IntBuffer Buffer);
public GLDXLoader(ByteBuffer Buffer, int ImageWidth, int ImageHeight) {
if (Buffer != null) {
this.BBuffer = Buffer;
this.IBuffer = BBuffer.order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
}
}
public GLDXLoader(int ImageWidth, int ImageHeight) {
ByteSize = ImageWidth * ImageHeight * 4;
BBuffer = ByteBuffer.allocateDirect(ByteSize).order(ByteOrder.LITTLE_ENDIAN);
IBuffer = BBuffer.asIntBuffer();
}
private void readBuffer(IntBuffer Buffer, OperatingMode Mode) {
Buffer.rewind();
switch (Mode) {
case OpenGL:
GetOpenGLBuffer(Buffer);
case DirectX:
GetDirectXBuffer(Buffer);
default:
break;
}
}
public void setMode(OperatingMode Mode) {
this.Mode = Mode;
}
public void GetBuffer(IntBuffer Buffer) {
readBuffer(Buffer, Mode);
}
}
C ++代码:
void JNIData::FlipBytes(void*& Result) //Copies my Bitmap Bytes in reverse order.. Flips the Bitmap Upside down and stores it in "Result".
{
unsigned long Chunk = (Bpp > 24 ? width * 4 : width * 3 + width % 4);
unsigned char* Destination = static_cast<unsigned char*>(Result);
unsigned char* Source = &BufferPixels[0] + Chunk * (height - 1);
while(Source != &BufferPixels[0])
{
std::memcpy(Destination, Source, Chunk);
Destination += Chunk;
Source -= Chunk;
}
}
extern "C" JNIEXPORT void JNICALL Java_JNI_GLDXLoader_GetOpenGLBuffer(JNIEnv *Env, jclass Cls, jobject Buffer)
{
//jclass IntBufferClass = Env->FindClass("java/nio/IntBuffer");
//jmethodID Rewind = Env->GetMethodID(IntBufferClass, "rewind", "()Ljava/nio/Buffer;");
//Env->CallObjectMethod(Buffer, Rewind);
void* CBuff = Env->GetDirectBufferAddress(Buffer);
JNIBuffer.FlipBytes(CBuff);
}
有人可以帮助我解释为什么中间人功能(GetBuffer)会让它崩溃吗?