primitives.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * This example creates an SDL window and renderer, and then draws some lines,
  3. * rectangles and points 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. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  16. {
  17. int i;
  18. SDL_SetAppMetadata("Example Renderer Primitives", "1.0", "com.example.renderer-primitives");
  19. if (!SDL_Init(SDL_INIT_VIDEO)) {
  20. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  21. return SDL_APP_FAILURE;
  22. }
  23. if (!SDL_CreateWindowAndRenderer("examples/renderer/primitives", 640, 480, SDL_WINDOW_RESIZABLE, &window, &renderer)) {
  24. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  25. return SDL_APP_FAILURE;
  26. }
  27. SDL_SetRenderLogicalPresentation(renderer, 640, 480, SDL_LOGICAL_PRESENTATION_LETTERBOX);
  28. /* set up some random points */
  29. for (i = 0; i < SDL_arraysize(points); i++) {
  30. points[i].x = (SDL_randf() * 440.0f) + 100.0f;
  31. points[i].y = (SDL_randf() * 280.0f) + 100.0f;
  32. }
  33. return SDL_APP_CONTINUE; /* carry on with the program! */
  34. }
  35. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  36. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  37. {
  38. if (event->type == SDL_EVENT_QUIT) {
  39. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  40. }
  41. return SDL_APP_CONTINUE; /* carry on with the program! */
  42. }
  43. /* This function runs once per frame, and is the heart of the program. */
  44. SDL_AppResult SDL_AppIterate(void *appstate)
  45. {
  46. SDL_FRect rect;
  47. /* as you can see from this, rendering draws over whatever was drawn before it. */
  48. SDL_SetRenderDrawColor(renderer, 33, 33, 33, SDL_ALPHA_OPAQUE); /* dark gray, full alpha */
  49. SDL_RenderClear(renderer); /* start with a blank canvas. */
  50. /* draw a filled rectangle in the middle of the canvas. */
  51. SDL_SetRenderDrawColor(renderer, 0, 0, 255, SDL_ALPHA_OPAQUE); /* blue, full alpha */
  52. rect.x = rect.y = 100;
  53. rect.w = 440;
  54. rect.h = 280;
  55. SDL_RenderFillRect(renderer, &rect);
  56. /* draw some points across the canvas. */
  57. SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE); /* red, full alpha */
  58. SDL_RenderPoints(renderer, points, SDL_arraysize(points));
  59. /* draw a unfilled rectangle in-set a little bit. */
  60. SDL_SetRenderDrawColor(renderer, 0, 255, 0, SDL_ALPHA_OPAQUE); /* green, full alpha */
  61. rect.x += 30;
  62. rect.y += 30;
  63. rect.w -= 60;
  64. rect.h -= 60;
  65. SDL_RenderRect(renderer, &rect);
  66. /* draw two lines in an X across the whole canvas. */
  67. SDL_SetRenderDrawColor(renderer, 255, 255, 0, SDL_ALPHA_OPAQUE); /* yellow, full alpha */
  68. SDL_RenderLine(renderer, 0, 0, 640, 480);
  69. SDL_RenderLine(renderer, 0, 480, 640, 0);
  70. SDL_RenderPresent(renderer); /* put it all on the screen! */
  71. return SDL_APP_CONTINUE; /* carry on with the program! */
  72. }
  73. /* This function runs once at shutdown. */
  74. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  75. {
  76. /* SDL will clean up the window/renderer for us. */
  77. }