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

167 lines
4.1 KiB

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