Latest starter Eclipse+cdt+android-ndk Write pure C + + Android app (with OpenGL Es)

Source: Internet
Author: User

First download Eclipse and CDT, my version number is: Version:indigo Service Release 2 and version:1.0.0.201202111925, then download Windows NDK, I'm using android-ndk-r9d.

What Cygwin such things, too disgusting, download slow, big killer! Complex, today gives a most cool compilation tutorial.

The previous CDT plugin how to pass here, online tutorials a lot of. Direct configuration ...

Start Eclipse, and then click Windows-prefrences-c/c++-build-envionment to add the following path


Then create an Android project, remove all the code, delete all resources, androidmanifest.xml content as follows

<?xml version= "1.0" encoding= "Utf-8"? ><manifest xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:tools=" Http://schemas.android.com/tools "package=" com.example.native_activity "Android:versioncode = "1" android:versionname= "1.0" > <uses-sdk android:minsdkversion= "9" tools:ignore= "Usesminsdkatt" Ributes "/> <application android:hascode=" false "android:label=" pure CPP Application "tools:ignore=" Allowb Ackup,missingapplicationicon "> <activity android:name=" android.app.NativeActivity "and            roid:configchanges= "Orientation|keyboardhidden" > <!--tell nativeactivity the name of or-so-and <meta-data android:name= "Android.app.lib_name" android:value= "native-activity"/&G            T <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <categor Y android:name= "android.iNtent.category.LAUNCHER "/> </intent-filter> </activity> </application></mani  Fest>

Then create a JNI directory with three files, followed by android.mk

Local_path: = $ (call My-dir) include $ (clear_vars) local_module    : = native-activitylocal_src_files: = Main.cpplocal_ Ldlibs    : =-llog-landroid-legl-lglesv1_cmlocal_static_libraries: = Android_native_app_glueinclude $ (BUILD_ Shared_library) $ (call Import-module,android/native_app_glue)

Application.mk

App_platform: = android-14

Main.cpp

