renderer-primitives.c 3.2 KB

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