mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-07-04 16:23:04 +02:00
feat: Combining resources and scenenodes, adding absolute position functionality to scenenode, adding updates to sceneNode
This commit is contained in:
parent
26382bafbb
commit
d82d8eb981
@ -47,4 +47,40 @@ void SceneNode::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
}
|
||||
}
|
||||
|
||||
void SceneNode::updateCurrent(sf::Time deltaTime)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void SceneNode::updateChildren(sf::Time deltaTime)
|
||||
{
|
||||
for (const ScenePointer& child : mChildren)
|
||||
{
|
||||
child -> update(deltaTime); // we also need to draw all the child nodes
|
||||
}
|
||||
}
|
||||
|
||||
void SceneNode::update(sf::Time deltaTime)
|
||||
{
|
||||
updateCurrent(deltaTime);
|
||||
updateChildren(deltaTime);
|
||||
}
|
||||
|
||||
sf::Transform SceneNode::getWorldTransform() const
|
||||
{
|
||||
sf::Transform transform = sf::Transform::Identity; // sf::Transform::Identity represents the identity transform - it does not have any effecft on the object, it is not necessary in the code but it clarifies how transforms are applied
|
||||
for (const SceneNode* node = this; node != nullptr; node = node -> mParent)
|
||||
{
|
||||
transform = node -> getTransform() * transform;
|
||||
}
|
||||
return transform;
|
||||
}
|
||||
|
||||
sf::Vector2f SceneNode::getWorldPosition() const
|
||||
{
|
||||
return getWorldTransform() * sf::Vector2f();
|
||||
}
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@ -13,6 +13,7 @@ class SceneNode : public sf::Transformable, public sf::Drawable, private sf::Non
|
||||
//SceneNode();
|
||||
void attachChild(ScenePointer child);
|
||||
ScenePointer detachChild(const SceneNode& node);
|
||||
void update(sf::Time deltaTime);
|
||||
private:
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; // we override draw() function of sf::Drawable
|
||||
// Virtual functions are member functions whose behavior can be overridden in derived classes
|
||||
@ -23,6 +24,10 @@ class SceneNode : public sf::Transformable, public sf::Drawable, private sf::Non
|
||||
window.draw(*node);
|
||||
*/
|
||||
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; // draws only the current object, and not the children
|
||||
virtual void updateCurrent(sf::Time deltaTime); // we reuse scene graph to reach all entities with world update, this one updates current node
|
||||
void updateChildren(sf::Time deltaTime); // this one updates child nodes
|
||||
sf::Transform getWorldTransform() const; // it takes into account all the parent transform
|
||||
sf::Vector2f getWorldPosition() const;
|
||||
private:
|
||||
std::vector<ScenePointer> mChildren;
|
||||
SceneNode* mParent;
|
||||
|
||||
@ -1,9 +1,32 @@
|
||||
#ifndef AIRCRAFT_CPP
|
||||
#define AIRCRAFT_CPP
|
||||
|
||||
Aircraft::Aircraft(Type type) : mType(type)
|
||||
Textures::ID toTextureID(Aircraft::Type type)
|
||||
{
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case Aircraft::Eagle:
|
||||
return Textures::Eagle;
|
||||
|
||||
case Aircraft::Raptor:
|
||||
return Textures::Raptor;
|
||||
|
||||
default:
|
||||
return Textures::Eagle;
|
||||
}
|
||||
return Textures::Eagle;
|
||||
}
|
||||
|
||||
|
||||
Aircraft::Aircraft(Type type, const TextureHolder& textures) : mType(type), mSprite(textures.get(toTextureID(type)))
|
||||
{
|
||||
sf::FloatRect bounds = mSprite.getLocalBounds(); // we get local bounding rectangle which means that we do not take transforms into account, as opposed to getGlobalBounds()
|
||||
mSprite.setOrigin(bounds.width / 2.f, bounds.height / 2.f); // we want to set origin of the sprite to the middle of a rectangle around it
|
||||
}
|
||||
|
||||
void Aircraft::drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(mSprite, states);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -12,10 +12,12 @@ class Aircraft : public Entity
|
||||
Raptor
|
||||
};
|
||||
public:
|
||||
explicit Aircraft(Type type);
|
||||
explicit Aircraft(Type type, const TextureHolder& textures);
|
||||
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||
|
||||
private:
|
||||
Type mType;
|
||||
sf::Sprite mSprite;
|
||||
|
||||
};
|
||||
|
||||
|
||||
Binary file not shown.
@ -17,4 +17,9 @@ sf::Vector2f Entity::getVelocity() const
|
||||
return mVelocity;
|
||||
}
|
||||
|
||||
void Entity::updateCurrent(sf::Time deltaTime)
|
||||
{
|
||||
move(mVelocity * deltaTime.asSeconds()); // shortcut for setPosition(getPosition() + offset)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@ -12,6 +12,7 @@ class Entity : public SceneNode
|
||||
|
||||
private:
|
||||
sf::Vector2f mVelocity; // default ocnstructor initializes this vector to a zero vector
|
||||
virtual void updateCurrent(sf::Time deltaTime);
|
||||
|
||||
};
|
||||
|
||||
|
||||
@ -12,6 +12,7 @@
|
||||
|
||||
|
||||
|
||||
|
||||
class Game
|
||||
{
|
||||
public:
|
||||
@ -29,14 +30,14 @@ class Game
|
||||
bool mIsMovingDown = false;
|
||||
private:
|
||||
sf::RenderWindow mWindow;
|
||||
ResourceHolder<sf::Texture, Textures::ID> mTexture;
|
||||
TextureHolder mTexture;
|
||||
sf::Sprite mPlayer;
|
||||
};
|
||||
|
||||
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer()
|
||||
{
|
||||
mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE);
|
||||
mPlayer.setTexture(mTexture.get(Textures::Airplane));
|
||||
mTexture.load(Textures::Eagle, PATH_TO_PLAYER_TEXTURE);
|
||||
mPlayer.setTexture(mTexture.get(Textures::Eagle));
|
||||
mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION);
|
||||
}
|
||||
|
||||
|
||||
Binary file not shown.
@ -4,9 +4,17 @@
|
||||
#include <assert.h>
|
||||
// Mostly Chapter 2
|
||||
// Handles resource management
|
||||
|
||||
|
||||
|
||||
|
||||
namespace Textures // This gives us a scope for the enumerators which allows us to write Textures::Airplane instead of just Airplane to avoid name collisions in the global scope
|
||||
{
|
||||
enum ID { Landscape, Airplane, Missile };
|
||||
enum ID
|
||||
{
|
||||
Eagle,
|
||||
Raptor,
|
||||
};
|
||||
}
|
||||
|
||||
template <typename Resource, typename Identifier>
|
||||
@ -24,6 +32,8 @@ class ResourceHolder
|
||||
// unique_ptr are class templates that act like pointers, this allows us to work with heavyweight objects without copying them all the time, or we can store classes that are non-cpyable like sf::Shader
|
||||
};
|
||||
|
||||
typedef ResourceHolder<sf::Texture, Textures::ID> TextureHolder;
|
||||
|
||||
|
||||
|
||||
#include "resources.inl"
|
||||
|
||||
Loading…
Reference in New Issue
Block a user