#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include "dbc.hpp" void Window_update(sf::RenderWindow &window, sf::Sprite &player) { window.clear(); window.draw(player); 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; } void Handle_events(sf::RenderWindow &window, BoxTest &box, sf::Sound &click) { // is this a main event loop while (const auto event = window.pollEvent()) { if(event->is()) { fmt::print("Exiting...\n"); window.close(); } else if (const auto* key = event->getIf()) { if(key->scancode == sf::Keyboard::Scan::Left) { b2Vec2 force(-200, 1000); box.body->ApplyForceToCenter(force, true); box.body->ApplyTorque(100.0f, true); click.play(); } else if(key->scancode == sf::Keyboard::Scan::Right) { b2Vec2 force(200, 1000); box.body->ApplyForceToCenter(force, true); box.body->ApplyTorque(-100.0f, true); click.play(); } } else if(const auto* mouse = event->getIf()) { if(mouse->button == sf::Mouse::Button::Left) { b2Vec2 force(-200, 1000); box.body->ApplyForceToCenter(force, true); box.body->ApplyTorque(100.0f, true); click.play(); } else if(mouse->button == sf::Mouse::Button::Right) { b2Vec2 force(200, 1000); box.body->ApplyForceToCenter(force, true); box.body->ApplyTorque(-100.0f, true); click.play(); } } } } sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Time &tick, BoxTest &box, sf::Sprite &player) { sf::Vector2u winSize = window.getSize(); float timeStep = 1.0f / 60.0f; int velocityIterations = 6; int positionIterations = 2; sf::Time since = clock.getElapsedTime(); sf::Time nextTick = since - tick > sf::seconds(1) ? since : tick; world.Step(timeStep, velocityIterations, positionIterations); b2Vec2 position = box.body->GetPosition(); float angle = box.body->GetAngle(); player.setPosition({position.x * 100.0f, winSize.y - position.y * 100.0f}); player.setRotation(sf::degrees(angle * 180.0f / M_PI)); Window_update(window, player); return nextTick; } sf::Sprite Create_player(sf::RenderWindow &window, sf::Texture &texture) { if(!texture.loadFromFile("sprite.png")) { fmt::print("Error loading sprite!"); } texture.setSmooth(true); sf::Sprite player(texture); player.setTexture(texture); // position the prite sf::Vector2u winSize = window.getSize(); player.setPosition({float(winSize.x) / 2, float(winSize.y) / 2}); player.setOrigin({50.f, 50.f}); return player; } int main() { fmt::print("Setting up a window for you...\n"); sf::RenderWindow window(sf::VideoMode({1280, 720}), "Simple Game Demo"); window.setFramerateLimit(60); window.setVerticalSyncEnabled(true); sf::SoundBuffer buffer; if(!buffer.loadFromFile("click.mp3")) { fmt::print("Failed to load click.ogg!\n"); } sf::Sound click(buffer); sf::Clock deltaClock; sf::Clock clock; sf::Time tick = clock.getElapsedTime(); sf::Texture texture; auto player = Create_player(window, texture); b2Vec2 gravity(0.0f, -10.0f); b2World world(gravity); BoxTest box = Box2d_setup(world); while (window.isOpen()) { Handle_events(window, box, click); // preparing for refactoring this into a class or struct for everything tick = Update_entities(window, world, clock, tick, box, player); } }