void drawSprite(Sprite &spr, RenderTarget &target) { RectangleShape shape; Texture &sftexture = const_cast<Texture&>(*spr.getTexture()); FloatRect r = spr.getLocalBounds(); shape.setSize(Vector2f(r.width, r.height)); shape.setOrigin(spr.getOrigin()); shape.setRotation(spr.getRotation()); shape.setPosition(spr.getPosition()); shape.setScale(spr.getScale()); shape.setTexture(spr.getTexture()); shape.setFillColor(spr.getColor()); sftexture.setRepeated(false); target.draw(shape); sftexture.setRepeated(true); }
void update(float time, int number_block) { if (step == MOVE_DOWN) step_one(rectangle, time, pos.y, step); else if (step == MOVE_RIGHT_AND_UP) step_two(rectangle, time, pos.x, pos.y, step, number_block, rotation); else if (step == MOVE_RIGHT_AND_TURN) step_three(rectangle, time, number_block, pos.x, pos.y, step); else if (step == MOVE_DOWN_AND_CHANGE_SCALE) step_four(rectangle, time, number_block, pos.x, pos.y, step, size_y); else if (step == MOVE_LEFT) step_five(rectangle, time, number_block, pos.x, pos.y, step, size_y); rectangle.setScale(size_x, size_y); rectangle.setRotation(rotation); rectangle.setPosition(pos.x, pos.y); };
void RenderSystem::update(float dt, std::vector<GameObject*>* objects) { for (Ite i = objects->begin(); i != objects->end(); i++) { RenderComponent* render = (*i)->getComponent<RenderComponent>(); if (render != nullptr) { RectangleShape shape = *render->getDrawable(); PhysicsComponent* physics = (*i)->getComponent<PhysicsComponent>(); if (physics != nullptr) { Vector2f position = mScale * flipY(physics->getBody()->GetPosition()); shape.setPosition(position); shape.setSize(1.02f * mScale * physics->getSize()); shape.setOrigin(shape.getSize() * 0.5f); // positive direction is opposite in Box2D shape.setRotation(-1.0f * physics->getBody()->GetAngle() * 180 / b2_pi); } mWindow->draw(shape); } } }
void Simulation::testons() { Event event; while(m_window.pollEvent(event)) { if(event.type == Event::Closed) m_window.close(); if(event.type == Event::MouseButtonReleased)//Si on clique, on ajoute un corps { if(event.mouseButton.button == Mouse::Left) { Vector2i mousePosition = Mouse::getPosition(m_window); m_test.setPosition(mousePosition.x, mousePosition.y); } } } //données: CircleShape cercle; CircleShape base; base.setPosition(390, 390); base.setRadius(10); base.setFillColor(Color::Red); double x(0), y(0), rayon(5); //int color[3]; m_test.getPosition(x, y); m_window.clear(Color::Black); cercle.setPosition((int)x-rayon, (int)y-rayon); cercle.setRadius(rayon); cercle.setFillColor(Color::White); double yb(400), xb(400); double coefdir = (yb - y) / (xb - x); double angle = atan(coefdir); if(x>xb) { angle = (angle+3.14); while(angle > 2*3.14) { angle -= 2*3.14; } } double angleD = angle*(360/(2*3.14)); RectangleShape vectorA; vectorA.setSize(Vector2f(50, 1)); vectorA.setPosition(x, y); vectorA.setRotation(angleD); RectangleShape vectorX; vectorX.setSize(Vector2f(50*cos(angle), 1)); vectorX.setPosition(x, y); vectorX.setFillColor(Color::Green); RectangleShape vectorY; vectorY.setSize(Vector2f(1, 50*sin(angle))); vectorY.setPosition(x, y); vectorY.setFillColor(Color::Green); m_window.draw(base); m_window.draw(cercle); m_window.draw(vectorA); m_window.draw(vectorX); m_window.draw(vectorY); m_affichageAux->setString("Angle: " + to_string(angleD)); m_window.draw(*m_affichageAux); m_window.display(); }
int main(int argc, char** argv) { RenderWindow window(sf::VideoMode(W_WIDTH,W_HEIGHT),"Hello",sf::Style::Close); View view{ FloatRect{0,0,V_WIDTH,V_HEIGHT} }; View defaultView = View{ FloatRect{ 0,0, 1600, 1200 } }; //window.setView(view); Physics physics{ {0.f, 9.8f}, 10.f }; //LightMap lights{view}; LightMap lights{ defaultView }; auto playerLight = lights.createLight("../assets/lightmask.png", 400, 370, 3.f); //lights.createLight("../assets/lightmask.png", 200, 100, 3.f); Clock clock; physics.spawnStaticBox(40.f, 60.f, 800.f, 40.f); physics.spawnStaticBox(80.f, 110.f, 800.f, 40.f); auto pPlayerBody = physics.spawnDynamicCircle(3.f, 400.f, 0.f); Texture tex; tex.loadFromFile("../assets/tile.png"); tex.setSmooth(true); Shader blendShader; if (!blendShader.loadFromFile("../src/test.frag", Shader::Fragment)) std::cout << "Failed to load blend fragment shader" << std::endl; Texture playerTex; playerTex.loadFromFile("../assets/player.png"); playerTex.setSmooth(true); sf::Event ev; Texture worldTex; worldTex.create(W_WIDTH, W_HEIGHT); Sprite displaySprite; Font font; font.loadFromFile("../assets/kenney_bold.ttf"); Text framerate; framerate.setFont(font); framerate.setCharacterSize(40); framerate.setPosition(1100, 200); framerate.setColor(sf::Color::White); while (window.isOpen()) { while (window.pollEvent(ev)) { if (ev.type == Event::Closed) { window.close(); } if (ev.type == Event::KeyPressed) { if (ev.key.code == sf::Keyboard::Left) { if (pPlayerBody->GetLinearVelocity().x - (MS_X / 100) > -(2 * MS_X / 100)) pPlayerBody->ApplyForceToCenter({-MS_X, 0.f},true); else pPlayerBody->ApplyForceToCenter({ -(2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true); } if (ev.key.code == sf::Keyboard::Right) { if (pPlayerBody->GetLinearVelocity().x + (MS_X / 100) < (2 * MS_X / 100)) pPlayerBody->ApplyForceToCenter({ MS_X, 0.f }, true); else pPlayerBody->ApplyForceToCenter({ (2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true); } if (ev.key.code == sf::Keyboard::Space) { if (pPlayerBody->GetLinearVelocity().y + (MS_X / 100) < (2 * MS_X / 100)) pPlayerBody->ApplyForceToCenter({ 0.f, -MS_X * 2 }, true); //else //pPlayerBody->ApplyForceToCenter({ 0.f, (3 * MS_X / 100) - pPlayerBody->GetLinearVelocity().y }, true); } } if (ev.type == Event::JoystickButtonPressed) { std::cout << ev.joystickButton.button << std::endl; } if (ev.type == Event::JoystickMoved) { std::cout << ev.joystickMove.axis << std::endl; } } physics.step(1.f / 30.f); window.clear(); auto playerPos = pPlayerBody->GetPosition(); view.setCenter(SCALE * playerPos.x, SCALE * playerPos.y); //window.setView(view); auto lightSize = playerLight->getSprite().getTexture()->getSize(); // 3.f is the player body radius playerLight->setPosition( playerPos.x * SCALE - lightSize.x - 3.f * SCALE * 2 , playerPos.y * SCALE - lightSize.y - 3.f * SCALE * 2); auto bodyIt = physics.getBodyList(); //lights.updateView(view); lights.render(); while (bodyIt != nullptr) { auto pos = bodyIt->GetPosition(); if (bodyIt->GetType() == b2_dynamicBody) { CircleShape sprite; sprite.setTexture(&playerTex); auto playerShape = *(b2CircleShape*)(bodyIt->GetFixtureList()->GetShape()); sprite.setRadius(playerShape.m_radius * SCALE); sprite.setOrigin(playerShape.m_radius * SCALE, playerShape.m_radius * SCALE); sprite.setPosition(SCALE * pos.x, SCALE * pos.y); sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi); window.draw(sprite); } else // ground { RectangleShape sprite; sprite.setSize({800.f, 40.f}); sprite.setOrigin(400, 20); sprite.setPosition(pos.x * SCALE, pos.y * SCALE); sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi); sprite.setTexture(&tex); window.draw(sprite); } bodyIt = bodyIt->GetNext(); } window.draw(Sprite{ lights.getLightMapTexture() }, BlendMultiply); float frameTime = 1.f / clock.getElapsedTime().asSeconds(); clock.restart(); framerate.setString(std::to_string((int)frameTime) + " fps"); window.draw(framerate); window.display(); } }