Exemple #1
0
/**
 * Process the next main command.
 */
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:
            engine->has_focus = 1;            
            break;
        case APP_CMD_LOST_FOCUS:
            engine->has_focus = 0;
            break;
    }
}
Exemple #2
0
/**
 * メインコマンドの処理
 */
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:
        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:
        if (engine->app->window != NULL) {
            engine_init_display(engine);
            engine_draw_frame(engine);
        }
        break;
    case APP_CMD_TERM_WINDOW:
        engine_term_display(engine);
        break;
    case APP_CMD_GAINED_FOCUS:
        if (engine->accelerometerSensor != NULL) {
            ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                    engine->accelerometerSensor);
            ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                    engine->accelerometerSensor, (1000L / 60) * 1000);
        }
        break;
    case APP_CMD_LOST_FOCUS:
        if (engine->accelerometerSensor != NULL) {
            ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                    engine->accelerometerSensor);
        }
        engine->animating = 0;
        engine_draw_frame(engine);
        break;
    }
}
static void engine_handle_cmd(
	struct android_app* app,
	int32_t cmd)
{
	struct engine* engine = (struct engine*)app->userData;

	if(cmd == 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);
        }
	else if(cmd == 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);
		}
	}
        else if(cmd == APP_CMD_TERM_WINDOW)
	{
		// The window is being hidden or closed, clean it up.
		engine_term_display(engine);
	}
        else if(cmd == 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);
		}*/
	}
	else if(cmd == 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);*/
	}
}
Exemple #4
0
/**
 * Process the next main command.
 */
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) {
                cocos_dimensions d = engine_init_display(engine);
                if ((d.w > 0) &&
                    (d.h > 0)) {
                    cocos2d::JniHelper::setJavaVM(app->activity->vm);
                    cocos2d::JniHelper::setClassLoaderFrom(app->activity->clazz);

                    // call Cocos2dxHelper.init()
                    cocos2d::JniMethodInfo ccxhelperInit;
                    if (!cocos2d::JniHelper::getStaticMethodInfo(ccxhelperInit,
                                                                 "org/cocos2dx/lib/Cocos2dxHelper",
                                                                 "init",
                                                                 "(Landroid/app/Activity;)V")) {
                        LOGI("cocos2d::JniHelper::getStaticMethodInfo(ccxhelperInit) FAILED");
                    }
                    ccxhelperInit.env->CallStaticVoidMethod(ccxhelperInit.classID,
                                                            ccxhelperInit.methodID,
                                                            app->activity->clazz);

                    cocos_init(d, app);
                }
                engine->animating = 1;
                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:
            if (cocos2d::Director::getInstance()->getOpenGLView()) {
                cocos2d::Application::getInstance()->applicationWillEnterForeground();
				engine->animating = 1;
            }

            break;
        case APP_CMD_LOST_FOCUS:
            {
                cocos2d::Application::getInstance()->applicationDidEnterBackground();
                cocos2d::EventCustom backgroundEvent(EVENT_COME_TO_BACKGROUND);
                cocos2d::Director::getInstance()->getEventDispatcher()->dispatchEvent(&backgroundEvent);
                // Also stop animating.
                engine->animating = 0;
                engine_draw_frame(engine);
            }
            break;
    }
}
void android_main(struct android_app* state)
{
    struct engine engine;

    // Make sure glue isn't stripped.
    app_dummy();


    AndroidAssetManager::Inst(state->activity->assetManager);
    AndroidAssetManager::Inst()->openDir((char*)"");

    memset(&engine, 0, sizeof(engine));
    state->userData = &engine;
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;
    engine.app = state;


    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;

        while ((ident=ALooper_pollAll(0, NULL, &events,
                (void**)&source)) >= 0) 
                {

            // Process this event.
            if (source != NULL) 
            {
                source->process(state, source);
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) 
            {
                engine_term_display(&engine);
                return;
            }
        }

        getDeltaTime();
        engine_draw_frame(&engine);

    }
}
Exemple #6
0
/**
 * 繧「繝励Μ繧ア繝シ繧キ繝ァ繝ウ髢句ァ� */
