🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

195 lines
5.0 KiB

  1. /*
  2. Copyright (C) 1997-2020 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. SDL_Quit();
  33. exit(rc);
  34. }
  35. int
  36. LoadSprite(char *file, SDL_Renderer *renderer)
  37. {
  38. SDL_Surface *temp;
  39. /* Load the sprite image */
  40. temp = SDL_LoadBMP(file);
  41. if (temp == NULL) {
  42. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
  43. return (-1);
  44. }
  45. sprite_w = temp->w;
  46. sprite_h = temp->h;
  47. /* Set transparent pixel as the pixel at (0,0) */
  48. if (temp->format->palette) {
  49. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  50. } else {
  51. switch (temp->format->BitsPerPixel) {
  52. case 15:
  53. SDL_SetColorKey(temp, SDL_TRUE,
  54. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  55. break;
  56. case 16:
  57. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  58. break;
  59. case 24:
  60. SDL_SetColorKey(temp, SDL_TRUE,
  61. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  62. break;
  63. case 32:
  64. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  65. break;
  66. }
  67. }
  68. /* Create textures from the image */
  69. sprite = SDL_CreateTextureFromSurface(renderer, temp);
  70. if (!sprite) {
  71. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  72. SDL_FreeSurface(temp);
  73. return (-1);
  74. }
  75. SDL_FreeSurface(temp);
  76. /* We're ready to roll. :) */
  77. return (0);
  78. }
  79. void
  80. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  81. {
  82. int i;
  83. int window_w = WINDOW_WIDTH;
  84. int window_h = WINDOW_HEIGHT;
  85. SDL_Rect *position, *velocity;
  86. /* Draw a gray background */
  87. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  88. SDL_RenderClear(renderer);
  89. /* Move the sprite, bounce at the wall, and draw */
  90. for (i = 0; i < NUM_SPRITES; ++i) {
  91. position = &positions[i];
  92. velocity = &velocities[i];
  93. position->x += velocity->x;
  94. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  95. velocity->x = -velocity->x;
  96. position->x += velocity->x;
  97. }
  98. position->y += velocity->y;
  99. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  100. velocity->y = -velocity->y;
  101. position->y += velocity->y;
  102. }
  103. /* Blit the sprite onto the screen */
  104. SDL_RenderCopy(renderer, sprite, NULL, position);
  105. }
  106. /* Update the screen! */
  107. SDL_RenderPresent(renderer);
  108. }
  109. void loop()
  110. {
  111. SDL_Event event;
  112. /* Check for events */
  113. while (SDL_PollEvent(&event)) {
  114. if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
  115. done = 1;
  116. }
  117. }
  118. MoveSprites(renderer, sprite);
  119. #ifdef __EMSCRIPTEN__
  120. if (done) {
  121. emscripten_cancel_main_loop();
  122. }
  123. #endif
  124. }
  125. int
  126. main(int argc, char *argv[])
  127. {
  128. SDL_Window *window;
  129. int i;
  130. /* Enable standard application logging */
  131. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  132. if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
  133. quit(2);
  134. }
  135. if (LoadSprite("icon.bmp", renderer) < 0) {
  136. quit(2);
  137. }
  138. /* Initialize the sprite positions */
  139. srand(time(NULL));
  140. for (i = 0; i < NUM_SPRITES; ++i) {
  141. positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
  142. positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
  143. positions[i].w = sprite_w;
  144. positions[i].h = sprite_h;
  145. velocities[i].x = 0;
  146. velocities[i].y = 0;
  147. while (!velocities[i].x && !velocities[i].y) {
  148. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  149. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  150. }
  151. }
  152. /* Main render loop */
  153. done = 0;
  154. #ifdef __EMSCRIPTEN__
  155. emscripten_set_main_loop(loop, 0, 1);
  156. #else
  157. while (!done) {
  158. loop();
  159. }
  160. #endif
  161. quit(0);
  162. return 0; /* to prevent compiler warning */
  163. }
  164. /* vi: set ts=4 sw=4 expandtab: */