diff options
author | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2025-08-30 16:53:58 -0700 |
commit | 6aaedb813fa11ba0679c3051bc2eb28646b9506c (patch) | |
tree | 34acbfc9840e02cb4753e6306ea7ce978bf8b58e /src/contrib/SDL-3.2.20/examples/renderer/05-rectangles | |
parent | 8f228ade99dd3d4c8da9b78ade1815c9adf85c8f (diff) |
Update to SDL3
Diffstat (limited to 'src/contrib/SDL-3.2.20/examples/renderer/05-rectangles')
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/README.txt | 3 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/onmouseover.webp | bin | 0 -> 75754 bytes | |||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/rectangles.c | 112 | ||||
-rw-r--r-- | src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/thumbnail.png | bin | 0 -> 3634 bytes |
4 files changed, 115 insertions, 0 deletions
diff --git a/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/README.txt b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/README.txt new file mode 100644 index 0000000..26613c7 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/README.txt | |||
@@ -0,0 +1,3 @@ | |||
1 | This example creates an SDL window and renderer, and then draws a few | ||
2 | rectangles that change size each frame. | ||
3 | |||
diff --git a/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/onmouseover.webp b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/onmouseover.webp new file mode 100644 index 0000000..cdfd376 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/onmouseover.webp | |||
Binary files differ | |||
diff --git a/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/rectangles.c b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/rectangles.c new file mode 100644 index 0000000..3aa7242 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/rectangles.c | |||
@@ -0,0 +1,112 @@ | |||
1 | /* | ||
2 | * This example creates an SDL window and renderer, and then draws some | ||
3 | * rectangles 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 | #define WINDOW_WIDTH 640 | ||
17 | #define WINDOW_HEIGHT 480 | ||
18 | |||
19 | /* This function runs once at startup. */ | ||
20 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
21 | { | ||
22 | SDL_SetAppMetadata("Example Renderer Rectangles", "1.0", "com.example.renderer-rectangles"); | ||
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/rectangles", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) { | ||
30 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
31 | return SDL_APP_FAILURE; | ||
32 | } | ||
33 | |||
34 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
35 | } | ||
36 | |||
37 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
38 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
39 | { | ||
40 | if (event->type == SDL_EVENT_QUIT) { | ||
41 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
42 | } | ||
43 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
44 | } | ||
45 | |||
46 | /* This function runs once per frame, and is the heart of the program. */ | ||
47 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
48 | { | ||
49 | SDL_FRect rects[16]; | ||
50 | const Uint64 now = SDL_GetTicks(); | ||
51 | int i; | ||
52 | |||
53 | /* we'll have the rectangles grow and shrink over a few seconds. */ | ||
54 | const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f; | ||
55 | const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction; | ||
56 | |||
57 | /* as you can see from this, rendering draws over whatever was drawn before it. */ | ||
58 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */ | ||
59 | SDL_RenderClear(renderer); /* start with a blank canvas. */ | ||
60 | |||
61 | /* Rectangles are comprised of set of X and Y coordinates, plus width and | ||
62 | height. (0, 0) is the top left of the window, and larger numbers go | ||
63 | down and to the right. This isn't how geometry works, but this is | ||
64 | pretty standard in 2D graphics. */ | ||
65 | |||
66 | /* Let's draw a single rectangle (square, really). */ | ||
67 | rects[0].x = rects[0].y = 100; | ||
68 | rects[0].w = rects[0].h = 100 + (100 * scale); | ||
69 | SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); /* red, full alpha */ | ||
70 | SDL_RenderRect(renderer, &rects[0]); | ||
71 | |||
72 | /* Now let's draw several rectangles with one function call. */ | ||
73 | for (i = 0; i < 3; i++) { | ||
74 | const float size = (i+1) * 50.0f; | ||
75 | rects[i].w = rects[i].h = size + (size * scale); | ||
76 | rects[i].x = (WINDOW_WIDTH - rects[i].w) / 2; /* center it. */ | ||
77 | rects[i].y = (WINDOW_HEIGHT - rects[i].h) / 2; /* center it. */ | ||
78 | } | ||
79 | SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); /* green, full alpha */ | ||
80 | SDL_RenderRects(renderer, rects, 3); /* draw three rectangles at once */ | ||
81 | |||
82 | /* those were rectangle _outlines_, really. You can also draw _filled_ rectangles! */ | ||
83 | rects[0].x = 400; | ||
84 | rects[0].y = 50; | ||
85 | rects[0].w = 100 + (100 * scale); | ||
86 | rects[0].h = 50 + (50 * scale); | ||
87 | SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); /* blue, full alpha */ | ||
88 | SDL_RenderFillRect(renderer, &rects[0]); | ||
89 | |||
90 | /* ...and also fill a bunch of rectangles at once... */ | ||
91 | for (i = 0; i < SDL_arraysize(rects); i++) { | ||
92 | const float w = (float) (WINDOW_WIDTH / SDL_arraysize(rects)); | ||
93 | const float h = i * 8.0f; | ||
94 | rects[i].x = i * w; | ||
95 | rects[i].y = WINDOW_HEIGHT - h; | ||
96 | rects[i].w = w; | ||
97 | rects[i].h = h; | ||
98 | } | ||
99 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ | ||
100 | SDL_RenderFillRects(renderer, rects, SDL_arraysize(rects)); | ||
101 | |||
102 | SDL_RenderPresent(renderer); /* put it all on the screen! */ | ||
103 | |||
104 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
105 | } | ||
106 | |||
107 | /* This function runs once at shutdown. */ | ||
108 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
109 | { | ||
110 | /* SDL will clean up the window/renderer for us. */ | ||
111 | } | ||
112 | |||
diff --git a/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/thumbnail.png b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/thumbnail.png new file mode 100644 index 0000000..64e6688 --- /dev/null +++ b/src/contrib/SDL-3.2.20/examples/renderer/05-rectangles/thumbnail.png | |||
Binary files differ | |||