Describe the use of JNI By porting the Soldado game on the Android platform (embed the Soldado game into the fcgame simulator, eliminating the need to load the Rom from the SD card)

Source: Internet
Author: User

Starting point:Traditional game simulators (FC, Neo, FBA, GBA, Mame, n64, NDS, and PSP) are all moved to Android phones, the game Rom is then saved to the SD card and run through the simulator. However, considering that mobile games must follow the fast forward and fast forward principles, I have implemented the following micro-innovations on this basis:

1. directly put the game Rom in the android APK, click the application to start the game, saving the process of searching for the game through the file browser.

2. Use interpolation algorithms to enlarge game images to support Android tablet resolutions

3. Make full use of the unique features of the mobile phone gravity sensor (compared with the other two screens, PC monitors and TVs), using the mobile phone sensor as the direction key



1. Select an open-source Linux-based fcgame Simulator

2. Implement the Java JNI interface for accessing the C layer

 Public class game {</P> <p> Public static native void loadgame (byte [] data) throws illegalstateexception; <br/> Public static native void destroygame () throws illegalstateexception; <br/> Public static native void sendevent (INT keystate, int event) throws illegalstateexception; <br/> Public static native void setvideosurface (surface) throws illegalstateexception; <br/> Public static native void setskipframe (INT skip); <br/>}

3. Use ralativielayout in the acitemedilayout layout of Android to move the five keys and buttons to the two ends of the game interface.



Common JNI practices can be obtained through JNI spec. Pay attention to the following points:

1. If the static native method is defined, the class of the object cannot be obtained using getobjectclass in C/C ++ code.

2. If the Java and C/C ++ layers need to share the memory, you can use the getdirectbufferadress () API

3. jchar occupies two bytes (typedef unsigned short jchar ;)

4. Global Reference remember to release (newglobalref, deleteglobalref)

5. Exceptions to be captured in Java can be thrown in JNI (that is, in the C/C ++ layer), such:

Int jnithrowexception (jnienv * ENV, const char * classname, const char * MSG) {<br/> jclass exceptionclass = env-> findclass (classname ); <br/> If (exceptionclass = NULL) {<br/> _ android_log_print (android_log_error, <br/> tag, <br/> "unable to find exception class % s", <br/> classname); <br/> return-1; <br/>}< br/> If (env-> thrownew (exceptionclass, MSG )! = Jni_ OK) {<br/> _ android_log_print (android_log_error, <br/> tag, <br/> "failed throwing '% s' % S '", <br/> classname, MSG); <br/>}< br/> return 0; <br/>}

6. It is best to register the native method at one time.


Running result:



Download: Soldado


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.