Advertisement
Guest User

Untitled

a guest
Aug 30th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <tuple>
  3. #include <algorithm>
  4. #include <SDL.h>
  5. #include <list>
  6.  
  7.  
  8. struct coordinate {
  9.     int x, y;
  10. };
  11.  
  12. struct line {
  13.     coordinate start, end;
  14. };
  15.  
  16. bool checkInBounds(const coordinate &a, SDL_Surface* surface) {
  17.     if (a.x > 0 && a.y > 0 && a.x <= surface->w && a.y <= surface->h) {
  18.         return true;
  19.     }
  20.     else return false;
  21. }
  22.  
  23. bool checkDirection(double m, int deltaX) {
  24.     bool flag = false;
  25.     if ((m >= -1 && deltaX >= 0) ||
  26.         (m <= -1 && deltaX <= 0)) {
  27.         flag = true;
  28.     }
  29.     return flag;
  30. }
  31.  
  32.  
  33. void drawPixel(const coordinate &coordA, std::uint32_t color, SDL_Surface* surface) {
  34.     if (!checkInBounds(coordA, surface)) {
  35.         std::cout << "Pixel not in bounds" << std::endl;
  36.         return;
  37.     }
  38.     reinterpret_cast<std::uint32_t*> (surface->pixels)[coordA.x + coordA.y * surface->w] = color;
  39. }
  40.  
  41. void drawLine(const line &lineA, std::uint32_t color, SDL_Surface* surface) {
  42.     if (!checkInBounds(lineA.start, surface) || !checkInBounds(lineA.end, surface)) {
  43.         std::cout << "Line not in bounds" << std::endl;
  44.         return;
  45.     }
  46.  
  47.  
  48.     coordinate drawCoord;
  49.     drawCoord.x = lineA.start.x;
  50.     drawCoord.y = lineA.start.y;
  51.     int driving, dEnd, passive, pEnd, i, dInc = -1, pInc = -1; // D for Driving, P for Passive
  52.     double m = 0, e, deltaX, deltaY;
  53.     bool flipped = false;
  54.  
  55.     deltaX = lineA.end.x - lineA.start.x;
  56.     deltaY = lineA.end.y - lineA.start.y;
  57.  
  58.     if (deltaX == 0 && deltaY == 0) { //Line is actually a point
  59.         drawPixel(drawCoord, color, surface);
  60.         return;
  61.     }
  62.     if (deltaX == 0) { //Straight up down line
  63.         if (deltaY < 0) {
  64.             drawCoord.y = lineA.end.y;
  65.         }
  66.         for (i = 0; i < abs(deltaY); ++i) {
  67.             drawPixel(drawCoord, color, surface);
  68.             drawCoord.y = drawCoord.y + 1;
  69.         }
  70.         return;
  71.     }
  72.  
  73.  
  74.     m = deltaY / deltaX;
  75.  
  76.  
  77.     // // SET x AS DRIVING
  78.     if (abs(deltaX) >= abs(deltaY)) {
  79.         driving = lineA.start.x;
  80.         dEnd = lineA.end.x;
  81.         passive = lineA.start.y;
  82.         pEnd = lineA.end.y;
  83.         if (deltaX >= 0) dInc = true;
  84.         if (deltaY >= 0) pInc = true;
  85.  
  86.     }
  87.  
  88.     // // SET y AS DRIVING
  89.     else {
  90.         driving = lineA.start.y;
  91.         dEnd = lineA.end.y;
  92.         passive = lineA.start.x;
  93.         pEnd = lineA.end.x;
  94.         m = 1 / m;
  95.         flipped = true;
  96.         if (deltaY >= 0) dInc = true;
  97.         if (deltaX >= 0) pInc = true;
  98.     }
  99.  
  100.     e = abs(m) - 1;
  101.  
  102.     while (driving != dEnd) {
  103.         drawCoord.x = passive;
  104.         drawCoord.y = driving;
  105.         if (flipped) {
  106.             std::swap(drawCoord.x, drawCoord.y);
  107.         }
  108.         drawPixel(drawCoord, color, surface);
  109.  
  110.         if (e >= 0) {
  111.             passive += pInc;
  112.             --e;
  113.         }
  114.         driving += dInc;
  115.         e += abs(m);
  116.     }
  117. }
  118.  
  119. int main(int, char**) {
  120.     SDL_Init(SDL_INIT_EVERYTHING);
  121.     std::atexit(&SDL_Quit);
  122.  
  123.  
  124.  
  125.  
  126.     auto s_window = SDL_CreateWindow("Fuck me", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280u, 720u, 0u);
  127.     auto s_surface = SDL_GetWindowSurface(s_window);
  128.     SDL_FillRect(s_surface, nullptr, 0xFFFFFFFF);
  129.  
  130.     SDL_Event s_event;
  131.     auto s_last_x = 0;
  132.     auto s_last_y = 0;
  133.     auto s_size = 0;
  134.  
  135.  
  136.     // // DEFINE LINE PROPERTIES // //
  137.     int red = 0xFFFF0000;
  138.  
  139.     coordinate pixelCoord;
  140.     pixelCoord.x = 10; pixelCoord.y = 10;
  141.     drawPixel(pixelCoord, red, s_surface);
  142.  
  143.     int startX = 300;
  144.     int startY = 300;
  145.     int endX[] = { 300, 300, 380 ,450, 480, 500, 480, 450, 380, 300, 220, 150, 120, 100, 120, 150, 200 };
  146.     int endY[] = { 300, 100, 130 ,150, 220, 300, 380, 450, 480, 500, 480, 450, 390, 300, 220, 150, 130 };
  147.     int numLines = sizeof(endX)/sizeof(endX[0]);
  148.  
  149.  
  150.     // // CREATE LINE DATA STRUCTURES // //
  151.     std::list<line> lines;
  152.     for (int i = 0; i < numLines; ++i) {
  153.         coordinate tempStart;
  154.         coordinate tempEnd;
  155.         line tempLine;
  156.         tempStart.x = startX;
  157.         tempStart.y = startY;
  158.         tempEnd.x = endX[i];
  159.         tempEnd.y = endY[i];
  160.         tempLine.start = tempStart;
  161.         tempLine.end = tempEnd;
  162.         lines.push_back(tempLine);
  163.     }
  164.  
  165.     // // DRAW LINES // //
  166.     auto it = lines.begin();
  167.     while (it != lines.end()) {
  168.         drawLine(*it, red, s_surface);
  169.         it++;
  170.     }
  171.  
  172.  
  173.  
  174.  
  175.  
  176.     while (!SDL_QuitRequested()) {
  177.         SDL_UpdateWindowSurface(s_window);
  178.     }
  179.  
  180.     SDL_DestroyWindow(s_window);
  181.     return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement