diff --git a/SFMLEngine/makingAGameTick/aircraft.cpp b/SFMLEngine/makingAGameTick/aircraft.cpp new file mode 100644 index 0000000..ca9b454 --- /dev/null +++ b/SFMLEngine/makingAGameTick/aircraft.cpp @@ -0,0 +1,9 @@ +#ifndef AIRCRAFT_CPP +#define AIRCRAFT_CPP + +Aircraft::Aircraft(Type type) : mType(type) +{ + +} + +#endif diff --git a/SFMLEngine/makingAGameTick/aircraft.hpp b/SFMLEngine/makingAGameTick/aircraft.hpp new file mode 100644 index 0000000..a2b254f --- /dev/null +++ b/SFMLEngine/makingAGameTick/aircraft.hpp @@ -0,0 +1,23 @@ +#ifndef AIRCRAFT_HPP +#define AIRCRAFT_HPP + +#include "entity.hpp" + +class Aircraft : public Entity +{ + public: + enum Type + { + Eagle, + Raptor + }; + public: + explicit Aircraft(Type type); + + private: + Type mType; + +}; + +#include "aircraft.cpp" +#endif diff --git a/SFMLEngine/makingAGameTick/app b/SFMLEngine/makingAGameTick/app index d6f4799..e4b3500 100755 Binary files a/SFMLEngine/makingAGameTick/app and b/SFMLEngine/makingAGameTick/app differ diff --git a/SFMLEngine/makingAGameTick/entity.cpp b/SFMLEngine/makingAGameTick/entity.cpp new file mode 100644 index 0000000..6532a2b --- /dev/null +++ b/SFMLEngine/makingAGameTick/entity.cpp @@ -0,0 +1,20 @@ +#ifndef ENTITY_CPP +#define ENTITY_CPP + +void Entity::SetVelocity(sf::Vector2f velocity) +{ + mVelocity = velocity; +} + +void Entity::setVelocity(float velocityX, float velocityY) +{ + mVelocity.x = velocityX; + mVelocity.y = velocityY; +} + +sf::Vector2f Entity::getVelocity() const +{ + return mVelocity; +} + +#endif diff --git a/SFMLEngine/makingAGameTick/entity.hpp b/SFMLEngine/makingAGameTick/entity.hpp new file mode 100644 index 0000000..45799c5 --- /dev/null +++ b/SFMLEngine/makingAGameTick/entity.hpp @@ -0,0 +1,18 @@ +#ifndef ENTITY_HPP +#define ENTITY_HPP + +class Entity +{ +public: + void SetVelocity(sf::Vector2f velocity); + void setVelocity(float velocityX, float velocityY); + sf::Vector2f getVelocity() const; + + private: + sf::Vector2f mVelocity; // default ocnstructor initializes this vector to a zero vector + +}; + +#include "entity.cpp" + +#endif diff --git a/SFMLEngine/makingAGameTick/game.cpp b/SFMLEngine/makingAGameTick/game.cpp index 69d6b2c..9b211b9 100644 --- a/SFMLEngine/makingAGameTick/game.cpp +++ b/SFMLEngine/makingAGameTick/game.cpp @@ -5,6 +5,9 @@ #include #include "constants.hpp" #include "resources.hpp" +#include "entity.hpp" +#include "aircraft.hpp" +#include "scenenode.hpp" #include "basic.cpp" @@ -32,9 +35,7 @@ class Game Game::Game() : mWindow(sf::VideoMode(640, 480), "SFML Application"), mTexture(), mPlayer() { - //ResourceHolder textures; mTexture.load(Textures::Airplane, PATH_TO_PLAYER_TEXTURE); - mPlayer.setTexture(mTexture.get(Textures::Airplane)); mPlayer.setPosition(PLAYER_X_POSITION, PLAYER_Y_POSITION); } diff --git a/SFMLEngine/makingAGameTick/game.o b/SFMLEngine/makingAGameTick/game.o index d04558e..d8fedc6 100644 Binary files a/SFMLEngine/makingAGameTick/game.o and b/SFMLEngine/makingAGameTick/game.o differ diff --git a/SFMLEngine/makingAGameTick/scenenode.cpp b/SFMLEngine/makingAGameTick/scenenode.cpp new file mode 100644 index 0000000..8845c16 --- /dev/null +++ b/SFMLEngine/makingAGameTick/scenenode.cpp @@ -0,0 +1,33 @@ +#ifndef SCENE_NODE_CPP +#define SCENE_NODE_CPP + +#include "scenenode.hpp" + +void SceneNode::attachChild(ScenePointer child) // takes ownership of the scene node +{ + child -> mParent = this; + mChildren.push_back(std::move(child)); +} + +SceneNode::ScenePointer SceneNode::detachChild(const SceneNode& node) // finds node, releases it and returns it to caller +{ + auto found = std::find_if + ( + mChildren.begin(), mChildren.end(), + [&] (ScenePointer& p) -> bool { return p.get() == &node; } + ); + // This is lambda expression + // [&] (ScenePointer& p) -> bool { return p.get() == &node; } + // [&] - how many and in what way will the lambda expression have access to the variables in surrounding scope, [] - no variables [&] - all variables by reference [=] - all variables by value + // (ScenePointer& p) - parameters passed to the function + // -> bool - return type + // function body encolsed in {} + assert(found != mChildren.end()); // We check validity of the iterator of the found element + + ScenePointer result = std::move(*found); // we move the found node out of the container to result + result -> mParent = nullptr; // node's parent is set to null pointer + mChildren.erase(found); // we erase this element from the container + return result; // and we return the pointer to the node +} + +#endif diff --git a/SFMLEngine/makingAGameTick/scenenode.hpp b/SFMLEngine/makingAGameTick/scenenode.hpp new file mode 100644 index 0000000..ac0f25c --- /dev/null +++ b/SFMLEngine/makingAGameTick/scenenode.hpp @@ -0,0 +1,34 @@ +#ifndef SCENE_NODE_HPP +#define SCENE_NODE_HPP + +class SceneNode : public sf::Transformable, public sf::Drawable, private sf::NonCopyable +// we derrive from transformable - to be able to store and modify position, rotation and scale +// we derrive from drawable - to be able to draw it on screen +// we derrive from noncopyable - so that copy constructor and copy assignemnt operators are disabled +// This is used to create scene graph (tree data structure) in order to manage transform hierarchies +{ + public: + typedef std::unique_ptr ScenePointer; // element types must be complete types and we do not want to manage memory ourselves so we use std::unique_ptr + public: + SceneNode(); + void attachChild(ScenePointer child); + ScenePointer detachChild(const SceneNode& node); + 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 + // draw() function allows our class to be used like this: + /* + sf::RenderWindow window(...); + SceneNode::ScenePointer node(...); + window.draw(*node); + */ + virtual void drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const; // draws only the current object, and not the children + private: + std::vector mChildren; + SceneNode* mParent; + +}; + +#include "scenenode.cpp" + +#endif