Xcode-iOS/Demos/src/accelerometer.c
changeset 10138 4a8b649fee5b
parent 7880 b8d30d14d535
equal deleted inserted replaced
10137:406f82002892 10138:4a8b649fee5b
    32 
    32 
    33 void
    33 void
    34 render(SDL_Renderer *renderer, int w, int h)
    34 render(SDL_Renderer *renderer, int w, int h)
    35 {
    35 {
    36 
    36 
       
    37     float speed;
    37 
    38 
    38     /* get joystick (accelerometer) axis values and normalize them */
    39     /* get joystick (accelerometer) axis values and normalize them */
    39     float ax = SDL_JoystickGetAxis(accelerometer, 0);
    40     float ax = SDL_JoystickGetAxis(accelerometer, 0);
    40     float ay = SDL_JoystickGetAxis(accelerometer, 1);
    41     float ay = SDL_JoystickGetAxis(accelerometer, 1);
    41 
    42 
    56         MILLESECONDS_PER_FRAME;
    57         MILLESECONDS_PER_FRAME;
    57     shipData.vy +=
    58     shipData.vy +=
    58         ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
    59         ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
    59         MILLESECONDS_PER_FRAME;
    60         MILLESECONDS_PER_FRAME;
    60 
    61 
    61     float speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
    62     speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
    62 
    63 
    63     if (speed > 0) {
    64     if (speed > 0) {
    64         /* compensate for friction */
    65         /* compensate for friction */
    65         float dirx = shipData.vx / speed;   /* normalized x velocity */
    66         float dirx = shipData.vx / speed;   /* normalized x velocity */
    66         float diry = shipData.vy / speed;   /* normalized y velocity */
    67         float diry = shipData.vy / speed;   /* normalized y velocity */
   205     shipData.vy = 0.0f;
   206     shipData.vy = 0.0f;
   206 
   207 
   207     done = 0;
   208     done = 0;
   208     /* enter main loop */
   209     /* enter main loop */
   209     while (!done) {
   210     while (!done) {
       
   211         SDL_Event event;
   210         startFrame = SDL_GetTicks();
   212         startFrame = SDL_GetTicks();
   211         SDL_Event event;
       
   212         while (SDL_PollEvent(&event)) {
   213         while (SDL_PollEvent(&event)) {
   213             if (event.type == SDL_QUIT) {
   214             if (event.type == SDL_QUIT) {
   214                 done = 1;
   215                 done = 1;
   215             }
   216             }
   216         }
   217         }