mirror of
https://github.com/kuhyx/testsAndMisc.git
synced 2026-11-22 13:52:25 +01:00
feat: Combining resources and scenenodes, adding absolute position functionality to scenenode, adding updates to sceneNode
This commit is contained in:
parent
1693bc6c4d
commit
5e46729227
@ -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
|
#endif
|
||||||
|
|||||||
@ -13,6 +13,7 @@ class SceneNode : public sf::Transformable, public sf::Drawable, private sf::Non
|
|||||||
//SceneNode();
|
//SceneNode();
|
||||||
void attachChild(ScenePointer child);
|
void attachChild(ScenePointer child);
|
||||||
ScenePointer detachChild(const SceneNode& node);
|
ScenePointer detachChild(const SceneNode& node);
|
||||||
|
void update(sf::Time deltaTime);
|
||||||
private:
|
private:
|
||||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; // we override draw() function of sf::Drawable
|
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
|
// 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);
|
window.draw(*node);
|
||||||
*/
|
*/
|
||||||
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; // draws only the current object, and not the children
|
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:
|
private:
|
||||||
std::vector<ScenePointer> mChildren;
|
std::vector<ScenePointer> mChildren;
|
||||||
SceneNode* mParent;
|
SceneNode* mParent;
|
||||||
|
|||||||
@ -1,9 +1,32 @@
|
|||||||
#ifndef AIRCRAFT_CPP
|
#ifndef AIRCRAFT_CPP
|
||||||
#define 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
|
#endif
|
||||||
|
|||||||
@ -12,10 +12,12 @@ class Aircraft : public Entity
|
|||||||
Raptor
|
Raptor
|
||||||
};
|
};
|
||||||
public:
|
public:
|
||||||
explicit Aircraft(Type type);
|
explicit Aircraft(Type type, const TextureHolder& textures);
|
||||||
|
virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Type mType;
|
Type mType;
|
||||||
|
sf::Sprite mSprite;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@ -17,4 +17,9 @@ sf::Vector2f Entity::getVelocity() const
|
|||||||
return mVelocity;
|
return mVelocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Entity::updateCurrent(sf::Time deltaTime)
|
||||||
|
{
|
||||||
|
move(mVelocity * deltaTime.asSeconds()); // shortcut for setPosition(getPosition() + offset)
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -12,6 +12,7 @@ class Entity : public SceneNode
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
sf::Vector2f mVelocity; // default ocnstructor initializes this vector to a zero vector
|
sf::Vector2f mVelocity; // default ocnstructor initializes this vector to a zero vector
|
||||||
|
virtual void updateCurrent(sf::Time deltaTime);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -29,14 +30,14 @@ class Game
|
|||||||
bool mIsMovingDown = false;
|
bool mIsMovingDown = false;
|
||||||
private:
|
private:
|
||||||
sf::RenderWindow mWindow;
|
sf::RenderWindow mWindow;
|
||||||
ResourceHolder<sf::Texture, Textures::ID> mTexture;
|
TextureHolder mTexture;
|
||||||
sf::Sprite mPlayer;
|
sf::Sprite mPlayer;
|
||||||
};
|
};
|
||||||
|
|
||||||
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer()
|
Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer()
|
||||||
{
|
{
|
||||||
mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE);
|
mTexture.load(Textures::Eagle, PATH_TO_PLAYER_TEXTURE);
|
||||||
mPlayer.setTexture(mTexture.get(Textures::Airplane));
|
mPlayer.setTexture(mTexture.get(Textures::Eagle));
|
||||||
mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION);
|
mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Binary file not shown.
@ -4,9 +4,17 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
// Mostly Chapter 2
|
// Mostly Chapter 2
|
||||||
// Handles resource management
|
// 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
|
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>
|
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
|
// 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"
|
#include "resources.inl"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user