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

166 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. {
  24. SDL_Window *window;
  25. SDL_Renderer *renderer;
  26. SDL_Texture *background;
  27. SDL_Texture *sprite;
  28. SDL_Rect sprite_rect;
  29. int scale_direction;
  30. } DrawState;
  31. DrawState *drawstates;
  32. int done;
  33. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  34. static void
  35. quit(int rc)
  36. {
  37. SDLTest_CommonQuit(state);
  38. exit(rc);
  39. }
  40. void 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 loop()
  65. {
  66. int i;
  67. SDL_Event event;
  68. /* Check for events */
  69. while (SDL_PollEvent(&event)) {
  70. SDLTest_CommonEvent(state, &event, &done);
  71. }
  72. for (i = 0; i < state->num_windows; ++i) {
  73. if (state->windows[i] == NULL) {
  74. continue;
  75. }
  76. Draw(&drawstates[i]);
  77. }
  78. #ifdef __EMSCRIPTEN__
  79. if (done) {
  80. emscripten_cancel_main_loop();
  81. }
  82. #endif
  83. }
  84. int 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: */