testspriteminimal.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. Copyright (C) 1997-2014 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. #include "SDL.h"
  15. #define WINDOW_WIDTH 640
  16. #define WINDOW_HEIGHT 480
  17. #define NUM_SPRITES 100
  18. #define MAX_SPEED 1
  19. static SDL_Texture *sprite;
  20. static SDL_Rect positions[NUM_SPRITES];
  21. static SDL_Rect velocities[NUM_SPRITES];
  22. static int sprite_w, sprite_h;
  23. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  24. static void
  25. quit(int rc)
  26. {
  27. exit(rc);
  28. }
  29. int
  30. LoadSprite(char *file, SDL_Renderer *renderer)
  31. {
  32. SDL_Surface *temp;
  33. /* Load the sprite image */
  34. temp = SDL_LoadBMP(file);
  35. if (temp == NULL) {
  36. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
  37. return (-1);
  38. }
  39. sprite_w = temp->w;
  40. sprite_h = temp->h;
  41. /* Set transparent pixel as the pixel at (0,0) */
  42. if (temp->format->palette) {
  43. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  44. } else {
  45. switch (temp->format->BitsPerPixel) {
  46. case 15:
  47. SDL_SetColorKey(temp, SDL_TRUE,
  48. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  49. break;
  50. case 16:
  51. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  52. break;
  53. case 24:
  54. SDL_SetColorKey(temp, SDL_TRUE,
  55. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  56. break;
  57. case 32:
  58. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  59. break;
  60. }
  61. }
  62. /* Create textures from the image */
  63. sprite = SDL_CreateTextureFromSurface(renderer, temp);
  64. if (!sprite) {
  65. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  66. SDL_FreeSurface(temp);
  67. return (-1);
  68. }
  69. SDL_FreeSurface(temp);
  70. /* We're ready to roll. :) */
  71. return (0);
  72. }
  73. void
  74. MoveSprites(SDL_Renderer * renderer, SDL_Texture * sprite)
  75. {
  76. int i;
  77. int window_w = WINDOW_WIDTH;
  78. int window_h = WINDOW_HEIGHT;
  79. SDL_Rect *position, *velocity;
  80. /* Draw a gray background */
  81. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  82. SDL_RenderClear(renderer);
  83. /* Move the sprite, bounce at the wall, and draw */
  84. for (i = 0; i < NUM_SPRITES; ++i) {
  85. position = &positions[i];
  86. velocity = &velocities[i];
  87. position->x += velocity->x;
  88. if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
  89. velocity->x = -velocity->x;
  90. position->x += velocity->x;
  91. }
  92. position->y += velocity->y;
  93. if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
  94. velocity->y = -velocity->y;
  95. position->y += velocity->y;
  96. }
  97. /* Blit the sprite onto the screen */
  98. SDL_RenderCopy(renderer, sprite, NULL, position);
  99. }
  100. /* Update the screen! */
  101. SDL_RenderPresent(renderer);
  102. }
  103. int
  104. main(int argc, char *argv[])
  105. {
  106. SDL_Window *window;
  107. SDL_Renderer *renderer;
  108. int i, done;
  109. SDL_Event event;
  110. /* Enable standard application logging */
  111. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  112. if (SDL_CreateWindowAndRenderer(WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer) < 0) {
  113. quit(2);
  114. }
  115. if (LoadSprite("icon.bmp", renderer) < 0) {
  116. quit(2);
  117. }
  118. /* Initialize the sprite positions */
  119. srand(time(NULL));
  120. for (i = 0; i < NUM_SPRITES; ++i) {
  121. positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
  122. positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
  123. positions[i].w = sprite_w;
  124. positions[i].h = sprite_h;
  125. velocities[i].x = 0;
  126. velocities[i].y = 0;
  127. while (!velocities[i].x && !velocities[i].y) {
  128. velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  129. velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
  130. }
  131. }
  132. /* Main render loop */
  133. done = 0;
  134. while (!done) {
  135. /* Check for events */
  136. while (SDL_PollEvent(&event)) {
  137. if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
  138. done = 1;
  139. }
  140. }
  141. MoveSprites(renderer, sprite);
  142. }
  143. quit(0);
  144. return 0; /* to prevent compiler warning */
  145. }
  146. /* vi: set ts=4 sw=4 expandtab: */