int CCApplication::run() { struct timespec time_struct; long current_time, update_time; // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) { return 0; } clock_gettime(CLOCK_REALTIME, &time_struct); update_time = time2millis(&time_struct); while (1) // or device wants to quit { CCEGLView::sharedOpenGLView()->handleEvents(); clock_gettime(CLOCK_REALTIME, &time_struct); current_time = time2millis(&time_struct); if ((current_time - update_time) > m_animationInterval) { update_time = current_time; CCDirector::sharedDirector()->mainLoop(); } else { // sleep(0); } } return -1; }
int CCApplication::run() { struct timespec time_struct; long update_time; // Initialize instance and cocos2d. if (!applicationDidFinishLaunching()) { return 0; } clock_gettime(CLOCK_REALTIME, &time_struct); update_time = time2millis(&time_struct); // XXX: Set to 1FPS while debugging emscripten_set_main_loop(&mainLoopIter, 0, 1); return -1; }
bool CCEGLView::handleEvents() { bps_event_t* event = NULL; mtouch_event_t mtouch_event; int touch_id = 0; int val = 0; int rc = 0; int domain = 0; char buf[4] = {0}; for (;;) { rc = bps_get_event(&event, 1); assert(rc == BPS_SUCCESS); // break if no more events if (event == NULL) break; domain = bps_event_get_domain(event); if (domain == navigator_get_domain()) { switch (bps_event_get_code(event)) { case NAVIGATOR_SWIPE_DOWN: CCDirector::sharedDirector()->getKeypadDispatcher()->dispatchKeypadMSG(kTypeMenuClicked); break; case NAVIGATOR_EXIT: // exit the application // release(); end(); break; case NAVIGATOR_WINDOW_INACTIVE: if (m_isWindowActive) { CCApplication::sharedApplication()->applicationDidEnterBackground(); m_isWindowActive = false; } break; case NAVIGATOR_WINDOW_ACTIVE: if (!m_isWindowActive) { CCApplication::sharedApplication()->applicationWillEnterForeground(); m_isWindowActive = true; } break; case NAVIGATOR_WINDOW_STATE: { switch(navigator_event_get_window_state(event)) { case NAVIGATOR_WINDOW_FULLSCREEN: if (!m_isWindowActive) { CCApplication::sharedApplication()->applicationWillEnterForeground(); m_isWindowActive = true; } break; case NAVIGATOR_WINDOW_THUMBNAIL: if (m_isWindowActive) { CCApplication::sharedApplication()->applicationDidEnterBackground(); m_isWindowActive = false; } break; } break; } default: break; } } else if (domain == screen_get_domain()) { m_screenEvent = screen_event_get_event(event); rc = screen_get_event_property_iv(m_screenEvent, SCREEN_PROPERTY_TYPE, &val); if (rc || val == SCREEN_EVENT_NONE) break; float x, y; switch (val) { case SCREEN_EVENT_CLOSE: fprintf(stderr, "SCREEN CLOSE EVENT!\n"); break; case SCREEN_EVENT_MTOUCH_RELEASE: screen_get_mtouch_event(m_screenEvent, &mtouch_event, 0); touch_id = mtouch_event.contact_id; x = mtouch_event.x; y = mtouch_event.y; handleTouchesEnd(1, &touch_id, &x, &y); break; case SCREEN_EVENT_MTOUCH_TOUCH: screen_get_mtouch_event(m_screenEvent, &mtouch_event, 0); touch_id = mtouch_event.contact_id; x = mtouch_event.x; y = mtouch_event.y; handleTouchesBegin(1, &touch_id, &x, &y); break; case SCREEN_EVENT_MTOUCH_MOVE: screen_get_mtouch_event(m_screenEvent, &mtouch_event, 0); touch_id = mtouch_event.contact_id; x = mtouch_event.x; y = mtouch_event.y; handleTouchesMove(1, &touch_id, &x, &y); break; case SCREEN_EVENT_POINTER: { int buttons = 0; int pair_[2] = {0}; float pair[2] = {0.0f}; static bool mouse_pressed = false; // this is a mouse move event, it is applicable to a device with a usb mouse or simulator screen_get_event_property_iv(m_screenEvent, SCREEN_PROPERTY_BUTTONS, &buttons); screen_get_event_property_iv(m_screenEvent, SCREEN_PROPERTY_SOURCE_POSITION, pair_); pair[0] = (float)pair_[0]; pair[1] = (float)pair_[1]; if (buttons & SCREEN_LEFT_MOUSE_BUTTON) { if (mouse_pressed) { handleTouchesMove(1, &touch_id, &pair[0], &pair[1]); } else { // Left mouse button is pressed mouse_pressed = true; handleTouchesBegin(1, &touch_id, &pair[0], &pair[1]); } } else { if (mouse_pressed) { mouse_pressed = false; handleTouchesEnd(1, &touch_id, &pair[0], &pair[1]); } } } break; case SCREEN_EVENT_KEYBOARD: screen_get_event_property_iv(m_screenEvent, SCREEN_PROPERTY_KEY_FLAGS, &val); if (val & KEY_DOWN) { screen_get_event_property_iv(m_screenEvent, SCREEN_PROPERTY_KEY_SYM, &val); if (val >= ' ' && val < '~') { buf[0] = val; buf[1]= '\0'; CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(buf, 1); } else { val = val - 0xf000; buf[0] = val; buf[1]= '\0'; switch (val) { case 8: // backspace // CCKeypadDispatcher::sharedDispatcher()->dispatchKeypadMSG(kTypeBackClicked); CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward(); break; default: CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(buf, 1); break; } } } break; default: break; } } else if (domain == sensor_get_domain()) { if (bps_event_get_code(event) == SENSOR_ACCELEROMETER_READING) { struct timespec time_struct; long current_time; float x, y, z; clock_gettime(CLOCK_REALTIME, &time_struct); current_time = time2millis(&time_struct); sensor_event_get_xyz(event, &x, &y, &z); CCDirector::sharedDirector()->getAccelerometer()->update(current_time, -x, -y, z); } } } return true; }
int main(int argc, char *argv[]) { int exit_application = 0; int rc; //Create a screen context that will be used to create an EGL surface to to receive libscreen events screen_create_context(&screen_cxt, 0); initGestures(); //Initialize BPS library bps_initialize(); //Use utility code to initialize EGL for 2D rendering with GL ES 1.1 if (EXIT_SUCCESS != bbutil_init_egl(screen_cxt, GL_ES_1, AUTO)) { fprintf(stderr, "bbutil_init_egl failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } //Initialize application logic if (EXIT_SUCCESS != initialize()) { fprintf(stderr, "initialize failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } //Signal BPS library that navigator and screen events will be requested if (BPS_SUCCESS != screen_request_events(screen_cxt)) { fprintf(stderr, "screen_request_events failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } if (BPS_SUCCESS != navigator_request_events(0)) { fprintf(stderr, "navigator_request_events failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } //Signal BPS library that navigator orientation is not to be locked if (BPS_SUCCESS != navigator_rotation_lock(false)) { fprintf(stderr, "navigator_rotation_lock failed\n"); bbutil_terminate(); screen_destroy_context(screen_cxt); return 0; } // set up our box2D tests fprintf(stderr, "Box2D Version %d.%d.%d\n", b2_version.major, b2_version.minor, b2_version.revision); testCount = 0; while (g_testEntries[testCount].createFcn != NULL) { ++testCount; } testIndex = b2Clamp(testIndex, 0, testCount-1); testSelection = testIndex; entry = g_testEntries + testIndex; test = entry->createFcn(); test->m_debugDraw.SetFont(font); test->m_debugDraw.SetScreenSize(width, height); struct timespec time_struct; clock_gettime(CLOCK_REALTIME, &time_struct); long update_time = time2millis(&time_struct); long current_time, last_time; #ifdef FPS int frames = 0; last_time = update_time; #endif for (;;) { //Request and process BPS next available event bps_event_t *event = NULL; rc = bps_get_event(&event, 1); assert(rc == BPS_SUCCESS); if (event) { int domain = bps_event_get_domain(event); if (domain == screen_get_domain()) { handleScreenEvent(event); } else if ((domain == navigator_get_domain()) && (NAVIGATOR_EXIT == bps_event_get_code(event))) { exit_application = 1; } } clock_gettime(CLOCK_REALTIME, &time_struct); current_time = time2millis(&time_struct); if ((current_time - update_time) > framePeriod) { update_time = current_time; render(); #ifdef FPS frames++; #endif } else { sleep(0); } #ifdef FPS if (current_time - last_time > 1000) { fprintf(stderr, "fps: %d\n", frames); frames = 0; last_time = current_time; } #endif if (exit_application) break; } // clean up gestures gesturesCleanup(); //Stop requesting events from libscreen screen_stop_events(screen_cxt); //Shut down BPS library for this process bps_shutdown(); //Destroy the font bbutil_destroy_font(font); //Use utility code to terminate EGL setup bbutil_terminate(); //Destroy libscreen context screen_destroy_context(screen_cxt); return 0; }