最新首发Eclipse+CDT+android-ndk写纯c++安卓应用(附openGL Es)

首先下载eclipse和cdt,我的版本号依次是:Version: Indigo Service Release 2和Version: 1.0.0.201202111925,再下载windows的ndk,我使用的是android-ndk-r9d

什么cygwin这等东西,太恶心了,下载慢,大的要命!复杂,今天给一个最爽的编译教程。

前面的cdt插件怎么这里pass,网上教程很多的。直接配置。。。

启动eclipse,然后点Windows-Prefrences-C/C++-Build-Envionment,添加以下路径


然后创建一个android工程,把代码全部删除,资源全部删除,AndroidManifest.xml内容如下

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:tools="http://schemas.android.com/tools"  
  4.     package="com.example.native_activity"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0" >  
  7.   
  8.     <uses-sdk  
  9.         android:minSdkVersion="9"  
  10.         tools:ignore="UsesMinSdkAttributes" />  
  11.   
  12.     <application  
  13.         android:hasCode="false"  
  14.         android:label="纯CPP应用"  
  15.         tools:ignore="AllowBackup,MissingApplicationIcon" >  
  16.         <activity  
  17.             android:name="android.app.NativeActivity"  
  18.             android:configChanges="orientation|keyboardHidden" >  
  19.   
  20.             <!-- Tell NativeActivity the name of or .so -->  
  21.             <meta-data  
  22.                 android:name="android.app.lib_name"  
  23.                 android:value="native-activity" />  
  24.   
  25.             <intent-filter>  
  26.                 <action android:name="android.intent.action.MAIN" />  
  27.   
  28.                 <category android:name="android.intent.category.LAUNCHER" />  
  29.             </intent-filter>  
  30.         </activity>  
  31.     </application>  
  32.   
  33. </manifest>   

然后创建jni目录,里面放三个文件,依次是Android.mk

[plain]  view plain copy
  1. LOCAL_PATH := $(call my-dir)  
  2.   
  3. include $(CLEAR_VARS)  
  4.   
  5. LOCAL_MODULE    := native-activity  
  6. LOCAL_SRC_FILES := main.cpp  
  7. LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM  
  8. LOCAL_STATIC_LIBRARIES := android_native_app_glue  
  9.   
  10. include $(BUILD_SHARED_LIBRARY)  
  11.   
  12. $(call import-module,android/native_app_glue)  

Application.mk

[plain]  view plain copy
  1. APP_PLATFORM := android-14  

main.cpp

