Advertisement
Guest User

Vector Example

a guest
Aug 12th, 2011
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. //Libraries used: SFML 2.0
  2. #include <SFML\Graphics.hpp>
  3. #include <cmath>
  4. #include <vector>
  5. #include <sstream>
  6.  
  7. //Window Properties and time constant for object movment
  8. #define width 800
  9. #define height 600
  10. #define time_constant 1.f/60.f
  11.  
  12. #define numberofobjects 100 //Number of objects to add each period
  13. #define frequency 1000 //Frequency in miliseconds
  14. #define speed 50 //Movement speed for the objects
  15.  
  16. //Convert Degrees (ΒΊ) to Radians (rad)
  17. double ConvRad(double deg)
  18. {
  19.     return deg/180*3.14;
  20. }
  21.  
  22.  
  23. //Main Object to be managed and inserted into the vector
  24. struct block
  25. {
  26.     static float currentangle; //Global direction angle
  27.     sf::Vector2f vector2Direction; //vector holding the direction of the movement
  28.     sf::Shape shapeBlock; //Drawable shape
  29.  
  30.     //Constructor
  31.     block() : shapeBlock(sf::Shape::Rectangle(0.f ,0, 10.f, 10.f, sf::Color::White))
  32.     {
  33.         shapeBlock.SetOrigin(5.f, 5.f);
  34.         shapeBlock.SetPosition(width / 2, height / 2);     
  35.         shapeBlock.SetRotation(currentangle);
  36.  
  37.         float dx = (float)std::cos(ConvRad(currentangle)) * speed * time_constant;
  38.         float dy = (float)std::sin(ConvRad(currentangle)) * speed * time_constant;
  39.         vector2Direction = sf::Vector2f(dx, dy);
  40.        
  41.         currentangle += 360.f / numberofobjects;
  42.         if(currentangle >= 360.f)
  43.             currentangle = 0.f;
  44.     }
  45.  
  46.     //Verify colosion between object and border
  47.     bool VerifyBorderColision()
  48.     {
  49.         sf::Vector2f currentpos(shapeBlock.GetPosition());
  50.         if(currentpos.x > width || currentpos.x < 0.f
  51.             || currentpos.y > height || currentpos.y < 0.f)
  52.             return true;
  53.         else return false;
  54.     }
  55.  
  56.     //Move the object
  57.     void Update()
  58.     {
  59.         shapeBlock.Move(vector2Direction);
  60.     }
  61. };
  62.  
  63. float block::currentangle = 0.f;
  64.  
  65. int main()
  66. {
  67.     //The main vector that will manage all the objects
  68.     std::vector<block> vectorBlocks;
  69.     vectorBlocks.reserve(1000);
  70.    
  71.     //Main RenderWindow
  72.     sf::RenderWindow renderwindow(sf::VideoMode(width, height, 32), "Vector Benchmark");
  73.     renderwindow.SetFramerateLimit(60);
  74.    
  75.     //Text object that will contain general information for
  76.     //FPS
  77.     //Number of objects
  78.     sf::Text textInfo;
  79.     textInfo.SetCharacterSize(12);
  80.  
  81.     //Clock that will manage the spawn frequency
  82.     sf::Clock clockFrequency;
  83.     clockFrequency.Reset();
  84.  
  85.     while(renderwindow.IsOpened())
  86.     {
  87.         sf::Event myevent;
  88.         while(renderwindow.PollEvent(myevent))
  89.         {
  90.             if((myevent.Type == sf::Event::Closed)||
  91.                 (myevent.Type == sf::Event::KeyPressed && myevent.Key.Code == sf::Keyboard::Escape))
  92.                 renderwindow.Close();
  93.         }
  94.  
  95.         //If it's time to spawn new objects
  96.         if(clockFrequency.GetElapsedTime() > frequency)
  97.         {
  98.             //Then add them all to the vector
  99.             for(unsigned int i = 0; i < numberofobjects; i++)
  100.             {
  101.                 block toadd;
  102.                 vectorBlocks.push_back(toadd);
  103.             }
  104.             clockFrequency.Reset();
  105.         }
  106.        
  107.         //Just updating the data for textInfo
  108.         std::ostringstream oss;
  109.         oss << "FPS: " << 1000.0 / renderwindow.GetFrameTime()
  110.             << "\nNumber of Blocks: " << vectorBlocks.size();
  111.         textInfo.SetString(oss.str());
  112.  
  113.         renderwindow.Clear();
  114.         //Starting to draw every object
  115.         for(unsigned int i = 0; i < vectorBlocks.size(); i++)
  116.         {
  117.             //Move the Objects
  118.             vectorBlocks[i].Update();
  119.             //Then verify if they're touching the borders and erase if they are
  120.             if(vectorBlocks[i].VerifyBorderColision())
  121.                 vectorBlocks.erase(vectorBlocks.begin()+i);
  122.             //Then Draw them
  123.             renderwindow.Draw(vectorBlocks[i].shapeBlock);
  124.         }
  125.         renderwindow.Draw(textInfo);
  126.         renderwindow.Display();
  127.     }
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement