void UIComponentImage::onRender(GraphicsDevice* renderer, UIView* view) { RectangleShape backgroundRect; backgroundRect.setRect(view->getBounds()); if(mTexture) backgroundRect.setTexture(mTexture); renderer->draw(backgroundRect); }
void UIComponentButton::onRender(Renderer* renderer, UIView* view) { if(hovering) { RectangleShape hoverRect; hoverRect.setRect(view->getBounds()); hoverRect.setColor(mHoverColor); if(mTexture) { hoverRect.setTexture(mHoverTexture); } renderer->draw(hoverRect); } else { RectangleShape hoverRect; hoverRect.setRect(view->getBounds()); hoverRect.setColor(mNormalColor); if(mTexture) { hoverRect.setTexture(mTexture); } renderer->draw(hoverRect); } // -- Label Text buttonLabel; buttonLabel.setFont(*view->mCore->m_defaultFont); buttonLabel.setString(mString); buttonLabel.setCharacterSize(view->mRect.height / 2); buttonLabel.setOrigin(static_cast<int>((buttonLabel.getLocalBounds().width / 2.f ) + 0.5f), static_cast<int>((buttonLabel.getLocalBounds().height / 2.f) + 0.5f)); buttonLabel.setPosition(static_cast<int>((view->mRect.left + view->mRect.width / 2.f ) + 0.5f), static_cast<int>((view->mRect.top + view->mRect.height / 2.f) + 0.5f)); if(buttonLabel.getLocalBounds().width > view->getSize().x * 0.9f) { // The text is too big and passes the 90% of the button's width float diminishRatio = (view->getSize().x * 0.9f) / buttonLabel.getLocalBounds().width; buttonLabel.setCharacterSize(static_cast<unsigned int>(static_cast<float>(buttonLabel.getCharacterSize()) * diminishRatio)); buttonLabel.setOrigin(static_cast<int>((buttonLabel.getLocalBounds().width / 2.f ) + 0.5f), static_cast<int>((buttonLabel.getLocalBounds().height / 2.f) + 0.5f)); } renderer->draw(buttonLabel); }
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); }
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(); } }