🛠️🐜 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.

100 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 <stdio.h>
  8. #include <stdlib.h>
  9. #include <time.h>
  10. #define SCREEN_WIDTH 320
  11. #define SCREEN_HEIGHT 480
  12. int
  13. randomInt(int min, int max)
  14. {
  15. return min + rand() % (max - min + 1);
  16. }
  17. void
  18. render(SDL_Renderer *renderer)
  19. {
  20. SDL_Rect rect;
  21. Uint8 r, g, b;
  22. /* Clear the screen */
  23. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  24. SDL_RenderClear(renderer);
  25. /* Come up with a random rectangle */
  26. rect.w = randomInt(64, 128);
  27. rect.h = randomInt(64, 128);
  28. rect.x = randomInt(0, SCREEN_WIDTH);
  29. rect.y = randomInt(0, SCREEN_HEIGHT);
  30. /* Come up with a random color */
  31. r = randomInt(50, 255);
  32. g = randomInt(50, 255);
  33. b = randomInt(50, 255);
  34. SDL_SetRenderDrawColor(renderer, r, g, b, 255);
  35. /* Fill the rectangle in the color */
  36. SDL_RenderFillRect(renderer, &rect);
  37. /* update screen */
  38. SDL_RenderPresent(renderer);
  39. }
  40. int
  41. main(int argc, char *argv[])
  42. {
  43. SDL_Window *window;
  44. SDL_Renderer *renderer;
  45. int done;
  46. SDL_Event event;
  47. /* initialize SDL */
  48. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  49. printf("Could not initialize SDL\n");
  50. return 1;
  51. }
  52. /* seed random number generator */
  53. srand(time(NULL));
  54. /* create window and renderer */
  55. window =
  56. SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
  57. SDL_WINDOW_OPENGL);
  58. if (!window) {
  59. printf("Could not initialize Window\n");
  60. return 1;
  61. }
  62. renderer = SDL_CreateRenderer(window, -1, 0);
  63. if (!renderer) {
  64. printf("Could not create renderer\n");
  65. return 1;
  66. }
  67. /* Enter render loop, waiting for user to quit */
  68. done = 0;
  69. while (!done) {
  70. while (SDL_PollEvent(&event)) {
  71. if (event.type == SDL_QUIT) {
  72. done = 1;
  73. }
  74. }
  75. render(renderer);
  76. SDL_Delay(1);
  77. }
  78. /* shutdown SDL */
  79. SDL_Quit();
  80. return 0;
  81. }