Atlas - clear.c

Home / ext / SDL / examples / renderer / 01-clear Lines: 1 | Size: 2680 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 * This example code creates an SDL window and renderer, and then clears the 3 * window to a different color every frame, so you'll effectively get a window 4 * that's smoothly fading between colors. 5 * 6 * This code is public domain. Feel free to use it for any purpose! 7 */ 8 9#define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */ 10#include <SDL3/SDL.h> 11#include <SDL3/SDL_main.h> 12 13/* We will use this renderer to draw into this window every frame. */ 14static SDL_Window *window = NULL; 15static SDL_Renderer *renderer = NULL; 16 17/* This function runs once at startup. */ 18SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) 19{ 20 SDL_SetAppMetadata("Example Renderer Clear", "1.0", "com.example.renderer-clear"); 21 22 if (!SDL_Init(SDL_INIT_VIDEO)) { 23 SDL_Log("Couldn't initialize SDL: %s", SDL_GetError()); 24 return SDL_APP_FAILURE; 25 } 26 27 if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) { 28 SDL_Log("Couldn't create window/renderer: %s", SDL_GetError()); 29 return SDL_APP_FAILURE; 30 } 31 SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX); 32 33 return SDL_APP_CONTINUE; /* carry on with the program! */ 34} 35 36/* This function runs when a new event (mouse input, keypresses, etc) occurs. */ 37SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) 38{ 39 if (event->type == SDL_EVENT_QUIT) { 40 return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ 41 } 42 return SDL_APP_CONTINUE; /* carry on with the program! */ 43} 44 45/* This function runs once per frame, and is the heart of the program. */ 46SDL_AppResult SDL_AppIterate(void *appstate) 47{ 48 const double now = ((double)SDL_GetTicks()) / 1000.0; /* convert from milliseconds to seconds. */ 49 /* choose the color for the frame we will draw. The sine wave trick makes it fade between colors smoothly. */ 50 const float red = (float) (0.5 + 0.5 * SDL_sin(now)); 51 const float green = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3)); 52 const float blue = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3)); 53 SDL_SetRenderDrawColorFloat(renderer, red, green, blue, SDL_ALPHA_OPAQUE_FLOAT); /* new color, full alpha. */ 54 55 /* clear the window to the draw color. */ 56 SDL_RenderClear(renderer); 57 58 /* put the newly-cleared rendering on the screen. */ 59 SDL_RenderPresent(renderer); 60 61 return SDL_APP_CONTINUE; /* carry on with the program! */ 62} 63 64/* This function runs once at shutdown. */ 65void SDL_AppQuit(void *appstate, SDL_AppResult result) 66{ 67 /* SDL will clean up the window/renderer for us. */ 68} 69 70
[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.