Guest User

main.cpp

a guest
Aug 30th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.54 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include "Box2D.h"
  3. #include "Box.h"
  4. #include <vector>
  5. #include <iostream>
  6.  
  7. /** We need this to easily convert between pixel and real-world coordinates*/
  8.  
  9.  
  10. /** Create the base for the boxes to land */
  11. void CreateGround(b2World& World, float X, float Y);
  12.  
  13. /** Create the boxes */
  14. void CreateBox(b2World& World, int MouseX, int MouseY);
  15.  
  16. int main()
  17. {
  18.     /** Prepare the window */
  19.     sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Test");
  20.     Window.setFramerateLimit(60);
  21.  
  22.     /** Prepare the world */
  23.     b2Vec2 Gravity(0.f, 9.8f);
  24.     b2World World(Gravity);
  25.     CreateGround(World, 400.f, 500.f);
  26.  
  27.     /** Prepare textures */
  28.     sf::Texture GroundTexture;
  29.     sf::Texture BoxTexture;
  30.     GroundTexture.loadFromFile("ground.png");
  31.     BoxTexture.loadFromFile("box.png");
  32.  
  33.     std::vector<Box> boxes;
  34.     while (Window.isOpen())
  35.     {
  36.         if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
  37.         {
  38.             int MouseX = sf::Mouse::getPosition(Window).x;
  39.             int MouseY = sf::Mouse::getPosition(Window).y;
  40.             Box box(&World, MouseX, MouseY);
  41.             box.setTexture(BoxTexture);
  42.             boxes.push_back(box);
  43.         }
  44.         World.Step(1/60.f, 8, 3);
  45.  
  46.         Window.clear(sf::Color::White);
  47.         int BodyCount = 0;
  48.         for (b2Body* BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext())
  49.         {
  50.             if (!(BodyIterator->GetType() == b2_dynamicBody))
  51.             {
  52.                 sf::Sprite GroundSprite;
  53.                 GroundSprite.setTexture(GroundTexture);
  54.                 GroundSprite.setOrigin(400.f, 8.f);
  55.                 GroundSprite.setPosition(BodyIterator->GetPosition().x * SCALE, BodyIterator->GetPosition().y * SCALE);
  56.                 GroundSprite.setRotation(180/b2_pi * BodyIterator->GetAngle());
  57.                 Window.draw(GroundSprite);
  58.             }
  59.         }
  60.         for(int i = 0; i < boxes.size(); i++)
  61.         {
  62.             boxes[i].update();
  63.             Window.draw(boxes[i].getSprite());
  64.         }
  65.         Window.display();
  66.     }
  67.  
  68.         return 0;
  69. }
  70.  
  71. void CreateGround(b2World& World, float X, float Y)
  72. {
  73.     b2BodyDef BodyDef;
  74.     BodyDef.position = b2Vec2(X/SCALE, Y/SCALE);
  75.     BodyDef.type = b2_staticBody;
  76.     b2Body* Body = World.CreateBody(&BodyDef);
  77.  
  78.     b2PolygonShape Shape;
  79.     Shape.SetAsBox((800.f/2)/SCALE, (16.f/2)/SCALE);
  80.     b2FixtureDef FixtureDef;
  81.     FixtureDef.density = 0.f;
  82.     FixtureDef.shape = &Shape;
  83.     Body->CreateFixture(&FixtureDef);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment