Atlas - lines.c

Home / ext / SDL / examples / renderer / 03-lines Lines: 1 | Size: 3820 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example creates an SDL window and renderer, and then draws some lines 3 * to it every frame. 4 * 5 * This code is public domain. Feel free to use it for any purpose! 6 */ 7 8#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 9#include <SDL3/SDL.h> 10#include <SDL3/SDL_main.h> 11 12/* We will use this renderer to draw into this window every frame. */ 13static SDL_Window *window = NULL; 14static SDL_Renderer *renderer = NULL; 15 16/* This function runs once at startup. */ 17SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 18{ 19 SDL_SetAppMetadata("Example Renderer Lines", "1.0", "com.example.renderer-lines"); 20 21 if (!SDL_Init(SDL_INIT_VIDEO)) { 22 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 23 return SDL_APP_FAILURE; 24 } 25 26 if (!SDL_CreateWindowAndRenderer("examples/renderer/lines", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 27 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 28 return SDL_APP_FAILURE; 29 } 30 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 31 32 return SDL_APP_CONTINUE; /* carry on with the program! */ 33} 34 35/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 36SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 37{ 38 if (event->type == SDL_EVENT_QUIT) { 39 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 40 } 41 return SDL_APP_CONTINUE; /* carry on with the program! */ 42} 43 44/* This function runs once per frame, and is the heart of the program. */ 45SDL_AppResult SDL_AppIterate(void *appstate) 46{ 47 int i; 48 49 /* Lines (line segments, really) are drawn in terms of points: a set of 50 X and Y coordinates, one set for each end of the line. 51 (0, 0) is the top left of the window, and larger numbers go down 52 and to the right. This isn't how geometry works, but this is pretty 53 standard in 2D graphics. */ 54 static const SDL_FPoint line_points[] = { 55 { 100, 354 }, { 220, 230 }, { 140, 230 }, { 320, 100 }, { 500, 230 }, 56 { 420, 230 }, { 540, 354 }, { 400, 354 }, { 100, 354 } 57 }; 58 59 /* as you can see from this, rendering draws over whatever was drawn before it. */ 60 SDL_SetRenderDrawColor(renderer, 100, 100, 100, SDL_ALPHA_OPAQUE); /* grey, full alpha */ 61 SDL_RenderClear(renderer); /* start with a blank canvas. */ 62 63 /* You can draw lines, one at a time, like these brown ones... */ 64 SDL_SetRenderDrawColor(renderer, 127, 49, 32, SDL_ALPHA_OPAQUE); 65 SDL_RenderLine(renderer, 240, 450, 400, 450); 66 SDL_RenderLine(renderer, 240, 356, 400, 356); 67 SDL_RenderLine(renderer, 240, 356, 240, 450); 68 SDL_RenderLine(renderer, 400, 356, 400, 450); 69 70 /* You can also draw a series of connected lines in a single batch... */ 71 SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); 72 SDL_RenderLines(renderer, line_points, SDL_arraysize(line_points)); 73 74 /* here's a bunch of lines drawn out from a center point in a circle. */ 75 /* we randomize the color of each line, so it functions as animation. */ 76 for (i = 0; i < 360; i++) { 77 const float size = 30.0f; 78 const float x = 320.0f; 79 const float y = 95.0f - (size / 2.0f); 80 const float r = (float) i * (SDL_PI_F / 180.0f); 81 SDL_SetRenderDrawColor(renderer, SDL_rand(256), SDL_rand(256), SDL_rand(256), SDL_ALPHA_OPAQUE); 82 SDL_RenderLine(renderer, x, y, x + SDL_cosf(r) * size, y + SDL_sinf(r) * size); 83 } 84 85 SDL_RenderPresent(renderer); /* put it all on the screen! */ 86 87 return SDL_APP_CONTINUE; /* carry on with the program! */ 88} 89 90/* This function runs once at shutdown. */ 91void SDL_AppQuit(void *appstate, SDL_AppResult result) 92{ 93 /* SDL will clean up the window/renderer for us. */ 94} 95 96
[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.