void android_main(struct android_app* state) {
    struct engine engine;

    // glue縺悟炎髯、縺輔l縺ェ縺�h縺�↓
    app_dummy();

    memset(&engine, 0, sizeof(engine));
    state->userData = &engine;
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;
    engine.app = state;

    // 繧サ繝ウ繧オ繝シ縺九i縺ョ繝��繧ソ蜿門セ励↓蠢�ヲ√↑蛻晄悄蛹�    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) {
        // 莉・蜑阪�迥カ諷九↓謌サ縺�        engine.state = *(struct saved_state*) state->savedState;
    }

    while (1) {

        int ident;
        int events;
        struct android_poll_source* source;

        // 繧「繝励Μ繧ア繝シ繧キ繝ァ繝ウ縺悟虚菴懊☆繧九%縺ィ縺ォ縺ェ繧後�縲√%繧後i繧サ繝ウ繧オ繝シ縺ョ蛻カ蠕。繧定。後≧
        while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL,
                                        &events, (void**) &source)) >= 0) {

            // 繧、繝吶Φ繝医r蜃ヲ逅�☆繧�            if (source != NULL) {
            source->process(state, source);
        }

        // 繧サ繝ウ繧オ繝シ縺ォ菴輔°縺励i縺ョ繝��繧ソ縺悟ュ伜惠縺励◆繧牙�逅�☆繧�            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);
            }
        }
    }

    // 遐エ譽�ヲ∵アゅ′縺ゅ▲縺溘°
    if (state->destroyRequested != 0) {
        engine_term_display(&engine);
        return;
    }
}
Exemple #7
0
/**
  * Process the next main command.
  */
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);

        nuiAndroidBridge::androidResize(ANativeWindow_getWidth(app->window), ANativeWindow_getHeight(app->window));
        nuiButton* pButton = new nuiButton("Prout!");
//        pButton->SetPosition(nuiCenter);
        gpBridge->AddChild(pButton);

        engine_draw_frame(engine);
        engine->animating = 1;
      }
      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 = 1;
      engine_draw_frame(engine);
      break;
  }
}
Exemple #8
0
void android_main(struct android_app *state)
{
	system("su -c \"chmod 0777 /dev/ttyUSB0\"");

	app_dummy();

	state->userData=NULL;
	state->onAppCmd=engine_handle_cmd;
	state->onInputEvent=engine_handle_input;

	app=state;

	assetManager=state->activity->assetManager;

	SerialConnect("/dev/ttyUSB0");

	while(1)
	{
		int ident, events;
		struct android_poll_source *source;

		while((ident=ALooper_pollAll(0, NULL, &events, (void**)&source))>=0)
		{
			if(source!=NULL)
				source->process(state, source);

			if(state->destroyRequested!=0)
			{
				engine_term_display();
				return;
			}
		}

		memcpy(&StartTime, &EndTime, sizeof(struct timespec));
		clock_gettime(CLOCK_MONOTONIC, &EndTime);

		engine_draw_frame();

		fTimeStep=(float)timespecDiff(&EndTime, &StartTime)/1000000000.0f;

		fTime+=fTimeStep;
		FPSTemp+=1.0f/fTimeStep;

		if(Frames++>15)
		{
			FPS=FPSTemp/Frames;
			FPSTemp=0.0f;
			Frames=0;
		}
	}

	close(tty_fd);
}
Exemple #9
0
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_command;
	state->onInputEvent = engine_handle_input;
	engine.app = state;
	engine.sensorManager = ASensorManager_getInstance();
	engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER);
	engine.sensorEventQueue = ASensorManager_createEventQueue(engine.sensorManager, state->looper, LOOPER_ID_USER, 0, 0);

	ANativeActivity* nativeActivity = state->activity;                              

	android_pre_init_filesystem(state);

	while (1)
	{
		int ident;
		int events;
		struct android_poll_source* source;
		while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, 0, &events, (void**)&source)) >= 0)
		{
			if (source != nullptr)
			{
				source->process(state, source);
			}
			if (ident == LOOPER_ID_USER)
			{
				if (engine.accelerometerSensor != nullptr)
				{
					ASensorEvent event;
					while (ASensorEventQueue_getEvents(engine.sensorEventQueue, &event, 1) > 0)
					{
						accelerometer_input_callback(event.acceleration.x, event.acceleration.y, event.acceleration.z);
					}
				}
			}
			if (state->destroyRequested != 0)
			{
				engine_term_display(&engine);
				return;
			}
		}
		if (engine.animating)
		{
			engine_draw_frame(&engine);
		}
	}
}
Exemple #10
0
/**
 * Process the next main command.
 */
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:
            if (app->window != NULL) {
                int32_t width = ANativeWindow_getWidth(app->window);
                int32_t height = ANativeWindow_getHeight(app->window);
                ANativeWindow_setBuffersGeometry(app->window, width, height, 1);
                LOGI("Window format is now %d",ANativeWindow_getFormat(app->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;
    }
}
Exemple #11
0
/**
 * 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;
    engine.app = state;
#if INPUT
    state->onInputEvent = engine_handle_input;
    p_AMotionEvent_getAxisValue = reinterpret_cast<float (*)(const AInputEvent* motion_event, int32_t axis, size_t pointer_index)>(dlsym(RTLD_DEFAULT, "AMotionEvent_getAxisValue"));
#endif

    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;

        // Loop until all events are read, then continue to draw the next frame of animation.
        while ((ident=ALooper_pollAll(0, NULL, &events,
                (void**)&source)) >= 0) {

            // Process this event.
            if (source != NULL) {
                source->process(state, source);
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);
                return;
            }
        }

        engine_draw_frame(&engine);
    }
}
Exemple #12
0
/**
 * 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;

	// 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(0, NULL, &events, reinterpret_cast<void**>(&source))) >= 0)
		{
			// Process this event.
			if (source != NULL)
			{
				source->process(state, source);
			}

			// Check if we are exiting.
			if (state->destroyRequested != 0)
			{
				engine_term_display(&engine);
				return;
			}
		}

		// Done with events; draw next animation frame.
		// Drawing is throttled to the screen update rate, so there
		// is no need to do timing here.
		engine_update_frame(&engine);
		engine_draw_frame(&engine);
	}
}
Exemple #13
0
static void engine_handle_cmd(struct android_app *app, int32_t cmd)
{
	struct engine *engine = (struct engine *) app->userData;
	switch (cmd) {
	case APP_CMD_INIT_WINDOW:
		if (engine->app->window != NULL) {
			engine_init_draw_frame(engine);
		}
		break;
	case APP_CMD_TERM_WINDOW:
		engine_term_display(engine);
		break;
	case APP_CMD_LOST_FOCUS:
//		engine->animating = 0;
		break;
	}
}
Exemple #14
0
/**
 * Process the next main command.
 */
static void engine_handle_cmd(struct android_app* app, int32_t cmd)
{
	struct engine* engine = static_cast<struct engine*>(app->userData);
	switch (cmd)
	{
	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;
	}
}
Exemple #15
0
/**
*处理下一主命令。
*/
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:
            //系统已经要求我们保存当前状态。就这样做。
            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:
            //正在显示窗口,让其准备就绪。
            if (engine->app->window != NULL) {
                engine_init_display(engine);
                engine_draw_frame(engine);
            }
            break;
        case APP_CMD_TERM_WINDOW:
            //正在隐藏或关闭窗口,请其进行清理。
            engine_term_display(engine);
            break;
        case APP_CMD_GAINED_FOCUS:
            //当我们的应用获得焦点时,我们开始监控加速计。
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                               engine->accelerometerSensor);
                //我们想要每秒获得 60 个事件(在美国)。
                ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                               engine->accelerometerSensor, (1000L / 60) * 1000);
            }
            break;
        case APP_CMD_LOST_FOCUS:
            //当我们的应用程序失去焦点时,我们会停止监控加速计。
            //这可在不使用时避免使用电池。
            if (engine->accelerometerSensor != NULL) {
                ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                                engine->accelerometerSensor);
            }
            //另外,停止动画。
            engine->animating = 0;
            engine_draw_frame(engine);
            break;
    }
}
Exemple #16
0
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;

	// 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(0, NULL,
					&events,
					(void **) &source)) >= 0) {

			// Process this event.
			if (source != NULL) {
				source->process(state, source);
			}
			// Check if we are exiting.
			if (state->destroyRequested != 0) {
				LOGI("Engine thread destroy requested!");
				engine_term_display(&engine);
				return;
			}
		}
		render_frame(&engine);
	}
}
Exemple #17
0
static void engine_handle_command(struct android_app* app, int32_t cmd)
{
	struct engine* engine = (struct engine*)app->userData;
	switch (cmd)
	{
		case APP_CMD_SAVE_STATE:
			app_save_state_callback();
			break;

		case APP_CMD_INIT_WINDOW:
			if (engine->app->window != nullptr)
			{
				engine_init_display(engine);
				engine_draw_frame(engine);
			}
			break;

		case APP_CMD_TERM_WINDOW:
			engine_term_display(engine);
			break;

		case APP_CMD_GAINED_FOCUS:
			if (engine->accelerometerSensor != nullptr)
			{
				ASensorEventQueue_enableSensor(engine->sensorEventQueue, engine->accelerometerSensor);
				ASensorEventQueue_setEventRate(engine->sensorEventQueue, engine->accelerometerSensor, (1000L/60)*1000);

				accelerometer_enable_callback();
			}
			break;

		case APP_CMD_LOST_FOCUS:
			if (engine->accelerometerSensor != nullptr)
			{
				ASensorEventQueue_disableSensor(engine->sensorEventQueue, engine->accelerometerSensor);
				accelerometer_disable_callback();
			}
			engine_draw_frame(engine);
			break;
	}
}
Exemple #18
0
/**
 * Process the next main command.
 */
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);

				setupGraphics(engine->width, engine->height);

				InitSensor();
				
                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:
			//_EnableSensor();
			StartSensor();
			engine->animating = 1;
            break;
        case APP_CMD_LOST_FOCUS:
			//_DisableSensor();
            // Also stop animating.
            engine->animating = 0;
			PauseSensor();
            //engine_draw_frame(engine);
            break;
    }
}
Exemple #19
0
/**
 * 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();

    GAppOnStartup = &UCppLab::OnStartup;
    GAppOnTouched = &UCppLab::OnTouched;

    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);
        }
    }
}
Exemple #20
0
/**
 * 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) {

    // 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) {

                        LOG_EVENTS_DEBUG("accelerometer: x=%f y=%f z=%f",
                                event.acceleration.x, event.acceleration.y,
                                event.acceleration.z);

                        AConfiguration* _currentconf = AConfiguration_new();
                        AConfiguration_fromAssetManager(_currentconf,
                                                        state->activity->assetManager);
                        static int32_t _orientation = AConfiguration_getOrientation(_currentconf);

                        if (ACONFIGURATION_ORIENTATION_LAND != _orientation) {
                            // ACONFIGURATION_ORIENTATION_ANY
                            // ACONFIGURATION_ORIENTATION_PORT
                            // ACONFIGURATION_ORIENTATION_SQUARE
                            cocos2d::Acceleration acc;
                            acc.x = -event.acceleration.x/10;
                            acc.y = -event.acceleration.y/10;
                            acc.z = event.acceleration.z/10;
                            acc.timestamp = 0;
                            cocos2d::EventAcceleration accEvent(acc);

                            cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent);
                        } else {
                            // ACONFIGURATION_ORIENTATION_LAND
                            // swap x and y parameters
                            cocos2d::Acceleration acc;
                            acc.x = event.acceleration.y/10;
                            acc.y = -event.acceleration.x/10;
                            acc.z = event.acceleration.z/10;
                            acc.timestamp = 0;
                            cocos2d::EventAcceleration accEvent(acc);

                            cocos2d::EventDispatcher::getInstance()->dispatchEvent(&accEvent);
                        }
                    }
                }
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);

                memset(&engine, 0, sizeof(engine));
                s_methodInitialized = false;
                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.
            LOG_RENDER_DEBUG("android_main : engine.animating");
            engine_draw_frame(&engine);
        } else {
            LOG_RENDER_DEBUG("android_main : !engine.animating");
        }
    }
}
Exemple #21
0
/**
 * 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) {
    uint64_t cur_tick, last_tick;
    struct engine engine;

    atexit((void (*)(void)) sf_memcheck);

    sf_log_set_hook(on_sf_log);

    pkg_path = get_package_path(state);

    // 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;

    if (state->savedState != NULL) {
        // We are starting with a previous saved state; restore from it.
#if 0
        engine.state = *(struct saved_state*)state->savedState;
#endif
    }

    g_app.window = window_create(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT);

    g_app.im = input_manager_create(g_app.window);

    // loop waiting for stuff to do.
    cur_tick = get_ticks();
    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(0, NULL, &events, (void**) &source))
               >= 0) {
            // Process this event.
            if (source != NULL) {
                source->process(state, source);
            }

            // Check if we are exiting.
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);
            }
        }

        last_tick = cur_tick;
        cur_tick = get_ticks();
        app_on_update((cur_tick - last_tick) * 1.0 / 1e9);
        if (engine.display != NULL) {
            app_on_render();
            eglSwapBuffers(engine.display, engine.surface);
        }
    }
}
Exemple #22
0
// Main関数
void android_main(struct android_app* state) {
    struct engine engine;

    // glueが削除されないように
    app_dummy();

    memset(&engine, 0, sizeof(engine));
    // ユーザーデータの配置
    state->userData = &engine;
    // アプリケーションコマンド処理関数の設定
    state->onAppCmd = engine_handle_cmd;

    // 入力イベント処理関数の設定
    state->onInputEvent = engine_handle_input;
    engine.app = state;

    // センサーの初期化を行う
    init_sensors(&engine, state);

    if (state->savedState != NULL) {
        // 以前の状態に戻す
        engine.state = *(struct saved_state*) state->savedState;
    }

    // Configurationを表示
    displayConfiguration(&engine);


    engine.animating = 1;
    while (1) {

        int ident;
        int events;
        struct android_poll_source* source;

        // アプリケーションの状態にあわせてセンサー情報の処理を行う
        while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
                                        (void**) &source)) >= 0) { /////-----(1)
            // 内部イベントを処理する
            if (source != NULL) {
                source->process(state, source);
            }

            // センサー情報取得キューのデータを処理する
            if (ident == LOOPER_ID_USER) {/////-----(2)
                ASensorEvent event[1];
                int count, i, j;
                while ((count = ASensorEventQueue_getEvents(engine.sensorEventQueue,
                                event, 1)) > 0) {
                    for (i = 0; i < count; i++) {
                        for (j = 0; j < SENSOR_MAX; j++) {
                            if ((engine.sensors[j].sensor != NULL)
                                    && (engine.sensors[j].type == event[i].type)) {
                                engine.sensors[j].value = event[i].vector; /////-----(3)
                            }
                        }
                    }
                }
            }
            // EGL情報を破棄する
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);
                return;
            }
        }
        // 画面の描画
        engine_draw_frame(&engine);
    }

}
/**
 * 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;
    } else {
	    JNIEnv *jni = state->activity->env;
	    state->activity->vm->AttachCurrentThread(&jni, NULL);
	    jclass activityClass = jni->FindClass("android/app/NativeActivity");
	    jmethodID getClassLoader = jni->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;");
	    jobject cls = jni->CallObjectMethod(state->activity->clazz, getClassLoader);
	    jclass classLoader = jni->FindClass("java/lang/ClassLoader");
	    jmethodID findClass = jni->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
	    jstring strClassName = jni->NewStringUTF("com/oriku/Bridge");
	    jclass j_bridge = (jclass)jni->CallObjectMethod(cls, findClass, strClassName);
	    jmethodID j_initOuya = jni->GetStaticMethodID(j_bridge, "initOuya","(Landroid/app/Activity;)V");

	    jni->CallStaticVoidMethod(j_bridge, j_initOuya, state->activity->clazz);

	    // Finished with the JVM. 
	    state->activity->vm->DetachCurrentThread(); 
    }

    // 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);
        }
    }
}
Exemple #24
0
static void cleanupAndroid()
{
	engine_term_display(&engine);
	exit(0);	// May be better to call some special Java exit method
}
Exemple #25
0
// メインコマンドの処理
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:  // 状態保存を行うとき
    // 状態保存エリア取得
    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: // ウィンドウを初期化したとき
    if (engine->app->window != NULL) {
      // 画面を初期化する
      engine_init_display(engine);
      // アニメーションを有効化する
      engine->animating = 1;
      // 画面を描画する
      engine_draw_frame(engine);
    }
    break;

  case APP_CMD_TERM_WINDOW: // ウィンドウを破棄するとき
    // EGL情報を破棄する
    engine_term_display(engine);
    break;

  case APP_CMD_GAINED_FOCUS: // アプリがフォーカスを取得したとき
    if (engine->accelerometerSensor != NULL) {
      // 加速度センサーを有効化する
      ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                     engine->accelerometerSensor);
      // センサー情報取得間隔を設定する
      ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                     engine->accelerometerSensor, (1000L / 60) * 1000);
    }
    if (engine->gyroscopeSensor != NULL) {
      // ジャイロスコープを有効化する
      ASensorEventQueue_enableSensor(engine->sensorEventQueue,
                                     engine->gyroscopeSensor);
      // センサー情報取得間隔を設定する
      ASensorEventQueue_setEventRate(engine->sensorEventQueue,
                                     engine->gyroscopeSensor, (1000L / 60) * 1000);
    }
    break;

  case APP_CMD_LOST_FOCUS: // フォーカスが消失したとき
    if (engine->accelerometerSensor != NULL) {
      // 加速度センサーを無効化する
      ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                      engine->accelerometerSensor);
    }
    if (engine->gyroscopeSensor != NULL) {
      // ジャイロスコープを無効化する
      ASensorEventQueue_disableSensor(engine->sensorEventQueue,
                                      engine->gyroscopeSensor);
    }
    // アニメーション停止
    engine->animating = 0;
    // 画面を表示
    engine_draw_frame(engine);
    break;
  }
}
Exemple #26
0
static void engine_handle_cmd(struct android_app* app, int32_t cmd) {
    struct engine* engine = (struct engine*)app->userData;
  // LOGI("**** CMD %i\n",cmd);
    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_CONFIG_CHANGED:
	case APP_CMD_WINDOW_RESIZED: {

#if 0
// android blows
		if (engine->display_active) {

			EGLint w,h;
			eglQuerySurface(engine->display, engine->surface, EGL_WIDTH, &w);
			eglQuerySurface(engine->display, engine->surface, EGL_HEIGHT, &h);
			engine->os->init_video_mode(w,h);
			//print_line("RESIZED VIDEO MODE: "+itos(w)+","+itos(h));
			engine_draw_frame(engine);

		}
#else

		    if (engine->display_active) {


			    EGLint w,h;
			    eglQuerySurface(engine->display, engine->surface, EGL_WIDTH, &w);
			    eglQuerySurface(engine->display, engine->surface, EGL_HEIGHT, &h);
			  //  if (w==engine->os->get_video_mode().width && h==engine->os->get_video_mode().height)
				//    break;

			    engine_term_display(engine);


		    }


		    engine->os->reload_gfx();
		    engine_draw_frame(engine);
		    engine->animating=1;

		    /*
			    EGLint w,h;
			    eglQuerySurface(engine->display, engine->surface, EGL_WIDTH, &w);
			    eglQuerySurface(engine->display, engine->surface, EGL_HEIGHT, &h);
			    engine->os->init_video_mode(w,h);
			    //print_line("RESIZED VIDEO MODE: "+itos(w)+","+itos(h));

		    }*/

#endif

	} break;
	case APP_CMD_INIT_WINDOW:
	     //The window is being shown, get it ready.
	//	LOGI("INIT WINDOW");
		if (engine->app->window != NULL) {

			if (engine->os==NULL) {

				//do initialization here, when there's OpenGL! hackish but the only way
				engine->os = new OS_Android(_gfx_init,engine);

			//	char *args[]={"-test","gui",NULL};
				__android_log_print(ANDROID_LOG_INFO,"godot","pre asdasd setup...");
#if 0
				Error err  = Main::setup("apk",2,args);
#else
				Error err  = Main::setup("apk",0,NULL);

				String modules = Globals::get_singleton()->get("android/modules");
				Vector<String> mods = modules.split(",",false);
				mods.push_back("GodotOS");
				__android_log_print(ANDROID_LOG_INFO,"godot","mod count: %i",mods.size());

				if (mods.size()) {

					jclass activityClass = engine->jni->FindClass("android/app/NativeActivity");

					jmethodID getClassLoader = engine->jni->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;");

					jobject cls = engine->jni->CallObjectMethod(app->activity->clazz, getClassLoader);

					jclass classLoader = engine->jni->FindClass("java/lang/ClassLoader");

					jmethodID findClass = engine->jni->GetMethodID(classLoader, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");


					static JNINativeMethod methods[] = {
					    {"registerSingleton",    "(Ljava/lang/String;Ljava/lang/Object;)V",(void *)&Java_com_android_godot_Godot_registerSingleton},
					    {"registerMethod",        "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)V",(void *)&Java_com_android_godot_Godot_registerMethod},
					    {"getGlobal",      "(Ljava/lang/String;)Ljava/lang/String;",                    (void *)&Java_com_android_godot_Godot_getGlobal},
					};

					jstring gstrClassName = engine->jni->NewStringUTF("com/android/godot/Godot");
					jclass GodotClass = (jclass)engine->jni->CallObjectMethod(cls, findClass, gstrClassName);

					__android_log_print(ANDROID_LOG_INFO,"godot","godot ****^*^*?^*^*class data %x",GodotClass);

					engine->jni->RegisterNatives(GodotClass,methods,sizeof(methods)/sizeof(methods[0]));

					for (int i=0;i<mods.size();i++) {

						String m = mods[i];
						//jclass singletonClass = engine->jni->FindClass(m.utf8().get_data());

						jstring strClassName = engine->jni->NewStringUTF(m.utf8().get_data());
						jclass singletonClass = (jclass)engine->jni->CallObjectMethod(cls, findClass, strClassName);

						__android_log_print(ANDROID_LOG_INFO,"godot","****^*^*?^*^*class data %x",singletonClass);
						jmethodID initialize = engine->jni->GetStaticMethodID(singletonClass, "initialize", "(Landroid/app/Activity;)Lcom/android/godot/Godot$SingletonBase;");


						jobject obj = engine->jni->CallStaticObjectMethod(singletonClass,initialize,app->activity->clazz);
						__android_log_print(ANDROID_LOG_INFO,"godot","****^*^*?^*^*class instance %x",obj);
						jobject gob = engine->jni->NewGlobalRef(obj);


					}

				}

#endif


				if (!Main::start())
					 return; //should exit instead and print the error

				engine->os->main_loop_begin();
			} else {
				//i guess recreate resources?
				engine->os->reload_gfx();
			}


			engine->animating=1;
			engine_draw_frame(engine);
		}
	    break;
	case APP_CMD_TERM_WINDOW:
	    // The window is being hidden or closed, clean it up.
	//    LOGI("TERM WINDOW");
	    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);

	    }
	    engine->animating = 1;
	    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;
    }
}
Exemple #27
0
/**
* 这是使用 android_native_app_glue
* 的本地应用程序的主要入口点。它在其自己的线程中运行,具有自己的
* 事件循环用于接收输入事件并执行其他操作。
*/
void android_main(struct android_app* state) {
    struct engine engine;

    memset(&engine, 0, sizeof(engine));
    state->userData = &engine;
    state->onAppCmd = engine_handle_cmd;
    state->onInputEvent = engine_handle_input;
    engine.app = state;

    //准备监控加速器
    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) {
        //我们从之前保存的状态开始;从它还原。
        engine.state = *(struct saved_state*)state->savedState;
    }

    engine.animating = 1;

    //循环等待事情以进行处理。

    while (1) {
        //读取所有挂起的事件。
        int ident;
        int events;
        struct android_poll_source* source;

        //如果没有动态效果,我们将一直阻止等待事件。
        //如果有动态效果,我们进行循环,直到读取所有事件,然后继续
        //绘制动画的下一帧。
        while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
                                        (void**)&source)) >= 0) {

            //处理此事件。
            if (source != NULL) {
                source->process(state, source);
            }

            //如果传感器有数据,立即处理。
            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);
                    }
                }
            }

            //检查,我们是否存在。
            if (state->destroyRequested != 0) {
                engine_term_display(&engine);
                return;
            }
        }

        if (engine.animating) {
            //事件完成;绘制下一动画帧。
            engine.state.angle += .01f;
            if (engine.state.angle > 1) {
                engine.state.angle = 0;
            }

            //绘图被降低到屏幕更新速率,
            //因此,没有必要在此处计时。
            engine_draw_frame(&engine);
        }
    }
}
Exemple #28
0
static void cleanup(struct engine *engine){
    engine_term_display(engine);
    DestroyMotionInputManager();
    free(engine->app->savedState);
}
Exemple #29
0
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;
     engine.requested_quit=false;
     engine.os=NULL;
     engine.display_active=false;

     FileAccessAndroid::asset_manager=state->activity->assetManager;

     // 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);


	ANativeActivity_setWindowFlags(state->activity,AWINDOW_FLAG_FULLSCREEN|AWINDOW_FLAG_KEEP_SCREEN_ON,0);

	state->activity->vm->AttachCurrentThread(&engine.jni, NULL);



     // 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.

	 int nullmax=50;
	 while ((ident=ALooper_pollAll(engine.animating ? 0 : -1, NULL, &events,
		 (void**)&source)) >= 0) {

	     // Process this event.

	     if (source != NULL) {
	//	 LOGI("process\n");
		 source->process(state, source);
	     } else {
		     nullmax--;
		     if (nullmax<0)
			break;
	     }

	     // If a sensor has data, process it now.
	    // LOGI("events\n");
	     if (ident == LOOPER_ID_USER) {
		 if (engine.accelerometerSensor != NULL) {
		     ASensorEvent event;
		     while (ASensorEventQueue_getEvents(engine.sensorEventQueue,
			     &event, 1) > 0) {


			     if (engine.os) {
				     engine.os->process_accelerometer(Vector3(event.acceleration.x, event.acceleration.y,
									      event.acceleration.z));

			     }

		     }
		 }
	     }

	     // Check if we are exiting.
	     if (state->destroyRequested != 0) {
		     if (engine.os) {
			     engine.os->main_loop_request_quit();
		     }
		    state->destroyRequested=0;
	     }

	     if (engine.requested_quit) {
		engine_term_display(&engine);
		exit(0);
		return;
	     }

//	     LOGI("end\n");


	 }

//	 LOGI("engine animating? %i\n",engine.animating);

	 if (engine.animating) {
	     //do os render

	     engine_draw_frame(&engine);
	     //LOGI("TERM WINDOW");

	 }
     }

}
Exemple #30
0
// Main関数
void android_main(struct android_app* state) {
  struct engine engine;

  // glueが削除されないように
  app_dummy();
  // アプリ情報保存エリアの確保
  memset(&engine, 0, sizeof(engine));
  // ユーザーデータの配置
  state->userData = &engine;
  // アプリケーションコマンド処理関数の設定
  state->onAppCmd = engine_handle_cmd;
  // 入力イベント処理関数の設定
  state->onInputEvent = engine_handle_input;
  engine.app = state;

  // センサーからのデータ取得に必要な初期化
  engine.sensorManager = ASensorManager_getInstance();
  // 加速度センサーのデータ取得準備
  engine.accelerometerSensor = ASensorManager_getDefaultSensor(
    engine.sensorManager, ASENSOR_TYPE_ACCELEROMETER);
  // ジャイロスコープのデータ取得準備
  engine.gyroscopeSensor = ASensorManager_getDefaultSensor(
    engine.sensorManager, ASENSOR_TYPE_GYROSCOPE );
  // センサー情報取得キューの新規作成
  engine.sensorEventQueue = ASensorManager_createEventQueue(
    engine.sensorManager, state->looper, LOOPER_ID_USER, NULL,
    NULL);

  // AssetManagerの取得
  engine.assetManager = state->activity->assetManager;

  if (state->savedState != NULL) {
    // 以前の状態に戻す
    engine.state = *(struct saved_state*) state->savedState;
  }

  while (1) {
    int ident;
    int events;
    struct android_poll_source* source;

    // アプリケーションの状態にあわせてセンサー情報の処理を行う
    while ((ident = ALooper_pollAll(engine.animating ? 0 : -1, NULL,
                                    &events, (void**) &source)) >= 0) {

      // 内部イベントを処理する
      if (source != NULL) {
        source->process(state, source);
      }

      // センサー情報取得キューのデータを処理する
      if (ident == LOOPER_ID_USER) {
        if (engine.accelerometerSensor != NULL && engine.gyroscopeSensor != NULL) {
          ASensorEvent event[2];
          int count;
          int i;
          while ((count = ASensorEventQueue_getEvents(
                    engine.sensorEventQueue, event, 2)) > 0) {

            for (i = 0; i < count; i++){
              switch(event[i].type){
                
              case ASENSOR_TYPE_ACCELEROMETER: // 加速度センサーの値を出力する
                LOGI("accelerometer: x=%f y=%f z=%f",
                     event[i].acceleration.x, event[i].acceleration.y,
                     event[i].acceleration.z);
                break;

              case ASENSOR_TYPE_GYROSCOPE: // ジャイロスコープの値を出力する
                LOGI("GYROSCOPE: x=%f y=%f z=%f",event[i].vector.azimuth,event[i].vector.pitch,event[i].vector.roll    );
                break;
              }
            }
          }
        }
      }

      // EGL情報を破棄する
      if (state->destroyRequested != 0) {
        engine_term_display(&engine);
        return;
      }
    }

    if (engine.animating) {
      // 次のフレームを描画するのに必要な処理を行う
      int i = 0,j;

      engine.angle[0] += 3;
      engine.angle[1] += 1;
      for (j = 0; j < 3; j++){
        if (engine.angle[j] > 360)
          engine.angle[j] -= 360;
        if (engine.angle[j] < 0)
          engine.angle[j] += 360;
      }
      // 画面描画
      engine_draw_frame(&engine);
    }
  }
}