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

146 lines
3.6 KiB

  1. /*
  2. Copyright (C) 1997-2023 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 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 main(int argc, char *argv[])
  82. {
  83. SDL_Window *window;
  84. int i;
  85. /* Enable standard application logging */
  86. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  87. if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
  88. quit(2);
  89. }
  90. sprite = LoadTexture(renderer, "icon.bmp", SDL_TRUE, &sprite_w, &sprite_h);
  91. if (sprite == NULL) {
  92. quit(2);
  93. }
  94. /* Initialize the sprite positions */
  95. srand((unsigned int)time(NULL));
  96. for (i = 0; i < NUM_SPRITES; ++i) {
  97. positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
  98. positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
  99. positions[i].w = sprite_w;
  100. positions[i].h = sprite_h;
  101. velocities[i].x = 0;
  102. velocities[i].y = 0;
  103. while (!velocities[i].x && !velocities[i].y) {
  104. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  105. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  106. }
  107. }
  108. /* Main render loop */
  109. done = 0;
  110. #ifdef __EMSCRIPTEN__
  111. emscripten_set_main_loop(loop, 0, 1);
  112. #else
  113. while (!done) {
  114. loop();
  115. }
  116. #endif
  117. quit(0);
  118. return 0; /* to prevent compiler warning */
  119. }
  120. /* vi: set ts=4 sw=4 expandtab: */