12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- /*
- * This example code creates an SDL window and renderer, and then clears the
- * window to a different color every frame, so you'll effectively get a window
- * that's smoothly fading between colors.
- *
- * This code is public domain. Feel free to use it for any purpose!
- */
- #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
- #include <SDL3/SDL.h>
- #include <SDL3/SDL_main.h>
- /* We will use this renderer to draw into this window every frame. */
- static SDL_Window *window = NULL;
- static SDL_Renderer *renderer = NULL;
- /* the current red color we're clearing to. */
- static Uint8 red = 0;
- /* When fading up, this is 1, when fading down, it's -1. */
- static int fade_direction = 1;
- /* This function runs once at startup. */
- SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
- {
- if (!SDL_Init(SDL_INIT_VIDEO)) {
- SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
- return SDL_APP_FAILURE;
- }
- if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, 0, &window, &renderer)) {
- SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
- return SDL_APP_FAILURE;
- }
- SDL_SetRenderVSync(renderer, 1); /* try to show frames at the monitor refresh rate. */
- return SDL_APP_CONTINUE; /* carry on with the program! */
- }
- /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
- SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
- {
- if (event->type == SDL_EVENT_QUIT) {
- return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
- }
- return SDL_APP_CONTINUE; /* carry on with the program! */
- }
- /* This function runs once per frame, and is the heart of the program. */
- SDL_AppResult SDL_AppIterate(void *appstate)
- {
- /* since we're always fading red, we leave green and blue at zero.
- alpha doesn't mean much here, so leave it at full (255, no transparency). */
- SDL_SetRenderDrawColor(renderer, red, 0, 0, 255);
- /* clear the window to the draw color. */
- SDL_RenderClear(renderer);
- /* put the newly-cleared rendering on the screen. */
- SDL_RenderPresent(renderer);
- /* update the color for the next frame we will draw. */
- if (fade_direction > 0) {
- if (red == 255) {
- fade_direction = -1;
- } else {
- red++;
- }
- } else if (fade_direction < 0) {
- if (red == 0) {
- fade_direction = 1;
- } else {
- red--;
- }
- }
- return SDL_APP_CONTINUE; /* carry on with the program! */
- }
- /* This function runs once at shutdown. */
- void SDL_AppQuit(void *appstate)
- {
- /* SDL will clean up the window/renderer for us. */
- }
|