primitives.c 3.3 KB

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