testscale.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. Copyright (C) 1997-2022 Sam Lantinga <[email protected]>
  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 <SDL3/SDL_main.h>
  17. #include "testutils.h"
  18. #define WINDOW_WIDTH 640
  19. #define WINDOW_HEIGHT 480
  20. static SDLTest_CommonState *state;
  21. typedef struct
  22. {
  23. SDL_Window *window;
  24. SDL_Renderer *renderer;
  25. SDL_Texture *background;
  26. SDL_Texture *sprite;
  27. SDL_Rect sprite_rect;
  28. int scale_direction;
  29. } DrawState;
  30. DrawState *drawstates;
  31. int done;
  32. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  33. static void
  34. quit(int rc)
  35. {
  36. SDLTest_CommonQuit(state);
  37. exit(rc);
  38. }
  39. void Draw(DrawState *s)
  40. {
  41. SDL_Rect viewport;
  42. SDL_RenderGetViewport(s->renderer, &viewport);
  43. /* Draw the background */
  44. SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
  45. /* Scale and draw the sprite */
  46. s->sprite_rect.w += s->scale_direction;
  47. s->sprite_rect.h += s->scale_direction;
  48. if (s->scale_direction > 0) {
  49. if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
  50. s->scale_direction = -1;
  51. }
  52. } else {
  53. if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
  54. s->scale_direction = 1;
  55. }
  56. }
  57. s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
  58. s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
  59. SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
  60. /* Update the screen! */
  61. SDL_RenderPresent(s->renderer);
  62. }
  63. void 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 main(int argc, char *argv[])
  84. {
  85. int i;
  86. int frames;
  87. Uint64 then, now;
  88. /* Enable standard application logging */
  89. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  90. /* Initialize test framework */
  91. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  92. if (state == NULL) {
  93. return 1;
  94. }
  95. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  96. SDLTest_CommonQuit(state);
  97. return 1;
  98. }
  99. drawstates = SDL_stack_alloc(DrawState, state->num_windows);
  100. for (i = 0; i < state->num_windows; ++i) {
  101. DrawState *drawstate = &drawstates[i];
  102. drawstate->window = state->windows[i];
  103. drawstate->renderer = state->renderers[i];
  104. drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE, NULL, NULL);
  105. drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE, NULL, NULL);
  106. if (!drawstate->sprite || !drawstate->background) {
  107. quit(2);
  108. }
  109. SDL_QueryTexture(drawstate->sprite, NULL, NULL,
  110. &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
  111. drawstate->scale_direction = 1;
  112. }
  113. /* Main render loop */
  114. frames = 0;
  115. then = SDL_GetTicks();
  116. done = 0;
  117. #ifdef __EMSCRIPTEN__
  118. emscripten_set_main_loop(loop, 0, 1);
  119. #else
  120. while (!done) {
  121. ++frames;
  122. loop();
  123. }
  124. #endif
  125. /* Print out some timing information */
  126. now = SDL_GetTicks();
  127. if (now > then) {
  128. double fps = ((double)frames * 1000) / (now - then);
  129. SDL_Log("%2.2f frames per second\n", fps);
  130. }
  131. SDL_stack_free(drawstates);
  132. quit(0);
  133. return 0;
  134. }
  135. /* vi: set ts=4 sw=4 expandtab: */