testspritesurface.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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: Test surface blitting. */
  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_Surface *sprite;
  22. static SDL_Rect positions[NUM_SPRITES];
  23. static SDL_Rect velocities[NUM_SPRITES];
  24. static int sprite_w, sprite_h;
  25. SDL_Window *window;
  26. SDL_Surface *window_surf;
  27. static int done;
  28. static SDL_Surface *CreateSurface(unsigned char *data, unsigned int len, int *w, int *h) {
  29. SDL_Surface *surface = NULL;
  30. SDL_IOStream *src = SDL_IOFromConstMem(data, len);
  31. if (src) {
  32. surface = SDL_LoadBMP_IO(src, true);
  33. if (surface) {
  34. /* Treat white as transparent */
  35. SDL_SetSurfaceColorKey(surface, true, SDL_MapSurfaceRGB(surface, 255, 255, 255));
  36. *w = surface->w;
  37. *h = surface->h;
  38. }
  39. }
  40. return surface;
  41. }
  42. static void MoveSprites(void)
  43. {
  44. int i;
  45. int window_w = WINDOW_WIDTH;
  46. int window_h = WINDOW_HEIGHT;
  47. SDL_Rect *position, *velocity;
  48. Uint32 background = SDL_MapSurfaceRGB(window_surf, 0xA0, 0xA0, 0xA0);
  49. SDL_FillSurfaceRect(window_surf, NULL, background);
  50. /* Move the sprite, bounce at the wall, and draw */
  51. for (i = 0; i < NUM_SPRITES; ++i) {
  52. position = &positions[i];
  53. velocity = &velocities[i];
  54. position->x += velocity->x;
  55. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  56. velocity->x = -velocity->x;
  57. position->x += velocity->x;
  58. }
  59. position->y += velocity->y;
  60. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  61. velocity->y = -velocity->y;
  62. position->y += velocity->y;
  63. }
  64. /* Blit the sprite onto the screen */
  65. SDL_BlitSurface(sprite, NULL, window_surf, position);
  66. }
  67. /* Update the screen! */
  68. SDL_UpdateWindowSurface(window);
  69. }
  70. static void loop(void)
  71. {
  72. SDL_Event event;
  73. /* Check for events */
  74. while (SDL_PollEvent(&event)) {
  75. if (event.type == SDL_EVENT_QUIT || event.type == SDL_EVENT_KEY_DOWN) {
  76. done = 1;
  77. }
  78. }
  79. MoveSprites();
  80. #ifdef SDL_PLATFORM_EMSCRIPTEN
  81. if (done) {
  82. emscripten_cancel_main_loop();
  83. }
  84. #endif
  85. }
  86. int main(int argc, char *argv[])
  87. {
  88. int return_code = -1;
  89. int i;
  90. if (argc > 1) {
  91. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "USAGE: %s", argv[0]);
  92. return_code = 1;
  93. goto quit;
  94. }
  95. if ((window = SDL_CreateWindow("testspritesurface", WINDOW_WIDTH, WINDOW_HEIGHT, 0)) == NULL) {
  96. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window (%s)", SDL_GetError());
  97. return_code = 2;
  98. goto quit;
  99. }
  100. if ((window_surf = SDL_GetWindowSurface(window)) == NULL) {
  101. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't acquire window surface (%s)", SDL_GetError());
  102. return_code = 3;
  103. goto quit;
  104. }
  105. sprite = CreateSurface(icon_bmp, icon_bmp_len, &sprite_w, &sprite_h);
  106. if (!sprite) {
  107. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create surface (%s)", SDL_GetError());
  108. return_code = 4;
  109. goto quit;
  110. }
  111. /* Initialize the sprite positions */
  112. for (i = 0; i < NUM_SPRITES; ++i) {
  113. positions[i].x = SDL_rand(WINDOW_WIDTH - sprite_w);
  114. positions[i].y = SDL_rand(WINDOW_HEIGHT - sprite_h);
  115. positions[i].w = sprite_w;
  116. positions[i].h = sprite_h;
  117. velocities[i].x = 0;
  118. velocities[i].y = 0;
  119. while (velocities[i].x == 0.f && velocities[i].y == 0.f) {
  120. velocities[i].x = (SDL_rand(MAX_SPEED * 2 + 1)) - MAX_SPEED;
  121. velocities[i].y = (SDL_rand(MAX_SPEED * 2 + 1)) - MAX_SPEED;
  122. }
  123. }
  124. /* Main render loop */
  125. done = 0;
  126. #ifdef SDL_PLATFORM_EMSCRIPTEN
  127. emscripten_set_main_loop(loop, 0, 1);
  128. #else
  129. while (!done) {
  130. loop();
  131. }
  132. #endif
  133. return_code = 0;
  134. quit:
  135. if (sprite) {
  136. SDL_DestroySurface(sprite);
  137. sprite = NULL;
  138. }
  139. if (window_surf) {
  140. SDL_DestroyWindowSurface(window);
  141. window_surf = NULL;
  142. }
  143. if (window) {
  144. SDL_DestroyWindow(window);
  145. window = NULL;
  146. }
  147. SDL_Quit();
  148. return return_code;
  149. }