🛠️🐜 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.

149 lines
3.6 KiB

  1. /*
  2. Copyright (C) 1997-2022 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. #include "testutils.h"
  19. #define WINDOW_WIDTH 640
  20. #define WINDOW_HEIGHT 480
  21. #define NUM_SPRITES 100
  22. #define MAX_SPEED 1
  23. static SDL_Texture *sprite;
  24. static SDL_Rect positions[NUM_SPRITES];
  25. static SDL_Rect velocities[NUM_SPRITES];
  26. static int sprite_w, sprite_h;
  27. SDL_Renderer *renderer;
  28. int done;
  29. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  30. static void
  31. quit(int rc)
  32. {
  33. SDL_Quit();
  34. exit(rc);
  35. }
  36. void
  37. MoveSprites()
  38. {
  39. int i;
  40. int window_w = WINDOW_WIDTH;
  41. int window_h = WINDOW_HEIGHT;
  42. SDL_Rect *position, *velocity;
  43. /* Draw a gray background */
  44. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  45. SDL_RenderClear(renderer);
  46. /* Move the sprite, bounce at the wall, and draw */
  47. for (i = 0; i < NUM_SPRITES; ++i) {
  48. position = &positions[i];
  49. velocity = &velocities[i];
  50. position->x += velocity->x;
  51. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  52. velocity->x = -velocity->x;
  53. position->x += velocity->x;
  54. }
  55. position->y += velocity->y;
  56. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  57. velocity->y = -velocity->y;
  58. position->y += velocity->y;
  59. }
  60. /* Blit the sprite onto the screen */
  61. SDL_RenderCopy(renderer, sprite, NULL, position);
  62. }
  63. /* Update the screen! */
  64. SDL_RenderPresent(renderer);
  65. }
  66. void loop()
  67. {
  68. SDL_Event event;
  69. /* Check for events */
  70. while (SDL_PollEvent(&event)) {
  71. if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
  72. done = 1;
  73. }
  74. }
  75. MoveSprites();
  76. #ifdef __EMSCRIPTEN__
  77. if (done) {
  78. emscripten_cancel_main_loop();
  79. }
  80. #endif
  81. }
  82. int
  83. main(int argc, char *argv[])
  84. {
  85. SDL_Window *window;
  86. int i;
  87. /* Enable standard application logging */
  88. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  89. if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
  90. quit(2);
  91. }
  92. sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &sprite_w, &sprite_h);
  93. if (sprite == NULL) {
  94. quit(2);
  95. }
  96. /* Initialize the sprite positions */
  97. srand((unsigned int)time(NULL));
  98. for (i = 0; i < NUM_SPRITES; ++i) {
  99. positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
  100. positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
  101. positions[i].w = sprite_w;
  102. positions[i].h = sprite_h;
  103. velocities[i].x = 0;
  104. velocities[i].y = 0;
  105. while (!velocities[i].x && !velocities[i].y) {
  106. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  107. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  108. }
  109. }
  110. /* Main render loop */
  111. done = 0;
  112. #ifdef __EMSCRIPTEN__
  113. emscripten_set_main_loop(loop, 0, 1);
  114. #else
  115. while (!done) {
  116. loop();
  117. }
  118. #endif
  119. quit(0);
  120. return 0; /* to prevent compiler warning */
  121. }
  122. /* vi: set ts=4 sw=4 expandtab: */