diff --git a/sfmldemo/main.cpp b/sfmldemo/main.cpp index d7f33b8..da90068 100644 --- a/sfmldemo/main.cpp +++ b/sfmldemo/main.cpp @@ -4,7 +4,8 @@ #include #include #include -#include +#include +#include #include #include #include @@ -30,9 +31,9 @@ void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock, sf::Time &tic ImGui::End(); } -void Window_update(sf::RenderWindow &window, sf::Shape &shape) { +void Window_update(sf::RenderWindow &window, sf::Sprite &player) { window.clear(); - window.draw(shape); + window.draw(player); ImGui::SFML::Render(window); window.display(); } @@ -88,9 +89,11 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { b2Vec2 force(-200, 1000); box.body->ApplyForceToCenter(force, true); + box.body->ApplyTorque(100.0f, true); } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { b2Vec2 force(200, 1000); box.body->ApplyForceToCenter(force, true); + box.body->ApplyTorque(-100.0f, true); } break; case sf::Event::MouseButtonPressed: @@ -108,7 +111,7 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box) { } } -sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Shape &shape) { +sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Sprite &player) { sf::Vector2u winSize = window.getSize(); float timeStep = 1.0f / 60.0f; int velocityIterations = 6; @@ -120,26 +123,29 @@ sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &cl 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); + player.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f); + player.setRotation(angle * 180.0f / M_PI); ImGui_update(window, deltaClock, tick); - Window_update(window, shape); + Window_update(window, player); return nextTick; } -sf::CircleShape Create_player(sf::RenderWindow &window) { - 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)); +void Create_player(sf::RenderWindow &window, sf::Sprite &player, sf::Texture &texture) { + if(!texture.loadFromFile("sprite.png")) { + fmt::print("Error loading sprite!"); + } - return shape; + texture.setSmooth(true); + + player.setTexture(texture); + + // position the prite + sf::Vector2u winSize = window.getSize(); + player.setPosition(winSize.x / 2, winSize.y / 2); + player.setOrigin(50.f, 50.f); } int main() { @@ -155,7 +161,9 @@ int main() { sf::Clock clock; sf::Time tick = clock.getElapsedTime(); - sf::CircleShape shape = Create_player(window); + sf::Sprite player; + sf::Texture texture; + Create_player(window, player, texture); b2Vec2 gravity(0.0f, -10.0f); b2World world(gravity); @@ -164,7 +172,7 @@ int main() { while (window.isOpen()) { Handle_events(window, box); // preparing for refactoring this into a class or struct for everything - tick = Update_entities(window, world, clock, deltaClock, tick, box, shape); + tick = Update_entities(window, world, clock, deltaClock, tick, box, player); } ImGui::SFML::Shutdown(); diff --git a/sfmldemo/sprite.png b/sfmldemo/sprite.png new file mode 100644 index 0000000..eec9fe9 Binary files /dev/null and b/sfmldemo/sprite.png differ