diff options
| author | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
|---|---|---|
| committer | 3gg <3gg@shellblade.net> | 2025-12-27 12:03:39 -0800 |
| commit | 5a079a2d114f96d4847d1ee305d5b7c16eeec50e (patch) | |
| tree | 8926ab44f168acf787d8e19608857b3af0f82758 /contrib/SDL-3.2.8/examples/renderer/04-points/points.c | |
Initial commit
Diffstat (limited to 'contrib/SDL-3.2.8/examples/renderer/04-points/points.c')
| -rw-r--r-- | contrib/SDL-3.2.8/examples/renderer/04-points/points.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/contrib/SDL-3.2.8/examples/renderer/04-points/points.c b/contrib/SDL-3.2.8/examples/renderer/04-points/points.c new file mode 100644 index 0000000..b7b5c21 --- /dev/null +++ b/contrib/SDL-3.2.8/examples/renderer/04-points/points.c | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | /* | ||
| 2 | * This example creates an SDL window and renderer, and then draws some points | ||
| 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 | static Uint64 last_time = 0; | ||
| 16 | |||
| 17 | #define WINDOW_WIDTH 640 | ||
| 18 | #define WINDOW_HEIGHT 480 | ||
| 19 | |||
| 20 | #define NUM_POINTS 500 | ||
| 21 | #define MIN_PIXELS_PER_SECOND 30 /* move at least this many pixels per second. */ | ||
| 22 | #define MAX_PIXELS_PER_SECOND 60 /* move this many pixels per second at most. */ | ||
| 23 | |||
| 24 | /* (track everything as parallel arrays instead of a array of structs, | ||
| 25 | so we can pass the coordinates to the renderer in a single function call.) */ | ||
| 26 | |||
| 27 | /* Points are plotted as a set of X and Y coordinates. | ||
| 28 | (0, 0) is the top left of the window, and larger numbers go down | ||
| 29 | and to the right. This isn't how geometry works, but this is pretty | ||
| 30 | standard in 2D graphics. */ | ||
| 31 | static SDL_FPoint points[NUM_POINTS]; | ||
| 32 | static float point_speeds[NUM_POINTS]; | ||
| 33 | |||
| 34 | /* This function runs once at startup. */ | ||
| 35 | SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) | ||
| 36 | { | ||
| 37 | int i; | ||
| 38 | |||
| 39 | SDL_SetAppMetadata("Example Renderer Points", "1.0", "com.example.renderer-points"); | ||
| 40 | |||
| 41 | if (!SDL_Init(SDL_INIT_VIDEO)) { | ||
| 42 | SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); | ||
| 43 | return SDL_APP_FAILURE; | ||
| 44 | } | ||
| 45 | |||
| 46 | if (!SDL_CreateWindowAndRenderer("examples/renderer/points", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) { | ||
| 47 | SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); | ||
| 48 | return SDL_APP_FAILURE; | ||
| 49 | } | ||
| 50 | |||
| 51 | /* set up the data for a bunch of points. */ | ||
| 52 | for (i = 0; i < SDL_arraysize(points); i++) { | ||
| 53 | points[i].x = SDL_randf() * ((float) WINDOW_WIDTH); | ||
| 54 | points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT); | ||
| 55 | point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND)); | ||
| 56 | } | ||
| 57 | |||
| 58 | last_time = SDL_GetTicks(); | ||
| 59 | |||
| 60 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 61 | } | ||
| 62 | |||
| 63 | /* This function runs when a new event (mouse input, keypresses, etc) occurs. */ | ||
| 64 | SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) | ||
| 65 | { | ||
| 66 | if (event->type == SDL_EVENT_QUIT) { | ||
| 67 | return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ | ||
| 68 | } | ||
| 69 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 70 | } | ||
| 71 | |||
| 72 | /* This function runs once per frame, and is the heart of the program. */ | ||
| 73 | SDL_AppResult SDL_AppIterate(void *appstate) | ||
| 74 | { | ||
| 75 | const Uint64 now = SDL_GetTicks(); | ||
| 76 | const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */ | ||
| 77 | int i; | ||
| 78 | |||
| 79 | /* let's move all our points a little for a new frame. */ | ||
| 80 | for (i = 0; i < SDL_arraysize(points); i++) { | ||
| 81 | const float distance = elapsed * point_speeds[i]; | ||
| 82 | points[i].x += distance; | ||
| 83 | points[i].y += distance; | ||
| 84 | if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) { | ||
| 85 | /* off the screen; restart it elsewhere! */ | ||
| 86 | if (SDL_rand(2)) { | ||
| 87 | points[i].x = SDL_randf() * ((float) WINDOW_WIDTH); | ||
| 88 | points[i].y = 0.0f; | ||
| 89 | } else { | ||
| 90 | points[i].x = 0.0f; | ||
| 91 | points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT); | ||
| 92 | } | ||
| 93 | point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND)); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | last_time = now; | ||
| 98 | |||
| 99 | /* as you can see from this, rendering draws over whatever was drawn before it. */ | ||
| 100 | SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */ | ||
| 101 | SDL_RenderClear(renderer); /* start with a blank canvas. */ | ||
| 102 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ | ||
| 103 | SDL_RenderPoints(renderer, points, SDL_arraysize(points)); /* draw all the points! */ | ||
| 104 | |||
| 105 | /* You can also draw single points with SDL_RenderPoint(), but it's | ||
| 106 | cheaper (sometimes significantly so) to do them all at once. */ | ||
| 107 | |||
| 108 | SDL_RenderPresent(renderer); /* put it all on the screen! */ | ||
| 109 | |||
| 110 | return SDL_APP_CONTINUE; /* carry on with the program! */ | ||
| 111 | } | ||
| 112 | |||
| 113 | /* This function runs once at shutdown. */ | ||
| 114 | void SDL_AppQuit(void *appstate, SDL_AppResult result) | ||
| 115 | { | ||
| 116 | /* SDL will clean up the window/renderer for us. */ | ||
| 117 | } | ||
| 118 | |||
