Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Libraries used: SFML 2.0
- #include <SFML\Graphics.hpp>
- #include <cmath>
- #include <vector>
- #include <sstream>
- //Window Properties and time constant for object movment
- #define width 800
- #define height 600
- #define time_constant 1.f/60.f
- #define numberofobjects 100 //Number of objects to add each period
- #define frequency 1000 //Frequency in miliseconds
- #define speed 50 //Movement speed for the objects
- //Convert Degrees (ΒΊ) to Radians (rad)
- double ConvRad(double deg)
- {
- return deg/180*3.14;
- }
- //Main Object to be managed and inserted into the vector
- struct block
- {
- static float currentangle; //Global direction angle
- sf::Vector2f vector2Direction; //vector holding the direction of the movement
- sf::Shape shapeBlock; //Drawable shape
- //Constructor
- block() : shapeBlock(sf::Shape::Rectangle(0.f ,0, 10.f, 10.f, sf::Color::White))
- {
- shapeBlock.SetOrigin(5.f, 5.f);
- shapeBlock.SetPosition(width / 2, height / 2);
- shapeBlock.SetRotation(currentangle);
- float dx = (float)std::cos(ConvRad(currentangle)) * speed * time_constant;
- float dy = (float)std::sin(ConvRad(currentangle)) * speed * time_constant;
- vector2Direction = sf::Vector2f(dx, dy);
- currentangle += 360.f / numberofobjects;
- if(currentangle >= 360.f)
- currentangle = 0.f;
- }
- //Verify colosion between object and border
- bool VerifyBorderColision()
- {
- sf::Vector2f currentpos(shapeBlock.GetPosition());
- if(currentpos.x > width || currentpos.x < 0.f
- || currentpos.y > height || currentpos.y < 0.f)
- return true;
- else return false;
- }
- //Move the object
- void Update()
- {
- shapeBlock.Move(vector2Direction);
- }
- };
- float block::currentangle = 0.f;
- int main()
- {
- //The main vector that will manage all the objects
- std::vector<block> vectorBlocks;
- vectorBlocks.reserve(1000);
- //Main RenderWindow
- sf::RenderWindow renderwindow(sf::VideoMode(width, height, 32), "Vector Benchmark");
- renderwindow.SetFramerateLimit(60);
- //Text object that will contain general information for
- //FPS
- //Number of objects
- sf::Text textInfo;
- textInfo.SetCharacterSize(12);
- //Clock that will manage the spawn frequency
- sf::Clock clockFrequency;
- clockFrequency.Reset();
- while(renderwindow.IsOpened())
- {
- sf::Event myevent;
- while(renderwindow.PollEvent(myevent))
- {
- if((myevent.Type == sf::Event::Closed)||
- (myevent.Type == sf::Event::KeyPressed && myevent.Key.Code == sf::Keyboard::Escape))
- renderwindow.Close();
- }
- //If it's time to spawn new objects
- if(clockFrequency.GetElapsedTime() > frequency)
- {
- //Then add them all to the vector
- for(unsigned int i = 0; i < numberofobjects; i++)
- {
- block toadd;
- vectorBlocks.push_back(toadd);
- }
- clockFrequency.Reset();
- }
- //Just updating the data for textInfo
- std::ostringstream oss;
- oss << "FPS: " << 1000.0 / renderwindow.GetFrameTime()
- << "\nNumber of Blocks: " << vectorBlocks.size();
- textInfo.SetString(oss.str());
- renderwindow.Clear();
- //Starting to draw every object
- for(unsigned int i = 0; i < vectorBlocks.size(); i++)
- {
- //Move the Objects
- vectorBlocks[i].Update();
- //Then verify if they're touching the borders and erase if they are
- if(vectorBlocks[i].VerifyBorderColision())
- vectorBlocks.erase(vectorBlocks.begin()+i);
- //Then Draw them
- renderwindow.Draw(vectorBlocks[i].shapeBlock);
- }
- renderwindow.Draw(textInfo);
- renderwindow.Display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement