Advertisement
Guest User

Lolechi asteroids faulty full source

a guest
Dec 29th, 2015
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.85 KB | None | 0 0
  1. #include <C:\SFML-2.3.2\include\SFML\Graphics.hpp>
  2. #include <iostream>
  3. #include <ctime> //Time
  4. #include <cstdlib> //Rand
  5. #include <math.h>
  6. #define PI 3.14159265
  7.  
  8. using namespace std;
  9.  
  10. class asteroid{
  11.     public:
  12.         sf::Vector2f pos;
  13.         float angle;
  14.         void create(sf::ConvexShape);
  15.         void update(sf::Vector2f, sf::ConvexShape);
  16. };
  17. /* Work in progress */
  18. void asteroid::update(sf::Vector2f a, sf::ConvexShape b){
  19.     pos += a;
  20.     b.setPosition(pos);
  21. };
  22.  
  23. void asteroid::create(sf::ConvexShape a){ //Tweak the drawing values for a better effect
  24.     cout << "Creating..." << endl;
  25.  
  26.     a.setPointCount(20);
  27.     srand(time(0)); //Set random seed
  28.     int minDelta = 25;
  29.     int maxDelta = 75;
  30.     int minRad = 25;
  31.     int maxRad = 50;
  32.     float step = (float)PI / 10;
  33.     int r = rand()%minRad + maxRad; //Radius
  34.     float x, y;
  35.  
  36.     for(int i = 0; i < a.getPointCount()+1; i++){ //Drawing asteroid
  37.         x = r*cos(step*i);
  38.         y = r*sin(step*i);
  39.         a.setPoint(i, sf::Vector2f(x + (rand()% minDelta + maxDelta), y + (rand()% minDelta + maxDelta)));
  40.     }
  41.     a.setFillColor(sf::Color::White);
  42.     pos = sf::Vector2f(rand()%800, rand()%600); //Random position on screen | Look into casting, convert rand() from int to float
  43.     a.setPosition(pos); //Spawn on that position
  44.  
  45.     cout << "Done!" << endl;
  46. };
  47.  
  48. int main()
  49. {
  50.     // Init //
  51.     sf::RenderWindow window(sf::VideoMode(800, 600), "Asteroids!", sf::Style::Default);
  52.  
  53.     /* Old
  54.     std::vector<sf::ConvexShape> allShapes; //List of all asteroid SHAPES
  55.     std::vector<asteroid> allAsteroids; //List of asteroid CLASS OBJECTS
  56.     allAsteroids.push_back(asteroid()); //New asteroid CLASS OBJECT
  57.  
  58.     for(std::vector<asteroid>::iterator it = allAsteroids.begin(); it != allAsteroids.end(); ++it){ //Creating an asteroid shape per 'asteroid' object inside "allAsteroids"
  59.         it->create(allShapes);
  60.     }
  61.  
  62.     //Send update() both a velocity vector and a iterator to access the shape
  63.     for(std::vector<asteroid>::iterator classIt = allAsteroids.begin(); classIt != allAsteroids.end(); ++classIt){
  64.         for(std::vector<sf::ConvexShape>::iterator shapeIt = allShapes.begin(); shapeIt != allShapes.end(); ++shapeIt){
  65.             auto shapePos = (*shapeIt);
  66.         }
  67.     }
  68.     */
  69.  
  70.     std::map<asteroid, sf::ConvexShape> asteroids;
  71.     asteroids.insert(std::pair<asteroid, sf::ConvexShape>(asteroid(),sf::ConvexShape())); //New CLASS OBJECT and SHAPE inside map
  72.     asteroids.insert(std::pair<asteroid, sf::ConvexShape>(asteroid(),sf::ConvexShape())); //New CLASS OBJECT and SHAPE inside map
  73.     for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Generate SHAPES for each CLASS OBJECT
  74.         auto classIt = tempIt->first;
  75.         auto shapeIt = tempIt->second;
  76.         classIt.create(shapeIt);
  77.     }
  78.  
  79.     // Loop //
  80.     while (window.isOpen())
  81.     {
  82.         // Event //
  83.         sf::Event event;
  84.         while (window.pollEvent(event))
  85.         {
  86.             switch (event.type){
  87.                 case sf::Event::Closed:
  88.                     window.close();
  89.                 break;
  90.                 case sf::Event::KeyPressed:
  91.                     if(event.key.code == sf::Keyboard::Escape){ //Escape = Close
  92.                         window.close();
  93.                     }
  94.                 break;
  95.             }
  96.         }
  97.         /*
  98.         for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Update every asteroid SHAPE's position
  99.             auto classIt = tempIt->first;
  100.             auto shapeIt = (*tempIt).second;
  101.             classIt.update(sf::Vector2f(10, 10), shapeIt); //Vector2f is velocity
  102.         }
  103.         */
  104.         // Display //
  105.         window.clear();
  106.         for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Draw all asteroid SHAPES
  107.             auto it = (*tempIt).second;
  108.             window.draw(it);
  109.         }
  110.         window.display();
  111.     }
  112.  
  113.     return 0;
  114. }
  115.  
  116. //          To-Do:
  117. // Troubleshoot errors. Starting with double to float conversion warnings.
  118. // Handle multiple asteroids. Errors?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement