clear.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. /* This function runs once at startup. */
  15. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  16. {
  17. if (!SDL_Init(SDL_INIT_VIDEO)) {
  18. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  19. return SDL_APP_FAILURE;
  20. }
  21. if (!SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, 0, &window, &renderer)) {
  22. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  23. return SDL_APP_FAILURE;
  24. }
  25. return SDL_APP_CONTINUE; /* carry on with the program! */
  26. }
  27. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  28. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  29. {
  30. if (event->type == SDL_EVENT_QUIT) {
  31. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  32. }
  33. return SDL_APP_CONTINUE; /* carry on with the program! */
  34. }
  35. /* This function runs once per frame, and is the heart of the program. */
  36. SDL_AppResult SDL_AppIterate(void *appstate)
  37. {
  38. const double now = ((double)SDL_GetTicks()) / 1000.0; /* convert from milliseconds to seconds. */
  39. /* choose the color for the frame we will draw. The sine wave trick makes it fade between colors smoothly. */
  40. const float red = (float) (0.5 + 0.5 * SDL_sin(now));
  41. const float green = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 2 / 3));
  42. const float blue = (float) (0.5 + 0.5 * SDL_sin(now + SDL_PI_D * 4 / 3));
  43. SDL_SetRenderDrawColorFloat(renderer, red, green, blue, 1.0f); /* new color, full alpha. */
  44. /* clear the window to the draw color. */
  45. SDL_RenderClear(renderer);
  46. /* put the newly-cleared rendering on the screen. */
  47. SDL_RenderPresent(renderer);
  48. return SDL_APP_CONTINUE; /* carry on with the program! */
  49. }
  50. /* This function runs once at shutdown. */
  51. void SDL_AppQuit(void *appstate)
  52. {
  53. /* SDL will clean up the window/renderer for us. */
  54. }