Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Sterowanie kamerą WSAD, skalowanie JK.
- Klocki można stawiać tylko w sektorze 100 na 100, bo nie zrobiłem zmiany sektorów,
- a nie widzę sensu rozwijania tego dalej, jeśli jest problem z wydajnością przy zapełnieniu 1/50 sektora.
- */
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #include <vector>
- int width = 1200;
- int height = 650;
- sf::RenderWindow okno( sf::VideoMode( width, height ), "Edytor" );
- float camera_position_x = 0, camera_position_y = 0;
- float scale = 20;
- class Box
- {
- int x, y;
- sf::RectangleShape shape;
- public:
- Box(int x, int y)
- {
- this -> x = x;
- this -> y = y;
- }
- void draw(int sector_X, int sector_Y)
- {
- shape.setPosition( (sector_X + x - camera_position_x ) * scale + width/2 , ( sector_Y + y - camera_position_y ) * scale + height/2 );
- shape.setSize( sf::Vector2f( scale, scale ));
- okno.draw(shape);
- }
- };
- class Sector
- {
- int licznik = 0;
- public:
- int X, Y;
- std::vector <Box> vec;
- void load(int x, int y)
- {
- X=x; Y=y;
- //fstream plik("mapa.map");
- }
- void createBox(int x, int y)
- {
- licznik++;
- Box pom(x,y);
- vec.push_back(pom);
- }
- void draw()
- {
- for(int i=0; i<licznik; i++) // To tutaj jest problem z wydajnością
- vec[i].draw(X,Y);
- //std::cout<<licznik<<std::endl;
- }
- };
- void eventsUpdate();
- void cameraUpdate();
- void scaleUpdate();
- void createBox();
- void inputUpdate();
- Sector sector[4];
- Sector *s1;
- Sector *s2;
- Sector *s3;
- Sector *s4;
- main()
- {
- sector[0].load(0,0);
- sector[1].load(100,0);
- sector[2].load(0,100);
- sector[3].load(100,100);
- s1 = §or[0];
- s2 = §or[1];
- s3 = §or[2];
- s4 = §or[3];
- //for(int i=0;i<25;i++)
- // for(int j=0;j<25;j++)
- // s1 -> createBox(i,j);
- Box test(0,0);
- while( okno.isOpen() )
- {
- eventsUpdate();
- cameraUpdate();
- scaleUpdate();
- inputUpdate();
- okno.clear( sf::Color( 3, 160, 204 ) );
- test.draw(0,0);
- s1 -> draw();
- okno.display();
- }
- }
- void eventsUpdate()
- {
- sf::Event event;
- while( okno.pollEvent( event ) )
- {
- if( event.type == sf::Event::Closed )
- okno.close();
- }
- }
- void cameraUpdate()
- {
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::A ) )
- camera_position_x -= 0.05;
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::W ) )
- camera_position_y -= 0.05;
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::S ) )
- camera_position_y += 0.05;
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::D ) )
- camera_position_x += 0.05;
- }
- void scaleUpdate()
- {
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::J ) )
- scale -= 0.05;
- if( sf::Keyboard::isKeyPressed( sf::Keyboard::K ) )
- scale += 0.05;
- }
- bool mouse = false;
- void inputUpdate()
- {
- if( sf::Mouse::isButtonPressed( sf::Mouse::Left ) )
- mouse = true;
- else
- {
- if(mouse == true)
- {
- mouse = false;
- createBox();
- }
- }
- }
- void createBox()
- {
- sf::Vector2i mpos = sf::Mouse::getPosition( okno );
- mpos.x = floor( mpos.x/scale + camera_position_x - width/2 / scale - s1 -> X );
- mpos.y = floor( mpos.y/scale + camera_position_y - height/2 / scale - s1 -> Y );
- if( mpos.x > s1 -> X && mpos.x < s1 -> X + 100 && mpos.y > s1 -> Y && mpos.y < s1 -> Y + 100 )
- s1 -> createBox(mpos.x,mpos.y);
- //else if( mpos.x > s2 -> X && mpos.x < s2 -> X + 100 && mpos.y > s2 -> Y && mpos.y < s2 -> Y + 100 );
- //else if( mpos.x > s3 -> X && mpos.x < s3 -> X + 100 && mpos.y > s3 -> Y && mpos.y < s3 -> Y + 100 );
- //else if( mpos.x > s4 -> X && mpos.x < s4 -> X + 100 && mpos.y > s4 -> Y && mpos.y < s4 -> Y + 100 );
- }
Add Comment
Please, Sign In to add comment