renderer-clear.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  9. #include <SDL3/SDL.h>
  10. #include <SDL3/SDL_main.h>
  11. /* We will use this renderer to draw into this window every frame. */
  12. static SDL_Window *window = NULL;
  13. static SDL_Renderer *renderer = NULL;
  14. /* the current red color we're clearing to. */
  15. static Uint8 red = 0;
  16. /* When fading up, this is 1, when fading down, it's -1. */
  17. static int fade_direction = 1;
  18. /* This function runs once at startup. */
  19. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  20. {
  21. if (!SDL_Init(SDL_INIT_VIDEO)) {
  22. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  23. return SDL_APP_FAILURE;
  24. }
  25. if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, 0, &window, &renderer)) {
  26. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  27. return SDL_APP_FAILURE;
  28. }
  29. SDL_SetRenderVSync(renderer, 1); /* try to show frames at the monitor refresh rate. */
  30. return SDL_APP_CONTINUE; /* carry on with the program! */
  31. }
  32. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  33. SDL_AppResult SDL_AppEvent(void *appstate, const SDL_Event *event)
  34. {
  35. if (event->type == SDL_EVENT_QUIT) {
  36. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  37. }
  38. return SDL_APP_CONTINUE; /* carry on with the program! */
  39. }
  40. /* This function runs once per frame, and is the heart of the program. */
  41. SDL_AppResult SDL_AppIterate(void *appstate)
  42. {
  43. /* since we're always fading red, we leave green and blue at zero.
  44. alpha doesn't mean much here, so leave it at full (255, no transparency). */
  45. SDL_SetRenderDrawColor(renderer, red, 0, 0, 255);
  46. /* clear the window to the draw color. */
  47. SDL_RenderClear(renderer);
  48. /* put the newly-cleared rendering on the screen. */
  49. SDL_RenderPresent(renderer);
  50. /* update the color for the next frame we will draw. */
  51. if (fade_direction > 0) {
  52. if (red == 255) {
  53. fade_direction = -1;
  54. } else {
  55. red++;
  56. }
  57. } else if (fade_direction < 0) {
  58. if (red == 0) {
  59. fade_direction = 1;
  60. } else {
  61. red--;
  62. }
  63. }
  64. return SDL_APP_CONTINUE; /* carry on with the program! */
  65. }
  66. /* This function runs once at shutdown. */
  67. void SDL_AppQuit(void *appstate)
  68. {
  69. /* SDL will clean up the window/renderer for us. */
  70. }