d-rot

Edytor

Nov 2nd, 2021 (edited)
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.74 KB | None | 0 0
  1. /*
  2. Sterowanie kamerą WSAD, skalowanie JK.
  3. Klocki można stawiać tylko w sektorze 100 na 100, bo nie zrobiłem zmiany sektorów,
  4. a nie widzę sensu rozwijania tego dalej, jeśli jest problem z wydajnością przy zapełnieniu 1/50 sektora.
  5. */
  6.  
  7. #include <SFML/Graphics.hpp>
  8. #include <iostream>
  9. #include <fstream>
  10. #include <cmath>
  11. #include <vector>
  12.  
  13. int width = 1200;
  14. int height = 650;
  15.  
  16. sf::RenderWindow okno( sf::VideoMode( width, height ), "Edytor" );
  17.  
  18. float camera_position_x = 0, camera_position_y = 0;
  19. float scale = 20;
  20.  
  21.  
  22. class Box
  23. {
  24.     int x, y;
  25.     sf::RectangleShape shape;
  26.    
  27. public:
  28.     Box(int x, int y)
  29.     {
  30.         this -> x = x;
  31.         this -> y = y;
  32.     }
  33.    
  34.     void draw(int sector_X, int sector_Y)
  35.     {
  36.         shape.setPosition( (sector_X + x - camera_position_x ) * scale + width/2 , ( sector_Y + y - camera_position_y ) * scale + height/2 );
  37.         shape.setSize( sf::Vector2f( scale, scale ));
  38.         okno.draw(shape);
  39.     }
  40. };
  41.  
  42.  
  43. class Sector
  44. {
  45.     int licznik = 0;
  46. public:
  47.     int X, Y;
  48.     std::vector <Box> vec;
  49.    
  50.     void load(int x, int y)
  51.     {
  52.         X=x; Y=y;
  53.         //fstream plik("mapa.map");
  54.     }
  55.    
  56.     void createBox(int x, int y)
  57.     {
  58.         licznik++;
  59.         Box pom(x,y);
  60.         vec.push_back(pom);
  61.     }
  62.    
  63.     void draw()
  64.     {
  65.         for(int i=0; i<licznik; i++)      // To tutaj jest problem z wydajnością
  66.             vec[i].draw(X,Y);
  67.         //std::cout<<licznik<<std::endl;
  68.     }
  69. };
  70.  
  71.  
  72.  
  73. void eventsUpdate();
  74. void cameraUpdate();
  75. void scaleUpdate();
  76. void createBox();
  77. void inputUpdate();
  78.  
  79. Sector sector[4];
  80.  
  81. Sector *s1;
  82. Sector *s2;
  83. Sector *s3;
  84. Sector *s4;
  85.    
  86. main()
  87. {  
  88.     sector[0].load(0,0);
  89.     sector[1].load(100,0);
  90.     sector[2].load(0,100);
  91.     sector[3].load(100,100);
  92.    
  93.     s1 = &sector[0];
  94.     s2 = &sector[1];
  95.     s3 = &sector[2];
  96.     s4 = &sector[3];
  97.    
  98.     //for(int i=0;i<25;i++)
  99.     //  for(int j=0;j<25;j++)
  100.     //      s1 -> createBox(i,j);
  101.    
  102.     Box test(0,0);
  103.    
  104.     while( okno.isOpen() )
  105.     {
  106.         eventsUpdate();
  107.         cameraUpdate();
  108.         scaleUpdate();
  109.         inputUpdate();
  110.        
  111.         okno.clear( sf::Color( 3, 160, 204 ) );
  112.        
  113.         test.draw(0,0);
  114.        
  115.         s1 -> draw();
  116.        
  117.         okno.display();
  118.     }
  119. }
  120.  
  121. void eventsUpdate()
  122. {
  123.     sf::Event event;
  124.     while( okno.pollEvent( event ) )
  125.     {
  126.         if( event.type == sf::Event::Closed )
  127.              okno.close();
  128.     }
  129. }
  130.  
  131. void cameraUpdate()
  132. {
  133.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
  134.         camera_position_x -= 0.05;
  135.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
  136.         camera_position_y -= 0.05;
  137.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
  138.         camera_position_y += 0.05;
  139.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
  140.         camera_position_x += 0.05;
  141. }
  142.  
  143. void scaleUpdate()
  144. {
  145.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::J ) )
  146.         scale -= 0.05;
  147.     if( sf::Keyboard::isKeyPressed( sf::Keyboard::K ) )
  148.         scale += 0.05;  
  149. }
  150.  
  151. bool mouse = false;
  152.  
  153. void inputUpdate()
  154. {
  155.     if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
  156.         mouse = true;
  157.     else
  158.     {
  159.         if(mouse == true)
  160.         {
  161.             mouse = false;
  162.            
  163.             createBox();
  164.         }
  165.     }
  166. }
  167.  
  168. void createBox()
  169. {
  170.     sf::Vector2i mpos = sf::Mouse::getPosition( okno );
  171.     mpos.x = floor( mpos.x/scale + camera_position_x - width/2 / scale - s1 -> X );
  172.     mpos.y = floor( mpos.y/scale + camera_position_y - height/2 / scale - s1 -> Y );
  173.    
  174.     if( mpos.x > s1 -> X && mpos.x < s1 -> X + 100 && mpos.y > s1 -> Y && mpos.y < s1 -> Y + 100 )
  175.         s1 -> createBox(mpos.x,mpos.y);
  176.     //else if( mpos.x > s2 -> X && mpos.x < s2 -> X + 100 && mpos.y > s2 -> Y && mpos.y < s2 -> Y + 100 );
  177.     //else if( mpos.x > s3 -> X && mpos.x < s3 -> X + 100 && mpos.y > s3 -> Y && mpos.y < s3 -> Y + 100 );
  178.     //else if( mpos.x > s4 -> X && mpos.x < s4 -> X + 100 && mpos.y > s4 -> Y && mpos.y < s4 -> Y + 100 );
  179. }
Add Comment
Please, Sign In to add comment