Atlas - points.c
Home / ext / SDL / examples / renderer / 04-points Lines: 1 | Size: 4617 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 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. */ 13static SDL_Window *window = NULL; 14static SDL_Renderer *renderer = NULL; 15static 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. */ 31static SDL_FPoint points[NUM_POINTS]; 32static float point_speeds[NUM_POINTS]; 33 34/* This function runs once at startup. */ 35SDL_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, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 47 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 48 return SDL_APP_FAILURE; 49 } 50 SDL_SetRenderLogicalPresentation(renderer, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 51 52 /* set up the data for a bunch of points. */ 53 for (i = 0; i < SDL_arraysize(points); i++) { 54 points[i].x = SDL_randf() * ((float) WINDOW_WIDTH); 55 points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT); 56 point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND)); 57 } 58 59 last_time = SDL_GetTicks(); 60 61 return SDL_APP_CONTINUE; /* carry on with the program! */ 62} 63 64/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 65SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 66{ 67 if (event->type == SDL_EVENT_QUIT) { 68 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 69 } 70 return SDL_APP_CONTINUE; /* carry on with the program! */ 71} 72 73/* This function runs once per frame, and is the heart of the program. */ 74SDL_AppResult SDL_AppIterate(void *appstate) 75{ 76 const Uint64 now = SDL_GetTicks(); 77 const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */ 78 int i; 79 80 /* let's move all our points a little for a new frame. */ 81 for (i = 0; i < SDL_arraysize(points); i++) { 82 const float distance = elapsed * point_speeds[i]; 83 points[i].x += distance; 84 points[i].y += distance; 85 if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) { 86 /* off the screen; restart it elsewhere! */ 87 if (SDL_rand(2)) { 88 points[i].x = SDL_randf() * ((float) WINDOW_WIDTH); 89 points[i].y = 0.0f; 90 } else { 91 points[i].x = 0.0f; 92 points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT); 93 } 94 point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND)); 95 } 96 } 97 98 last_time = now; 99 100 /* as you can see from this, rendering draws over whatever was drawn before it. */ 101 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */ 102 SDL_RenderClear(renderer); /* start with a blank canvas. */ 103 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ 104 SDL_RenderPoints(renderer, points, SDL_arraysize(points)); /* draw all the points! */ 105 106 /* You can also draw single points with SDL_RenderPoint(), but it's 107 cheaper (sometimes significantly so) to do them all at once. */ 108 109 SDL_RenderPresent(renderer); /* put it all on the screen! */ 110 111 return SDL_APP_CONTINUE; /* carry on with the program! */ 112} 113 114/* This function runs once at shutdown. */ 115void SDL_AppQuit(void *appstate, SDL_AppResult result) 116{ 117 /* SDL will clean up the window/renderer for us. */ 118} 119 120[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.