testscale.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. #ifdef __EMSCRIPTEN__
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. #include <SDL3/SDL_test_common.h>
  16. #include "testutils.h"
  17. #define WINDOW_WIDTH 640
  18. #define WINDOW_HEIGHT 480
  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_RenderGetViewport(s->renderer, &viewport);
  42. /* Draw the background */
  43. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  44. /* Scale and draw the sprite */
  45. s->sprite_rect.w += s->scale_direction;
  46. s->sprite_rect.h += s->scale_direction;
  47. if (s->scale_direction > 0) {
  48. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  49. s->scale_direction = -1;
  50. }
  51. } else {
  52. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  53. s->scale_direction = 1;
  54. }
  55. }
  56. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  57. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  58. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  59. /* Update the screen! */
  60. SDL_RenderPresent(s->renderer);
  61. }
  62. void
  63. loop()
  64. {
  65. int i;
  66. SDL_Event event;
  67. /* Check for events */
  68. while (SDL_PollEvent(&event)) {
  69. SDLTest_CommonEvent(state, &event, &done);
  70. }
  71. for (i = 0; i < state->num_windows; ++i) {
  72. if (state->windows[i] == NULL) {
  73. continue;
  74. }
  75. Draw(&drawstates[i]);
  76. }
  77. #ifdef __EMSCRIPTEN__
  78. if (done) {
  79. emscripten_cancel_main_loop();
  80. }
  81. #endif
  82. }
  83. int
  84. main(int argc, char *argv[])
  85. {
  86. int i;
  87. int frames;
  88. Uint32 then, now;
  89. /* Enable standard application logging */
  90. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  91. /* Initialize test framework */
  92. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  93. if (state == NULL) {
  94. return 1;
  95. }
  96. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  97. SDLTest_CommonQuit(state);
  98. return 1;
  99. }
  100. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  101. for (i = 0; i < state->num_windows; ++i) {
  102. DrawState *drawstate = &drawstates[i];
  103. drawstate->window = state->windows[i];
  104. drawstate->renderer = state->renderers[i];
  105. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  106. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
  107. if (!drawstate->sprite || !drawstate->background) {
  108. quit(2);
  109. }
  110. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  111. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  112. drawstate->scale_direction = 1;
  113. }
  114. /* Main render loop */
  115. frames = 0;
  116. then = SDL_GetTicks();
  117. done = 0;
  118. #ifdef __EMSCRIPTEN__
  119. emscripten_set_main_loop(loop, 0, 1);
  120. #else
  121. while (!done) {
  122. ++frames;
  123. loop();
  124. }
  125. #endif
  126. /* Print out some timing information */
  127. now = SDL_GetTicks();
  128. if (now > then) {
  129. double fps = ((double) frames * 1000) / (now - then);
  130. SDL_Log("%2.2f frames per second\n", fps);
  131. }
  132. SDL_stack_free(drawstates);
  133. quit(0);
  134. return 0;
  135. }
  136. /* vi: set ts=4 sw=4 expandtab: */