It's free to join Gamasutra!|Have a question? Want to know who runs this site? Here you go.|Targeting the game development market with your product or service? Get info on advertising here.||For altering your contact information or changing email subscription preferences.
Registered members can log in here.Back to the home page.

Search articles, jobs, buyers guide, and more.

By Bernd Kreimeier
Gamasutra
June 11, 1999

This article originally appeared in the
July, 1999 issue of:


Letters to the Editor:
Write a letter
View all letters


Features

Listing 1. Native code making use of Java.

Content
Introduction

Talking to the Natives

Code Listing 1

Double Indirection

The Invocation API

Two Architectures

Code Listing 4

Encapsulated Native: A Magic Bullet?

For Further Info
Java for Games

Java Documentation

Tools & Products

Open Source

Sidebar
Java wraps native code in Prax War

id Adandons jave for Quake 3: Arena

Embedded Java in Vampire: The Masquerade

// Java class, server-side Game Logic scripting.

package somegame;

class ScriptEngine {

// This uses native error() callback.

public static void init() {..}

...

public static native void error( int code );

}

#include <jni.h>

// Native error callback provided to Java.

extern void SV_ErrorScriptEngine( jint i );

JNINativeMethod svSE = {"Java_somegame_ScriptEngine_error","(I)V", SV_ErrorScriptEngine };

// Native function to set up scripting module.

void SV_InitScriptEngine( JNIEnv* env ) {

jclass clazz = NULL;

jmethodID method = NULL;

jint err;

// Lookup class, loads the class if not yet done.

clazz = (*env)->FindClass( env, "somegame.ScriptEngine" );

if ( clazz != NULL ) {

// Register native method, returns zero on success.

err = RegisterNatives( env, clazz, &svSE, 1 );

// Lookup Java methodID.

method = (*env)->GetStaticMethodID(env, clazz, "init", "()V");

}

// Handle errors.

...

// Call static method.

(*env)->CallStaticVoidMethod(env,clazz,method);

}

It is tempting to use static (class) methods, as you do not have to handle an object in addition to the class. In many cases this is absolutely sufficient — servers will likely not run two script engines in parallel. In many other cases though, this leads to bad object-oriented design on the Java side.


Double Indirection


join | contact us | advertise | write | my profile
news | features | companies | jobs | resumes | education | product guide | projects | store



Copyright © 2003 CMP Media LLC

privacy policy
| terms of service