#include <jni.h> #include <errno.h> #include <EGL/egl.h> #include <GLES/gl.h> #include < string.h> #include <android/sensor.h> #include <android/log.h> #include <android_native_app_glue.h > #define LOGI (...) (void) __android_log_print (android_log_info, "native-activity", __va_args__)) #define LOGW (...) (void) __android_log_print (Android_log_warn, "native-activity", __va_args__))/** * Our saved state data. */struct saved_state {float angle;int32_t x;int32_t y;};/ * * Shared state for our app. */struct engine {struct android_app* app; asensormanager* sensormanager;const asensor* accelerometersensor; asensoreventqueue* Sensoreventqueue;int animating; Egldisplay display; Eglsurface surface; Eglcontext context;int32_t width;int32_t height;struct saved_state state;};/ * * Initialize an EGL context for the current display. */static int Engine_init_display (struct engine* engine) {//Initialize OpenGL ES and egl/* * Here specify the attributes O f the desired CONFIguration. * Below, we select an EGLConfig with at least 8 bits per color * component compatible with on-screen Windows */const Eglin T attribs[] = {egl_surface_type, egl_window_bit, egl_blue_size,8, Egl_green_size, 8, egl_red_size, 8, EGL_NONE}; Eglint W, h, dummy, format; Eglint Numconfigs; EGLConfig config; Eglsurface surface; Eglcontext context; Egldisplay display = Eglgetdisplay (egl_default_display) eglinitialize (display, 0, 0);/* Here, the application chooses The configuration it desires. In this * sample, we had a very simplified selection process, where we pick * The first EGLConfig that matches we criter IA */eglchooseconfig (Display, Attribs, &config, 1, &numconfigs);/* egl_native_visual_id is an attribute of the EGL Config is * guaranteed to being accepted by Anativewindow_setbuffersgeometry (). * As soon as we picked a EGLConfig, we can safely reconfigure the * Anativewindow buffers to match, using Egl_native_visua l_id. */eglgetconfigattrib (display, config, Egl_native_visual_id, &format); Anativewindow_setbuffersgeometry (Engine->app->window, 0, 0, format); surface = Eglcreatewindowsurface (display, Config, engine->app->window,null); context = Eglcreatecontext (display, config, null, NULL); if (Eglmakecurrent ( Display, surface, surface, context) = = Egl_false) {LOGW ("Unable to eglmakecurrent"); return-1;} Eglquerysurface (display, surface, egl_width, &w); Eglquerysurface (display, surface, egl_height, &h); engine- >display = Display;engine->context = Context;engine->surface = Surface;engine->width = W;engine->height = H;engine->state.angle = 0;//Initialize GL state.glhint (Gl_perspective_correction_hint, gl_fastest); GlEnable (GL_ Cull_face); Glshademodel (Gl_smooth); gldisable (gl_depth_test); return 0;} /** * Just The current frame in the display. */static void Engine_draw_frame (struct engine* engine) {if (Engine->display = = NULL) {//No Display.return;} Just fill With a color.glclearcolor ((float) engine->state.x)/Engine->width, Engine->state.angle, ((float) engine->state.y)/engine->height, 1); Glclear (GL_COLOR_ Buffer_bit); Eglswapbuffers (Engine->display, engine->surface);} /** * Tear down the EGL context currently associated with the display. */static void Engine_term_display (struct engine* engine) {if (Engine->display! = egl_no_display) {eglmakecurrent ( Engine->display, Egl_no_surface, Egl_no_surface,egl_no_context); if (engine->context! = EGL_NO_CONTEXT) { Egldestroycontext (Engine->display, engine->context);} if (engine->surface! = egl_no_surface) {egldestroysurface (Engine->display, engine->surface);} Eglterminate (Engine->display);} engine->animating = 0;engine->display = Egl_no_display;engine->context = Egl_no_context;engine->surface = Egl_no_surface;} /** * Process the next input event. */static int32_t engine_handle_input (struct android_app* app,ainputevent* event) {struct engine* engine = (struct engine* ) App->userdata;if (AinputevenT_gettype (Event) = = ainput_event_type_motion) {engine->animating = 1;engine->state.x = Amotionevent_getx (event, 0); engine->state.y = Amotionevent_gety (event, 0); return 1;} return 0;} /** * Process the next main command. */static void Engine_handle_cmd (struct android_app* app, int32_t cmd) {struct engine* engine = (struct engine*) app->us  Erdata;switch (cmd) {case app_cmd_save_state://The system have asked us to SAVE our current state. Do so.engine->app->savedstate = malloc ((size_t) sizeof (struct saved_state)); * (struct saved_state*) engine-> app->savedstate) = Engine->state;engine->app->savedstatesize = sizeof (struct saved_state); Break;case app _cmd_init_window://the WINDOW is being shown, get it ready.if (engine->app->window! = NULL) {engine_init_display (en Gine); Engine_draw_frame (engine);} Break;case app_cmd_term_window://The WINDOW is being hidden or closed, clean it up.engine_term_display (engine); Break;cas E app_cmd_gained_focus://when we APP gains focus, we start monitoring the Accelerometer.if (engine->accelerometersensor! = NULL) {Asensoreventqueue_ Enablesensor (engine->sensoreventqueue,engine->accelerometersensor);//We ' d like to get the events per second (in US ). Asensoreventqueue_seteventrate (Engine->sensoreventqueue,engine->accelerometersensor, (1000L/60) * 1000);} Break;case app_cmd_lost_focus://When we have APP loses FOCUS, we stop monitoring the accelerometer.//this was to avoid consum ing battery while not being used.if (engine->accelerometersensor! = NULL) {asensoreventqueue_disablesensor (engine- >sensoreventqueue,engine->accelerometersensor);} Also Stop animating.engine->animating = 0;engine_draw_frame (engine); break;}}  /** * This is the main entry point of a native application, which is using * android_native_app_glue. It runs in its own thread, with its own * Event loop for receiving input events and doing other things. */void android_main (struct android_app* state) {struct engine engine = {0};Make sure glue isn ' t stripped.app_dummy (); state->userdata = &engine;state->onappcmd = Engine_handle_cmd; State->oninputevent = Engine_handle_input;engine.app = state;//Prepare to monitor Accelerometerengine.sensormanager = Asensormanager_getinstance (); engine.accelerometersensor = ASensorManager_ Getdefaultsensor (Engine.sensormanager, asensor_type_accelerometer); engine.sensoreventqueue = ASensorManager_ Createeventqueue (Engine.sensormanager, state->looper, looper_id_user, NULL, NULL); if (state->savedstate! = NULL ) {//We is starting with a previous saved, restore from it.engine.state = * (struct saved_state*) state->savedst ate;} Loop waiting for Stuff to Do.while (true) {//Read all pending Events.int ident;int events;struct android_poll_source* source;//if not animating, we'll block forever waiting for events.//If animating, we loop until all events is read, T Hen continue//to draw the next frame of animation.while (ident = Alooper_pollall (engine.anImating? 0:-1, Null,&events, (void**) &source)) >= 0) {//Process this event.if (Source = NULL) {source->process (s Tate, source);} If a sensor has data, process it now.if (ident = = Looper_id_user) {if (engine.accelerometersensor! = NULL) {Asensoreven T Event;while (Asensoreventqueue_getevents (engine.sensoreventqueue,&event, 1) > 0) {logi ("Accelerometer:x=%f y =%f z=%f ", event.acceleration.x, EVENT.ACCELERATION.Y, event.acceleration.z);}}} Check If we are exiting.if (state->destroyrequested! = 0) {engine_term_display (&engine); return;}} if (engine.animating) {//Done with events; draw Next animation Frame.engine.state.angle + = 01f;if (Engine.state.angle &G T 1) {engine.state.angle = 0;} Engine_draw_frame (&engine);}}}

Create a finish, and then create another project. The path must be the JNI directory of the project you just created, and the name is random, focus on the diagram


Okay, click Finish, then open main.cpp found n multiple errors, directly under Set environment variables, right-click Project, properties (is the C + + project just created)


Next look at the picture and add all the libraries.



Finally add a symbol, in fact, is to define a macro, tell the compiler I now platform is Android,add


The last point OK, all functions can be recognized normally, the prompt function can also be used. The development efficiency is much higher. Compile a direct dot hammer on the line.


Then run the installation on the original project!


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.