Now box 2d lets you move the box around with right and left mouse buttons.

master
Zed A. Shaw 7 months ago
parent 285cd4971f
commit 38c0fee65c
  1. 67
      sfmldemo/main.cpp
  2. 1
      sfmldemo/meson.build
  3. 1
      sfmldemo/reset_build.ps1
  4. 1
      sfmldemo/reset_build.sh
  5. 1
      sfmldemo/setup.ps1
  6. 18
      sfmldemo/setup.sh

@ -1,7 +1,7 @@
#include "imgui.h"
#include "imgui-SFML.h"
#include <fmt/core.h>
#include <box2d/box2d.h>
#include <SFML/Graphics/CircleShape.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System.hpp>
@ -35,22 +35,55 @@ void Window_update(sf::RenderWindow &window, sf::Shape &shape) {
window.display();
}
struct BoxTest {
b2Body *groundBody;
b2Body *body;
};
struct BoxTest Box2d_setup(b2World &world) {
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0.0f, -10.0f);
b2Body *groundBody = world.CreateBody(&groundBodyDef);
b2PolygonShape groundBox;
groundBox.SetAsBox(50.0f, 10.0f);
groundBody->CreateFixture(&groundBox, 0.0f);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(3.0f, 4.0f);
b2Body *body = world.CreateBody(&bodyDef);
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(1.0f, 1.0f);
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.3f;
body->CreateFixture(&fixtureDef);
BoxTest box {groundBody, body};
return box;
}
int main() {
fmt::print("Setting up a window for you...\n");
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings);
sf::RenderWindow window(sf::VideoMode(720, 480), "Simple Game Demo", sf::Style::Default, settings);
// window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
ImGui_setup(window);
sf::CircleShape shape(100.f, 4);
sf::Vector2u size = window.getSize();
shape.setPosition(size.x / 2, size.y / 2);
shape.setOrigin(100.f, 100.f);
sf::CircleShape shape(50.f, 4);
sf::Vector2u winSize = window.getSize();
shape.setPosition(winSize.x / 2, winSize.y / 2);
shape.setOrigin(50.f, 50.f);
shape.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f);
shape.setOutlineColor(sf::Color(250, 150, 100));
@ -69,6 +102,14 @@ int main() {
thread.launch();
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
BoxTest box = Box2d_setup(world);
float timeStep = 1.0f / 60.0f;
int velocityIterations = 6;
int positionIterations = 2;
while (window.isOpen()) {
sf::Event event;
@ -96,8 +137,11 @@ int main() {
break;
case sf::Event::MouseButtonPressed:
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
sf::Vector2i clickAt = sf::Mouse::getPosition(window);
shape.setPosition(clickAt.x, clickAt.y);
b2Vec2 force(-200, 1000);
box.body->ApplyForceToCenter(force, true);
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
b2Vec2 force(200, 1000);
box.body->ApplyForceToCenter(force, true);
}
break;
}
@ -110,8 +154,13 @@ int main() {
tick = since;
}
world.Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = box.body->GetPosition();
float angle = box.body->GetAngle();
shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
shape.setRotation(angle * 180.0f / M_PI);
shape.rotate(1);
ImGui_update(window, deltaClock, tick);
Window_update(window, shape);

@ -5,6 +5,7 @@ dependencies = [
dependency('sfml'),
dependency('imgui-sfml'),
dependency('fmt'),
dependency('box2d'),
]
executable('sfmldemo', 'main.cpp',

@ -14,4 +14,5 @@ meson wrap install sfml
meson wrap install vorbis
meson wrap install zlib
meson wrap install fmt
meson wrap install box2d
meson setup -Ddefault_library=static builddir

@ -17,4 +17,5 @@ meson wrap install sfml
meson wrap install vorbis
meson wrap install zlib
meson wrap install fmt
meson wrap install box2d
meson setup builddir

@ -10,4 +10,5 @@ meson wrap install openal-soft
meson wrap install sfml
meson wrap install vorbis
meson wrap install zlib
meson wrap install box2d
meson setup -Ddefault_library=static builddir

@ -0,0 +1,18 @@
#!/usr/bin/env bash
set -ex
mkdir subprojects
mkdir builddir
meson wrap install flac
meson wrap install freetype2
meson wrap install imgui-sfml
meson wrap install imgui
meson wrap install libpng
meson wrap install ogg
meson wrap install openal-soft
meson wrap install sfml
meson wrap install vorbis
meson wrap install zlib
meson wrap install fmt
meson wrap install box2d
meson setup builddir
Loading…
Cancel
Save