testspriteminimal.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely.
  9. */
  10. /* Simple program: Move N sprites around on the screen as fast as possible */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <time.h>
  14. #ifdef __EMSCRIPTEN__
  15. #include <emscripten/emscripten.h>
  16. #endif
  17. #include "SDL.h"
  18. #define WINDOW_WIDTH 640
  19. #define WINDOW_HEIGHT 480
  20. #define NUM_SPRITES 100
  21. #define MAX_SPEED 1
  22. static SDL_Texture *sprite;
  23. static SDL_Rect positions[NUM_SPRITES];
  24. static SDL_Rect velocities[NUM_SPRITES];
  25. static int sprite_w, sprite_h;
  26. SDL_Renderer *renderer;
  27. int done;
  28. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  29. static void
  30. quit(int rc)
  31. {
  32. exit(rc);
  33. }
  34. int
  35. LoadSprite(char *file, SDL_Renderer *renderer)
  36. {
  37. SDL_Surface *temp;
  38. /* Load the sprite image */
  39. temp = SDL_LoadBMP(file);
  40. if (temp == NULL) {
  41. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
  42. return (-1);
  43. }
  44. sprite_w = temp->w;
  45. sprite_h = temp->h;
  46. /* Set transparent pixel as the pixel at (0,0) */
  47. if (temp->format->palette) {
  48. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  49. } else {
  50. switch (temp->format->BitsPerPixel) {
  51. case 15:
  52. SDL_SetColorKey(temp, SDL_TRUE,
  53. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  54. break;
  55. case 16:
  56. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  57. break;
  58. case 24:
  59. SDL_SetColorKey(temp, SDL_TRUE,
  60. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  61. break;
  62. case 32:
  63. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  64. break;
  65. }
  66. }
  67. /* Create textures from the image */
  68. sprite = SDL_CreateTextureFromSurface(renderer, temp);
  69. if (!sprite) {
  70. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  71. SDL_FreeSurface(temp);
  72. return (-1);
  73. }
  74. SDL_FreeSurface(temp);
  75. /* We're ready to roll. :) */
  76. return (0);
  77. }
  78. void
  79. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  80. {
  81. int i;
  82. int window_w = WINDOW_WIDTH;
  83. int window_h = WINDOW_HEIGHT;
  84. SDL_Rect *position, *velocity;
  85. /* Draw a gray background */
  86. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  87. SDL_RenderClear(renderer);
  88. /* Move the sprite, bounce at the wall, and draw */
  89. for (i = 0; i < NUM_SPRITES; ++i) {
  90. position = &positions[i];
  91. velocity = &velocities[i];
  92. position->x += velocity->x;
  93. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  94. velocity->x = -velocity->x;
  95. position->x += velocity->x;
  96. }
  97. position->y += velocity->y;
  98. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  99. velocity->y = -velocity->y;
  100. position->y += velocity->y;
  101. }
  102. /* Blit the sprite onto the screen */
  103. SDL_RenderCopy(renderer, sprite, NULL, position);
  104. }
  105. /* Update the screen! */
  106. SDL_RenderPresent(renderer);
  107. }
  108. void loop()
  109. {
  110. SDL_Event event;
  111. /* Check for events */
  112. while (SDL_PollEvent(&event)) {
  113. if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
  114. done = 1;
  115. }
  116. }
  117. MoveSprites(renderer, sprite);
  118. #ifdef __EMSCRIPTEN__
  119. if (done) {
  120. emscripten_cancel_main_loop();
  121. }
  122. #endif
  123. }
  124. int
  125. main(int argc, char *argv[])
  126. {
  127. SDL_Window *window;
  128. int i;
  129. /* Enable standard application logging */
  130. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  131. if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
  132. quit(2);
  133. }
  134. if (LoadSprite("icon.bmp", renderer) < 0) {
  135. quit(2);
  136. }
  137. /* Initialize the sprite positions */
  138. srand(time(NULL));
  139. for (i = 0; i < NUM_SPRITES; ++i) {
  140. positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
  141. positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
  142. positions[i].w = sprite_w;
  143. positions[i].h = sprite_h;
  144. velocities[i].x = 0;
  145. velocities[i].y = 0;
  146. while (!velocities[i].x && !velocities[i].y) {
  147. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  148. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  149. }
  150. }
  151. /* Main render loop */
  152. done = 0;
  153. #ifdef __EMSCRIPTEN__
  154. emscripten_set_main_loop(loop, 0, 1);
  155. #else
  156. while (!done) {
  157. loop();
  158. }
  159. #endif
  160. quit(0);
  161. return 0; /* to prevent compiler warning */
  162. }
  163. /* vi: set ts=4 sw=4 expandtab: */