Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SFML/Graphics.hpp>
- #include "Box2D.h"
- #include "Box.h"
- #include <vector>
- #include <iostream>
- /** We need this to easily convert between pixel and real-world coordinates*/
- /** Create the base for the boxes to land */
- void CreateGround(b2World& World, float X, float Y);
- /** Create the boxes */
- void CreateBox(b2World& World, int MouseX, int MouseY);
- int main()
- {
- /** Prepare the window */
- sf::RenderWindow Window(sf::VideoMode(800, 600, 32), "Test");
- Window.setFramerateLimit(60);
- /** Prepare the world */
- b2Vec2 Gravity(0.f, 9.8f);
- b2World World(Gravity);
- CreateGround(World, 400.f, 500.f);
- /** Prepare textures */
- sf::Texture GroundTexture;
- sf::Texture BoxTexture;
- GroundTexture.loadFromFile("ground.png");
- BoxTexture.loadFromFile("box.png");
- std::vector<Box> boxes;
- while (Window.isOpen())
- {
- if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
- {
- int MouseX = sf::Mouse::getPosition(Window).x;
- int MouseY = sf::Mouse::getPosition(Window).y;
- Box box(&World, MouseX, MouseY);
- box.setTexture(BoxTexture);
- boxes.push_back(box);
- }
- World.Step(1/60.f, 8, 3);
- Window.clear(sf::Color::White);
- int BodyCount = 0;
- for (b2Body* BodyIterator = World.GetBodyList(); BodyIterator != 0; BodyIterator = BodyIterator->GetNext())
- {
- if (!(BodyIterator->GetType() == b2_dynamicBody))
- {
- sf::Sprite GroundSprite;
- GroundSprite.setTexture(GroundTexture);
- GroundSprite.setOrigin(400.f, 8.f);
- GroundSprite.setPosition(BodyIterator->GetPosition().x * SCALE, BodyIterator->GetPosition().y * SCALE);
- GroundSprite.setRotation(180/b2_pi * BodyIterator->GetAngle());
- Window.draw(GroundSprite);
- }
- }
- for(int i = 0; i < boxes.size(); i++)
- {
- boxes[i].update();
- Window.draw(boxes[i].getSprite());
- }
- Window.display();
- }
- return 0;
- }
- void CreateGround(b2World& World, float X, float Y)
- {
- b2BodyDef BodyDef;
- BodyDef.position = b2Vec2(X/SCALE, Y/SCALE);
- BodyDef.type = b2_staticBody;
- b2Body* Body = World.CreateBody(&BodyDef);
- b2PolygonShape Shape;
- Shape.SetAsBox((800.f/2)/SCALE, (16.f/2)/SCALE);
- b2FixtureDef FixtureDef;
- FixtureDef.density = 0.f;
- FixtureDef.shape = &Shape;
- Body->CreateFixture(&FixtureDef);
- }
Advertisement
Add Comment
Please, Sign In to add comment