accelerometer.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * accelerometer.c
  3. * written by Holmes Futrell
  4. * use however you want
  5. */
  6. #include <SDL3/SDL.h>
  7. #include <SDL3/SDL_main.h>
  8. #include <math.h>
  9. #include "common.h"
  10. #define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
  11. #define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */
  12. #define GRAVITY_CONSTANT 0.004f /* how sensitive the ship is to the accelerometer */
  13. /* If we aren't on an iPhone, then this definition ought to yield reasonable behavior */
  14. #ifndef SDL_IPHONE_MAX_GFORCE
  15. #define SDL_IPHONE_MAX_GFORCE 5.0f
  16. #endif
  17. static SDL_Joystick *accelerometer; /* used for controlling the ship */
  18. static struct
  19. {
  20. float x, y; /* position of ship */
  21. float vx, vy; /* velocity of ship (in pixels per millesecond) */
  22. SDL_Rect rect; /* (drawn) position and size of ship */
  23. } shipData;
  24. static SDL_Texture *ship = 0; /* texture for spaceship */
  25. static SDL_Texture *space = 0; /* texture for space (background */
  26. void
  27. render(SDL_Renderer *renderer, int w, int h, double deltaTime)
  28. {
  29. double deltaMilliseconds = deltaTime * 1000;
  30. float speed;
  31. /* get joystick (accelerometer) axis values and normalize them */
  32. float ax = SDL_GetJoystickAxis(accelerometer, 0);
  33. float ay = SDL_GetJoystickAxis(accelerometer, 1);
  34. /* ship screen constraints */
  35. Uint32 minx = 0.0f;
  36. Uint32 maxx = w - shipData.rect.w;
  37. Uint32 miny = 0.0f;
  38. Uint32 maxy = h - shipData.rect.h;
  39. #define SINT16_MAX ((float)(0x7FFF))
  40. /* update velocity from accelerometer
  41. the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
  42. SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
  43. */
  44. shipData.vx +=
  45. ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
  46. deltaMilliseconds;
  47. shipData.vy +=
  48. ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT *
  49. deltaMilliseconds;
  50. speed = SDL_sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
  51. if (speed > 0) {
  52. /* compensate for friction */
  53. float dirx = shipData.vx / speed; /* normalized x velocity */
  54. float diry = shipData.vy / speed; /* normalized y velocity */
  55. /* update velocity due to friction */
  56. if (speed - FRICTION * deltaMilliseconds > 0) {
  57. /* apply friction */
  58. shipData.vx -= dirx * FRICTION * deltaMilliseconds;
  59. shipData.vy -= diry * FRICTION * deltaMilliseconds;
  60. } else {
  61. /* applying friction would MORE than stop the ship, so just stop the ship */
  62. shipData.vx = 0.0f;
  63. shipData.vy = 0.0f;
  64. }
  65. }
  66. /* update ship location */
  67. shipData.x += shipData.vx * deltaMilliseconds;
  68. shipData.y += shipData.vy * deltaMilliseconds;
  69. if (shipData.x > maxx) {
  70. shipData.x = maxx;
  71. shipData.vx = -shipData.vx * DAMPING;
  72. } else if (shipData.x < minx) {
  73. shipData.x = minx;
  74. shipData.vx = -shipData.vx * DAMPING;
  75. }
  76. if (shipData.y > maxy) {
  77. shipData.y = maxy;
  78. shipData.vy = -shipData.vy * DAMPING;
  79. } else if (shipData.y < miny) {
  80. shipData.y = miny;
  81. shipData.vy = -shipData.vy * DAMPING;
  82. }
  83. /* draw the background */
  84. SDL_RenderTexture(renderer, space, NULL, NULL);
  85. /* draw the ship */
  86. shipData.rect.x = shipData.x;
  87. shipData.rect.y = shipData.y;
  88. SDL_RenderTexture(renderer, ship, NULL, &shipData.rect);
  89. /* update screen */
  90. SDL_RenderPresent(renderer);
  91. }
  92. void
  93. initializeTextures(SDL_Renderer *renderer)
  94. {
  95. SDL_Surface *bmp_surface;
  96. /* load the ship */
  97. bmp_surface = SDL_LoadBMP("ship.bmp");
  98. if (bmp_surface == NULL) {
  99. fatalError("could not ship.bmp");
  100. }
  101. /* set blue to transparent on the ship */
  102. SDL_SetSurfaceColorKey(bmp_surface, 1,
  103. SDL_MapRGB(bmp_surface->format, 0, 0, 255));
  104. /* create ship texture from surface */
  105. ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
  106. if (ship == NULL) {
  107. fatalError("could not create ship texture");
  108. }
  109. SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND);
  110. /* set the width and height of the ship from the surface dimensions */
  111. shipData.rect.w = bmp_surface->w;
  112. shipData.rect.h = bmp_surface->h;
  113. SDL_DestroySurface(bmp_surface);
  114. /* load the space background */
  115. bmp_surface = SDL_LoadBMP("space.bmp");
  116. if (bmp_surface == NULL) {
  117. fatalError("could not load space.bmp");
  118. }
  119. /* create space texture from surface */
  120. space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
  121. if (space == NULL) {
  122. fatalError("could not create space texture");
  123. }
  124. SDL_DestroySurface(bmp_surface);
  125. }
  126. int
  127. main(int argc, char *argv[])
  128. {
  129. SDL_Window *window; /* main window */
  130. SDL_Renderer *renderer;
  131. int done; /* should we clean up and exit? */
  132. int w, h;
  133. /* initialize SDL */
  134. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
  135. fatalError("Could not initialize SDL");
  136. }
  137. /* create main window and renderer */
  138. window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
  139. renderer = SDL_CreateRenderer(window, NULL, 0);
  140. SDL_GetWindowSize(window, &w, &h);
  141. SDL_SetRenderLogicalSize(renderer, w, h);
  142. /* print out some info about joysticks and try to open accelerometer for use */
  143. printf("There are %d joysticks available\n", SDL_GetNumJoysticks());
  144. printf("Default joystick (index 0) is %s\n", SDL_GetJoystickName(0));
  145. accelerometer = SDL_OpenJoystick(0);
  146. if (accelerometer == NULL) {
  147. fatalError("Could not open joystick (accelerometer)");
  148. }
  149. printf("joystick number of axis = %d\n",
  150. SDL_GetNumJoystickAxes(accelerometer));
  151. printf("joystick number of hats = %d\n",
  152. SDL_GetNumJoystickHats(accelerometer));
  153. printf("joystick number of buttons = %d\n",
  154. SDL_GetNumJoystickButtons(accelerometer));
  155. /* load graphics */
  156. initializeTextures(renderer);
  157. /* setup ship */
  158. shipData.x = (w - shipData.rect.w) / 2;
  159. shipData.y = (h - shipData.rect.h) / 2;
  160. shipData.vx = 0.0f;
  161. shipData.vy = 0.0f;
  162. done = 0;
  163. /* enter main loop */
  164. while (!done) {
  165. double deltaTime = updateDeltaTime();
  166. SDL_Event event;
  167. while (SDL_PollEvent(&event)) {
  168. if (event.type == SDL_QUIT) {
  169. done = 1;
  170. }
  171. }
  172. render(renderer, w, h, deltaTime);
  173. SDL_Delay(1);
  174. }
  175. /* delete textures */
  176. SDL_DestroyTexture(ship);
  177. SDL_DestroyTexture(space);
  178. /* shutdown SDL */
  179. SDL_Quit();
  180. return 0;
  181. }