testscale.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. {
  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. void 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 loop()
  63. {
  64. int i;
  65. SDL_Event event;
  66. /* Check for events */
  67. while (SDL_PollEvent(&event)) {
  68. SDLTest_CommonEvent(state, &event, &done);
  69. }
  70. for (i = 0; i < state->num_windows; ++i) {
  71. if (state->windows[i] == NULL) {
  72. continue;
  73. }
  74. Draw(&drawstates[i]);
  75. }
  76. #ifdef __EMSCRIPTEN__
  77. if (done) {
  78. emscripten_cancel_main_loop();
  79. }
  80. #endif
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. int i;
  85. int frames;
  86. Uint64 then, now;
  87. /* Enable standard application logging */
  88. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  89. /* Initialize test framework */
  90. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  91. if (state == NULL) {
  92. return 1;
  93. }
  94. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  95. SDLTest_CommonQuit(state);
  96. return 1;
  97. }
  98. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  99. for (i = 0; i < state->num_windows; ++i) {
  100. DrawState *drawstate = &drawstates[i];
  101. drawstate->window = state->windows[i];
  102. drawstate->renderer = state->renderers[i];
  103. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  104. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
  105. if (!drawstate->sprite || !drawstate->background) {
  106. quit(2);
  107. }
  108. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  109. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  110. drawstate->scale_direction = 1;
  111. }
  112. /* Main render loop */
  113. frames = 0;
  114. then = SDL_GetTicks();
  115. done = 0;
  116. #ifdef __EMSCRIPTEN__
  117. emscripten_set_main_loop(loop, 0, 1);
  118. #else
  119. while (!done) {
  120. ++frames;
  121. loop();
  122. }
  123. #endif
  124. /* Print out some timing information */
  125. now = SDL_GetTicks();
  126. if (now > then) {
  127. double fps = ((double)frames * 1000) / (now - then);
  128. SDL_Log("%2.2f frames per second\n", fps);
  129. }
  130. SDL_stack_free(drawstates);
  131. quit(0);
  132. return 0;
  133. }
  134. /* vi: set ts=4 sw=4 expandtab: */