Advertisement
carefulnow

SDL Geometry Example

Aug 3rd, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.64 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <SDL2/SDL.h>
  5.  
  6. #define SCREEN_WIDTH 640
  7. #define SCREEN_HEIGHT 480
  8.  
  9. #define true (1)
  10. #define false (0)
  11. typedef unsigned char bool;
  12.  
  13. // Colour information
  14. typedef struct {
  15.     uint8_t r;
  16.     uint8_t g;
  17.     uint8_t b;
  18.     uint8_t a;
  19. } col_t;
  20.  
  21. // Like SDL_Rect, but for a line
  22. typedef struct {
  23.     int x1;
  24.     int y1;
  25.     int x2;
  26.     int y2;
  27. } line_t;
  28.  
  29. SDL_Window* window = NULL;
  30. SDL_Renderer* renderer = NULL;
  31.  
  32. /* Function init
  33.  * Initialises SDL, the window and the renderer and returns the success */
  34.  
  35. bool
  36. init() {
  37.     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  38.         fprintf(stderr, "Error: SDL could not be initialised! SDL says: %s\n", SDL_GetError());
  39.         return false;
  40.     }
  41.  
  42.     // Try to set texture filtering to linear
  43.     if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
  44.         printf("Warning: the texture filtering could not be set to linear.");
  45.     }
  46.  
  47.     // Create the window
  48.     window = SDL_CreateWindow(
  49.         "Asteroids",
  50.         SDL_WINDOWPOS_UNDEFINED,
  51.         SDL_WINDOWPOS_UNDEFINED,
  52.         SCREEN_WIDTH,
  53.         SCREEN_HEIGHT,
  54.         SDL_WINDOW_SHOWN
  55.     );
  56.  
  57.     if (window == NULL) {
  58.         fprintf(stderr, "Error: the window could not be created! SDL says: %s\n", SDL_GetError());
  59.         return false;
  60.     }
  61.  
  62.     // Create the renderer
  63.     renderer = SDL_CreateRenderer(
  64.         window,
  65.         -1,
  66.         SDL_RENDERER_ACCELERATED
  67.     );
  68.  
  69.     if (renderer == NULL) {
  70.         fprintf(stderr, "Error: the renderer could not be created! SDL says: %s\n", SDL_GetError());
  71.         return false;
  72.     }
  73.  
  74.     // Renderer colour
  75.     if (
  76.         SDL_SetRenderDrawColor(
  77.             renderer,
  78.             0xFF, 0xFF, 0xFF, 0xFF
  79.         ) < 0
  80.     ) {
  81.         fprintf(stderr, "Error: the renderer colour could not be set! SDL says: %s\n", SDL_GetError());
  82.         return false;
  83.     }
  84.  
  85.     return true;
  86. }
  87.  
  88. /* Function draw_rect
  89.  * Draws a rectangle with the points contained in rect and returns the success. */
  90.  
  91. bool
  92. draw_rect(
  93.     SDL_Rect* rect, // contains the points of the rectangle
  94.     bool fill
  95. ) {
  96.     if (fill) {
  97.         if (SDL_RenderFillRect(renderer, rect) < 0) {
  98.             return false;
  99.         }
  100.     } else {
  101.         if (SDL_RenderDrawRect(renderer, rect) < 0) {
  102.             return false;
  103.         }
  104.     }
  105.  
  106.     return true;
  107. }
  108.  
  109. /* Function draw_line
  110.  * Draws a line on the screen with the points contained in line */
  111.  
  112. bool
  113. draw_line(line_t* line) {
  114.     if (
  115.         SDL_RenderDrawLine(
  116.             renderer,
  117.             line->x1,
  118.             line->y1,
  119.             line->x2,
  120.             line->y2
  121.         ) < 0
  122.     ) {
  123.         return false;
  124.     }
  125.  
  126.     return true;
  127. }
  128.  
  129. /* Function free_sdl
  130.  * Free memory and quit SDL subsystems. */
  131.  
  132. void
  133. free_sdl() {
  134.     SDL_DestroyRenderer(renderer);
  135.     renderer = NULL;
  136.     SDL_DestroyWindow(window);
  137.     window = NULL;
  138.  
  139.     SDL_Quit();
  140. }
  141.  
  142. /* Function set_colour
  143.  * Sets the render colour based off of the col_t struct passed in. */
  144.  
  145. bool
  146. set_colour(col_t* col) {   
  147.     if (
  148.         SDL_SetRenderDrawColor(
  149.             renderer,
  150.             col->r,
  151.             col->g,
  152.             col->b,
  153.             col->a
  154.         ) < 0
  155.     ) {
  156.         return false;
  157.     }
  158.  
  159.     return true;
  160. }
  161.  
  162. int
  163. main(void) {
  164.     if (!init()) {
  165.         return EXIT_FAILURE;
  166.     }
  167.  
  168.     // Begin drawing
  169.  
  170.     if (SDL_RenderClear(renderer) < 0) {
  171.         fprintf(stderr, "Error: could not clear the renderer! SDL says: %s\n", SDL_GetError());
  172.         free_sdl();
  173.         return EXIT_FAILURE;
  174.     }
  175.  
  176.     // Red filled quad
  177.  
  178.     col_t col;
  179.  
  180.     col.r = 0xFF;
  181.     col.g = 0x00;
  182.     col.b = 0x00;
  183.     col.a = 0xFF;
  184.  
  185.     if (!set_colour(&col)) {   
  186.         fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
  187.         free_sdl();
  188.         return EXIT_FAILURE;
  189.     }
  190.  
  191.     SDL_Rect rect;
  192.  
  193.     rect.x = SCREEN_WIDTH / 4;
  194.     rect.y = SCREEN_HEIGHT / 4;
  195.     rect.w = SCREEN_WIDTH / 2;
  196.     rect.h = SCREEN_HEIGHT / 2;
  197.  
  198.     if (
  199.         !draw_rect(
  200.             &rect,
  201.             true
  202.         )
  203.     ) {
  204.         fprintf(stderr, "Error: could not draw the red filled quad. SDL says: %s\n", SDL_GetError());
  205.         free_sdl();
  206.         return EXIT_FAILURE;
  207.     }
  208.  
  209.     // Outlined green quad
  210.  
  211.     col.r = 0x00;
  212.     col.g = 0xFF;
  213.     col.b = 0x00;
  214.     col.a = 0xFF;
  215.    
  216.     if (!set_colour(&col)) {   
  217.         fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
  218.         free_sdl();
  219.         return EXIT_FAILURE;
  220.     }
  221.  
  222.     rect.x = SCREEN_WIDTH / 6;
  223.     rect.y = SCREEN_HEIGHT / 6;
  224.     rect.w = SCREEN_WIDTH * 2 / 3;
  225.     rect.h = SCREEN_HEIGHT * 2 / 3;
  226.  
  227.     if (
  228.         !draw_rect(
  229.             &rect,
  230.             false
  231.         )
  232.     ) {
  233.         fprintf(stderr, "Error: could not draw the outline green quad. SDL says: %s\n", SDL_GetError());
  234.         free_sdl();
  235.         return EXIT_FAILURE;
  236.     }
  237.  
  238.     // Blue horizontal line
  239.  
  240.     col.r = 0x00;
  241.     col.g = 0x00;
  242.     col.b = 0xFF;
  243.     col.a = 0xFF;
  244.    
  245.     if (!set_colour(&col)) {   
  246.         fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
  247.         free_sdl();
  248.         return EXIT_FAILURE;
  249.     }
  250.  
  251.     line_t line;
  252.     line.x1 = 0;
  253.     line.y1 = SCREEN_HEIGHT / 2;
  254.     line.x2 = SCREEN_WIDTH;
  255.     line.y2 = SCREEN_HEIGHT / 2;
  256.  
  257.     if (!draw_line(&line)) {
  258.         fprintf(stderr, "Error: could not draw the blue horizontal line. SDL says: %s\n", SDL_GetError());
  259.         free_sdl();
  260.         return EXIT_FAILURE;
  261.     }
  262.  
  263.     // Vertical line of black dots
  264.  
  265.     col.r = 0x00;
  266.     col.g = 0x00;
  267.     col.b = 0x00;
  268.     col.a = 0xFF;
  269.    
  270.     if (!set_colour(&col)) {   
  271.         fprintf(stderr, "Error: could not set the renderer colour. SDL says: %s\n", SDL_GetError());
  272.         free_sdl();
  273.         return EXIT_FAILURE;
  274.     }
  275.  
  276.     for (int i = 0; i < SCREEN_HEIGHT; i += 4) {
  277.         if (SDL_RenderDrawPoint(renderer, SCREEN_WIDTH / 2, i) < 0) {
  278.             fprintf(stderr, "Error: could not one of the yellow points. SDL says: %s\n", SDL_GetError());
  279.             free_sdl();
  280.             return EXIT_FAILURE;
  281.         }
  282.     }
  283.  
  284.     SDL_RenderPresent(renderer); // Update the screen
  285.  
  286.     bool running = true;
  287.     SDL_Event e;
  288.  
  289.     while (running) {
  290.         // Handle events on queue
  291.         while (SDL_PollEvent(&e) != 0) {
  292.             if (e.type == SDL_QUIT) {
  293.                 running = false;
  294.             }
  295.         }
  296.     }
  297.  
  298.     free_sdl();
  299.     return EXIT_SUCCESS;
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement