renderer-primitives.c 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * This example code creates an SDL window and renderer, and then draws a few
  3. * lines and rectangles to it every frame.
  4. *
  5. * This code is public domain. Feel free to use it for any purpose!
  6. */
  7. #define SDL_MAIN_USE_CALLBACKS 1 /* use the callbacks instead of main() */
  8. #include <SDL3/SDL.h>
  9. #include <SDL3/SDL_main.h>
  10. /* We will use this renderer to draw into this window every frame. */
  11. static SDL_Window *window = NULL;
  12. static SDL_Renderer *renderer = NULL;
  13. static SDL_FPoint points[500];
  14. /* This function runs once at startup. */
  15. int SDL_AppInit(void **appstate, int argc, char *argv[])
  16. {
  17. int i;
  18. if (SDL_CreateWindowAndRenderer("examples/renderer/clear", 640, 480, 0, &window, &renderer) == -1) {
  19. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  20. return SDL_APP_FAILURE;
  21. }
  22. /* set up some random points */
  23. SDL_srand(0); /* seed the random number generator with current time */
  24. for (i = 0; i < SDL_arraysize(points); i++) {
  25. points[i].x = (SDL_randf() * 440.0f) + 100.0f;
  26. points[i].y = (SDL_randf() * 280.0f) + 100.0f;
  27. }
  28. return SDL_APP_CONTINUE; /* carry on with the program! */
  29. }
  30. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  31. int SDL_AppEvent(void *appstate, const SDL_Event *event)
  32. {
  33. if (event->type == SDL_EVENT_QUIT) {
  34. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  35. }
  36. return SDL_APP_CONTINUE; /* carry on with the program! */
  37. }
  38. /* This function runs once per frame, and is the heart of the program. */
  39. int SDL_AppIterate(void *appstate)
  40. {
  41. SDL_FRect rect;
  42. /* as you can see from this, rendering draws over whatever was drawn before it. */
  43. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
  44. SDL_RenderClear(renderer); /* start with a blank canvas. */
  45. /* draw a filled rectangle in the middle of the canvas. */
  46. SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); /* blue, full alpha */
  47. rect.x = rect.y = 100;
  48. rect.w = 440;
  49. rect.h = 280;
  50. SDL_RenderFillRect(renderer, &rect);
  51. /* draw a unfilled rectangle in-set a little bit. */
  52. SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); /* green, full alpha */
  53. rect.x += 30;
  54. rect.y += 30;
  55. rect.w -= 60;
  56. rect.h -= 60;
  57. SDL_RenderRect(renderer, &rect);
  58. /* draw two lines in an X across the whole canvas. */
  59. SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); /* yellow, full alpha */
  60. SDL_RenderLine(renderer, 0, 0, 640, 480);
  61. SDL_RenderLine(renderer, 0, 480, 640, 0);
  62. /* draw some points across the canvas. */
  63. SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); /* red, full alpha */
  64. SDL_RenderPoints(renderer, points, SDL_arraysize(points));
  65. SDL_RenderPresent(renderer); /* put it all on the screen! */
  66. return SDL_APP_CONTINUE; /* carry on with the program! */
  67. }
  68. /* This function runs once at shutdown. */
  69. void SDL_AppQuit(void *appstate)
  70. {
  71. /* SDL will clean up the window/renderer for us. */
  72. }