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

175 lines
4.5 KiB

  1. /*
  2. Copyright (C) 1997-2022 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. #include "testutils.h"
  19. static SDLTest_CommonState *state;
  20. typedef struct {
  21. SDL_Window *window;
  22. SDL_Renderer *renderer;
  23. SDL_Texture *background;
  24. SDL_Texture *sprite;
  25. SDL_Rect sprite_rect;
  26. int scale_direction;
  27. } DrawState;
  28. DrawState *drawstates;
  29. int done;
  30. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  31. static void
  32. quit(int rc)
  33. {
  34. SDLTest_CommonQuit(state);
  35. exit(rc);
  36. }
  37. void
  38. Draw(DrawState *s)
  39. {
  40. SDL_Rect viewport;
  41. SDL_Texture *target;
  42. SDL_Point *center=NULL;
  43. SDL_Point origin = {0,0};
  44. SDL_RenderGetViewport(s->renderer, &viewport);
  45. target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
  46. SDL_SetRenderTarget(s->renderer, target);
  47. /* Draw the background */
  48. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  49. /* Scale and draw the sprite */
  50. s->sprite_rect.w += s->scale_direction;
  51. s->sprite_rect.h += s->scale_direction;
  52. if (s->scale_direction > 0) {
  53. center = &origin;
  54. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  55. s->scale_direction = -1;
  56. }
  57. } else {
  58. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  59. s->scale_direction = 1;
  60. }
  61. }
  62. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  63. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  64. SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, (SDL_RendererFlip)s->scale_direction);
  65. SDL_SetRenderTarget(s->renderer, NULL);
  66. SDL_RenderCopy(s->renderer, target, NULL, NULL);
  67. SDL_DestroyTexture(target);
  68. /* Update the screen! */
  69. SDL_RenderPresent(s->renderer);
  70. /* SDL_Delay(10); */
  71. }
  72. void loop()
  73. {
  74. int i;
  75. SDL_Event event;
  76. /* Check for events */
  77. while (SDL_PollEvent(&event)) {
  78. SDLTest_CommonEvent(state, &event, &done);
  79. }
  80. for (i = 0; i < state->num_windows; ++i) {
  81. if (state->windows[i] == NULL)
  82. continue;
  83. Draw(&drawstates[i]);
  84. }
  85. #ifdef __EMSCRIPTEN__
  86. if (done) {
  87. emscripten_cancel_main_loop();
  88. }
  89. #endif
  90. }
  91. int
  92. main(int argc, char *argv[])
  93. {
  94. int i;
  95. int frames;
  96. Uint32 then, now;
  97. /* Enable standard application logging */
  98. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  99. /* Initialize test framework */
  100. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  101. if (!state) {
  102. return 1;
  103. }
  104. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  105. SDLTest_CommonQuit(state);
  106. return 1;
  107. }
  108. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  109. for (i = 0; i < state->num_windows; ++i) {
  110. DrawState *drawstate = &drawstates[i];
  111. drawstate->window = state->windows[i];
  112. drawstate->renderer = state->renderers[i];
  113. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  114. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
  115. if (!drawstate->sprite || !drawstate->background) {
  116. quit(2);
  117. }
  118. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  119. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  120. drawstate->scale_direction = 1;
  121. }
  122. /* Main render loop */
  123. frames = 0;
  124. then = SDL_GetTicks();
  125. done = 0;
  126. #ifdef __EMSCRIPTEN__
  127. emscripten_set_main_loop(loop, 0, 1);
  128. #else
  129. while (!done) {
  130. ++frames;
  131. loop();
  132. }
  133. #endif
  134. /* Print out some timing information */
  135. now = SDL_GetTicks();
  136. if (now > then) {
  137. double fps = ((double) frames * 1000) / (now - then);
  138. SDL_Log("%2.2f frames per second\n", fps);
  139. }
  140. SDL_stack_free(drawstates);
  141. quit(0);
  142. return 0;
  143. }
  144. /* vi: set ts=4 sw=4 expandtab: */