Atlas - testscale.c

Home / ext / SDL / test Lines: 1 | Size: 5086 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 24static SDLTest_CommonState *state; 25 26typedef 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 36static DrawState *drawstates; 37static int done; 38 39/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 40static void 41quit(int rc) 42{ 43 SDLTest_CommonQuit(state); 44 /* Let 'main()' return normally */ 45 if (rc != 0) { 46 exit(rc); 47 } 48} 49 50static 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 80static 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 102int main(int argc, char *argv[]) 103{ 104 int i; 105 int frames; 106 Uint64 then, now; 107 SDL_ScaleMode scale_mode = SDL_SCALEMODE_PIXELART; 108 109 /* Initialize test framework */ 110 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 111 if (!state) { 112 return 1; 113 } 114 115 /* Parse commandline */ 116 for (i = 1; i < argc;) { 117 int consumed; 118 119 consumed = SDLTest_CommonArg(state, i); 120 if (consumed == 0) { 121 consumed = -1; 122 if (SDL_strcasecmp(argv[i], "--nearest") == 0) { 123 scale_mode = SDL_SCALEMODE_NEAREST; 124 consumed = 1; 125 } else if (SDL_strcasecmp(argv[i], "--linear") == 0) { 126 scale_mode = SDL_SCALEMODE_LINEAR; 127 consumed = 1; 128 } else if (SDL_strcasecmp(argv[i], "--pixelart") == 0) { 129 scale_mode = SDL_SCALEMODE_PIXELART; 130 consumed = 1; 131 } 132 } 133 if (consumed < 0) { 134 static const char *options[] = { 135 "[--nearest]", 136 "[--linear]", 137 "[--pixelart]", 138 NULL 139 }; 140 SDLTest_CommonLogUsage(state, argv[0], options); 141 return 1; 142 } 143 i += consumed; 144 } 145 146 if (!SDLTest_CommonInit(state)) { 147 quit(1); 148 } 149 150 drawstates = SDL_stack_alloc(DrawState, state->num_windows); 151 for (i = 0; i < state->num_windows; ++i) { 152 DrawState *drawstate = &drawstates[i]; 153 154 drawstate->window = state->windows[i]; 155 drawstate->renderer = state->renderers[i]; 156 drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png", true); 157 drawstate->background = LoadTexture(drawstate->renderer, "sample.png", false); 158 if (!drawstate->sprite || !drawstate->background) { 159 quit(2); 160 } 161 SDL_GetTextureSize(drawstate->sprite, &drawstate->sprite_rect.w, &drawstate->sprite_rect.h); 162 SDL_SetTextureScaleMode(drawstate->background, scale_mode); 163 SDL_SetTextureScaleMode(drawstate->sprite, scale_mode); 164 drawstate->scale_direction = 1; 165 } 166 167 /* Main render loop */ 168 frames = 0; 169 then = SDL_GetTicks(); 170 done = 0; 171 172#ifdef SDL_PLATFORM_EMSCRIPTEN 173 emscripten_set_main_loop(loop, 0, 1); 174#else 175 while (!done) { 176 ++frames; 177 loop(); 178 } 179#endif 180 181 /* Print out some timing information */ 182 now = SDL_GetTicks(); 183 if (now > then) { 184 double fps = ((double)frames * 1000) / (now - then); 185 SDL_Log("%2.2f frames per second", fps); 186 } 187 188 SDL_stack_free(drawstates); 189 190 quit(0); 191 return 0; 192} 193
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.