Advertisement
BlueX

SFML Painter

Sep 19th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.46 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <SFML\Graphics.hpp>
  4.  
  5.  
  6. bool isInSquare(float min_x, float max_x, float min_y, float max_y, float x, float y);
  7.  
  8. int main()
  9. {
  10.     sf::RenderWindow window(sf::VideoMode(800, 800), "SFML Works!");
  11.  
  12.     sf::RectangleShape rect(sf::Vector2f(49, 49));
  13.  
  14.     sf::RectangleShape color_1(sf::Vector2f(50, 50));
  15.     sf::RectangleShape color_2(sf::Vector2f(50, 50));
  16.     sf::RectangleShape color_3(sf::Vector2f(50, 50));
  17.  
  18.  
  19.     rect.setFillColor(sf::Color::Black);
  20.     rect.setPosition(sf::Vector2f(0, 0));
  21.  
  22.     color_1.setFillColor(sf::Color::Red);
  23.     color_1.setPosition(sf::Vector2f(100, 25));
  24.  
  25.     color_2.setFillColor(sf::Color::Green);
  26.     color_2.setPosition(sf::Vector2f(175, 25));
  27.  
  28.     color_3.setFillColor(sf::Color::Blue);
  29.     color_3.setPosition(sf::Vector2f(250, 25));
  30.  
  31.     std::vector < sf::Vertex> pixels;
  32.  
  33.  
  34.     sf::Vertex _line[] = {
  35.         sf::Vertex(sf::Vector2f(100,100)),
  36.         sf::Vertex(sf::Vector2f(700, 100)),
  37.         sf::Vertex(sf::Vector2f(700, 700)),
  38.         sf::Vertex(sf::Vector2f(100, 700)),
  39.         sf::Vertex(sf::Vector2f(100,700)),
  40.         sf::Vertex(sf::Vector2f(100, 100))
  41.     };
  42.  
  43.     std::vector < sf::Vertex> vertices;
  44.    
  45.     float squares[144][4][2];
  46.  
  47.     sf::Vector2f square[12][12][2];
  48.  
  49.     //grid generator..
  50.  
  51.     for (int i = 1; i <= 12; i++)
  52.     {
  53.         //visual part
  54.  
  55.         //draw horizontal line
  56.  
  57.         float y = 100 + (50 * i);
  58.  
  59.         vertices.push_back(sf::Vertex(sf::Vector2f(100,y)));
  60.         vertices.push_back(sf::Vertex(sf::Vector2f(700,y)));
  61.  
  62.         //draw vertical line
  63.  
  64.         float x = 100 + (50 * i);
  65.  
  66.         vertices.push_back(sf::Vertex(sf::Vector2f(x, 100)));
  67.         vertices.push_back(sf::Vertex(sf::Vector2f(x, 700)));
  68.  
  69.         //end of visual part
  70.  
  71.         //array part
  72.        
  73.         ////////////////////////
  74.         squares[i][0][0] = x - 50;  //A
  75.         squares[i][0][1] = y - 50;
  76.         /////////////////////////
  77.         squares[i][1][0] = x; //B
  78.         squares[i][1][1] = y - 50;
  79.         ////////////////////////
  80.         squares[i][2][0] = x; // C
  81.         squares[i][2][1] = y;
  82.         ////////////////////////
  83.         squares[i][3][0] = x - 50; // D
  84.         squares[i][3][1] = y;
  85.  
  86.  
  87.     }
  88.  
  89.     for (int i = 0; i < 12; i++) //row
  90.     {
  91.         for (int z = 0; z < 12; z++) //column
  92.         {
  93.             float y = 100 + (50 * (float)i);
  94.             float x = 100 + (50 * (float)z);
  95.  
  96.             square[i][z][0] = sf::Vector2f(x,y); //min
  97.             square[i][z][1] = sf::Vector2f(x+50,y+50); //max
  98.  
  99.             /*std::cout << "min (" << square[i][z][0].x << "," << square[i][z][0].y << ")" << std::endl;
  100.             std::cout << "max (" << square[i][z][1].x << "," << square[i][z][1].y << ")" << std::endl;*/
  101.         }
  102.  
  103.        
  104.     }
  105.  
  106.     std::cout << "min (" << square[0][0][0].x << "," << square[0][0][0].y << ")" << std::endl;
  107.     std::cout << "max (" << square[0][0][1].x << "," << square[0][0][1].y << ")" << std::endl;
  108.  
  109.     std::cout << "min (" << square[11][11][0].x << "," << square[11][11][0].y << ")" << std::endl;
  110.     std::cout << "max (" << square[11][11][1].x << "," << square[11][11][1].y << ")" << std::endl;
  111.  
  112.  
  113.  
  114.     //finish
  115.    
  116.  
  117.     while (window.isOpen())
  118.     {
  119.  
  120.         sf::Event event;
  121.         while (window.pollEvent(event))
  122.         {
  123.             if (event.type == sf::Event::Closed)
  124.                 window.close();
  125.  
  126.             switch (event.type)
  127.             {
  128.                 case sf::Event::Closed:
  129.                     window.close();
  130.                     break;
  131.  
  132.                 case sf::Event::MouseButtonPressed:
  133.                     if (event.mouseButton.button == sf::Mouse::Left)
  134.                     {
  135.                         std::cout << "You pressed left mouse at x: " << event.mouseButton.x << " y: " << event.mouseButton.y << std::endl;
  136.  
  137.                         if(isInSquare(100,150,25,75,event.mouseButton.x,event.mouseButton.y)){
  138.                             rect.setFillColor(sf::Color::Red);
  139.  
  140.                         } else if (isInSquare(175, 225, 25, 75, event.mouseButton.x, event.mouseButton.y)) {
  141.                             rect.setFillColor(sf::Color::Green);
  142.  
  143.                         } else if (isInSquare(250, 300, 25, 75, event.mouseButton.x, event.mouseButton.y)) {
  144.                             rect.setFillColor(sf::Color::Blue);
  145.                         }
  146.  
  147.                         for (int i = 0; i < 12; i++)
  148.                         {
  149.  
  150.                             for (int z = 0; z < 12; z++)
  151.                             {
  152.                                 if (isInSquare(square[i][z][0].x, square[i][z][1].x, square[i][z][0].y, square[i][z][1].y, event.mouseButton.x, event.mouseButton.y)) {
  153.                                     std::cout << "sucess" << std::endl;
  154.  
  155.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][0].y), rect.getFillColor()));
  156.  
  157.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x-1, square[i][z][0].y), rect.getFillColor()));
  158.  
  159.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x-1, square[i][z][1].y-1), rect.getFillColor()));
  160.  
  161.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][1].y-1), rect.getFillColor()));
  162.  
  163.                                     //create a pixel
  164.  
  165.                                 }
  166.                             }
  167.                         }
  168.                     }
  169.                     else if (event.mouseButton.button == sf::Mouse::Right)
  170.                     {
  171.                         for (int i = 0; i < 12; i++)
  172.                         {
  173.                             for (int z = 0; z < 12; z++)
  174.                             {
  175.                                 if (isInSquare(square[i][z][0].x, square[i][z][1].x, square[i][z][0].y, square[i][z][1].y, event.mouseButton.x, event.mouseButton.y)) {
  176.                                     std::cout << "sucess" << std::endl;
  177.  
  178.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][0].y), sf::Color::Black));
  179.  
  180.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x - 1, square[i][z][0].y), sf::Color::Black));
  181.  
  182.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x - 1, square[i][z][1].y - 1), sf::Color::Black));
  183.  
  184.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][1].y - 1), sf::Color::Black));
  185.  
  186.                                     //create a pixel
  187.  
  188.                                 }
  189.                             }
  190.                         }
  191.                     }
  192.  
  193.                    
  194.                     break;
  195.                 case sf::Event::MouseMoved:
  196.                     if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
  197.                     {
  198.                         for (int i = 0; i < 12; i++)
  199.                         {
  200.  
  201.                             for (int z = 0; z < 12; z++)
  202.                             {
  203.                                 if (isInSquare(square[i][z][0].x, square[i][z][1].x, square[i][z][0].y, square[i][z][1].y, event.mouseMove.x, event.mouseMove.y)) {
  204.                                     std::cout << "sucess" << std::endl;
  205.  
  206.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][0].y), rect.getFillColor()));
  207.  
  208.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x - 1, square[i][z][0].y), rect.getFillColor()));
  209.  
  210.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][1].x - 1, square[i][z][1].y - 1), rect.getFillColor()));
  211.  
  212.                                     pixels.push_back(sf::Vertex(sf::Vector2f(square[i][z][0].x, square[i][z][1].y - 1), rect.getFillColor()));
  213.  
  214.                                     //create a pixel
  215.  
  216.                                 }
  217.                             }
  218.                         }
  219.                     }
  220.  
  221.                     for (int i = 0; i < 12; i++)
  222.                     {
  223.                         for (int z = 0; z < 12; z++)
  224.                         {
  225.                             if (isInSquare(square[i][z][0].x, square[i][z][1].x, square[i][z][0].y, square[i][z][1].y, event.mouseMove.x, event.mouseMove.y)) {
  226.                                 //std::cout << "sucess" << std::endl;
  227.  
  228.                                 rect.setPosition(square[i][z][0].x, square[i][z][0].y);
  229.                                
  230.                             }
  231.                         }
  232.                     }
  233.                     break;
  234.                 default:
  235.                     break;
  236.             }
  237.         }
  238.  
  239.         window.clear();
  240.         window.draw(_line, 6, sf::LinesStrip);
  241.         //draw the grid
  242.         window.draw(&vertices[0], vertices.size(), sf::Lines);
  243.         window.draw(&pixels[0], pixels.size(), sf::Quads);
  244.         window.draw(rect);
  245.         window.draw(color_1);
  246.         window.draw(color_2);
  247.         window.draw(color_3);
  248.         window.display();
  249.     }
  250.  
  251.     return 0;
  252. }
  253.  
  254. bool isInSquare(float min_x, float max_x, float min_y, float max_y, float x, float y)
  255. {
  256.     /*std::cout << "IF " << x << " >= " << min_x << " && " << x << " <= " << max_x << std::endl;
  257.     std::cout << "IF " << y << " >= " << min_y << " && " << y << " <= " << max_y << std::endl;*/
  258.     if (x >= min_x && x <= max_x)
  259.     {
  260.         if (y >= min_y && y <= max_y)
  261.         {
  262.             return true;
  263.         }
  264.     }
  265.  
  266.     return false;
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement