int main() { //OOP封装思想 MySample app; return app.run(); }
/** * 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(); LOGI("\n\n********************************"); LOGI(" STARTING SAMPLE"); LOGI("********************************\n\n"); // create an instance of my sample MySample *pSample = new MySample(); if (!pSample) { LOGI("Failed to allocate MySample"); return; } // Assign the sample back into the app state state->userData = pSample; state->onAppCmd = cput_handle_cmd; state->onInputEvent = CPUT_OGL::cput_handle_input; // We make the assumption we are running from the executable's dir in // the CPUT SampleStart directory or it won't be able to use the relative paths to find the default // resources cString ResourceDirectory; CPUTFileSystem::GetExecutableDirectory(&ResourceDirectory); // Different executable and assets locations on different OS'es. // Consistency should be maintained in all OS'es and API's. ResourceDirectory.append(GUI_LOCATION); // Initialize the system and give it the base CPUT resource directory (location of GUI images/etc) // For now, we assume it's a relative directory from the executable directory. Might make that resource // directory location an env variable/hardcoded later CPUTWindowAndroid::SetAppState(state); pSample->CPUTInitialize(ResourceDirectory); CPUTFileSystem::GetExecutableDirectory(&ResourceDirectory); // Different executable and assets locations on different OS'es. // Consistency should be maintained in all OS'es and API's. ResourceDirectory.append(SYSTEM_LOCATION); CPUTAssetLibrary *pAssetLibrary = CPUTAssetLibrary::GetAssetLibrary(); pAssetLibrary->SetSystemDirectoryName(ResourceDirectory); // start the main message loop pSample->CPUTMessageLoop(); // cleanup resources SAFE_DELETE(pSample); pSample = NULL; state->userData = NULL; }
static void cput_handle_cmd(struct android_app* app, int32_t cmd) { MySample *pSample = (MySample *)app->userData; switch (cmd) { case APP_CMD_SAVE_STATE: LOGI("APP_CMD_SAVE_STATE"); break; case APP_CMD_INIT_WINDOW: LOGI("APP_CMD_INIT_WINDOW"); if (!pSample->HasWindow()) { LOGI("Creating window"); CPUTResult result; // window and device parameters CPUTWindowCreationParams params; params.samples = 1; // create the window and device context result = pSample->CPUTCreateWindowAndContext(_L("CPUTWindow OpenGLES"), params); if (result != CPUT_SUCCESS) LOGI("Unable to create window"); } else { LOGI("Window already created"); } break; case APP_CMD_TERM_WINDOW: LOGI("APP_CMD_TERM_WINDOW"); exit(0); // Need clear window create and destroy calls // The window is being hidden or closed, clean it up. if (pSample->HasWindow()) { pSample->DeviceShutdown(); } break; case APP_CMD_GAINED_FOCUS: LOGI("APP_CMD_GAINED_FOCUS"); break; case APP_CMD_LOST_FOCUS: exit(0); LOGI("APP_CMD_LOST_FOCUS"); break; case APP_CMD_WINDOW_RESIZED: LOGI("APP_CMD_WINDOW_RESIZED"); break; } }
int32_t CPUT_OGL::cput_handle_input(struct android_app* app, AInputEvent* event) { int n; MySample *pSample = (MySample *)app->userData; int lEventType = AInputEvent_getType(event); static float drag_center_x = 0.0f, drag_center_y = 0.0f; static float dist_squared = 0.0f; static bool isPanning = false; switch (lEventType) { case AINPUT_EVENT_TYPE_MOTION: { ndk_helper::GESTURE_STATE doubleTapState = pSample->mDoubletapDetector.Detect(event); ndk_helper::GESTURE_STATE dragState = pSample->mDragDetector.Detect(event); ndk_helper::GESTURE_STATE pinchState = pSample->mPinchDetector.Detect(event); if( doubleTapState == ndk_helper::GESTURE_STATE_ACTION ) { LOGI("DOUBLE TAP RECEIVED"); } else { //Handle drag state if( dragState & ndk_helper::GESTURE_STATE_START ) { if (isPanning == false) { //LOGI("GESTURE_STATE_START - drag"); ndk_helper::Vec2 v; float x, y; pSample->mDragDetector.GetPointer( v ); v.Value(x, y); //LOGI(" TOUCH POINT: %f, %f", x, y); pSample->CPUTHandleMouseEvent(x, y, 0.0f, CPUT_MOUSE_LEFT_DOWN); } } else if( dragState & ndk_helper::GESTURE_STATE_MOVE ) { if (isPanning == false) { //LOGI("GESTURE_STATE_MOVE - drag"); ndk_helper::Vec2 v; float x, y; pSample->mDragDetector.GetPointer( v ); v.Value(x, y); pSample->CPUTHandleMouseEvent(x, y, 0.0f, CPUT_MOUSE_LEFT_DOWN); } } else if( dragState & ndk_helper::GESTURE_STATE_END ) { pSample->CPUTHandleMouseEvent(0.0f, 0.0f, 0.0f, CPUT_MOUSE_NONE ); pSample->HandleKeyboardEvent(KEY_A, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_D, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_E, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_W, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_S, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_Q, CPUT_KEY_UP); isPanning = false; //LOGI("GESTURE_STATE_END - drag"); } //Handle pinch state if( pinchState & ndk_helper::GESTURE_STATE_START ) { if (isPanning == false) { //LOGI("GESTURE_STATE_START - pinch"); //Start new pinch ndk_helper::Vec2 v1; ndk_helper::Vec2 v2; float x1, y1, x2, y2; pSample->mPinchDetector.GetPointers( v1, v2 ); v1.Value(x1, y1); v2.Value(x2, y2); drag_center_x = (x1 + x2) / 2.0f; drag_center_y = (y1 + y2) / 2.0f; dist_squared = ((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)); } } else if( pinchState & ndk_helper::GESTURE_STATE_MOVE ) { isPanning = true; CPUTKey key = (CPUTKey)0; CPUTKeyState state = (CPUTKeyState)0; //LOGI("GESTURE_STATE_MOVE - pinch"); ndk_helper::Vec2 v1; ndk_helper::Vec2 v2; float x1, y1, x2, y2; float new_center_x, new_center_y; float new_dist_squared; float delta_x, delta_y; pSample->mPinchDetector.GetPointers( v1, v2 ); v1.Value(x1, y1); v2.Value(x2, y2); new_center_x = (x1 + x2) / 2.0f; new_center_y = (y1 + y2) / 2.0f; new_dist_squared = ((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)); delta_x = drag_center_x - new_center_x; delta_y = drag_center_y - new_center_y; // // For each direction of movement, the opposite direction is cancelled (KEY_UP) // // Handle pinch and zoom actions if (abs(new_dist_squared - dist_squared) > 1000.0f) { if (new_dist_squared < dist_squared) { pSample->HandleKeyboardEvent(KEY_S, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_W, CPUT_KEY_DOWN); } else { pSample->HandleKeyboardEvent(KEY_W, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_S, CPUT_KEY_DOWN); } } else { pSample->HandleKeyboardEvent(KEY_W, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_S, CPUT_KEY_UP); } // handle left and right drag if (delta_x >= 2.0f) { pSample->HandleKeyboardEvent(KEY_A, CPUT_KEY_DOWN); pSample->HandleKeyboardEvent(KEY_D, CPUT_KEY_UP); } else if (delta_x <= -2.0f) { pSample->HandleKeyboardEvent(KEY_D, CPUT_KEY_DOWN); pSample->HandleKeyboardEvent(KEY_A, CPUT_KEY_UP); } else if (delta_x < 2.0 && delta_x > -2.0) { pSample->HandleKeyboardEvent(KEY_A, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_D, CPUT_KEY_UP); } // handle up and down drag if (delta_y >= 2.0f) { pSample->HandleKeyboardEvent(KEY_Q, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_E, CPUT_KEY_DOWN); } else if (delta_y <= -2.0f) { pSample->HandleKeyboardEvent(KEY_E, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_Q, CPUT_KEY_DOWN); } else if (delta_y < 2.0 && delta_y > -2.0) { pSample->HandleKeyboardEvent(KEY_E, CPUT_KEY_UP); pSample->HandleKeyboardEvent(KEY_Q, CPUT_KEY_UP); } // current values become old values for next frame dist_squared = new_dist_squared; drag_center_x = new_center_x; drag_center_y = new_center_y; } } } case AINPUT_EVENT_TYPE_KEY: { int aKey = AKeyEvent_getKeyCode(event); CPUTKey cputKey = ConvertToCPUTKey(aKey); int aAction = AKeyEvent_getAction(event); CPUTKeyState cputKeyState = ConvertToCPUTKeyState(aAction); pSample->CPUTHandleKeyboardEvent(cputKey, cputKeyState); return 1; } default: return 0; } return 0; }
// Application entry point. Execution begins here. //----------------------------------------------------------------------------- int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow ) { // Prevent unused parameter compiler warnings UNREFERENCED_PARAMETER(hInstance); UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(nCmdShow); #ifdef DEBUG // tell VS to report leaks at any exit of the program _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); #endif CPUTResult result=CPUT_SUCCESS; int returnCode=0; // create an instance of my sample MySample *pSample = new MySample(); // We make the assumption we are running from the executable's dir in // the CPUT SampleStart directory or it won't be able to use the relative paths to find the default // resources cString ResourceDirectory; CPUTOSServices::GetOSServices()->GetExecutableDirectory(&ResourceDirectory); ResourceDirectory.append(_L(".\\CPUT\\resources\\")); // Initialize the system and give it the base CPUT resource directory (location of GUI images/etc) // For now, we assume it's a relative directory from the executable directory. Might make that resource // directory location an env variable/hardcoded later pSample->CPUTInitialize(ResourceDirectory); // window and device parameters CPUTWindowCreationParams params; params.deviceParams.refreshRate = 60; params.deviceParams.swapChainBufferCount= 1; params.deviceParams.swapChainFormat = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB; params.deviceParams.swapChainUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT | DXGI_USAGE_SHADER_INPUT; // parse out the parameter settings or reset them to defaults if not specified cString AssetFilename; cString CommandLine(lpCmdLine); pSample->CPUTParseCommandLine(CommandLine, ¶ms, &AssetFilename); // create the window and device context result = pSample->CPUTCreateWindowAndContext(_L("CPUTWindow DirectX 11"), params); ASSERT( CPUTSUCCESS(result), _L("CPUT Error creating window and context.") ); // initialize the task manager gTaskMgr.Init(); // start the main message loop returnCode = pSample->CPUTMessageLoop(); pSample->DeviceShutdown(); // shutdown task manage gTaskMgr.Shutdown(); // cleanup resources delete pSample; return returnCode; }