Atlas - testrendercopyex.c
Home / ext / SDL / test Lines: 1 | Size: 4512 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 SDL_Texture *target; 54 SDL_FPoint *center = NULL; 55 SDL_FPoint origin = { 0.0f, 0.0f }; 56 57 SDL_GetRenderViewport(s->renderer, &viewport); 58 59 target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h); 60 SDL_SetRenderTarget(s->renderer, target); 61 62 /* Draw the background */ 63 SDL_RenderTexture(s->renderer, s->background, NULL, NULL); 64 65 /* Scale and draw the sprite */ 66 s->sprite_rect.w += s->scale_direction; 67 s->sprite_rect.h += s->scale_direction; 68 if (s->scale_direction > 0) { 69 center = &origin; 70 if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) { 71 s->scale_direction = -1; 72 } 73 } else { 74 if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) { 75 s->scale_direction = 1; 76 } 77 } 78 s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2; 79 s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2; 80 81 SDL_RenderTextureRotated(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, SDL_FLIP_NONE); 82 83 SDL_SetRenderTarget(s->renderer, NULL); 84 SDL_RenderTexture(s->renderer, target, NULL, NULL); 85 SDL_DestroyTexture(target); 86 87 /* Update the screen! */ 88 SDL_RenderPresent(s->renderer); 89 /* SDL_Delay(10); */ 90} 91 92static void loop(void) 93{ 94 int i; 95 SDL_Event event; 96 97 /* Check for events */ 98 99 while (SDL_PollEvent(&event)) { 100 SDLTest_CommonEvent(state, &event, &done); 101 } 102 for (i = 0; i < state->num_windows; ++i) { 103 if (state->windows[i] == NULL) { 104 continue; 105 } 106 Draw(&drawstates[i]); 107 } 108#ifdef SDL_PLATFORM_EMSCRIPTEN 109 if (done) { 110 emscripten_cancel_main_loop(); 111 } 112#endif 113} 114 115int main(int argc, char *argv[]) 116{ 117 int i; 118 int frames; 119 Uint64 then, now; 120 121 /* Initialize test framework */ 122 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 123 if (!state) { 124 return 1; 125 } 126 127 if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { 128 SDLTest_CommonQuit(state); 129 return 1; 130 } 131 132 drawstates = SDL_stack_alloc(DrawState, state->num_windows); 133 for (i = 0; i < state->num_windows; ++i) { 134 DrawState *drawstate = &drawstates[i]; 135 136 drawstate->window = state->windows[i]; 137 drawstate->renderer = state->renderers[i]; 138 drawstate->sprite = LoadTexture(drawstate->renderer, "icon.png", true); 139 drawstate->background = LoadTexture(drawstate->renderer, "sample.png", false); 140 if (!drawstate->sprite || !drawstate->background) { 141 quit(2); 142 } 143 SDL_GetTextureSize(drawstate->sprite, &drawstate->sprite_rect.w, &drawstate->sprite_rect.h); 144 drawstate->scale_direction = 1; 145 } 146 147 /* Main render loop */ 148 frames = 0; 149 then = SDL_GetTicks(); 150 done = 0; 151 152#ifdef SDL_PLATFORM_EMSCRIPTEN 153 emscripten_set_main_loop(loop, 0, 1); 154#else 155 while (!done) { 156 ++frames; 157 loop(); 158 } 159#endif 160 /* Print out some timing information */ 161 now = SDL_GetTicks(); 162 if (now > then) { 163 double fps = ((double)frames * 1000) / (now - then); 164 SDL_Log("%2.2f frames per second", fps); 165 } 166 167 SDL_stack_free(drawstates); 168 169 quit(0); 170 return 0; 171} 172[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.