Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.90 KB | None | 0 0
  1. #include "draw.h"
  2. #include "stdafx.h"
  3.  
  4. #define RGB32(r, g, b) static_cast<uint32_t>((((static_cast<uint32_t>(b) << 8) | g) << 8) | r)
  5.  
  6.  
  7. void put_pixel32(SDL_Surface* surface, int x, int y, Uint32 pixel)
  8. {
  9.     assert(NULL != surface);
  10.     assert(x < SCREEN_WIDTH);
  11.     assert(y < SCREEN_HEIGHT);
  12.  
  13.     Uint32* pixels = (Uint32*)surface->pixels;
  14.     pixels[(y * surface->w) + x] = pixel;
  15. }
  16.  
  17. Uint32 get_pixel32(SDL_Surface* surface, int x, int y)
  18. {
  19.     assert(NULL != surface);
  20.     assert(x < SCREEN_WIDTH);
  21.     assert(y < SCREEN_HEIGHT);
  22.  
  23.     Uint32* pixels = (Uint32*)surface->pixels;
  24.     return pixels[(y * surface->w) + x];
  25. }
  26.  
  27.  
  28. void draw_horizontal_line(SDL_Surface* s, int start_x, int final_x, int y, int color)
  29. {
  30.     int hatch_frequncy = (SCREEN_WIDTH / (AMOUNT_HATCHS + 1));
  31.     for (int x = start_x; x < final_x; x++)
  32.     {
  33.         put_pixel32(s, x, y, color);
  34.     }
  35. }
  36.  
  37. void draw_vertical_line(SDL_Surface* s, int start_y, int final_y, int x, int color)
  38. {
  39.     int max = (start_y > final_y) ? start_y : final_y;
  40.     int min = (start_y < final_y) ? start_y : final_y;
  41.     for (int y = min; y < max; y++)
  42.     {
  43.         put_pixel32(s, x, y, color);
  44.     }
  45. }
  46.  
  47. void draw_horizontal_hatches(SDL_Surface* s, int start_y, int final_y, int x)
  48. {
  49.     int hatch_frequncy = (SCREEN_HEIGHT / (AMOUNT_HATCHS + 1));
  50.     for (int y = start_y; y < final_y; y++)
  51.     {
  52.         if (y % hatch_frequncy == 0)
  53.             draw_horizontal_line(s, x - HATCH_LEN, x + HATCH_LEN, y, RGB(255, 255, 255));
  54.     }
  55. }
  56.  
  57. void draw_vertical_hatches(SDL_Surface* s, int start_x, int final_x, int y)
  58. {
  59.     int hatch_frequncy = (SCREEN_HEIGHT / (AMOUNT_HATCHS + 1));
  60.     for (int x = start_x; x < final_x; x++)
  61.     {
  62.         if (x % hatch_frequncy == 0)
  63.             draw_vertical_line(s, y - HATCH_LEN, y + HATCH_LEN, x, RGB(255, 255, 255));
  64.     }
  65. }
  66.  
  67. void draw_angular_line(SDL_Surface* s, int start_x, int final_x, int start_y, int final_y, int color)
  68. {
  69.     int amount_iterations = 8;
  70.     int delta_y = (final_y - start_y) / amount_iterations;
  71.     int delta_x = (final_x - start_x) / amount_iterations;
  72.  
  73.     for (int i = 0; i < amount_iterations; i++)
  74.     {
  75.         put_pixel32(s, start_x, start_y, color);
  76.         start_x += delta_x;
  77.         start_y += delta_y;
  78.     }
  79. }
  80.  
  81. void draw_first_graphic(SDL_Surface* s)
  82. {
  83.     int cur_x;
  84.     int cur_y;
  85.     int prev_x = SCREEN_WIDTH / 2;
  86.     int prev_y = SCREEN_HEIGHT / 2;
  87.     for (int x = 0; x < 15; x++)
  88.     {
  89.         cur_x = (SCREEN_WIDTH / 2) - x * 6;
  90.         cur_y = (SCREEN_HEIGHT / 2) - (3 * x * x);
  91.         put_pixel32(s, cur_x, cur_y, RGB(240, 0, 178));
  92.  
  93.         draw_angular_line(s, prev_x, cur_x, prev_y, cur_y, RGB(240, 0, 178));
  94.         prev_x = cur_x;
  95.         prev_y = cur_y;
  96.     }
  97.  
  98.  
  99.     prev_x = SCREEN_WIDTH / 2;
  100.     prev_y = SCREEN_HEIGHT / 2;
  101.     for (int x = 0; x < 15; x++)
  102.     {
  103.         cur_x = (SCREEN_WIDTH / 2) + x * 6;
  104.         cur_y = (SCREEN_HEIGHT / 2) - (3 * x * x);
  105.         put_pixel32(s, cur_x, cur_y, RGB(240, 0, 178));
  106.  
  107.         draw_angular_line(s, prev_x, cur_x, prev_y, cur_y, RGB(240, 0, 178));
  108.         prev_x = cur_x;
  109.         prev_y = cur_y;
  110.     }
  111. }
  112.  
  113. void draw_second_graphic(SDL_Surface* s)
  114. {
  115.     draw_horizontal_line(s, 0, 450, 500, RGB(0, 0, 178));
  116.     draw_horizontal_line(s, 490, 1000, 500, RGB(0, 0, 178));
  117.  
  118.     draw_vertical_line(s, 470, 0, 470, RGB(0, 0, 178));
  119.     draw_vertical_line(s, 470, 0, 480, RGB(0, 0, 178));
  120.  
  121.     int y = 470;
  122.     for (int x = 470; x > 450;) {
  123.         put_pixel32(s, x, y, RGB(0, 0, 178));
  124.         if (x < 460) {
  125.             x -= 2;
  126.             y += 1;
  127.         }
  128.         else {
  129.             y += 2;
  130.             x -= 1;
  131.         }
  132.     }
  133.  
  134.     y = 470;
  135.     for (int x = 480; x < 490;) {
  136.         put_pixel32(s, x, y, RGB(0, 0, 178));
  137.         if (x < 485) {
  138.             x += 1;
  139.             y += 3;
  140.         }
  141.         else {
  142.             y += 2;
  143.             x += 2;
  144.         }
  145.     }
  146. }
  147.  
  148.  
  149. void draw(SDL_Surface* s, SDL_Renderer* gRenderer)
  150. {
  151.     int y;
  152.     int x;
  153.  
  154.     draw_horizontal_line(s, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2, RGB(255, 255, 255));
  155.     draw_vertical_line(s, 0, SCREEN_HEIGHT, SCREEN_WIDTH / 2, RGB(255, 255, 255));
  156.     draw_horizontal_hatches(s, 0, SCREEN_HEIGHT, SCREEN_WIDTH / 2);
  157.     draw_vertical_hatches(s, 0, SCREEN_HEIGHT, SCREEN_WIDTH / 2);
  158.  
  159.     draw_first_graphic(s);
  160.     draw_second_graphic(s);
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement