extend getSummary() to include array of current scores; add util to set an object

field and remove dead code
This commit is contained in:
eehouse 2010-05-05 04:11:08 +00:00
parent b6a89fd8c1
commit 671461f4d4
3 changed files with 37 additions and 28 deletions

View file

@ -158,7 +158,7 @@ getObject( JNIEnv* env, jobject obj, const char* name, const char* sig,
jclass cls = (*env)->GetObjectClass( env, obj );
XP_ASSERT( !!cls );
jfieldID fid = (*env)->GetFieldID( env, cls, name, sig );
XP_ASSERT( !!fid ); /* failed */
XP_ASSERT( !!fid );
*ret = (*env)->GetObjectField( env, obj, fid );
XP_ASSERT( !!*ret );
@ -166,6 +166,19 @@ getObject( JNIEnv* env, jobject obj, const char* name, const char* sig,
return true;
}
void
setObject( JNIEnv* env, jobject obj, const char* name, const char* sig,
jobject val )
{
jclass cls = (*env)->GetObjectClass( env, obj );
XP_ASSERT( !!cls );
jfieldID fid = (*env)->GetFieldID( env, cls, name, sig );
XP_ASSERT( !!fid );
(*env)->SetObjectField( env, obj, fid, val );
(*env)->DeleteLocalRef( env, cls );
}
/* return false on failure, e.g. exception raised */
bool
getBool( JNIEnv* env, jobject obj, const char* name )
@ -286,27 +299,6 @@ getMethodID( JNIEnv* env, jobject obj, const char* proc, const char* sig )
return mid;
}
jobjectArray
makeBitmapsArray( JNIEnv* env, const XP_Bitmaps* bitmaps )
{
jobjectArray result = NULL;
if ( !!bitmaps && bitmaps->nBitmaps > 0 ) {
jclass clazz = (*env)->FindClass( env,
"android/graphics/drawable/BitmapDrawable" );
XP_ASSERT( !!clazz );
result = (*env)->NewObjectArray( env, bitmaps->nBitmaps, clazz, NULL );
(*env)->DeleteLocalRef( env, clazz );
int ii;
for ( ii = 0; ii < bitmaps->nBitmaps; ++ii ) {
(*env)->SetObjectArrayElement( env, result, ii, bitmaps->bmps[ii] );
}
}
return result;
}
void
setJAddrRec( JNIEnv* env, jobject jaddr, const CommsAddrRec* addr )
{

View file

@ -40,6 +40,8 @@ bool setBool( JNIEnv* env, jobject obj, const char* name, bool value );
bool setString( JNIEnv* env, jobject obj, const char* name, const XP_UCHAR* value );
void getString( JNIEnv* env, jobject jlp, const char* name, XP_UCHAR* buf,
int bufLen );
void setObject( JNIEnv* env, jobject obj, const char* name, const char* sig,
jobject val );
bool getObject( JNIEnv* env, jobject obj, const char* name, const char* sig,
jobject* ret );
@ -55,8 +57,6 @@ void setBoolArray( JNIEnv* env, jbooleanArray jarr, int count,
jobjectArray makeStringArray( JNIEnv *env, int size, const XP_UCHAR** vals );
jstring streamToJString( MPFORMAL JNIEnv* env, XWStreamCtxt* stream );
jobjectArray makeBitmapsArray( JNIEnv* env, const XP_Bitmaps* bitmaps );
/* Note: jmethodID can be cached. Should not look up more than once. */
jmethodID getMethodID( JNIEnv* env, jobject obj, const char* proc,
const char* sig );

View file

@ -927,14 +927,14 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1receiveMessage
JNIEXPORT void JNICALL
Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize
( JNIEnv* env, jclass C, jint gamePtr, jobject jsummary )
( JNIEnv* env, jclass C, jint gamePtr, jint nPlayers, jobject jsummary )
{
LOG_FUNC();
XWJNI_START();
XP_S16 nMoves = model_getNMoves( state->game.model );
setInt( env, jsummary, "nMoves", nMoves );
setBool( env, jsummary, "gameOver",
server_getGameIsOver( state->game.server ) );
XP_Bool gameOver = server_getGameIsOver( state->game.server );
setBool( env, jsummary, "gameOver", gameOver );
if ( !!state->game.comms ) {
CommsAddrRec addr;
@ -949,6 +949,23 @@ Java_org_eehouse_android_xw4_jni_XwJNI_game_1summarize
}
}
jint jvals[nPlayers];
int ii;
if ( gameOver ) {
ScoresArray scores;
model_figureFinalScores( state->game.model, &scores, NULL );
for ( ii = 0; ii < nPlayers; ++ii ) {
jvals[ii] = scores.arr[ii];
}
} else {
for ( ii = 0; ii < nPlayers; ++ii ) {
jvals[ii] = model_getPlayerScore( state->game.model, ii );
}
}
jintArray jarr = makeIntArray( env, nPlayers, jvals );
setObject( env, jsummary, "scores", "[I", jarr );
(*env)->DeleteLocalRef( env, jarr );
XWJNI_END();
LOG_RETURN_VOID();
}