Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <C:\SFML-2.3.2\include\SFML\Graphics.hpp>
- #include <iostream>
- #include <ctime> //Time
- #include <cstdlib> //Rand
- #include <math.h>
- #define PI 3.14159265
- using namespace std;
- class asteroid{
- public:
- sf::Vector2f pos;
- float angle;
- void create(sf::ConvexShape);
- void update(sf::Vector2f, sf::ConvexShape);
- };
- /* Work in progress */
- void asteroid::update(sf::Vector2f a, sf::ConvexShape b){
- pos += a;
- b.setPosition(pos);
- };
- void asteroid::create(sf::ConvexShape a){ //Tweak the drawing values for a better effect
- cout << "Creating..." << endl;
- a.setPointCount(20);
- srand(time(0)); //Set random seed
- int minDelta = 25;
- int maxDelta = 75;
- int minRad = 25;
- int maxRad = 50;
- float step = (float)PI / 10;
- int r = rand()%minRad + maxRad; //Radius
- float x, y;
- for(int i = 0; i < a.getPointCount()+1; i++){ //Drawing asteroid
- x = r*cos(step*i);
- y = r*sin(step*i);
- a.setPoint(i, sf::Vector2f(x + (rand()% minDelta + maxDelta), y + (rand()% minDelta + maxDelta)));
- }
- a.setFillColor(sf::Color::White);
- pos = sf::Vector2f(rand()%800, rand()%600); //Random position on screen | Look into casting, convert rand() from int to float
- a.setPosition(pos); //Spawn on that position
- cout << "Done!" << endl;
- };
- int main()
- {
- // Init //
- sf::RenderWindow window(sf::VideoMode(800, 600), "Asteroids!", sf::Style::Default);
- /* Old
- std::vector<sf::ConvexShape> allShapes; //List of all asteroid SHAPES
- std::vector<asteroid> allAsteroids; //List of asteroid CLASS OBJECTS
- allAsteroids.push_back(asteroid()); //New asteroid CLASS OBJECT
- for(std::vector<asteroid>::iterator it = allAsteroids.begin(); it != allAsteroids.end(); ++it){ //Creating an asteroid shape per 'asteroid' object inside "allAsteroids"
- it->create(allShapes);
- }
- //Send update() both a velocity vector and a iterator to access the shape
- for(std::vector<asteroid>::iterator classIt = allAsteroids.begin(); classIt != allAsteroids.end(); ++classIt){
- for(std::vector<sf::ConvexShape>::iterator shapeIt = allShapes.begin(); shapeIt != allShapes.end(); ++shapeIt){
- auto shapePos = (*shapeIt);
- }
- }
- */
- std::map<asteroid, sf::ConvexShape> asteroids;
- asteroids.insert(std::pair<asteroid, sf::ConvexShape>(asteroid(),sf::ConvexShape())); //New CLASS OBJECT and SHAPE inside map
- asteroids.insert(std::pair<asteroid, sf::ConvexShape>(asteroid(),sf::ConvexShape())); //New CLASS OBJECT and SHAPE inside map
- for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Generate SHAPES for each CLASS OBJECT
- auto classIt = tempIt->first;
- auto shapeIt = tempIt->second;
- classIt.create(shapeIt);
- }
- // Loop //
- while (window.isOpen())
- {
- // Event //
- sf::Event event;
- while (window.pollEvent(event))
- {
- switch (event.type){
- case sf::Event::Closed:
- window.close();
- break;
- case sf::Event::KeyPressed:
- if(event.key.code == sf::Keyboard::Escape){ //Escape = Close
- window.close();
- }
- break;
- }
- }
- /*
- for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Update every asteroid SHAPE's position
- auto classIt = tempIt->first;
- auto shapeIt = (*tempIt).second;
- classIt.update(sf::Vector2f(10, 10), shapeIt); //Vector2f is velocity
- }
- */
- // Display //
- window.clear();
- for(std::map<asteroid, sf::ConvexShape>::iterator tempIt = asteroids.begin(); tempIt != asteroids.end(); ++tempIt){ //Draw all asteroid SHAPES
- auto it = (*tempIt).second;
- window.draw(it);
- }
- window.display();
- }
- return 0;
- }
- // To-Do:
- // Troubleshoot errors. Starting with double to float conversion warnings.
- // Handle multiple asteroids. Errors?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement