diff options
Diffstat (limited to 'contrib/SDL-3.2.8/test/testscale.c')
| -rw-r--r-- | contrib/SDL-3.2.8/test/testscale.c | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/test/testscale.c b/contrib/SDL-3.2.8/test/testscale.c new file mode 100644 index 0000000..a282b07 --- /dev/null +++ b/contrib/SDL-3.2.8/test/testscale.c | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | /* | ||
| 2 | Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org> | ||
| 3 | |||
| 4 | This software is provided 'as-is', without any express or implied | ||
| 5 | warranty. In no event will the authors be held liable for any damages | ||
| 6 | arising from the use of this software. | ||
| 7 | |||
| 8 | Permission is granted to anyone to use this software for any purpose, | ||
| 9 | including commercial applications, and to alter it and redistribute it | ||
| 10 | freely. | ||
| 11 | */ | ||
| 12 | /* Simple program: Move N sprites around on the screen as fast as possible */ | ||
| 13 | |||
| 14 | #include <SDL3/SDL_test_common.h> | ||
| 15 | #include <SDL3/SDL_main.h> | ||
| 16 | #include "testutils.h" | ||
| 17 | |||
| 18 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 19 | #include <emscripten/emscripten.h> | ||
| 20 | #endif | ||
| 21 | |||
| 22 | #include <stdlib.h> | ||
| 23 | |||
| 24 | static SDLTest_CommonState *state; | ||
| 25 | |||
| 26 | typedef struct | ||
| 27 | { | ||
| 28 | SDL_Window *window; | ||
| 29 | SDL_Renderer *renderer; | ||
| 30 | SDL_Texture *background; | ||
| 31 | SDL_Texture *sprite; | ||
| 32 | SDL_FRect sprite_rect; | ||
| 33 | int scale_direction; | ||
| 34 | } DrawState; | ||
| 35 | |||
| 36 | static DrawState *drawstates; | ||
| 37 | static int done; | ||
| 38 | |||
| 39 | /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ | ||
| 40 | static void | ||
| 41 | quit(int rc) | ||
| 42 | { | ||
| 43 | SDLTest_CommonQuit(state); | ||
| 44 | /* Let 'main()' return normally */ | ||
| 45 | if (rc != 0) { | ||
| 46 | exit(rc); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | static void Draw(DrawState *s) | ||
| 51 | { | ||
| 52 | SDL_Rect viewport; | ||
| 53 | |||
| 54 | SDL_GetRenderViewport(s->renderer, &viewport); | ||
| 55 | |||
| 56 | /* Draw the background */ | ||
| 57 | SDL_RenderTexture(s->renderer, s->background, NULL, NULL); | ||
| 58 | |||
| 59 | /* Scale and draw the sprite */ | ||
| 60 | s->sprite_rect.w += s->scale_direction; | ||
| 61 | s->sprite_rect.h += s->scale_direction; | ||
| 62 | if (s->scale_direction > 0) { | ||
| 63 | if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) { | ||
| 64 | s->scale_direction = -1; | ||
| 65 | } | ||
| 66 | } else { | ||
| 67 | if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) { | ||
| 68 | s->scale_direction = 1; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2; | ||
| 72 | s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2; | ||
| 73 | |||
| 74 | SDL_RenderTexture(s->renderer, s->sprite, NULL, &s->sprite_rect); | ||
| 75 | |||
| 76 | /* Update the screen! */ | ||
| 77 | SDL_RenderPresent(s->renderer); | ||
| 78 | } | ||
| 79 | |||
| 80 | static void loop(void) | ||
| 81 | { | ||
| 82 | int i; | ||
| 83 | SDL_Event event; | ||
| 84 | |||
| 85 | /* Check for events */ | ||
| 86 | while (SDL_PollEvent(&event)) { | ||
| 87 | SDLTest_CommonEvent(state, &event, &done); | ||
| 88 | } | ||
| 89 | for (i = 0; i < state->num_windows; ++i) { | ||
| 90 | if (state->windows[i] == NULL) { | ||
| 91 | continue; | ||
| 92 | } | ||
| 93 | Draw(&drawstates[i]); | ||
| 94 | } | ||
| 95 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 96 | if (done) { | ||
| 97 | emscripten_cancel_main_loop(); | ||
| 98 | } | ||
| 99 | #endif | ||
| 100 | } | ||
| 101 | |||
| 102 | int main(int argc, char *argv[]) | ||
| 103 | { | ||
| 104 | int i; | ||
| 105 | int frames; | ||
| 106 | Uint64 then, now; | ||
| 107 | |||
| 108 | /* Initialize test framework */ | ||
| 109 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); | ||
| 110 | if (!state) { | ||
| 111 | return 1; | ||
| 112 | } | ||
| 113 | |||
| 114 | /* Parse commandline */ | ||
| 115 | if (!SDLTest_CommonDefaultArgs(state, argc, argv)) { | ||
| 116 | return 1; | ||
| 117 | } | ||
| 118 | |||
| 119 | if (!SDLTest_CommonInit(state)) { | ||
| 120 | quit(1); | ||
| 121 | } | ||
| 122 | |||
| 123 | drawstates = SDL_stack_alloc(DrawState, state->num_windows); | ||
| 124 | for (i = 0; i < state->num_windows; ++i) { | ||
| 125 | DrawState *drawstate = &drawstates[i]; | ||
| 126 | |||
| 127 | drawstate->window = state->windows[i]; | ||
| 128 | drawstate->renderer = state->renderers[i]; | ||
| 129 | drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", true, NULL, NULL); | ||
| 130 | drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", false, NULL, NULL); | ||
| 131 | if (!drawstate->sprite || !drawstate->background) { | ||
| 132 | quit(2); | ||
| 133 | } | ||
| 134 | SDL_GetTextureSize(drawstate->sprite, &drawstate->sprite_rect.w, &drawstate->sprite_rect.h); | ||
| 135 | drawstate->scale_direction = 1; | ||
| 136 | } | ||
| 137 | |||
| 138 | /* Main render loop */ | ||
| 139 | frames = 0; | ||
| 140 | then = SDL_GetTicks(); | ||
| 141 | done = 0; | ||
| 142 | |||
| 143 | #ifdef SDL_PLATFORM_EMSCRIPTEN | ||
| 144 | emscripten_set_main_loop(loop, 0, 1); | ||
| 145 | #else | ||
| 146 | while (!done) { | ||
| 147 | ++frames; | ||
| 148 | loop(); | ||
| 149 | } | ||
| 150 | #endif | ||
| 151 | |||
| 152 | /* Print out some timing information */ | ||
| 153 | now = SDL_GetTicks(); | ||
| 154 | if (now > then) { | ||
| 155 | double fps = ((double)frames * 1000) / (now - then); | ||
| 156 | SDL_Log("%2.2f frames per second", fps); | ||
| 157 | } | ||
| 158 | |||
| 159 | SDL_stack_free(drawstates); | ||
| 160 | |||
| 161 | quit(0); | ||
| 162 | return 0; | ||
| 163 | } | ||
