testspriteminimal.c 3.6 KB

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