diff options
Diffstat (limited to 'contrib/SDL-3.2.8/examples/renderer/03-lines/lines.c')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/renderer/03-lines/lines.c | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/renderer/03-lines/lines.c b/contrib/SDL-3.2.8/examples/renderer/03-lines/lines.c new file mode 100644 index 0000000..54f7eb1 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/renderer/03-lines/lines.c | |||
| @@ -0,0 +1,93 @@ | |||
| 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. */ | ||
| 13 | static SDL_Window *window = NULL; | ||
| 14 | static SDL_Renderer *renderer = NULL; | ||
| 15 | |||
| 16 | /* This function runs once at startup. */ | ||
| 17 | SDL_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, 0, &window, &renderer)) { | ||
| 27 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 28 | return SDL_APP_FAILURE; | ||
| 29 | } | ||
| 30 | |||
| 31 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 32 | } | ||
| 33 | |||
| 34 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 35 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 36 | { | ||
| 37 | if (event->type == SDL_EVENT_QUIT) { | ||
| 38 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 39 | } | ||
| 40 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 41 | } | ||
| 42 | |||
| 43 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 44 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 45 | { | ||
| 46 | int i; | ||
| 47 | |||
| 48 | /* Lines (line segments, really) are drawn in terms of points: a set of | ||
| 49 | X and Y coordinates, one set for each end of the line. | ||
| 50 | (0, 0) is the top left of the window, and larger numbers go down | ||
| 51 | and to the right. This isn't how geometry works, but this is pretty | ||
| 52 | standard in 2D graphics. */ | ||
| 53 | static const SDL_FPoint line_points[] = { | ||
| 54 | { 100, 354 }, { 220, 230 }, { 140, 230 }, { 320, 100 }, { 500, 230 }, | ||
| 55 | { 420, 230 }, { 540, 354 }, { 400, 354 }, { 100, 354 } | ||
| 56 | }; | ||
| 57 | |||
| 58 | /* as you can see from this, rendering draws over whatever was drawn before it. */ | ||
| 59 | SDL_SetRenderDrawColor(renderer, 100, 100, 100, SDL_ALPHA_OPAQUE); /* grey, full alpha */ | ||
| 60 | SDL_RenderClear(renderer); /* start with a blank canvas. */ | ||
| 61 | |||
| 62 | /* You can draw lines, one at a time, like these brown ones... */ | ||
| 63 | SDL_SetRenderDrawColor(renderer, 127, 49, 32, SDL_ALPHA_OPAQUE); | ||
| 64 | SDL_RenderLine(renderer, 240, 450, 400, 450); | ||
| 65 | SDL_RenderLine(renderer, 240, 356, 400, 356); | ||
| 66 | SDL_RenderLine(renderer, 240, 356, 240, 450); | ||
| 67 | SDL_RenderLine(renderer, 400, 356, 400, 450); | ||
| 68 | |||
| 69 | /* You can also draw a series of connected lines in a single batch... */ | ||
| 70 | SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); | ||
| 71 | SDL_RenderLines(renderer, line_points, SDL_arraysize(line_points)); | ||
| 72 | |||
| 73 | /* here's a bunch of lines drawn out from a center point in a circle. */ | ||
| 74 | /* we randomize the color of each line, so it functions as animation. */ | ||
| 75 | for (i = 0; i < 360; i++) { | ||
| 76 | const float size = 30.0f; | ||
| 77 | const float x = 320.0f; | ||
| 78 | const float y = 95.0f - (size / 2.0f); | ||
| 79 | SDL_SetRenderDrawColor(renderer, SDL_rand(256), SDL_rand(256), SDL_rand(256), SDL_ALPHA_OPAQUE); | ||
| 80 | SDL_RenderLine(renderer, x, y, x + SDL_sinf((float) i) * size, y + SDL_cosf((float) i) * size); | ||
| 81 | } | ||
| 82 | |||
| 83 | SDL_RenderPresent(renderer); /* put it all on the screen! */ | ||
| 84 | |||
| 85 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 86 | } | ||
| 87 | |||
| 88 | /* This function runs once at shutdown. */ | ||
| 89 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 90 | { | ||
| 91 | /* SDL will clean up the window/renderer for us. */ | ||
| 92 | } | ||
| 93 | |||
