happy.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * happy.c
  3. * written by Holmes Futrell
  4. * use however you want
  5. */
  6. #include "SDL.h"
  7. #include "common.h"
  8. #define NUM_HAPPY_FACES 100 /* number of faces to draw */
  9. #define HAPPY_FACE_SIZE 32 /* width and height of happyface */
  10. static SDL_Texture *texture = 0; /* reference to texture holding happyface */
  11. static struct
  12. {
  13. float x, y; /* position of happyface */
  14. float xvel, yvel; /* velocity of happyface */
  15. } faces[NUM_HAPPY_FACES];
  16. /*
  17. Sets initial positions and velocities of happyfaces
  18. units of velocity are pixels per millesecond
  19. */
  20. void
  21. initializeHappyFaces(SDL_Renderer *renderer)
  22. {
  23. int i;
  24. int w;
  25. int h;
  26. SDL_RenderGetLogicalSize(renderer, &w, &h);
  27. for (i = 0; i < NUM_HAPPY_FACES; i++) {
  28. faces[i].x = randomFloat(0.0f, w - HAPPY_FACE_SIZE);
  29. faces[i].y = randomFloat(0.0f, h - HAPPY_FACE_SIZE);
  30. faces[i].xvel = randomFloat(-60.0f, 60.0f);
  31. faces[i].yvel = randomFloat(-60.0f, 60.0f);
  32. }
  33. }
  34. void
  35. render(SDL_Renderer *renderer, double deltaTime)
  36. {
  37. int i;
  38. SDL_Rect srcRect;
  39. SDL_Rect dstRect;
  40. int w;
  41. int h;
  42. SDL_RenderGetLogicalSize(renderer, &w, &h);
  43. /* setup boundaries for happyface bouncing */
  44. int maxx = w - HAPPY_FACE_SIZE;
  45. int maxy = h - HAPPY_FACE_SIZE;
  46. int minx = 0;
  47. int miny = 0;
  48. /* setup rects for drawing */
  49. srcRect.x = 0;
  50. srcRect.y = 0;
  51. srcRect.w = HAPPY_FACE_SIZE;
  52. srcRect.h = HAPPY_FACE_SIZE;
  53. dstRect.w = HAPPY_FACE_SIZE;
  54. dstRect.h = HAPPY_FACE_SIZE;
  55. /* fill background in with black */
  56. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  57. SDL_RenderClear(renderer);
  58. /*
  59. loop through all the happy faces:
  60. - update position
  61. - update velocity (if boundary is hit)
  62. - draw
  63. */
  64. for (i = 0; i < NUM_HAPPY_FACES; i++) {
  65. faces[i].x += faces[i].xvel * deltaTime;
  66. faces[i].y += faces[i].yvel * deltaTime;
  67. if (faces[i].x > maxx) {
  68. faces[i].x = maxx;
  69. faces[i].xvel = -faces[i].xvel;
  70. } else if (faces[i].y > maxy) {
  71. faces[i].y = maxy;
  72. faces[i].yvel = -faces[i].yvel;
  73. }
  74. if (faces[i].x < minx) {
  75. faces[i].x = minx;
  76. faces[i].xvel = -faces[i].xvel;
  77. } else if (faces[i].y < miny) {
  78. faces[i].y = miny;
  79. faces[i].yvel = -faces[i].yvel;
  80. }
  81. dstRect.x = faces[i].x;
  82. dstRect.y = faces[i].y;
  83. SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
  84. }
  85. /* update screen */
  86. SDL_RenderPresent(renderer);
  87. }
  88. /*
  89. loads the happyface graphic into a texture
  90. */
  91. void
  92. initializeTexture(SDL_Renderer *renderer)
  93. {
  94. SDL_Surface *bmp_surface;
  95. /* load the bmp */
  96. bmp_surface = SDL_LoadBMP("icon.bmp");
  97. if (bmp_surface == NULL) {
  98. fatalError("could not load bmp");
  99. }
  100. /* set white to transparent on the happyface */
  101. SDL_SetColorKey(bmp_surface, 1,
  102. SDL_MapRGB(bmp_surface->format, 255, 255, 255));
  103. /* convert RGBA surface to texture */
  104. texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
  105. if (texture == 0) {
  106. fatalError("could not create texture");
  107. }
  108. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  109. /* free up allocated memory */
  110. SDL_FreeSurface(bmp_surface);
  111. }
  112. int
  113. main(int argc, char *argv[])
  114. {
  115. SDL_Window *window;
  116. SDL_Renderer *renderer;
  117. int done;
  118. int width;
  119. int height;
  120. /* initialize SDL */
  121. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  122. fatalError("Could not initialize SDL");
  123. }
  124. /* The specified window size doesn't matter - except for its aspect ratio,
  125. * which determines whether the window is in portrait or landscape on iOS
  126. * (if SDL_WINDOW_RESIZABLE isn't specified). */
  127. window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_ALLOW_HIGHDPI);
  128. renderer = SDL_CreateRenderer(window, -1, 0);
  129. SDL_GetWindowSize(window, &width, &height);
  130. SDL_RenderSetLogicalSize(renderer, width, height);
  131. initializeTexture(renderer);
  132. initializeHappyFaces(renderer);
  133. /* main loop */
  134. done = 0;
  135. while (!done) {
  136. SDL_Event event;
  137. double deltaTime = updateDeltaTime();
  138. while (SDL_PollEvent(&event)) {
  139. if (event.type == SDL_QUIT) {
  140. done = 1;
  141. }
  142. }
  143. render(renderer, deltaTime);
  144. SDL_Delay(1);
  145. }
  146. /* cleanup */
  147. SDL_DestroyTexture(texture);
  148. /* shutdown SDL */
  149. SDL_Quit();
  150. return 0;
  151. }