Skip to content

Commit

Permalink
Fixed getState and setEpisodeStartTime in Java binding, buffers type …
Browse files Browse the repository at this point in the history
…changed to byte[]

Former-commit-id: 504afe9
  • Loading branch information
mwydmuch committed Oct 2, 2016
1 parent 4910480 commit 244bdbd
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 22 deletions.
20 changes: 11 additions & 9 deletions src/lib_java/ViZDoomGameJava.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

#include "ViZDoomGameJava.h"

#include <iostream>

JNI_EXPORT(void, DoomGameNative) {
constructJavaObject<DoomGameJava>(jEnv, jObj);
}
Expand Down Expand Up @@ -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, "<init>", "(ILjava/lang/StringI)V");
jmethodID jLabelConstructor = jEnv->GetMethodID(jLabelClass, "<init>", "(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, "<init>", "(I[I[I[I[I[I[Lvizdoom/Label)V");
jmethodID jStateConstructor = jEnv->GetMethodID(jStateClass, "<init>", "(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;
}
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/lib_java/ViZDoomGameJava.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
15 changes: 14 additions & 1 deletion src/lib_java/ViZDoomJava.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

#include <jni.h>

#include <cstdlib>
#include <cstring>
#include <functional>
#include <type_traits>

Expand Down Expand Up @@ -111,11 +113,22 @@ jstring castTojstring(JNIEnv *jEnv, std::string val){
return jEnv->NewStringUTF(val.c_str());
}

template<class T>
jbyteArray castTojbyteArray(JNIEnv *jEnv, std::vector<T>& 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<class T>
jintArray castTojintArray(JNIEnv *jEnv, std::vector<T>& 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;
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib_java/java_classes/GameState.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib_java/java_classes/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 244bdbd

Please sign in to comment.