Skip to content

Instantly share code, notes, and snippets.

@jmcgill
Created November 27, 2011 00:55
Show Gist options
  • Save jmcgill/1396667 to your computer and use it in GitHub Desktop.
Save jmcgill/1396667 to your computer and use it in GitHub Desktop.
SFML and Box2D Demo
#include <Box2D/Box2D.h>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <stdio.h>
#include <cstring>
#include "DebugDraw.h"
#define kBodies 5
b2Body* CreateBody(b2World& world, int32 index) {
// Define the dynamic body. We set its position and call the body factory.
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(10.0f, index * 4.0f);
bodyDef.angle = rand() * 3.14;
b2Body* body = world.CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
// Set the box density to be non-zero, so it will be dynamic.
fixtureDef.density = 0.3f;
fixtureDef.restitution = 0.4f;
// Override the default friction.
fixtureDef.friction = 0.3f;
// Add the shape to the body.
body->CreateFixture(&fixtureDef);
return body;
}
int main() {
b2Vec2 gravity(0.0f, -5.0f);
b2World world(gravity);
world.SetAllowSleeping(true);
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(10.0f, -30.0f);
b2Body* groundBody = world.CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// The extents are the half-widths of the box.
groundBox.SetAsBox(40.0f, 1.0f);
// Add the ground fixture to the ground body.
groundBody->CreateFixture(&groundBox, 0.0f);
b2BodyDef groundBodyDef2;
groundBodyDef2.position.Set(-10.0f, 0.0f);
b2Body* groundBody2 = world.CreateBody(&groundBodyDef2);
// Define the ground box shape.
b2PolygonShape groundBox2;
// The extents are the half-widths of the box.
groundBox2.SetAsBox(1.0f, 40.0f);
// Add the ground fixture to the ground body.
groundBody2->CreateFixture(&groundBox2, 0.0f);
b2Body* body[kBodies + 10];
for (int i = 0; i < kBodies; ++i) {
body[i] = CreateBody(world, i);
}
float32 timeStep = 1.0f / 60.0f;
int32 velocityIterations = 15;
int32 positionIterations = 15;
// Create the main window
bool use_vsync = true;
sf::RenderWindow App(sf::VideoMode(924, 704), "SFML window");
App.UseVerticalSync(use_vsync);
DebugDraw draw(App);
world.SetDebugDraw(&draw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
draw.SetFlags(flags);
// Create a graphical string to display
sf::Font Arial;
if (!Arial.LoadFromFile("Vera.ttf")) return EXIT_FAILURE;
sf::String Text("Hello SFML", Arial, 25);
Text.SetX(700);
Text.SetY(600);
// FPS as a string.
char fps_string[25];
// Start the game loop
while (App.IsOpened()) {
sf::Event Event;
while (App.GetEvent(Event)) {
// Did someone close the application?
if (Event.Type == sf::Event::Closed) {
App.Close();
}
// Did someone press V to toggle vsync?
if (Event.Type == sf::Event::KeyPressed && Event.Key.Code == sf::Key::V) {
use_vsync = !use_vsync;
App.UseVerticalSync(use_vsync);
}
}
// Clear screen
App.Clear();
world.Step(timeStep, velocityIterations, positionIterations);
world.DrawDebugData();
// Draw the current framerate.
float Framerate = 1.f / App.GetFrameTime();
sprintf(fps_string, "%f", Framerate);
Text.SetText(fps_string);
App.Draw(Text);
// Update the window
App.Display();
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment