points.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. if (!SDL_Init(SDL_INIT_VIDEO)) {
  32. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't initialize SDL!", SDL_GetError(), NULL);
  33. return SDL_APP_FAILURE;
  34. }
  35. if (!SDL_CreateWindowAndRenderer("examples/renderer/points", WINDOW_WIDTH, WINDOW_HEIGHT, 0, &window, &renderer)) {
  36. SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Couldn't create window/renderer!", SDL_GetError(), NULL);
  37. return SDL_APP_FAILURE;
  38. }
  39. /* set up the data for a bunch of points. */
  40. for (i = 0; i < SDL_arraysize(points); i++) {
  41. points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
  42. points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
  43. point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
  44. }
  45. last_time = SDL_GetTicks();
  46. return SDL_APP_CONTINUE; /* carry on with the program! */
  47. }
  48. /* This function runs when a new event (mouse input, keypresses, etc) occurs. */
  49. SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
  50. {
  51. if (event->type == SDL_EVENT_QUIT) {
  52. return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
  53. }
  54. return SDL_APP_CONTINUE; /* carry on with the program! */
  55. }
  56. /* This function runs once per frame, and is the heart of the program. */
  57. SDL_AppResult SDL_AppIterate(void *appstate)
  58. {
  59. const Uint64 now = SDL_GetTicks();
  60. const float elapsed = ((float) (now - last_time)) / 1000.0f; /* seconds since last iteration */
  61. int i;
  62. /* let's move all our points a little for a new frame. */
  63. for (i = 0; i < SDL_arraysize(points); i++) {
  64. const float distance = elapsed * point_speeds[i];
  65. points[i].x += distance;
  66. points[i].y += distance;
  67. if ((points[i].x >= WINDOW_WIDTH) || (points[i].y >= WINDOW_HEIGHT)) {
  68. /* off the screen; restart it elsewhere! */
  69. if (SDL_rand(2)) {
  70. points[i].x = SDL_randf() * ((float) WINDOW_WIDTH);
  71. points[i].y = 0.0f;
  72. } else {
  73. points[i].x = 0.0f;
  74. points[i].y = SDL_randf() * ((float) WINDOW_HEIGHT);
  75. }
  76. point_speeds[i] = MIN_PIXELS_PER_SECOND + (SDL_randf() * (MAX_PIXELS_PER_SECOND - MIN_PIXELS_PER_SECOND));
  77. }
  78. }
  79. last_time = now;
  80. /* as you can see from this, rendering draws over whatever was drawn before it. */
  81. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); /* black, full alpha */
  82. SDL_RenderClear(renderer); /* start with a blank canvas. */
  83. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); /* white, full alpha */
  84. SDL_RenderPoints(renderer, points, SDL_arraysize(points)); /* draw all the points! */
  85. /* You can also draw single points with SDL_RenderPoint(), but it's
  86. cheaper (sometimes significantly so) to do them all at once. */
  87. SDL_RenderPresent(renderer); /* put it all on the screen! */
  88. return SDL_APP_CONTINUE; /* carry on with the program! */
  89. }
  90. /* This function runs once at shutdown. */
  91. void SDL_AppQuit(void *appstate)
  92. {
  93. /* SDL will clean up the window/renderer for us. */
  94. }