Atlas - rectangles.c
Home / ext / SDL / examples / renderer / 05-rectangles Lines: 1 | Size: 4491 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 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. */ 13static SDL_Window *window = NULL; 14static SDL_Renderer *renderer = NULL; 15 16#define WINDOW_WIDTH 640 17#define WINDOW_HEIGHT 480 18 19/* This function runs once at startup. */ 20SDL_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, 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, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_LOGICAL_PRESENTATION_LETTERBOX); 34 35 return SDL_APP_CONTINUE; /* carry on with the program! */ 36} 37 38/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 39SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 40{ 41 if (event->type == SDL_EVENT_QUIT) { 42 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 43 } 44 return SDL_APP_CONTINUE; /* carry on with the program! */ 45} 46 47/* This function runs once per frame, and is the heart of the program. */ 48SDL_AppResult SDL_AppIterate(void *appstate) 49{ 50 SDL_FRect rects[16]; 51 const Uint64 now = SDL_GetTicks(); 52 int i; 53 54 /* we'll have the rectangles grow and shrink over a few seconds. */ 55 const float direction = ((now % 2000) >= 1000) ? 1.0f : -1.0f; 56 const float scale = ((float) (((int) (now % 1000)) - 500) / 500.0f) * direction; 57 58 /* as you can see from this, rendering draws over whatever was drawn before it. */ 59 SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */ 60 SDL_RenderClear(renderer); /* start with a blank canvas. */ 61 62 /* Rectangles are comprised of set of X and Y coordinates, plus width and 63 height. (0, 0) is the top left of the window, and larger numbers go 64 down and to the right. This isn't how geometry works, but this is 65 pretty standard in 2D graphics. */ 66 67 /* Let's draw a single rectangle (square, really). */ 68 rects[0].x = rects[0].y = 100; 69 rects[0].w = rects[0].h = 100 + (100 * scale); 70 SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); /* red, full alpha */ 71 SDL_RenderRect(renderer, &rects[0]); 72 73 /* Now let's draw several rectangles with one function call. */ 74 for (i = 0; i < 3; i++) { 75 const float size = (i+1) * 50.0f; 76 rects[i].w = rects[i].h = size + (size * scale); 77 rects[i].x = (WINDOW_WIDTH - rects[i].w) / 2; /* center it. */ 78 rects[i].y = (WINDOW_HEIGHT - rects[i].h) / 2; /* center it. */ 79 } 80 SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); /* green, full alpha */ 81 SDL_RenderRects(renderer, rects, 3); /* draw three rectangles at once */ 82 83 /* those were rectangle _outlines_, really. You can also draw _filled_ rectangles! */ 84 rects[0].x = 400; 85 rects[0].y = 50; 86 rects[0].w = 100 + (100 * scale); 87 rects[0].h = 50 + (50 * scale); 88 SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); /* blue, full alpha */ 89 SDL_RenderFillRect(renderer, &rects[0]); 90 91 /* ...and also fill a bunch of rectangles at once... */ 92 for (i = 0; i < SDL_arraysize(rects); i++) { 93 const float w = (float) (WINDOW_WIDTH / SDL_arraysize(rects)); 94 const float h = i * 8.0f; 95 rects[i].x = i * w; 96 rects[i].y = WINDOW_HEIGHT - h; 97 rects[i].w = w; 98 rects[i].h = h; 99 } 100 SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */ 101 SDL_RenderFillRects(renderer, rects, SDL_arraysize(rects)); 102 103 SDL_RenderPresent(renderer); /* put it all on the screen! */ 104 105 return SDL_APP_CONTINUE; /* carry on with the program! */ 106} 107 108/* This function runs once at shutdown. */ 109void SDL_AppQuit(void *appstate, SDL_AppResult result) 110{ 111 /* SDL will clean up the window/renderer for us. */ 112} 113 114[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.