Xcode-iOS/Demos/src/accelerometer.c
changeset 7861 29d012c8f2a9
parent 7497 109f37bca7cc
child 7880 b8d30d14d535
equal deleted inserted replaced
7860:2b0bcdea3a79 7861:29d012c8f2a9
    29 
    29 
    30 static SDL_Texture *ship = 0;        /* texture for spaceship */
    30 static SDL_Texture *ship = 0;        /* texture for spaceship */
    31 static SDL_Texture *space = 0;       /* texture for space (background */
    31 static SDL_Texture *space = 0;       /* texture for space (background */
    32 
    32 
    33 void
    33 void
    34 render(SDL_Renderer *renderer)
    34 render(SDL_Renderer *renderer, int w, int h)
    35 {
    35 {
    36 
    36 
    37 
    37 
    38     /* get joystick (accelerometer) axis values and normalize them */
    38     /* get joystick (accelerometer) axis values and normalize them */
    39     float ax = SDL_JoystickGetAxis(accelerometer, 0);
    39     float ax = SDL_JoystickGetAxis(accelerometer, 0);
    40     float ay = -SDL_JoystickGetAxis(accelerometer, 1);
    40     float ay = SDL_JoystickGetAxis(accelerometer, 1);
    41 
    41 
    42     /* ship screen constraints */
    42     /* ship screen constraints */
    43     Uint32 minx = 0.0f;
    43     Uint32 minx = 0.0f;
    44     Uint32 maxx = SCREEN_WIDTH - shipData.rect.w;
    44     Uint32 maxx = w - shipData.rect.w;
    45     Uint32 miny = 0.0f;
    45     Uint32 miny = 0.0f;
    46     Uint32 maxy = SCREEN_HEIGHT - shipData.rect.h;
    46     Uint32 maxy = h - shipData.rect.h;
    47 
    47 
    48 #define SINT16_MAX ((float)(0x7FFF))
    48 #define SINT16_MAX ((float)(0x7FFF))
    49 
    49 
    50     /* update velocity from accelerometer
    50     /* update velocity from accelerometer
    51        the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
    51        the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
   160 
   160 
   161     SDL_Window *window;         /* main window */
   161     SDL_Window *window;         /* main window */
   162     SDL_Renderer *renderer;
   162     SDL_Renderer *renderer;
   163     Uint32 startFrame;          /* time frame began to process */
   163     Uint32 startFrame;          /* time frame began to process */
   164     Uint32 endFrame;            /* time frame ended processing */
   164     Uint32 endFrame;            /* time frame ended processing */
   165     Uint32 delay;               /* time to pause waiting to draw next frame */
   165     Sint32 delay;               /* time to pause waiting to draw next frame */
   166     int done;                   /* should we clean up and exit? */
   166     int done;                   /* should we clean up and exit? */
       
   167     int w, h;
   167 
   168 
   168     /* initialize SDL */
   169     /* initialize SDL */
   169     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   170     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   170         fatalError("Could not initialize SDL");
   171         fatalError("Could not initialize SDL");
   171     }
   172     }
   173     /* create main window and renderer */
   174     /* create main window and renderer */
   174     window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
   175     window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
   175                                 SDL_WINDOW_OPENGL |
   176                                 SDL_WINDOW_OPENGL |
   176                                 SDL_WINDOW_BORDERLESS);
   177                                 SDL_WINDOW_BORDERLESS);
   177     renderer = SDL_CreateRenderer(window, 0, 0);
   178     renderer = SDL_CreateRenderer(window, 0, 0);
       
   179     
       
   180     SDL_GetWindowSize(window, &w, &h);
   178 
   181 
   179     /* print out some info about joysticks and try to open accelerometer for use */
   182     /* print out some info about joysticks and try to open accelerometer for use */
   180     printf("There are %d joysticks available\n", SDL_NumJoysticks());
   183     printf("There are %d joysticks available\n", SDL_NumJoysticks());
   181     printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
   184     printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
   182     accelerometer = SDL_JoystickOpen(0);
   185     accelerometer = SDL_JoystickOpen(0);
   194 
   197 
   195     /* load graphics */
   198     /* load graphics */
   196     initializeTextures(renderer);
   199     initializeTextures(renderer);
   197 
   200 
   198     /* setup ship */
   201     /* setup ship */
   199     shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
   202     shipData.x = (w - shipData.rect.w) / 2;
   200     shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2;
   203     shipData.y = (h - shipData.rect.h) / 2;
   201     shipData.vx = 0.0f;
   204     shipData.vx = 0.0f;
   202     shipData.vy = 0.0f;
   205     shipData.vy = 0.0f;
   203 
   206 
   204     done = 0;
   207     done = 0;
   205     /* enter main loop */
   208     /* enter main loop */
   209         while (SDL_PollEvent(&event)) {
   212         while (SDL_PollEvent(&event)) {
   210             if (event.type == SDL_QUIT) {
   213             if (event.type == SDL_QUIT) {
   211                 done = 1;
   214                 done = 1;
   212             }
   215             }
   213         }
   216         }
   214         render(renderer);
   217         render(renderer, w, h);
   215         endFrame = SDL_GetTicks();
   218         endFrame = SDL_GetTicks();
   216 
   219 
   217         /* figure out how much time we have left, and then sleep */
   220         /* figure out how much time we have left, and then sleep */
   218         delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
   221         delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
   219         if (delay < 0) {
   222         if (delay < 0) {