points.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * This example creates an SDL window and renderer, and then draws some points
  3. * 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 Uint64 last_time = 0;
  14. #define WINDOW_WIDTH 640
  15. #define WINDOW_HEIGHT 480
  16. #define NUM_POINTS 500
  17. #define MIN_PIXELS_PER_SECOND 30 /* move at least this many pixels per second. */
  18. #define MAX_PIXELS_PER_SECOND 60 /* move this many pixels per second at most. */
  19. /* (track everything as parallel arrays instead of a array of structs,
  20. so we can pass the coordinates to the renderer in a single function call.) */
  21. /* Points are plotted as a set of X and Y coordinates.
  22. (0, 0) is the top left of the window, and larger numbers go down
  23. and to the right. This isn't how geometry works, but this is pretty
  24. standard in 2D graphics. */
  25. static SDL_FPoint points[NUM_POINTS];
  26. static float point_speeds[NUM_POINTS];
  27. /* This function runs once at startup. */
  28. SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
  29. {
  30. int i;
  31. SDL_SetAppMetadata("Example Renderer Points", "1.0", "com.example.renderer-points");
  32. if (!SDL_Init(SDL_INIT_VIDEO)) {
  33. SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
  34. return SDL_APP_FAILURE;
  35. }
  36. if (!SDL_CreateWindowAndRenderer("examples/renderer/points", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
  37. SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
  38. return SDL_APP_FAILURE;
  39. }
  40. /* set up the data for a bunch of points. */
  41. for (i = 0; i < SDL_arraysize(points); i++) {
  42. points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
  43. points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
  44. point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
  45. }
  46. last_time = SDL_GetTicks();
  47. return SDL_APP_CONTINUE; /* carry on with the program! */
  48. }
  49. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  50. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  51. {
  52. if (event->type == SDL_EVENT_QUIT) {
  53. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  54. }
  55. return SDL_APP_CONTINUE; /* carry on with the program! */
  56. }
  57. /* This function runs once per frame, and is the heart of the program. */
  58. SDL_AppResult SDL_AppIterate(void *appstate)
  59. {
  60. const Uint64 now = SDL_GetTicks();
  61. const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */
  62. int i;
  63. /* let's move all our points a little for a new frame. */
  64. for (i = 0; i < SDL_arraysize(points); i++) {
  65. const float distance = elapsed * point_speeds[i];
  66. points[i].x += distance;
  67. points[i].y += distance;
  68. if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) {
  69. /* off the screen; restart it elsewhere! */
  70. if (SDL_rand(2)) {
  71. points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
  72. points[i].y = 0.0f;
  73. } else {
  74. points[i].x = 0.0f;
  75. points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
  76. }
  77. point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
  78. }
  79. }
  80. last_time = now;
  81. /* as you can see from this, rendering draws over whatever was drawn before it. */
  82. SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); /* black, full alpha */
  83. SDL_RenderClear(renderer); /* start with a blank canvas. */
  84. SDL_SetRenderDrawColor(renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); /* white, full alpha */
  85. SDL_RenderPoints(renderer, points, SDL_arraysize(points)); /* draw all the points! */
  86. /* You can also draw single points with SDL_RenderPoint(), but it's
  87. cheaper (sometimes significantly so) to do them all at once. */
  88. SDL_RenderPresent(renderer); /* put it all on the screen! */
  89. return SDL_APP_CONTINUE; /* carry on with the program! */
  90. }
  91. /* This function runs once at shutdown. */
  92. void SDL_AppQuit(void *appstate, SDL_AppResult result)
  93. {
  94. /* SDL will clean up the window/renderer for us. */
  95. }