🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB

  1. /*
  2. * rectangles.c
  3. * written by Holmes Futrell
  4. * use however you want
  5. */
  6. #include "SDL.h"
  7. #include <time.h>
  8. #include "common.h"
  9. void
  10. render(SDL_Renderer *renderer)
  11. {
  12. Uint8 r, g, b;
  13. int renderW;
  14. int renderH;
  15. SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
  16. /* Come up with a random rectangle */
  17. SDL_Rect rect;
  18. rect.w = randomInt(64, 128);
  19. rect.h = randomInt(64, 128);
  20. rect.x = randomInt(0, renderW);
  21. rect.y = randomInt(0, renderH);
  22. /* Come up with a random color */
  23. r = randomInt(50, 255);
  24. g = randomInt(50, 255);
  25. b = randomInt(50, 255);
  26. /* Fill the rectangle in the color */
  27. SDL_SetRenderDrawColor(renderer, r, g, b, 255);
  28. SDL_RenderFillRect(renderer, &rect);
  29. /* update screen */
  30. SDL_RenderPresent(renderer);
  31. }
  32. int
  33. main(int argc, char *argv[])
  34. {
  35. SDL_Window *window;
  36. SDL_Renderer *renderer;
  37. int done;
  38. SDL_Event event;
  39. int windowW;
  40. int windowH;
  41. /* initialize SDL */
  42. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  43. fatalError("Could not initialize SDL");
  44. }
  45. /* seed random number generator */
  46. srand(time(NULL));
  47. /* create window and renderer */
  48. window = SDL_CreateWindow(NULL, 0, 0, 320, 480, SDL_WINDOW_ALLOW_HIGHDPI);
  49. if (window == 0) {
  50. fatalError("Could not initialize Window");
  51. }
  52. renderer = SDL_CreateRenderer(window, -1, 0);
  53. if (!renderer) {
  54. fatalError("Could not create renderer");
  55. }
  56. SDL_GetWindowSize(window, &windowW, &windowH);
  57. SDL_RenderSetLogicalSize(renderer, windowW, windowH);
  58. /* Fill screen with black */
  59. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  60. SDL_RenderClear(renderer);
  61. /* Enter render loop, waiting for user to quit */
  62. done = 0;
  63. while (!done) {
  64. while (SDL_PollEvent(&event)) {
  65. if (event.type == SDL_QUIT) {
  66. done = 1;
  67. }
  68. }
  69. render(renderer);
  70. SDL_Delay(1);
  71. }
  72. /* shutdown SDL */
  73. SDL_Quit();
  74. return 0;
  75. }