Improved performance by only rendering onscreen sprites.
This commit is contained in:
parent
b52af711a0
commit
a5b01bf3c5
3 changed files with 13 additions and 5 deletions
|
@ -232,18 +232,17 @@ void
|
||||||
World::step(int elapsed) {
|
World::step(int elapsed) {
|
||||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||||
for (auto it = v->second.begin(); it != v->second.end(); it++) {
|
for (auto it = v->second.begin(); it != v->second.end(); it++) {
|
||||||
auto spriteA = *it;
|
auto& spriteA = *it;
|
||||||
|
sf::Vector2f speed = spriteA->getSpeed() * (elapsed / 1000.0f);
|
||||||
if (spriteA->getDelete()) {
|
if (spriteA->getDelete()) {
|
||||||
remove(spriteA);
|
remove(spriteA);
|
||||||
it--;
|
it--;
|
||||||
}
|
}
|
||||||
// Apply movement for movable sprites.
|
// Apply movement for movable sprites.
|
||||||
else if ((*it)->getSpeed() != sf::Vector2f()) {
|
else if ((*it)->getSpeed() != sf::Vector2f()) {
|
||||||
sf::Vector2f speed = spriteA->getSpeed();
|
|
||||||
speed *= elapsed / 1000.0f;
|
|
||||||
bool overlap = false;
|
bool overlap = false;
|
||||||
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
|
for (auto w = mDrawables.begin(); w != mDrawables.end(); w++) {
|
||||||
for (auto spriteB : w->second) {
|
for (auto& spriteB : w->second) {
|
||||||
if (spriteA == spriteB)
|
if (spriteA == spriteB)
|
||||||
continue;
|
continue;
|
||||||
// Ignore anything that is filtered by masks.
|
// Ignore anything that is filtered by masks.
|
||||||
|
@ -491,8 +490,12 @@ World::getArea(const sf::Vector2f& point) const {
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
World::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
World::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||||
|
sf::FloatRect screen(target.getViewport(target.getView()));
|
||||||
|
screen.left += target.getView().getCenter().x - target.getView().getSize().x / 2;
|
||||||
|
screen.top += target.getView().getCenter().y - target.getView().getSize().y / 2;
|
||||||
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
for (auto v = mDrawables.begin(); v != mDrawables.end(); v++) {
|
||||||
for (auto item : v->second) {
|
for (auto item : v->second) {
|
||||||
|
if (item->isInside(screen))
|
||||||
target.draw(static_cast<sf::Drawable&>(*item), states);
|
target.draw(static_cast<sf::Drawable&>(*item), states);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -149,6 +149,10 @@ Sprite::collisionEnabled(Category category) const {
|
||||||
return (category & mMask) != 0;
|
return (category & mMask) != 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool
|
||||||
|
Sprite::isInside(const sf::FloatRect& rect) const {
|
||||||
|
return rect.intersects(mShape.shape->getGlobalBounds());
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Called when a collision with another Sprite occured. Override this method
|
* Called when a collision with another Sprite occured. Override this method
|
||||||
* to manage collision events.
|
* to manage collision events.
|
||||||
|
|
|
@ -66,6 +66,7 @@ public:
|
||||||
sf::Vector2f getSize() const;
|
sf::Vector2f getSize() const;
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
bool collisionEnabled(Category category) const;
|
bool collisionEnabled(Category category) const;
|
||||||
|
bool isInside(const sf::FloatRect& rect) const;
|
||||||
|
|
||||||
virtual void onCollide(std::shared_ptr<Sprite> other);
|
virtual void onCollide(std::shared_ptr<Sprite> other);
|
||||||
|
|
||||||
|
|
Reference in a new issue