Xcode-iPhoneOS/Demos/src/accelerometer.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Mar 2011 11:17:11 -0700
changeset 5483 ccec742c9b7d
parent 5208 78db79f5a4e2
permissions -rw-r--r--
Removed unreferenced variable
     1 /*
     2  *	accelerometer.c
     3  *	written by Holmes Futrell
     4  *	use however you want
     5  */
     6 
     7 #include "SDL.h"
     8 #include "math.h"
     9 #include "common.h"
    10 
    11 #define MILLESECONDS_PER_FRAME 16       /* about 60 frames per second */
    12 #define DAMPING 0.5f;           /* after bouncing off a wall, damping coefficient determines final speed */
    13 #define FRICTION 0.0008f        /* coefficient of acceleration that opposes direction of motion */
    14 #define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
    15 
    16 /*	If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
    17 #ifndef SDL_IPHONE_MAX_GFORCE
    18 #define SDL_IPHONE_MAX_GFORCE 5.0f
    19 #endif
    20 
    21 static SDL_Joystick *accelerometer;     /* used for controlling the ship */
    22 
    23 static struct
    24 {
    25     float x, y;                 /* position of ship */
    26     float vx, vy;               /* velocity of ship (in pixels per millesecond) */
    27     SDL_Rect rect;              /* (drawn) position and size of ship */
    28 } shipData;
    29 
    30 static SDL_Texture *ship = 0;        /* texture for spaceship */
    31 static SDL_Texture *space = 0;       /* texture for space (background */
    32 
    33 void
    34 render(SDL_Renderer *renderer)
    35 {
    36 
    37 
    38     /* get joystick (accelerometer) axis values and normalize them */
    39     float ax = SDL_JoystickGetAxis(accelerometer, 0);
    40     float ay = -SDL_JoystickGetAxis(accelerometer, 1);
    41 
    42     /* ship screen constraints */
    43     Uint32 minx = 0.0f;
    44     Uint32 maxx = SCREEN_WIDTH - shipData.rect.w;
    45     Uint32 miny = 0.0f;
    46     Uint32 maxy = SCREEN_HEIGHT - shipData.rect.h;
    47 
    48 #define SINT16_MAX ((float)(0x7FFF))
    49 
    50     /* update velocity from accelerometer
    51        the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between 
    52        SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
    53      */
    54     shipData.vx +=
    55         ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
    56         MILLESECONDS_PER_FRAME;
    57     shipData.vy +=
    58         ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
    59         MILLESECONDS_PER_FRAME;
    60 
    61     float speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
    62 
    63     if (speed > 0) {
    64         /* compensate for friction */
    65         float dirx = shipData.vx / speed;   /* normalized x velocity */
    66         float diry = shipData.vy / speed;   /* normalized y velocity */
    67 
    68         /* update velocity due to friction */
    69         if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) {
    70             /* apply friction */
    71             shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME;
    72             shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME;
    73         } else {
    74             /* applying friction would MORE than stop the ship, so just stop the ship */
    75             shipData.vx = 0.0f;
    76             shipData.vy = 0.0f;
    77         }
    78     }
    79 
    80     /* update ship location */
    81     shipData.x += shipData.vx * MILLESECONDS_PER_FRAME;
    82     shipData.y += shipData.vy * MILLESECONDS_PER_FRAME;
    83 
    84     if (shipData.x > maxx) {
    85         shipData.x = maxx;
    86         shipData.vx = -shipData.vx * DAMPING;
    87     } else if (shipData.x < minx) {
    88         shipData.x = minx;
    89         shipData.vx = -shipData.vx * DAMPING;
    90     }
    91     if (shipData.y > maxy) {
    92         shipData.y = maxy;
    93         shipData.vy = -shipData.vy * DAMPING;
    94     } else if (shipData.y < miny) {
    95         shipData.y = miny;
    96         shipData.vy = -shipData.vy * DAMPING;
    97     }
    98 
    99     /* draw the background */
   100     SDL_RenderCopy(renderer, space, NULL, NULL);
   101 
   102     /* draw the ship */
   103     shipData.rect.x = shipData.x;
   104     shipData.rect.y = shipData.y;
   105 
   106     SDL_RenderCopy(renderer, ship, NULL, &shipData.rect);
   107 
   108     /* update screen */
   109     SDL_RenderPresent(renderer);
   110 
   111 }
   112 
   113 void
   114 initializeTextures(SDL_Renderer *renderer)
   115 {
   116 
   117     SDL_Surface *bmp_surface;
   118 
   119     /* load the ship */
   120     bmp_surface = SDL_LoadBMP("ship.bmp");
   121     if (bmp_surface == NULL) {
   122         fatalError("could not ship.bmp");
   123     }
   124     /* set blue to transparent on the ship */
   125     SDL_SetColorKey(bmp_surface, 1,
   126                     SDL_MapRGB(bmp_surface->format, 0, 0, 255));
   127 
   128     /* create ship texture from surface */
   129     ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
   130     if (ship == 0) {
   131         fatalError("could not create ship texture");
   132     }
   133     SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND);
   134 
   135     /* set the width and height of the ship from the surface dimensions */
   136     shipData.rect.w = bmp_surface->w;
   137     shipData.rect.h = bmp_surface->h;
   138 
   139     SDL_FreeSurface(bmp_surface);
   140 
   141     /* load the space background */
   142     bmp_surface = SDL_LoadBMP("space.bmp");
   143     if (bmp_surface == NULL) {
   144         fatalError("could not load space.bmp");
   145     }
   146     /* create space texture from surface */
   147     space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
   148     if (space == 0) {
   149         fatalError("could not create space texture");
   150     }
   151     SDL_FreeSurface(bmp_surface);
   152 
   153 }
   154 
   155 
   156 
   157 int
   158 main(int argc, char *argv[])
   159 {
   160 
   161     SDL_Window *window;         /* main window */
   162 	SDL_Renderer *renderer;
   163     Uint32 startFrame;          /* time frame began to process */
   164     Uint32 endFrame;            /* time frame ended processing */
   165     Uint32 delay;               /* time to pause waiting to draw next frame */
   166     int done;                   /* should we clean up and exit? */
   167 
   168     /* initialize SDL */
   169     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
   170         fatalError("Could not initialize SDL");
   171     }
   172 
   173     /* create main window and renderer */
   174     window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
   175                                 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
   176                                 SDL_WINDOW_BORDERLESS);
   177     renderer = SDL_CreateRenderer(window, 0, 0);
   178 
   179     /* print out some info about joysticks and try to open accelerometer for use */
   180     printf("There are %d joysticks available\n", SDL_NumJoysticks());
   181     printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
   182     accelerometer = SDL_JoystickOpen(0);
   183     if (accelerometer == NULL) {
   184         fatalError("Could not open joystick (accelerometer)");
   185     }
   186     printf("joystick number of axis = %d\n",
   187            SDL_JoystickNumAxes(accelerometer));
   188     printf("joystick number of hats = %d\n",
   189            SDL_JoystickNumHats(accelerometer));
   190     printf("joystick number of balls = %d\n",
   191            SDL_JoystickNumBalls(accelerometer));
   192     printf("joystick number of buttons = %d\n",
   193            SDL_JoystickNumButtons(accelerometer));
   194 
   195     /* load graphics */
   196     initializeTextures(renderer);
   197 
   198     /* setup ship */
   199     shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
   200     shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2;
   201     shipData.vx = 0.0f;
   202     shipData.vy = 0.0f;
   203 
   204     done = 0;
   205     /* enter main loop */
   206     while (!done) {
   207         startFrame = SDL_GetTicks();
   208         SDL_Event event;
   209         while (SDL_PollEvent(&event)) {
   210             if (event.type == SDL_QUIT) {
   211                 done = 1;
   212             }
   213         }
   214         render(renderer);
   215         endFrame = SDL_GetTicks();
   216 
   217         /* figure out how much time we have left, and then sleep */
   218         delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
   219         if (delay < 0) {
   220             delay = 0;
   221         } else if (delay > MILLESECONDS_PER_FRAME) {
   222             delay = MILLESECONDS_PER_FRAME;
   223         }
   224         SDL_Delay(delay);
   225     }
   226 
   227     /* delete textures */
   228     SDL_DestroyTexture(ship);
   229     SDL_DestroyTexture(space);
   230 
   231     /* shutdown SDL */
   232     SDL_Quit();
   233 
   234     return 0;
   235 
   236 }