happy.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
  10. #define HAPPY_FACE_SIZE 32 /* width and height of happyface (pixels) */
  11. static SDL_Texture *texture = 0; /* reference to texture holding happyface */
  12. static struct
  13. {
  14. float x, y; /* position of happyface */
  15. float xvel, yvel; /* velocity of happyface */
  16. } faces[NUM_HAPPY_FACES];
  17. /*
  18. Sets initial positions and velocities of happyfaces
  19. units of velocity are pixels per millesecond
  20. */
  21. void
  22. initializeHappyFaces()
  23. {
  24. int i;
  25. for (i = 0; i < NUM_HAPPY_FACES; i++) {
  26. faces[i].x = randomFloat(0.0f, SCREEN_WIDTH - HAPPY_FACE_SIZE);
  27. faces[i].y = randomFloat(0.0f, SCREEN_HEIGHT - HAPPY_FACE_SIZE);
  28. faces[i].xvel = randomFloat(-0.1f, 0.1f);
  29. faces[i].yvel = randomFloat(-0.1f, 0.1f);
  30. }
  31. }
  32. void
  33. render(SDL_Renderer *renderer)
  34. {
  35. int i;
  36. SDL_Rect srcRect;
  37. SDL_Rect dstRect;
  38. /* setup boundaries for happyface bouncing */
  39. Uint16 maxx = SCREEN_WIDTH - HAPPY_FACE_SIZE;
  40. Uint16 maxy = SCREEN_HEIGHT - HAPPY_FACE_SIZE;
  41. Uint16 minx = 0;
  42. Uint16 miny = 0;
  43. /* setup rects for drawing */
  44. srcRect.x = 0;
  45. srcRect.y = 0;
  46. srcRect.w = HAPPY_FACE_SIZE;
  47. srcRect.h = HAPPY_FACE_SIZE;
  48. dstRect.w = HAPPY_FACE_SIZE;
  49. dstRect.h = HAPPY_FACE_SIZE;
  50. /* fill background in with black */
  51. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  52. SDL_RenderClear(renderer);
  53. /*
  54. loop through all the happy faces:
  55. - update position
  56. - update velocity (if boundary is hit)
  57. - draw
  58. */
  59. for (i = 0; i < NUM_HAPPY_FACES; i++) {
  60. faces[i].x += faces[i].xvel * MILLESECONDS_PER_FRAME;
  61. faces[i].y += faces[i].yvel * MILLESECONDS_PER_FRAME;
  62. if (faces[i].x > maxx) {
  63. faces[i].x = maxx;
  64. faces[i].xvel = -faces[i].xvel;
  65. } else if (faces[i].y > maxy) {
  66. faces[i].y = maxy;
  67. faces[i].yvel = -faces[i].yvel;
  68. }
  69. if (faces[i].x < minx) {
  70. faces[i].x = minx;
  71. faces[i].xvel = -faces[i].xvel;
  72. } else if (faces[i].y < miny) {
  73. faces[i].y = miny;
  74. faces[i].yvel = -faces[i].yvel;
  75. }
  76. dstRect.x = faces[i].x;
  77. dstRect.y = faces[i].y;
  78. SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
  79. }
  80. /* update screen */
  81. SDL_RenderPresent(renderer);
  82. }
  83. /*
  84. loads the happyface graphic into a texture
  85. */
  86. void
  87. initializeTexture(SDL_Renderer *renderer)
  88. {
  89. SDL_Surface *bmp_surface;
  90. /* load the bmp */
  91. bmp_surface = SDL_LoadBMP("icon.bmp");
  92. if (bmp_surface == NULL) {
  93. fatalError("could not load bmp");
  94. }
  95. /* set white to transparent on the happyface */
  96. SDL_SetColorKey(bmp_surface, 1,
  97. SDL_MapRGB(bmp_surface->format, 255, 255, 255));
  98. /* convert RGBA surface to texture */
  99. texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
  100. if (texture == 0) {
  101. fatalError("could not create texture");
  102. }
  103. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  104. /* free up allocated memory */
  105. SDL_FreeSurface(bmp_surface);
  106. }
  107. int
  108. main(int argc, char *argv[])
  109. {
  110. SDL_Window *window;
  111. SDL_Renderer *renderer;
  112. Uint32 startFrame;
  113. Uint32 endFrame;
  114. Uint32 delay;
  115. int done;
  116. /* initialize SDL */
  117. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  118. fatalError("Could not initialize SDL");
  119. }
  120. window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
  121. SDL_WINDOW_OPENGL |
  122. SDL_WINDOW_BORDERLESS);
  123. renderer = SDL_CreateRenderer(window, -1, 0);
  124. initializeTexture(renderer);
  125. initializeHappyFaces();
  126. /* main loop */
  127. done = 0;
  128. while (!done) {
  129. startFrame = SDL_GetTicks();
  130. SDL_Event event;
  131. while (SDL_PollEvent(&event)) {
  132. if (event.type == SDL_QUIT) {
  133. done = 1;
  134. }
  135. }
  136. render(renderer);
  137. endFrame = SDL_GetTicks();
  138. /* figure out how much time we have left, and then sleep */
  139. delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
  140. if (delay < 0) {
  141. delay = 0;
  142. } else if (delay > MILLESECONDS_PER_FRAME) {
  143. delay = MILLESECONDS_PER_FRAME;
  144. }
  145. SDL_Delay(delay);
  146. }
  147. /* cleanup */
  148. SDL_DestroyTexture(texture);
  149. /* shutdown SDL */
  150. SDL_Quit();
  151. return 0;
  152. }