[Original] 0 Basic learning SDL developed on Android using SDL2.0 display BMP diagram

Source: Internet
Author: User

Refer to my previous article on how to transplant SDL2.0 to Android: [Original] 0 Basic learning SDL Development porting SDL2.0 to Android

In an article we mainly use SDL2.0 to load a BMP graph to render the display.

Bo Master Development Environment: Ubuntu 14.04 64-bit, Eclipse + CDT + adt+ndk

The blogger used the NDK to compile the libsdl2.so, and then used the shared library to invoke the functions in libSDL2, and found SDL\SRC\CORE\ANDROID\SDL_ANDROID.C This JNI function does not write enough to do the other transplant, and the org.libsdl.app.SDLActivity coupling is too close. If you want to use shared libraries to invoke functions in SDL, it is best to write the JNI registration and call function implementations yourself.

First, the introduction of SDL source code

Or to import the SDL source Android-project project into eclipse, into the Jni folder, create a new SDL file, the SDL2-2.0.3\SRC, Sdl2-2.0.3\include, sdl2-2.0.3\ The Android.mk file is copied to the android-project\jni\sdl\.

Create a new android.mk in Android-project with the following content:

Include $ (call All-subdir-makefiles)

Create a new application.mk in Android-project with the following content:

App_abi: = armeabiapp_platform:= android-9

Second, the new function module

Create a new folder under Jni src, create a new sdl_logger.h to use log from Android to print logs with the following:

/** log.h * * Created On:aug 8, * Author:clarck*/#ifndef Log_h_#defineLog_h_#include<android/log.h>#defineAPPNAME "Sdl_lesson"#defineLOGV (...) __android_log_print (Android_log_verbose, APPNAME, __va_args__)#defineLOGD (...) __android_log_print (Android_log_debug, APPNAME, __va_args__)#defineLogi (...) __android_log_print (Android_log_info, APPNAME, __va_args__)#defineLOGW (...) __android_log_print (Android_log_warn, APPNAME, __va_args__)#defineLOGE (...) __android_log_print (Android_log_error, APPNAME, __va_args__)#endif/* Log_h_ */

New SDL_LESSON.C, used to create the SDL window, loading BMP, rendering the image function, specific content:

/** SDL_LESSON.C * * Created On:aug 8, * Author:clarck*/#ifdef __android__#include<jni.h>#include"SDL.h"#include"sdl_log.h"#include"Sdl_main.h"//The attributes of the screenConstintScreen_width =640; ConstintScreen_height =480; ConstintSCREEN_BPP = +; struct Sdl_window*window =null;struct Sdl_renderer*render =null;struct Sdl_surface*bmp =null;struct sdl_texture*texture =NULL;intMainintargcChar*argv[]) {    Char*filepath = argv[1]; Logi ("natvie_sdl%s", filepath); if(Sdl_init (Sdl_init_video | Sdl_init_audio | Sdl_init_timer) = =-1) {LOGE ("Sdl_init failed%s", Sdl_geterror ()); } logi ("Sdl_createwindow"); Window= Sdl_createwindow ("Hello world!", -, -,640,480, Sdl_window_shown); if(window = = NULL) {LOGE ("Sdl_createwindow failed%s", Sdl_geterror ()); } logi ("Sdl_createrenderer"); Render= Sdl_createrenderer (window,-1, sdl_renderer_accelerated|Sdl_renderer_presentvsync); if(render = = NULL) {LOGE ("Sdl_createrenderer failed%s", Sdl_geterror ()); } BMP=sdl_loadbmp (filepath); if(bmp = = NULL) {LOGE ("Sdl_loadbmp failed:%s", Sdl_geterror ()); } Texture=Sdl_createtexturefromsurface (render, BMP);    Sdl_freesurface (BMP);    Sdl_renderclear (render);    Sdl_rendercopy (render, texture, NULL, NULL);    Sdl_renderpresent (render); Sdl_delay ( -);    Sdl_destroytexture (texture);    Sdl_destroyrenderer (render);    Sdl_destroywindow (window); //Quit SDLSdl_quit (); Return0;}#endif/* __android__ */

Write the android.mk under SRC to compile the sdl_lesson.c with the following content:

Local_path: = $ (call my-dir) include $ (clear_vars) Local_module:= Mainsdl_path:=. /sdllocal_c_includes:= $ (Local_path)/$ (sdl_path)/include# Add Your application source files here ... Local_src_files:= $ (sdl_path)/src/main/android/sdl_android_main.c     sdl_lesson.clocal_shared_ LIBRARIES:= sdl2local_ldlibs:=-lglesv1_cm-lglesv2-lloginclude $ (build_shared_library)

Third, modify the sdlactivity, sdl\src\main\sdl_android_main.c to accept incoming parameters:

To modify the Sdlmain class in sdlactivity, modify the following:

/* *    Simple Nativeinit () runnable*/class Sdlmain implements runnable {    @Override public    void Run ( ) {        //  Runs sdl_main ()        String sdcard = environment.getexternalstoragedirectory ( ). GetAbsolutePath ();         " hello.bmp " );         // log.v ("SDL", "SDL thread terminated");     }}

Modify the SDL\SRC\MAIN\SDL_ANDROID_MAIN.C and modify the following:

/*sdl_android_main.c, placed in the public domain by Sam Lantinga 3/13/14*/#include".. /.. /sdl_internal.h"#ifdef __android__/*Include The SDL main definition header*/#include"Sdl_main.h"/******************************************************************************* Functions called by JNI *******************************************************************************/#include<jni.h>/*called before Sdl_main () to initialize JNI bindings in SDL library*/extern void Sdl_android_init (jnienv*Env, Jclass CLS);Char* JSTRINGTOSTR (jnienv*Env, jstring jstr) {    Char* PStr =NULL; Jclass Jstrobj= (*Env)->findclass (Env,"java/lang/string"); Jstring encode= (*Env)->newstringutf (Env,"Utf-8"); Jmethodid Methodid= (*Env)->getmethodid (Env, Jstrobj,"getBytes",            "(ljava/lang/string;) [B"); Jbytearray ByteArray= (Jbytearray) (*Env)->callobjectmethod (Env, Jstr, Methodid, encode); Jsize StrLen= (*Env)->getarraylength (Env, ByteArray); Jbyte*jbuf = (*Env)->getbytearrayelements (Env, ByteArray, Jni_false); if(Jbuf >0) {pStr= (Char*) malloc (StrLen +1); if(!pStr)        {return NULL;        } memcpy (PStr, Jbuf, StrLen); Pstr[strlen]=0; }    (*Env)->releasebytearrayelements (Env, ByteArray, Jbuf,0); return pStr;}/*Start up the SDL app*/void Java_org_libsdl_app_sdlactivity_nativeinit (jnienv*Env, Jclass cls, Jobject obj) {    /*This interface could expand with ABI negotiation, calbacks, etc.*/Sdl_android_init (Env, CLS);    Sdl_setmainready (); Char*filepath = Jstringtostr (Env, obj); /*Run the application code!*/    intstatus; Char*argv[2]; argv[0] = Sdl_strdup ("Sdl_app"); argv[1] =FilePath; Status= Sdl_main (1, argv); /*Don't issue an exit or the whole application would terminate instead of just the SDL thread*/    /*exit (status);*/}#endif/* __android__ *//*vi:set ts=4 sw=4 expandtab:*/

Finally, the previous one runs:

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: info-contact@alibabacloud.com 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.