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

216 lines
5.5 KiB

  1. /*
  2. Copyright (C) 1997-2019 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_test_common.h"
  18. #define WINDOW_WIDTH 640
  19. #define WINDOW_HEIGHT 480
  20. static SDLTest_CommonState *state;
  21. typedef struct {
  22. SDL_Window *window;
  23. SDL_Renderer *renderer;
  24. SDL_Texture *background;
  25. SDL_Texture *sprite;
  26. SDL_Rect sprite_rect;
  27. int scale_direction;
  28. } DrawState;
  29. DrawState *drawstates;
  30. int done;
  31. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  32. static void
  33. quit(int rc)
  34. {
  35. SDLTest_CommonQuit(state);
  36. exit(rc);
  37. }
  38. SDL_Texture *
  39. LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
  40. {
  41. SDL_Surface *temp;
  42. SDL_Texture *texture;
  43. /* Load the sprite image */
  44. temp = SDL_LoadBMP(file);
  45. if (temp == NULL) {
  46. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
  47. return NULL;
  48. }
  49. /* Set transparent pixel as the pixel at (0,0) */
  50. if (transparent) {
  51. if (temp->format->palette) {
  52. SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
  53. } else {
  54. switch (temp->format->BitsPerPixel) {
  55. case 15:
  56. SDL_SetColorKey(temp, SDL_TRUE,
  57. (*(Uint16 *) temp->pixels) & 0x00007FFF);
  58. break;
  59. case 16:
  60. SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
  61. break;
  62. case 24:
  63. SDL_SetColorKey(temp, SDL_TRUE,
  64. (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
  65. break;
  66. case 32:
  67. SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
  68. break;
  69. }
  70. }
  71. }
  72. /* Create textures from the image */
  73. texture = SDL_CreateTextureFromSurface(renderer, temp);
  74. if (!texture) {
  75. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
  76. SDL_FreeSurface(temp);
  77. return NULL;
  78. }
  79. SDL_FreeSurface(temp);
  80. /* We're ready to roll. :) */
  81. return texture;
  82. }
  83. void
  84. Draw(DrawState *s)
  85. {
  86. SDL_Rect viewport;
  87. SDL_RenderGetViewport(s->renderer, &viewport);
  88. /* Draw the background */
  89. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  90. /* Scale and draw the sprite */
  91. s->sprite_rect.w += s->scale_direction;
  92. s->sprite_rect.h += s->scale_direction;
  93. if (s->scale_direction > 0) {
  94. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  95. s->scale_direction = -1;
  96. }
  97. } else {
  98. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  99. s->scale_direction = 1;
  100. }
  101. }
  102. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  103. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  104. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  105. /* Update the screen! */
  106. SDL_RenderPresent(s->renderer);
  107. }
  108. void
  109. loop()
  110. {
  111. int i;
  112. SDL_Event event;
  113. /* Check for events */
  114. while (SDL_PollEvent(&event)) {
  115. SDLTest_CommonEvent(state, &event, &done);
  116. }
  117. for (i = 0; i < state->num_windows; ++i) {
  118. if (state->windows[i] == NULL)
  119. continue;
  120. Draw(&drawstates[i]);
  121. }
  122. #ifdef __EMSCRIPTEN__
  123. if (done) {
  124. emscripten_cancel_main_loop();
  125. }
  126. #endif
  127. }
  128. int
  129. main(int argc, char *argv[])
  130. {
  131. int i;
  132. int frames;
  133. Uint32 then, now;
  134. /* Enable standard application logging */
  135. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  136. /* Initialize test framework */
  137. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  138. if (!state) {
  139. return 1;
  140. }
  141. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  142. SDLTest_CommonQuit(state);
  143. return 1;
  144. }
  145. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  146. for (i = 0; i < state->num_windows; ++i) {
  147. DrawState *drawstate = &drawstates[i];
  148. drawstate->window = state->windows[i];
  149. drawstate->renderer = state->renderers[i];
  150. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
  151. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
  152. if (!drawstate->sprite || !drawstate->background) {
  153. quit(2);
  154. }
  155. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  156. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  157. drawstate->scale_direction = 1;
  158. }
  159. /* Main render loop */
  160. frames = 0;
  161. then = SDL_GetTicks();
  162. done = 0;
  163. #ifdef __EMSCRIPTEN__
  164. emscripten_set_main_loop(loop, 0, 1);
  165. #else
  166. while (!done) {
  167. ++frames;
  168. loop();
  169. }
  170. #endif
  171. /* Print out some timing information */
  172. now = SDL_GetTicks();
  173. if (now > then) {
  174. double fps = ((double) frames * 1000) / (now - then);
  175. SDL_Log("%2.2f frames per second\n", fps);
  176. }
  177. SDL_stack_free(drawstates);
  178. quit(0);
  179. return 0;
  180. }
  181. /* vi: set ts=4 sw=4 expandtab: */