tags:

views:

237

answers:

2

I'm trying to convert to Java some code that uses SDL. I'm using the sdljava bindings.

sdljava uses SWIG as the bridge between the C datatypes and Java. To get the equivalent of SDL_GetKeyState(), sdljava provides the method SWIG_SDLEvent.SDL_GetKeyState(), which returns something called a SWIGTYPE_p_unsigned_char.

Of course, Java has no unsigned char type, and I don't know what the compiler thinks this SWIG type actually represents to Java. Normal use of SDL_GetKeyState() in C/C++ would be something like:

Uint8 *ks = SDL_GetKeyState(NULL);
if ( ks[SDLK_UP] ) { /* handle Up key */ }

... where the SDL keystate values like SDLK_UP index into an array.

However, the following Java code:

SWIGTYPE_p_unsigned_char ks = SWIG_SDLEvent.SDL_GetKeyState(null);
if ( ks[SDLKeyValues.SDLK_UP] != 0) { /* handle Up key */ }

results in the compiler error, "The type of the expression must be an array type, but it resolved to SWIGTYPE_p_unsigned_char."

What I want to know is, after calling SWIG_SDLEvent.SDL_GetKeyState(), how do you use what it returns to inspect the state of the individual keys?

+1  A: 

Yep, this is a special case of the same issue as your other question. At first I thought carrays.i or arrays_java.i could help you, but they can't; neither SWIG nor Java knows how big the array returned by SDL_GetKeyState() is, so they can't generate a Java array for you.

In this particular case, SWIG has something called FillMeInAsSizeCannotBeDeterminedAutomatically that can solve your problem. It's of limited use in general, but assuming the array pointed to by the return value of SDL_GetKeyState() is of constant size, it's good enough. Search the documentation and mailing lists for it, meditate on the array sections of the Java SWIG docs, and the way forward should become clear.

David Seiler
This sounds promising. I'd assume that the array is of constant size, since it represents the keys on a keyboard.
Buggieboy
Right, though the numkeys out param to GetKeyState() makes me a little wary.
David Seiler
A: 

I spoke with William Fulton, who is responsible for SWIG Java support, and he responded:

The existence of SWIGTYPE_p_unsigned_char indicates the wrappers have not been thought through very well as this "type wrapper" class is not very useful for access from Java.

Please read the Java chapter for solutions for arrays - http://www.swig.org/Doc1.3/Java.html or just add in a helper method which you can then call from Java:

%inline %{
 Uint8 getValueFromArray(Uint8* array, size_t i) {
  return array[i];
 }
%}
Buggieboy