Ball(float mX, float mY) { shape.setPosition(mX, mY); shape.setRadius(ballRadius); shape.setFillColor(Color::Red); shape.setOrigin(ballRadius, ballRadius); }
void Sun::draw(DrawData drawData) { RenderWindow* window = drawData.window; double zoom = drawData.zoom; Vector2i viewPos = drawData.viewPos; CircleShape sun; int r = radius / zoom; r = r < 2 ? 2 : r; sun.setRadius(r); sun.setPointCount(1024); sun.setPosition(globalToDrawCoords(viewPos, Vector2i(0, 0), zoom)); sun.setOrigin(Vector2f(r, r)); sun.setFillColor(Color(200, 200, 0)); window->draw(sun); for (int i = 0; i < moons.size(); i++) { moons.at(i).draw(window, zoom, viewPos); } for (int i = 0; i < planets.size(); i++) { planets.at(i).draw(window, zoom, viewPos); } }
// Let's create the Ball constructor. // (argument mX -> starting x coordinate) // (argument mY -> starting y coordinate) Ball(float mX, float mY) { // Apply position, radius, color and origin // to the CircleShape `shape`. shape.setPosition(mX, mY); shape.setRadius(ballRadius); shape.setFillColor(Color::Red); shape.setOrigin(ballRadius, ballRadius); }
void GameGraphics::drawCannonBalls(Time elapsedTime) { for (CannonBallList* cbl = game->cannonBallsManager.registeredCannonBalls; cbl != NULL; cbl=cbl->down) { CircleShape circle; circle.setRadius(CANNON_BALL_RADIUS); circle.setFillColor(Color::Yellow); circle.setOrigin(CANNON_BALL_RADIUS, CANNON_BALL_RADIUS); circle.setPosition(cbl->cannonBall.position); game->window.draw(circle); } }
void Crawler::DebugDraw( RenderTarget *target ) { if( !dead ) { CircleShape cs; cs.setFillColor( Color::Cyan ); cs.setRadius( 10 ); cs.setOrigin( cs.getLocalBounds().width / 2, cs.getLocalBounds().height / 2 ); V2d g = ground->GetPoint( edgeQuantity ); cs.setPosition( g.x, g.y ); //owner->window->draw( cs ); //UpdateHitboxes(); physBody.DebugDraw( target ); } // hurtBody.DebugDraw( target ); // hitBody.DebugDraw( target ); }
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(); } }