织梦CMS - 轻松建站从此开始!

罗索

当前位置: 主页>嵌入式开发>Android>

无Java开发Android应用(NativeActivity) (2)

落鹤生 发布于 2011-01-19 18:04 点击:次 
static void engine_handle_cmd( struct android_app*app,int32_tcmd){ struct engine*engine=( struct engine*)app-userData; switch (cmd){ case APP_CMD_SAVE_STATE: //Thesystemhasaskedustosaveourcurrentstate
TAG:

  • static void engine_handle_cmd(struct android_app* app, int32_t cmd) { 
  •     struct engine* engine = (struct engine*)app->userData; 
  •     switch (cmd) { 
  •         case APP_CMD_SAVE_STATE: 
  •             // The system has asked us to save our current state.  Do so. 
  •             engine->app->savedState = malloc(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(engine); 
  •                 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
  •         case APP_CMD_GAINED_FOCUS: 
  •             // When our app gains focus, we start monitoring the accelerometer. 
  •             if (engine->accelerometerSensor != NULL) { 
  •                 ASensorEventQueue_enableSensor(engine->sensorEventQueue, 
  •                         engine->accelerometerSensor); 
  •                 // We'd like to get 60 events per second (in us). 
  •                 ASensorEventQueue_setEventRate(engine->sensorEventQueue, 
  •                         engine->accelerometerSensor, (1000L/60)*1000); 
  •             } 
  •             break
  •         case APP_CMD_LOST_FOCUS: 
  •             // When our app loses focus, we stop monitoring the accelerometer. 
  •             // This is to avoid consuming 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 that 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; 
  •  
  •     // Make sure glue isn't stripped. 
  •     app_dummy(); 
  •  
  •     memset(&engine, 0, sizeof(engine)); 
  •     state->userData = &engine; 
  •     state->onAppCmd = engine_handle_cmd; 
  •     state->onInputEvent = engine_handle_input; 
  •     engine.app = state; 
  •  
  •     // Prepare to monitor accelerometer 
  •     engine.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 are starting with a previous saved state; restore from it. 
  •         engine.state = *(struct saved_state*)state->savedState; 
  •     } 
  •  
  •     // loop waiting for stuff to do. 
  •  
  •     while (1) { 
  •         // Read all pending events. 
  •         int ident; 
  •         int events; 
  •         struct android_poll_source* source; 
  •  
  •         // If not animating, we will block forever waiting for events. 
  •         // If animating, we loop until all events are read, then 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(state, source); 
  •             } 
  •  
  •             // If a sensor has data, process it now. 
  •             if (ident == LOOPER_ID_USER) { 
  •                 if (engine.accelerometerSensor != NULL) { 
  •                     ASensorEvent 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 > 1) { 
  •                 engine.state.angle = 0; 
  •             } 
  •  
  •             // Drawing is throttled to the screen update rate, so there 
  •             // is no need to do timing here. 
  •             engine_draw_frame(&engine); 
  •         } 
  •     } 
  • (红薯 )
    本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201101/10785.html]
    本文出处:开源中国社区 作者:红薯
    顶一下
    (1)
    100%
    踩一下
    (0)
    0%
    ------分隔线----------------------------
    发表评论
    请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
    评价:
    表情:
    用户名: 验证码:点击我更换图片
    栏目列表
    将本文分享到微信
    织梦二维码生成器
    推荐内容