Atlas - primitives.c

Home / ext / SDL / examples / renderer / 02-primitives Lines: 1 | Size: 3451 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 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. */ 13static SDL_Window *window = NULL; 14static SDL_Renderer *renderer = NULL; 15static SDL_FPoint points[500]; 16 17/* This function runs once at startup. */ 18SDL_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, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 30 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 31 return SDL_APP_FAILURE; 32 } 33 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 34 35 /* set up some random points */ 36 for (i = 0; i < SDL_arraysize(points); i++) { 37 points[i].x = (SDL_randf() * 440.0f) + 100.0f; 38 points[i].y = (SDL_randf() * 280.0f) + 100.0f; 39 } 40 41 return SDL_APP_CONTINUE; /* carry on with the program! */ 42} 43 44/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 45SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 46{ 47 if (event->type == SDL_EVENT_QUIT) { 48 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 49 } 50 return SDL_APP_CONTINUE; /* carry on with the program! */ 51} 52 53/* This function runs once per frame, and is the heart of the program. */ 54SDL_AppResult SDL_AppIterate(void *appstate) 55{ 56 SDL_FRect rect; 57 58 /* as you can see from this, rendering draws over whatever was drawn before it. */ 59 SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* dark gray, full alpha */ 60 SDL_RenderClear(renderer); /* start with a blank canvas. */ 61 62 /* draw a filled rectangle in the middle of the canvas. */ 63 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); /* blue, full alpha */ 64 rect.x = rect.y = 100; 65 rect.w = 440; 66 rect.h = 280; 67 SDL_RenderFillRect(renderer, &rect); 68 69 /* draw some points across the canvas. */ 70 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); /* red, full alpha */ 71 SDL_RenderPoints(renderer, points, SDL_arraysize(points)); 72 73 /* draw a unfilled rectangle in-set a little bit. */ 74 SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); /* green, full alpha */ 75 rect.x += 30; 76 rect.y += 30; 77 rect.w -= 60; 78 rect.h -= 60; 79 SDL_RenderRect(renderer, &rect); 80 81 /* draw two lines in an X across the whole canvas. */ 82 SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /* yellow, full alpha */ 83 SDL_RenderLine(renderer, 0, 0, 640, 480); 84 SDL_RenderLine(renderer, 0, 480, 640, 0); 85 86 SDL_RenderPresent(renderer); /* put it all on the screen! */ 87 88 return SDL_APP_CONTINUE; /* carry on with the program! */ 89} 90 91/* This function runs once at shutdown. */ 92void SDL_AppQuit(void *appstate, SDL_AppResult result) 93{ 94 /* SDL will clean up the window/renderer for us. */ 95} 96 97
[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.