Exemplo n.º 1
0
void app_event_loop() {
    bool isRunning=true;
    SDL_Event event;
    uint32_t lastTick;
    int32_t delay;
    int i;
    struct tm* now;
    printf("[DEBUG] Got app_event_loop\n");
    while (isRunning) {
        lastTick=SDL_GetTicks ();
        SDL_PumpEvents ();
        while (SDL_PollEvent (&event)) {
            switch (event.type) {
            case(SDL_QUIT): {
                isRunning=false;
            }
            break;
            case(SDL_KEYDOWN): {
                switch (event.key.keysym.sym) {
                case(SDLK_ESCAPE): {
                    isRunning=false;
                }
                break;
                case(SDLK_F1): {
                    bodyID=(bodyID+1)%BODY_COUNT;
                    bodyRender=true;
                }
                break;
                case(SDLK_F2): {
                    shadow=!shadow;
                    markDirty(true);
                    bodyRender=true;
                }
                break;
                case(SDLK_F3): {
                    toggle_24h_style();
                    if (clock_is_24h_style ())
                        SDL_WM_SetCaption("Pebble Local Simulator - 24H Style",0);
                    else
                        SDL_WM_SetCaption("Pebble Local Simulator - 12H Style",0);
                }
                break;
                case(SDLK_F4): {
                    toggle_bluetooth_connection();
                    printf("[INFO] Toggle bluetooth %s\n", bluetooth_connection_service_peek() ? "ON":"OFF");
                }
                break;
                case(SDLK_F5): {
                    BatteryChargeState state;
                    toggle_battery_charger_plugged();
                    state = battery_state_service_peek();
                    printf("[INFO] Toggle plugged: %d%%, %s charging, %s plugged\n",
                           state.charge_percent, state.is_charging ? "":"not", state.is_plugged ? "":"not");
                }
                break;
                case(SDLK_F12): {
                    time_t timeSec=time(0);
                    now=localtime(&timeSec);
                    strftime (titleBuffer,strlen(titleBuffer),"./simdata/screenshots/%Y-%m-%e-%H-%M-%S",now);
                    strcat(titleBuffer,".bmp");
                    if (SDL_SaveBMP(screen,titleBuffer)<0)
                        printf("[WARN] SDL_SaveBMP: %s\n",SDL_GetError ());
                    else
                        printf ("[INFO] Saved screenshot: %s\n",titleBuffer);
                }
                break;
                case(SDLK_PLUS): {
                    BatteryChargeState state;
                    battery_charge_increase();
                    state = battery_state_service_peek();
                    printf("[INFO] Battery state: %d%%, %s charging, %s plugged\n",
                           state.charge_percent, state.is_charging ? "":"not", state.is_plugged ? "":"not");
                }
                break;
                case(SDLK_MINUS): {
                    BatteryChargeState state;
                    battery_charge_decrease();
                    state = battery_state_service_peek();
                    printf("[INFO] Battery state: %d%%, %s charging, %s plugged\n",
                           state.charge_percent, state.is_charging ? "":"not", state.is_plugged ? "":"not");
                }
                break;
                case (SDLK_x): {
                    int32_t direction=1;
                    if(event.key.keysym.mod & KMOD_SHIFT) {
                        direction=-1;
                    }
                    accel_do_tap_on_axis(ACCEL_AXIS_X, direction);
                    printf("[INFO] Tap X %d\n", direction);
                }
                break;
                case (SDLK_y): {
                    int32_t direction=1;
                    if(event.key.keysym.mod & KMOD_SHIFT) {
                        direction=-1;
                    }
                    accel_do_tap_on_axis(ACCEL_AXIS_Y, direction);
                    printf("[INFO] Tap Y %d\n", direction);
                }
                break;
                case (SDLK_z): {
                    int32_t direction=1;
                    if(event.key.keysym.mod & KMOD_SHIFT) {
                        direction=-1;
                    }
                    accel_do_tap_on_axis(ACCEL_AXIS_Z, direction);
                    printf("[INFO] Tap Z %d\n", direction);
                }
                break;
                case (KEY_BUTTON_BACK): {
                    buttonState[BUTTON_ID_BACK]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_BACK);
                }
                break;
                case (KEY_BUTTON_UP): {
                    buttonState[BUTTON_ID_UP]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_UP);
                }
                break;
                case (KEY_BUTTON_SELECT): {
                    buttonState[BUTTON_ID_SELECT]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_SELECT);
                }
                break;
                case (KEY_BUTTON_DOWN): {
                    buttonState[BUTTON_ID_DOWN]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_DOWN);
                }
                break;
                default:
                    ;
                }
            }
            break;
            case(SDL_KEYUP): {
                switch (event.key.keysym.sym) {
                case(KEY_BUTTON_BACK): {
                    buttonState[BUTTON_ID_BACK]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_BACK);
                }
                break;
                case(KEY_BUTTON_UP): {
                    buttonState[BUTTON_ID_UP]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_UP);
                }
                break;
                case(KEY_BUTTON_SELECT): {
                    buttonState[BUTTON_ID_SELECT]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_SELECT);
                }
                break;
                case(KEY_BUTTON_DOWN): {
                    buttonState[BUTTON_ID_DOWN]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_DOWN);
                }
                break;
                default:
                    ;
                }
            }
            break;
            default:
                ;
            }
        }

		for (i=0;i<SIM_SERVICE_COUNT;i++)
            (serviceData.services[i]) ();
        if (lastVibeState!=getVibeState()) {
            bodyRender=true;
            lastVibeState=getVibeState();
        }
        if (lastLightState!=getLightState()) {
            bodyRender=true;
            lastLightState=getLightState();
        }

        if (render ()||bodyRender)
            simulatorRender ();

        delay=(SDL_GetTicks()-lastTick);
        elapsed=delay/1000.0f;
        delay=16-delay; //equals about 60 frames per second
        lastTick=SDL_GetTicks();
        if (delay>0)
            SDL_Delay(delay);
    }
}
Exemplo n.º 2
0
void app_event_loop() {
    bool isRunning=true;
    SDL_Event event;
    uint32_t lastTick;
    int32_t delay;
    int i;
    struct tm* now;
    printf("[DEBUG] Got app_event_loop\n");
    while (isRunning) {
        lastTick=SDL_GetTicks ();
        SDL_PumpEvents ();
        while (SDL_PollEvent (&event)) {
            switch (event.type) {
            case(SDL_QUIT): {
                isRunning=false;
            }
            break;
            case(SDL_KEYDOWN): {
                switch (event.key.keysym.sym) {
                case(SDLK_ESCAPE): {
                    isRunning=false;
                }
                break;
                case(SDLK_F1): {
                    bodyID=(bodyID+1)%BODY_COUNT;
                    bodyRender=true;
                }
                break;
                case(SDLK_F2): {
                    shadow=!shadow;
                    markDirty(true);
                    bodyRender=true;
                }
                break;
                case(SDLK_F3): {
                    toggle_24h_style();
                    if (clock_is_24h_style ())
                        SDL_WM_SetCaption("Pebble Local Simulator - 24H Style",0);
                    else
                        SDL_WM_SetCaption("Pebble Local Simulator - 12H Style",0);
                }
                break;
                case(SDLK_F12): {
                    time_t timeSec=time(0);
                    now=localtime(&timeSec);
                    strftime (titleBuffer,strlen(titleBuffer),"./simdata/screenshots/%Y-%m-%e-%H-%M-%S",now);
                    strcat(titleBuffer,".bmp");
                    if (SDL_SaveBMP(screen,titleBuffer)<0)
                        printf("[WARN] SDL_SaveBMP: %s\n",SDL_GetError ());
                    else
                        printf ("[INFO] Saved screenshot: %s\n",titleBuffer);
                }
                break;
                case (KEY_BUTTON_BACK): {
                    buttonState[BUTTON_ID_BACK]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_BACK);
                }
                break;
                case (KEY_BUTTON_UP): {
                    buttonState[BUTTON_ID_UP]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_UP);
                }
                break;
                case (KEY_BUTTON_SELECT): {
                    buttonState[BUTTON_ID_SELECT]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_SELECT);
                }
                break;
                case (KEY_BUTTON_DOWN): {
                    buttonState[BUTTON_ID_DOWN]=true;
                    bodyRender=true;
                    onButtonDown(BUTTON_ID_DOWN);
                }
                break;
                default:
                    ;
                }
            }
            break;
            case(SDL_KEYUP): {
                switch (event.key.keysym.sym) {
                case(KEY_BUTTON_BACK): {
                    buttonState[BUTTON_ID_BACK]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_BACK);
                }
                break;
                case(KEY_BUTTON_UP): {
                    buttonState[BUTTON_ID_UP]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_UP);
                }
                break;
                case(KEY_BUTTON_SELECT): {
                    buttonState[BUTTON_ID_SELECT]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_SELECT);
                }
                break;
                case(KEY_BUTTON_DOWN): {
                    buttonState[BUTTON_ID_DOWN]=false;
                    bodyRender=true;
                    onButtonUp(BUTTON_ID_DOWN);
                }
                break;
                default:
                    ;
                }
            }
            break;
            default:
                ;
            }
        }

        for (i=0; i<SIM_SERVICE_COUNT; i++)
            (serviceData.services[i]) ();
        if (lastVibeState!=getVibeState()) {
            bodyRender=true;
            lastVibeState=getVibeState();
        }
        if (lastLightState!=getLightState()) {
            bodyRender=true;
            lastLightState=getLightState();
        }

        if (render ()||bodyRender)
            simulatorRender ();

        delay=(SDL_GetTicks()-lastTick);
        elapsed=delay/1000.0f;
        delay=16-delay; //equals about 60 frames per second
        lastTick=SDL_GetTicks();
        if (delay>0)
            SDL_Delay(delay);
    }
}