Atlas - testgeometry.c
Home / ext / SDL / test Lines: 1 | Size: 8786 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 13/* Simple program: draw a RGB triangle, with texture */ 14 15#include <stdlib.h> 16 17#include "testutils.h" 18#include <SDL3/SDL.h> 19#include <SDL3/SDL_main.h> 20#include <SDL3/SDL_test_common.h> 21 22#ifdef SDL_PLATFORM_EMSCRIPTEN 23#include <emscripten/emscripten.h> 24#endif 25 26static SDLTest_CommonState *state; 27static bool use_texture = false; 28static SDL_Texture **sprites; 29static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE; 30static float angle = 0.0f; 31static int translate_cx = 0; 32static int translate_cy = 0; 33static float pinch_scale = 1.0f; 34 35static int done; 36 37/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ 38static void 39quit(int rc) 40{ 41 SDL_free(sprites); 42 SDLTest_CommonQuit(state); 43 /* Let 'main()' return normally */ 44 if (rc != 0) { 45 exit(rc); 46 } 47} 48 49static int LoadSprite(const char *file) 50{ 51 int i; 52 53 for (i = 0; i < state->num_windows; ++i) { 54 /* This does the SDL_LoadPNG step repeatedly, but that's OK for test code. */ 55 sprites[i] = LoadTexture(state->renderers[i], file, true); 56 if (!sprites[i]) { 57 return -1; 58 } 59 if (!SDL_SetTextureBlendMode(sprites[i], blendMode)) { 60 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set blend mode: %s", SDL_GetError()); 61 SDL_DestroyTexture(sprites[i]); 62 return -1; 63 } 64 } 65 66 /* We're ready to roll. :) */ 67 return 0; 68} 69 70static void loop(void) 71{ 72 int i; 73 SDL_Event event; 74 75 /* Check for events */ 76 while (SDL_PollEvent(&event)) { 77 78 if (event.type == SDL_EVENT_MOUSE_MOTION) { 79 if (event.motion.state) { 80 float xrel, yrel; 81 int window_w, window_h; 82 SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID); 83 SDL_GetWindowSize(window, &window_w, &window_h); 84 xrel = event.motion.xrel; 85 yrel = event.motion.yrel; 86 if (event.motion.y < (float)window_h / 2.0f) { 87 angle += xrel; 88 } else { 89 angle -= xrel; 90 } 91 if (event.motion.x < (float)window_w / 2.0f) { 92 angle -= yrel; 93 } else { 94 angle += yrel; 95 } 96 } 97 } else if (event.type == SDL_EVENT_KEY_DOWN) { 98 if (event.key.key == SDLK_LEFT) { 99 translate_cx -= 1; 100 } else if (event.key.key == SDLK_RIGHT) { 101 translate_cx += 1; 102 } else if (event.key.key == SDLK_UP) { 103 translate_cy -= 1; 104 } else if (event.key.key == SDLK_DOWN) { 105 translate_cy += 1; 106 } else { 107 108 } 109 } else if (event.type == SDL_EVENT_PINCH_BEGIN) { 110 } else if (event.type == SDL_EVENT_PINCH_UPDATE) { 111 pinch_scale *= event.pinch.scale; 112 } else if (event.type == SDL_EVENT_PINCH_END) { 113 } 114 SDLTest_CommonEvent(state, &event, &done); 115 } 116 117 for (i = 0; i < state->num_windows; ++i) { 118 SDL_Renderer *renderer = state->renderers[i]; 119 if (state->windows[i] == NULL) { 120 continue; 121 } 122 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 123 SDL_RenderClear(renderer); 124 125 { 126 SDL_Rect viewport; 127 SDL_Vertex verts[3]; 128 float a; 129 float d; 130 int cx, cy; 131 132 /* Query the sizes */ 133 SDL_GetRenderViewport(renderer, &viewport); 134 SDL_zeroa(verts); 135 cx = viewport.x + viewport.w / 2; 136 cy = viewport.y + viewport.h / 2; 137 d = (viewport.w + viewport.h) / 5.f; 138 139 cx += translate_cx; 140 cy += translate_cy; 141 142 a = (angle * SDL_PI_F) / 180.0f; 143 verts[0].position.x = cx + (d * SDL_cosf(a)) * pinch_scale; 144 verts[0].position.y = cy + (d * SDL_sinf(a)) * pinch_scale; 145 verts[0].color.r = 1.0f; 146 verts[0].color.g = 0; 147 verts[0].color.b = 0; 148 verts[0].color.a = 1.0f; 149 150 a = ((angle + 120) * SDL_PI_F) / 180.0f; 151 verts[1].position.x = cx + (d * SDL_cosf(a)) * pinch_scale; 152 verts[1].position.y = cy + (d * SDL_sinf(a)) * pinch_scale; 153 verts[1].color.r = 0; 154 verts[1].color.g = 1.0f; 155 verts[1].color.b = 0; 156 verts[1].color.a = 1.0f; 157 158 a = ((angle + 240) * SDL_PI_F) / 180.0f; 159 verts[2].position.x = cx + (d * SDL_cosf(a)) * pinch_scale; 160 verts[2].position.y = cy + (d * SDL_sinf(a)) * pinch_scale; 161 verts[2].color.r = 0; 162 verts[2].color.g = 0; 163 verts[2].color.b = 1.0f; 164 verts[2].color.a = 1.0f; 165 166 if (use_texture) { 167 verts[0].tex_coord.x = 0.5f; 168 verts[0].tex_coord.y = 0.0f; 169 verts[1].tex_coord.x = 1.0f; 170 verts[1].tex_coord.y = 1.0f; 171 verts[2].tex_coord.x = 0.0f; 172 verts[2].tex_coord.y = 1.0f; 173 } 174 175 SDL_RenderGeometry(renderer, sprites[i], verts, 3, NULL, 0); 176 } 177 178 SDL_RenderPresent(renderer); 179 } 180#ifdef SDL_PLATFORM_EMSCRIPTEN 181 if (done) { 182 emscripten_cancel_main_loop(); 183 } 184#endif 185} 186 187int main(int argc, char *argv[]) 188{ 189 int i; 190 const char *icon = "icon.png"; 191 Uint64 then, now; 192 Uint32 frames; 193 194 /* Initialize test framework */ 195 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 196 if (!state) { 197 return 1; 198 } 199 200 for (i = 1; i < argc;) { 201 int consumed; 202 203 consumed = SDLTest_CommonArg(state, i); 204 if (consumed == 0) { 205 consumed = -1; 206 if (SDL_strcasecmp(argv[i], "--blend") == 0) { 207 if (argv[i + 1]) { 208 if (SDL_strcasecmp(argv[i + 1], "none") == 0) { 209 blendMode = SDL_BLENDMODE_NONE; 210 consumed = 2; 211 } else if (SDL_strcasecmp(argv[i + 1], "blend") == 0) { 212 blendMode = SDL_BLENDMODE_BLEND; 213 consumed = 2; 214 } else if (SDL_strcasecmp(argv[i + 1], "add") == 0) { 215 blendMode = SDL_BLENDMODE_ADD; 216 consumed = 2; 217 } else if (SDL_strcasecmp(argv[i + 1], "mod") == 0) { 218 blendMode = SDL_BLENDMODE_MOD; 219 consumed = 2; 220 } else if (SDL_strcasecmp(argv[i + 1], "mul") == 0) { 221 blendMode = SDL_BLENDMODE_MUL; 222 consumed = 2; 223 } 224 } 225 } else if (SDL_strcasecmp(argv[i], "--use-texture") == 0) { 226 use_texture = true; 227 consumed = 1; 228 } 229 } 230 if (consumed < 0) { 231 static const char *options[] = { "[--blend none|blend|add|mod|mul]", "[--use-texture]", NULL }; 232 SDLTest_CommonLogUsage(state, argv[0], options); 233 return 1; 234 } 235 i += consumed; 236 } 237 if (!SDLTest_CommonInit(state)) { 238 return 2; 239 } 240 241 /* Create the windows, initialize the renderers, and load the textures */ 242 sprites = 243 (SDL_Texture **)SDL_malloc(state->num_windows * sizeof(*sprites)); 244 if (!sprites) { 245 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Out of memory!"); 246 quit(2); 247 } 248 /* Create the windows and initialize the renderers */ 249 for (i = 0; i < state->num_windows; ++i) { 250 SDL_Renderer *renderer = state->renderers[i]; 251 SDL_SetRenderDrawBlendMode(renderer, blendMode); 252 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 253 SDL_RenderClear(renderer); 254 sprites[i] = NULL; 255 } 256 if (use_texture) { 257 if (LoadSprite(icon) < 0) { 258 quit(2); 259 } 260 } 261 262 /* Main render loop */ 263 frames = 0; 264 then = SDL_GetTicks(); 265 done = 0; 266 267#ifdef SDL_PLATFORM_EMSCRIPTEN 268 emscripten_set_main_loop(loop, 0, 1); 269#else 270 while (!done) { 271 ++frames; 272 loop(); 273 } 274#endif 275 276 /* Print out some timing information */ 277 now = SDL_GetTicks(); 278 if (now > then) { 279 double fps = ((double)frames * 1000) / (now - then); 280 SDL_Log("%2.2f frames per second", fps); 281 } 282 283 quit(0); 284 285 return 0; 286} 287[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.