diff options
Diffstat (limited to 'contrib/SDL-3.2.8/examples/renderer/02-primitives/primitives.c')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/renderer/02-primitives/primitives.c | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/renderer/02-primitives/primitives.c b/contrib/SDL-3.2.8/examples/renderer/02-primitives/primitives.c new file mode 100644 index 0000000..5cfd731 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/renderer/02-primitives/primitives.c | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | /* | ||
| 2 | * This example creates an SDL window and renderer, and then draws some lines, | ||
| 3 | * rectangles and points 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 | static SDL_FPoint points[500]; | ||
| 16 | |||
| 17 | /* This function runs once at startup. */ | ||
| 18 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 19 | { | ||
| 20 | int i; | ||
| 21 | |||
| 22 | SDL_SetAppMetadata("Example Renderer Primitives", "1.0", "com.example.renderer-primitives"); | ||
| 23 | |||
| 24 | if (!SDL_Init(SDL_INIT_VIDEO)) { | ||
| 25 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 26 | return SDL_APP_FAILURE; | ||
| 27 | } | ||
| 28 | |||
| 29 | if (!SDL_CreateWindowAndRenderer("examples/renderer/primitives", 640, 480, 0, &window, &renderer)) { | ||
| 30 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 31 | return SDL_APP_FAILURE; | ||
| 32 | } | ||
| 33 | |||
| 34 | /* set up some random points */ | ||
| 35 | for (i = 0; i < SDL_arraysize(points); i++) { | ||
| 36 | points[i].x = (SDL_randf() * 440.0f) + 100.0f; | ||
| 37 | points[i].y = (SDL_randf() * 280.0f) + 100.0f; | ||
| 38 | } | ||
| 39 | |||
| 40 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 41 | } | ||
| 42 | |||
| 43 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 44 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 45 | { | ||
| 46 | if (event->type == SDL_EVENT_QUIT) { | ||
| 47 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 48 | } | ||
| 49 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 50 | } | ||
| 51 | |||
| 52 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 53 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 54 | { | ||
| 55 | SDL_FRect rect; | ||
| 56 | |||
| 57 | /* as you can see from this, rendering draws over whatever was drawn before it. */ | ||
| 58 | SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* dark gray, full alpha */ | ||
| 59 | SDL_RenderClear(renderer); /* start with a blank canvas. */ | ||
| 60 | |||
| 61 | /* draw a filled rectangle in the middle of the canvas. */ | ||
| 62 | SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); /* blue, full alpha */ | ||
| 63 | rect.x = rect.y = 100; | ||
| 64 | rect.w = 440; | ||
| 65 | rect.h = 280; | ||
| 66 | SDL_RenderFillRect(renderer, &rect); | ||
| 67 | |||
| 68 | /* draw some points across the canvas. */ | ||
| 69 | SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); /* red, full alpha */ | ||
| 70 | SDL_RenderPoints(renderer, points, SDL_arraysize(points)); | ||
| 71 | |||
| 72 | /* draw a unfilled rectangle in-set a little bit. */ | ||
| 73 | SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); /* green, full alpha */ | ||
| 74 | rect.x += 30; | ||
| 75 | rect.y += 30; | ||
| 76 | rect.w -= 60; | ||
| 77 | rect.h -= 60; | ||
| 78 | SDL_RenderRect(renderer, &rect); | ||
| 79 | |||
| 80 | /* draw two lines in an X across the whole canvas. */ | ||
| 81 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /* yellow, full alpha */ | ||
| 82 | SDL_RenderLine(renderer, 0, 0, 640, 480); | ||
| 83 | SDL_RenderLine(renderer, 0, 480, 640, 0); | ||
| 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. */ | ||
| 91 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 92 | { | ||
| 93 | /* SDL will clean up the window/renderer for us. */ | ||
| 94 | } | ||
| 95 | |||