[cpp]  view plain copy
  1. #include <jni.h>  
  2. #include <errno.h>  
  3. #include <EGL/egl.h>  
  4. #include <GLES/gl.h>  
  5. #include <string.h>  
  6. #include <android/sensor.h>  
  7. #include <android/log.h>  
  8. #include <android_native_app_glue.h>  
  9.   
  10. #define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-activity", __VA_ARGS__))  
  11. #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "native-activity", __VA_ARGS__))  
  12.   
  13. /** 
  14.  * Our saved state data. 
  15.  */  
  16. struct saved_state {  
  17.     float angle;  
  18.     int32_t x;  
  19.     int32_t y;  
  20. };  
  21.   
  22. /** 
  23.  * Shared state for our app. 
  24.  */  
  25. struct engine {  
  26.     struct android_app* app;  
  27.   
  28.     ASensorManager* sensorManager;  
  29.     const ASensor* accelerometerSensor;  
  30.     ASensorEventQueue* sensorEventQueue;  
  31.   
  32.     int animating;  
  33.     EGLDisplay display;  
  34.     EGLSurface surface;  
  35.     EGLContext context;  
  36.     int32_t width;  
  37.     int32_t height;  
  38.     struct saved_state state;  
  39. };  
  40.   
  41. /** 
  42.  * Initialize an EGL context for the current display. 
  43.  */  
  44. static int engine_init_display(struct engine* engine) {  
  45.     // initialize OpenGL ES and EGL  
  46.   
  47.     /* 
  48.      * Here specify the attributes of the desired configuration. 
  49.      * Below, we select an EGLConfig with at least 8 bits per color 
  50.      * component compatible with on-screen windows 
  51.      */  
  52.     const EGLint attribs[] = { EGL_SURFACE_TYPE, EGL_WINDOW_BIT, EGL_BLUE_SIZE,  
  53.             8, EGL_GREEN_SIZE, 8, EGL_RED_SIZE, 8, EGL_NONE };  
  54.     EGLint w, h, dummy, format;  
  55.     EGLint numConfigs;  
  56.     EGLConfig config;  
  57.     EGLSurface surface;  
  58.     EGLContext context;  
  59.   
  60.     EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY);  
  61.   
  62.     eglInitialize(display, 0, 0);  
  63.   
  64.     /* Here, the application chooses the configuration it desires. In this 
  65.      * sample, we have a very simplified selection process, where we pick 
  66.      * the first EGLConfig that matches our criteria */  
  67.     eglChooseConfig(display, attribs, &config, 1, &numConfigs);  
  68.   
  69.     /* EGL_NATIVE_VISUAL_ID is an attribute of the EGLConfig that is 
  70.      * guaranteed to be accepted by ANativeWindow_setBuffersGeometry(). 
  71.      * As soon as we picked a EGLConfig, we can safely reconfigure the 
  72.      * ANativeWindow buffers to match, using EGL_NATIVE_VISUAL_ID. */  
  73.     eglGetConfigAttrib(display, config, EGL_NATIVE_VISUAL_ID, &format);  
  74.   
  75.     ANativeWindow_setBuffersGeometry(engine->app->window, 0, 0, format);  
  76.   
  77.     surface = eglCreateWindowSurface(display, config, engine->app->window,  
  78.             NULL);  
  79.     context = eglCreateContext(display, config, NULL, NULL);  
  80.   
  81.     if (eglMakeCurrent(display, surface, surface, context) == EGL_FALSE) {  
  82.         LOGW("Unable to eglMakeCurrent");  
  83.         return -1;  
  84.     }  
  85.   
  86.     eglQuerySurface(display, surface, EGL_WIDTH, &w);  
  87.     eglQuerySurface(display, surface, EGL_HEIGHT, &h);  
  88.   
  89.     engine->display = display;  
  90.     engine->context = context;  
  91.     engine->surface = surface;  
  92.     engine->width = w;  
  93.     engine->height = h;  
  94.     engine->state.angle = 0;  
  95.   
  96.     // Initialize GL state.  
  97.     glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST);  
  98.     glEnable(GL_CULL_FACE);  
  99.     glShadeModel(GL_SMOOTH);  
  100.     glDisable(GL_DEPTH_TEST);  
  101.   
  102.     return 0;  
  103. }  
  104.   
  105. /** 
  106.  * Just the current frame in the display. 
  107.  */  
  108. static void engine_draw_frame(struct engine* engine) {  
  109.     if (engine->display == NULL) {  
  110.         // No display.  
  111.         return;  
  112.     }  
  113.   
  114.     // Just fill the screen with a color.  
  115.     glClearColor(((float) engine->state.x) / engine->width, engine->state.angle,  
  116.             ((float) engine->state.y) / engine->height, 1);  
  117.     glClear(GL_COLOR_BUFFER_BIT);  
  118.   
  119.     eglSwapBuffers(engine->display, engine->surface);  
  120. }  
  121.   
  122. /** 
  123.  * Tear down the EGL context currently associated with the display. 
  124.  */  
  125. static void engine_term_display(struct engine* engine) {  
  126.     if (engine->display != EGL_NO_DISPLAY) {  
  127.         eglMakeCurrent(engine->display, EGL_NO_SURFACE, EGL_NO_SURFACE,  
  128.                 EGL_NO_CONTEXT);  
  129.         if (engine->context != EGL_NO_CONTEXT) {  
  130.             eglDestroyContext(engine->display, engine->context);  
  131.         }  
  132.         if (engine->surface != EGL_NO_SURFACE) {  
  133.             eglDestroySurface(engine->display, engine->surface);  
  134.         }  
  135.         eglTerminate(engine->display);  
  136.     }  
  137.     engine->animating = 0;  
  138.     engine->display = EGL_NO_DISPLAY;  
  139.     engine->context = EGL_NO_CONTEXT;  
  140.     engine->surface = EGL_NO_SURFACE;  
  141. }  
  142.   
  143. /** 
  144.  * Process the next input event. 
  145.  */  
  146. static int32_t engine_handle_input(struct android_app* app,  
  147.         AInputEvent* event) {  
  148.     struct engine* engine = (struct engine*) app->userData;  
  149.     if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {  
  150.         engine->animating = 1;  
  151.         engine->state.x = AMotionEvent_getX(event, 0);  
  152.         engine->state.y = AMotionEvent_getY(event, 0);  
  153.         return 1;  
  154.     }  
  155.     return 0;  
  156. }  
  157.   
  158. /** 
  159.  * Process the next main command. 
  160.  */  
  161. static void engine_handle_cmd(struct android_app* app, int32_t cmd) {  
  162.     struct engine* engine = (struct engine*) app->userData;  
  163.     switch (cmd) {  
  164.     case APP_CMD_SAVE_STATE:  
  165.         // The system has asked us to save our current state.  Do so.  
  166.         engine->app->savedState = malloc((size_t)sizeof(struct saved_state));  
  167.         *((struct saved_state*) engine->app->savedState) = engine->state;  
  168.         engine->app->savedStateSize = sizeof(struct saved_state);  
  169.         break;  
  170.     case APP_CMD_INIT_WINDOW:  
  171.         // The window is being shown, get it ready.  
  172.         if (engine->app->window != NULL) {  
  173.             engine_init_display(engine);  
  174.             engine_draw_frame(engine);  
  175.         }  
  176.         break;  
  177.     case APP_CMD_TERM_WINDOW:  
  178.         // The window is being hidden or closed, clean it up.  
  179.         engine_term_display(engine);  
  180.         break;  
  181.     case APP_CMD_GAINED_FOCUS:  
  182.         // When our app gains focus, we start monitoring the accelerometer.  
  183.         if (engine->accelerometerSensor != NULL) {  
  184.             ASensorEventQueue_enableSensor(engine->sensorEventQueue,  
  185.                     engine->accelerometerSensor);  
  186.             // We'd like to get 60 events per second (in us).  
  187.             ASensorEventQueue_setEventRate(engine->sensorEventQueue,  
  188.                     engine->accelerometerSensor, (1000L / 60) * 1000);  
  189.         }  
  190.         break;  
  191.     case APP_CMD_LOST_FOCUS:  
  192.         // When our app loses focus, we stop monitoring the accelerometer.  
  193.         // This is to avoid consuming battery while not being used.  
  194.         if (engine->accelerometerSensor != NULL) {  
  195.             ASensorEventQueue_disableSensor(engine->sensorEventQueue,  
  196.                     engine->accelerometerSensor);  
  197.         }  
  198.         // Also stop animating.  
  199.         engine->animating = 0;  
  200.         engine_draw_frame(engine);  
  201.         break;  
  202.     }  
  203. }  
  204.   
  205. /** 
  206.  * This is the main entry point of a native application that is using 
  207.  * android_native_app_glue.  It runs in its own thread, with its own 
  208.  * event loop for receiving input events and doing other things. 
  209.  */  
  210. void android_main(struct android_app* state) {  
  211.     struct engine engine = {0};  
  212.     // Make sure glue isn't stripped.  
  213.     app_dummy();  
  214.     state->userData = &engine;  
  215.     state->onAppCmd = engine_handle_cmd;  
  216.     state->onInputEvent = engine_handle_input;  
  217.     engine.app = state;  
  218.   
  219.     // Prepare to monitor accelerometer  
  220.     engine.sensorManager = ASensorManager_getInstance();  
  221.     engine.accelerometerSensor = ASensorManager_getDefaultSensor(  
  222.             engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER);  
  223.     engine.sensorEventQueue = ASensorManager_createEventQueue(  
  224.             engine.sensorManager, state->looper, LOOPER_ID_USER, NULL, NULL);  
  225.   
  226.     if (state->savedState != NULL) {  
  227.         // We are starting with a previous saved state; restore from it.  
  228.         engine.state = *(struct saved_state*) state->savedState;  
  229.     }  
  230.   
  231.     // loop waiting for stuff to do.  
  232.   
  233.     while (true) {  
  234.         // Read all pending events.  
  235.         int ident;  
  236.         int events;  
  237.         struct android_poll_source* source;  
  238.   
  239.         // If not animating, we will block forever waiting for events.  
  240.         // If animating, we loop until all events are read, then continue  
  241.         // to draw the next frame of animation.  
  242.         while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL,  
  243.                 &events, (void**) &source)) >= 0) {  
  244.   
  245.             // Process this event.  
  246.             if (source != NULL) {  
  247.                 source->process(state, source);  
  248.             }  
  249.   
  250.             // If a sensor has data, process it now.  
  251.             if (ident == LOOPER_ID_USER) {  
  252.                 if (engine.accelerometerSensor != NULL) {  
  253.                     ASensorEvent event;  
  254.                     while (ASensorEventQueue_getEvents(engine.sensorEventQueue,  
  255.                             &event, 1) > 0) {  
  256.                         LOGI("accelerometer: x=%f y=%f z=%f", event.acceleration.x, event.acceleration.y, event.acceleration.z);  
  257.                     }  
  258.                 }  
  259.             }  
  260.   
  261.             // Check if we are exiting.  
  262.             if (state->destroyRequested != 0) {  
  263.                 engine_term_display(&engine);  
  264.                 return;  
  265.             }  
  266.         }  
  267.   
  268.         if (engine.animating) {  
  269.             // Done with events; draw next animation frame.  
  270.             engine.state.angle += .01f;  
  271.             if (engine.state.angle > 1) {  
  272.                 engine.state.angle = 0;  
  273.             }  
  274.             engine_draw_frame(&engine);  
  275.         }  
  276.     }  
  277. }  

创建完成收工,然后创建另外一个工程。路径必须是刚才创建工程的jni目录,名字随便,重点看图


好了点完成,然后打开main.cpp发现N多错误,直接下设置一下环境变量,右键工程,属性(是刚创建的C++工程)


接下来看图,把所有的库加进去。



最后加一个Symbol,其实就是定义一个宏,告诉编译器我现在的平台是Android,add


最后点OK,所有的函数都能正常识别,提示功能也可以用了。开发效率高多了。编译直接点锤子就行了。


然后在原来的工程运行安装就行了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值