diff --git a/src/lib_java/ViZDoomGameJava.cpp b/src/lib_java/ViZDoomGameJava.cpp index 0c7a96a27..759923267 100644 --- a/src/lib_java/ViZDoomGameJava.cpp +++ b/src/lib_java/ViZDoomGameJava.cpp @@ -22,6 +22,8 @@ #include "ViZDoomGameJava.h" +#include + JNI_EXPORT(void, DoomGameNative) { constructJavaObject(jEnv, jObj); } @@ -50,27 +52,27 @@ JNI_EXPORT(jobject, getState){ if (jStateClass == 0) return NULL; jintArray jGameVariables = castTojintArray(jEnv, state->gameVariables); - jintArray jScreenBuffer = state->screenBuffer != nullptr ? castTojintArray(jEnv, *state->screenBuffer) : 0; - jintArray jDepthBuffer = state->depthBuffer != nullptr ? castTojintArray(jEnv, *state->depthBuffer) : 0; - jintArray jLabelsBuffer = state->labelsBuffer != nullptr ? castTojintArray(jEnv, *state->labelsBuffer) : 0; - jintArray jAutomapBuffer = state->automapBuffer != nullptr ? castTojintArray(jEnv, *state->automapBuffer) : 0; + jbyteArray jScreenBuffer = state->screenBuffer != nullptr ? castTojbyteArray(jEnv, *state->screenBuffer) : NULL; + jbyteArray jDepthBuffer = state->depthBuffer != nullptr ? castTojbyteArray(jEnv, *state->depthBuffer) : NULL; + jbyteArray jLabelsBuffer = state->labelsBuffer != nullptr ? castTojbyteArray(jEnv, *state->labelsBuffer) : NULL; + jbyteArray jAutomapBuffer = state->automapBuffer != nullptr ? castTojbyteArray(jEnv, *state->automapBuffer) : NULL; jclass jLabelClass = jEnv->FindClass("vizdoom/Label"); if (jLabelClass == 0) return NULL; jobjectArray jLabels = jEnv->NewObjectArray(state->labels.size(), jLabelClass, NULL); - jmethodID jLabelConstructor = jEnv->GetMethodID(jLabelClass, "", "(ILjava/lang/StringI)V"); + jmethodID jLabelConstructor = jEnv->GetMethodID(jLabelClass, "", "(ILjava/lang/String;B)V"); if (jLabelConstructor == 0) return NULL; for(size_t i = 0; i < state->labels.size(); ++i){ jobject jLabel = jEnv->NewObject(jLabelClass, jLabelConstructor, (jint)state->labels[i].objectId, - castTojstring(jEnv, state->labels[i].objectName), (jint)state->labels[i].value); + castTojstring(jEnv, state->labels[i].objectName), (jint)state->labels[i].value); jEnv->SetObjectArrayElement(jLabels, i, jLabel); } - jmethodID jStateConstructor = jEnv->GetMethodID(jStateClass, "", "(I[I[I[I[I[I[Lvizdoom/Label)V"); + jmethodID jStateConstructor = jEnv->GetMethodID(jStateClass, "", "(I[I[B[B[B[B[Lvizdoom/Label;)V"); if (jStateConstructor == 0) return NULL; jobject jState = jEnv->NewObject(jStateClass, jStateConstructor, (jint)state->number, - jGameVariables, jScreenBuffer, jDepthBuffer, jLabelsBuffer, jAutomapBuffer, jLabels); + jGameVariables, jScreenBuffer, jDepthBuffer, jLabelsBuffer, jAutomapBuffer, jLabels); return jState; } @@ -149,7 +151,7 @@ JNI_METHOD(void, setDoomConfigPath, setDoomConfigPath, jstring) JNI_METHOD(jint, getSeed, getSeed) JNI_METHOD(void, setSeed, setSeed, jint) JNI_METHOD(jint, getEpisodeStartTime, getEpisodeStartTime) -JNI_METHOD(void, setEpisodeStartTime, setEpisodeTimeout, jint) +JNI_METHOD(void, setEpisodeStartTime, setEpisodeStartTime, jint) JNI_METHOD(jint, getEpisodeTimeout, getEpisodeTimeout) JNI_METHOD(void, setEpisodeTimeout, setEpisodeTimeout, jint) JNI_METHOD(jint, getEpisodeTime, getEpisodeTime) diff --git a/src/lib_java/ViZDoomGameJava.h b/src/lib_java/ViZDoomGameJava.h index 11a44c0fd..2d79b3721 100644 --- a/src/lib_java/ViZDoomGameJava.h +++ b/src/lib_java/ViZDoomGameJava.h @@ -20,11 +20,11 @@ THE SOFTWARE. */ -#include "ViZDoomJava.h" - #ifndef __VIZDOOM_GAME_JAVA_H__ #define __VIZDOOM_GAME_JAVA_H__ +#include "ViZDoomJava.h" + namespace vizdoom { class DoomGameJava : public DoomGame { diff --git a/src/lib_java/ViZDoomJava.h b/src/lib_java/ViZDoomJava.h index 3b5e0fc65..10ba470e6 100644 --- a/src/lib_java/ViZDoomJava.h +++ b/src/lib_java/ViZDoomJava.h @@ -27,6 +27,8 @@ #include +#include +#include #include #include @@ -111,11 +113,22 @@ jstring castTojstring(JNIEnv *jEnv, std::string val){ return jEnv->NewStringUTF(val.c_str()); } +template +jbyteArray castTojbyteArray(JNIEnv *jEnv, std::vector& val) { + jbyteArray jVal = jEnv->NewByteArray(val.size()); + jbyte *jValArr = jEnv->GetByteArrayElements(jVal, NULL); + if(sizeof(jbyte) == sizeof(T)) std::memcpy(jValArr, val.data(), val.size()); + else for (int i = 0; i < val.size(); ++i) jValArr[i] = (jint)val[i]; + jEnv->ReleaseByteArrayElements(jVal, jValArr, NULL); + return jVal; +} + template jintArray castTojintArray(JNIEnv *jEnv, std::vector& val) { jintArray jVal = jEnv->NewIntArray(val.size()); jint *jValArr = jEnv->GetIntArrayElements(jVal, NULL); - for (int i=0; i < val.size(); ++i) jValArr[i] = (jint)val[i]; + if(sizeof(jint) == sizeof(T)) std::memcpy(jValArr, val.data(), val.size()); + else for (int i = 0; i < val.size(); ++i) jValArr[i] = (jint)val[i]; jEnv->ReleaseIntArrayElements(jVal, jValArr, NULL); return jVal; } diff --git a/src/lib_java/java_classes/GameState.java b/src/lib_java/java_classes/GameState.java index f8ce46509..0cbc3f403 100644 --- a/src/lib_java/java_classes/GameState.java +++ b/src/lib_java/java_classes/GameState.java @@ -8,19 +8,19 @@ public class GameState{ public int number; public int[] gameVariables; - public int[] screenBuffer; - public int[] depthBuffer; - public int[] labelsBuffer; - public int[] automapBuffer; + public byte[] screenBuffer; + public byte[] depthBuffer; + public byte[] labelsBuffer; + public byte[] automapBuffer; public Label[] labels; GameState(int number, int[] gameVariables, - int[] screenBuffer, - int[] depthBuffer, - int[] labelsBuffer, - int[] automapBuffer, + byte[] screenBuffer, + byte[] depthBuffer, + byte[] labelsBuffer, + byte[] automapBuffer, Label[] labels){ this.number = number; diff --git a/src/lib_java/java_classes/Label.java b/src/lib_java/java_classes/Label.java index e9f218e1e..a9ef93e48 100644 --- a/src/lib_java/java_classes/Label.java +++ b/src/lib_java/java_classes/Label.java @@ -7,9 +7,9 @@ public class Label{ public int objectId; public String objectName; - public int value; + public byte value; - Label(int objectId, String objectName, int value){ + Label(int objectId, String objectName, byte value){ this.objectId = objectId; this.objectName = objectName; this.value = value;