testspriteminimal.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. Copyright (C) 1997-2025 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 <SDL3/SDL.h>
  12. #include <SDL3/SDL_main.h>
  13. #ifdef SDL_PLATFORM_EMSCRIPTEN
  14. #include <emscripten/emscripten.h>
  15. #endif
  16. #include "icon.h"
  17. #define WINDOW_WIDTH 640
  18. #define WINDOW_HEIGHT 480
  19. #define NUM_SPRITES 100
  20. #define MAX_SPEED 1
  21. static SDL_Texture *sprite;
  22. static SDL_FRect positions[NUM_SPRITES];
  23. static SDL_FRect velocities[NUM_SPRITES];
  24. static int sprite_w, sprite_h;
  25. static SDL_Renderer *renderer;
  26. static int done;
  27. static SDL_Texture *CreateTexture(SDL_Renderer *r, unsigned char *data, unsigned int len, int *w, int *h)
  28. {
  29. SDL_Texture *texture = NULL;
  30. SDL_Surface *surface;
  31. SDL_IOStream *src = SDL_IOFromConstMem(data, len);
  32. if (src) {
  33. surface = SDL_LoadBMP_IO(src, true);
  34. if (surface) {
  35. /* Treat white as transparent */
  36. SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
  37. texture = SDL_CreateTextureFromSurface(r, surface);
  38. *w = surface->w;
  39. *h = surface->h;
  40. SDL_DestroySurface(surface);
  41. }
  42. }
  43. return texture;
  44. }
  45. static void MoveSprites(void)
  46. {
  47. int i;
  48. int window_w = WINDOW_WIDTH;
  49. int window_h = WINDOW_HEIGHT;
  50. SDL_FRect *position, *velocity;
  51. /* Draw a gray background */
  52. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  53. SDL_RenderClear(renderer);
  54. /* Move the sprite, bounce at the wall, and draw */
  55. for (i = 0; i < NUM_SPRITES; ++i) {
  56. position = &positions[i];
  57. velocity = &velocities[i];
  58. position->x += velocity->x;
  59. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  60. velocity->x = -velocity->x;
  61. position->x += velocity->x;
  62. }
  63. position->y += velocity->y;
  64. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  65. velocity->y = -velocity->y;
  66. position->y += velocity->y;
  67. }
  68. /* Blit the sprite onto the screen */
  69. SDL_RenderTexture(renderer, sprite, NULL, position);
  70. }
  71. /* Update the screen! */
  72. SDL_RenderPresent(renderer);
  73. }
  74. static void loop(void)
  75. {
  76. SDL_Event event;
  77. /* Check for events */
  78. while (SDL_PollEvent(&event)) {
  79. if (event.type == SDL_EVENT_QUIT ||
  80. (event.type == SDL_EVENT_KEY_DOWN && event.key.key == SDLK_ESCAPE)) {
  81. done = 1;
  82. }
  83. }
  84. MoveSprites();
  85. #ifdef SDL_PLATFORM_EMSCRIPTEN
  86. if (done) {
  87. emscripten_cancel_main_loop();
  88. }
  89. #endif
  90. }
  91. int main(int argc, char *argv[])
  92. {
  93. SDL_Window *window = NULL;
  94. int return_code = -1;
  95. int i;
  96. if (argc > 1) {
  97. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s", argv[0]);
  98. return_code = 1;
  99. goto quit;
  100. }
  101. if (!SDL_CreateWindowAndRenderer("testspriteminimal", WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  102. return_code = 2;
  103. goto quit;
  104. }
  105. SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  106. sprite = CreateTexture(renderer, icon_bmp, icon_bmp_len, &sprite_w, &sprite_h);
  107. if (!sprite) {
  108. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture (%s)", SDL_GetError());
  109. return_code = 3;
  110. goto quit;
  111. }
  112. /* Initialize the sprite positions */
  113. for (i = 0; i < NUM_SPRITES; ++i) {
  114. positions[i].x = (float)SDL_rand(WINDOW_WIDTH - sprite_w);
  115. positions[i].y = (float)SDL_rand(WINDOW_HEIGHT - sprite_h);
  116. positions[i].w = (float)sprite_w;
  117. positions[i].h = (float)sprite_h;
  118. velocities[i].x = 0.0f;
  119. velocities[i].y = 0.0f;
  120. while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
  121. velocities[i].x = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
  122. velocities[i].y = (float)(SDL_rand(MAX_SPEED * 2 + 1) - MAX_SPEED);
  123. }
  124. }
  125. /* Main render loop */
  126. done = 0;
  127. #ifdef SDL_PLATFORM_EMSCRIPTEN
  128. emscripten_set_main_loop(loop, 0, 1);
  129. #else
  130. while (!done) {
  131. loop();
  132. }
  133. #endif
  134. return_code = 0;
  135. quit:
  136. SDL_DestroyRenderer(renderer);
  137. SDL_DestroyWindow(window);
  138. SDL_Quit();
  139. return return_code;
  140. }