Reputation: 133619
I'm working on a game that has simple pixel-art sprites. Since I'm planning to do it scalable (I mean the whole game graphics scales) I would like to implement a good scaling algorithm suited for pixel-art, searching around internet I found this HQX so that I can scale all the sprites (not in real-time) and then use these new versions.
I found the implementation of this HQX algorithm in C code, everything reduces to a simple function that takes the uint32_t
array of the source image and fills a new uint32_t
array already allocated with the scaled version of the sprite.
The signature is just
HQX_API void HQX_CALLCONV hq2x_32( uint32_t * sp, uint32_t * dp, int Xres, int Yres )
where the two symbols are declared as
#if defined( __GNUC__ )
#ifdef __MINGW32__
#define HQX_CALLCONV __stdcall
#else
#define HQX_CALLCONV
#endif
#else
#define HQX_CALLCONV
#endif
#if defined(_WIN32)
#ifdef DLL_EXPORT
#define HQX_API __declspec(dllexport)
#else
#define HQX_API __declspec(dllimport)
#endif
#else
#define HQX_API
#endif
So that the target can be build as standalone or library.
What I would like to do is to invoke this method from Java thorugh JNI. Which tasks should I accomplish to do it? In addition I would like to know if the performance would be an issue or not, I don't think so since, also if JNI call is expensive, just a call is done for every sprite (but it possibly requires a lot of memory to be copied back and forth from JVM and native code).
I know both languages (Java and C), so I just need to know the steps involved in doing this task, assuming it's enough simple, otherwise I'll just take the JNI reference and study it..
Thanks in advance
Upvotes: 1
Views: 478
Reputation: 86469
If you are scaling the sprites infrequently, you may find it easier to work with JNA (Java Native Access) than JNI. It's much friendlier, but slower. With JNA, you may not need to write any additional native code.
If you do use JNI, typically you'll create "native methods" in Java (empty methods with a "native" qualifier"), use javah to generate a C header file, and write a corresponding C source file to implement the "native method." At runtime, you'll need to load your native library with System.loadLibrary(). (If you're on a Mac, the dynamic library extension must be .jnilib.)
JNI calls can be measured in nanoseconds. Large arrays might be copied, increasing that time. Earlier this year I measured it at about 80 microseconds for 100,000 element array of ints. I think that can be avoided with the java.io type DirectByteBuffer.
Upvotes: 2