-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Creative-Rift/feature/sprite
Add Sprite and SpriteAnimator component
- Loading branch information
Showing
34 changed files
with
8,384 additions
and
4 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
** ShipWreck Engine v0.1, graphical library made to create games | ||
** Current file: Sprite.hpp | ||
*/ | ||
|
||
#ifndef SHIPWRECK_ENGINE_SPRITE_HPP | ||
#define SHIPWRECK_ENGINE_SPRITE_HPP | ||
|
||
#include "SWEngine.hpp" | ||
#include "base/Component.hpp" | ||
#include "utils/Color.hpp" | ||
#include "utils/VertexArray.hpp" | ||
#include "utils/Rect.hpp" | ||
#include "resources/Texture.hpp" | ||
#include "utils/Shader.hpp" | ||
|
||
namespace sw { | ||
|
||
class SpriteManager; | ||
|
||
class SW_MODULE_EXPORT Sprite : public Component { | ||
protected: | ||
VertexArray m_vertexArray; | ||
// Material m_material; | ||
std::shared_ptr<sw::Shader> m_shader; | ||
std::shared_ptr<Texture> m_texture; | ||
Color m_color; | ||
FloatRect m_rect; | ||
bool m_invertedX; | ||
bool m_invertedY; | ||
|
||
public: | ||
/// \brief Default constructor | ||
/// | ||
/// \param gameObject - sw::GameObject | ||
explicit Sprite(sw::GameObject &gameObject); | ||
|
||
/// \brief Get the sprite's texture | ||
/// | ||
/// \return Shared pointer to the texture | ||
[[nodiscard]] std::shared_ptr<Texture> texture() const; | ||
|
||
/// \brief Define the texture | ||
/// | ||
/// \param texture Texture's Name | ||
/// \return A reference to the sprite | ||
Sprite &setTexture(std::string name); | ||
|
||
/// \brief Define a sub-rectangle of the texture displayed on the Sprite | ||
/// | ||
/// \param rect Dimension (width, height) of the rect | ||
/// \return A reference to the sprite | ||
Sprite &setTextureRect(sw::FloatRect rect); | ||
|
||
/// \brief Define a color (this color is multiplied with the texture pixels) | ||
/// | ||
/// \param color - sw::Color | ||
/// \return A reference to the sprite | ||
Sprite &setColor(const sw::Color &color); | ||
|
||
/// \brief Flip the texture on X axis. Flipped (true) not flipped (false) | ||
/// | ||
/// \param invert Boolean | ||
/// \return Reference to the Sprite | ||
Sprite &flipOnX(bool invert); | ||
|
||
/// \brief Flip the texture on Y axis. Flipped (true) not flipped (false) | ||
/// | ||
/// \param invert Boolean | ||
/// \return Reference to the Sprite | ||
Sprite &flipOnY(bool invert); | ||
|
||
/// \brief Get the value if the Sprite texture is flipped on X axis. | ||
/// Flipped (true) not flipped (false) | ||
/// | ||
/// \return Boolean | ||
[[nodiscard]] bool isFlippedX() const; | ||
|
||
/// \brief Get the value if the Sprite texture is flipped on Y axis. | ||
/// Flipped (true) not flipped (false) | ||
/// | ||
/// \return Boolean | ||
[[nodiscard]] bool isFlippedY() const; | ||
|
||
/// \brief Get the shader | ||
/// | ||
/// \return A Shared pointer to the Shader | ||
[[nodiscard]] std::shared_ptr<Shader> getShader() const noexcept; | ||
|
||
/// \brief Get the shader associated | ||
/// | ||
/// \return A reference to the Shader | ||
// [[nodiscard]] Material &getMaterial() noexcept; | ||
|
||
friend SpriteManager; | ||
}; // class Sprite | ||
|
||
} // sw | ||
|
||
#endif //SHIPWRECK_ENGINE_SPRITE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
/* | ||
** ShipWreck Engine v0.1, graphical library made to create games | ||
** Current file: SpriteAnimator.hpp | ||
*/ | ||
|
||
#ifndef SHIPWRECK_ENGINE_SPRITEANIMATOR_HPP | ||
#define SHIPWRECK_ENGINE_SPRITEANIMATOR_HPP | ||
|
||
#include "SWEngine.hpp" | ||
#include "base/Component.hpp" | ||
#include "utils/Vector2.hpp" | ||
#include "utils/Rect.hpp" | ||
#include "Sprite.hpp" | ||
|
||
namespace sw { | ||
|
||
class SpriteAnimatorManager; | ||
|
||
class SW_MODULE_EXPORT SpriteAnimator : public sw::Component { | ||
public: | ||
/// \brief Define the type of the animation | ||
enum AnimType { | ||
ANIM_SPRITE, // One animation on the whole texture | ||
ANIM_LINE // One animation per line | ||
}; | ||
|
||
/// \brief Default constructor | ||
/// | ||
/// \param gameObject Reference to the gameObject | ||
explicit SpriteAnimator(sw::GameObject &gameObject); | ||
|
||
/// \brief Play your animation | ||
void play(); | ||
|
||
/// \brief Pause on your animation | ||
void pause(); | ||
|
||
/// \brief Reset your animation (set on first frame) | ||
void reset(); | ||
|
||
/// \brief Return a bool if the animation is currently playing | ||
/// | ||
/// \return bool | ||
[[nodiscard]] bool isPlaying() const; | ||
|
||
/// \brief Define if your animation loop | ||
/// | ||
/// \param loop boolean | ||
/// \param delay delay between two animation | ||
/// \return reference to your sprite SpriteAnimator | ||
SpriteAnimator &setLoop(bool loop, float delay = 0); | ||
|
||
/// \brief If you have one animation per line on your texture, you can define | ||
/// the line you want and the number of the frame (if you have | ||
/// less frame than the length of your texture) | ||
/// | ||
/// \param line Number of the line where the animation is | ||
/// \param end Number of frame in your animation | ||
/// \return reference to your sprite SpriteAnimator | ||
SpriteAnimator &setLine(int line, int end = -1); | ||
|
||
/// \brief Define the displayed rect on your texture | ||
/// | ||
/// \param rect Rect's size (width, height) | ||
/// \return reference to your sprite SpriteAnimator | ||
SpriteAnimator &setRect(sw::Vector2u rect); | ||
|
||
/// \brief Define the speed (number of frame per second) of your animation | ||
/// | ||
/// \param fps float | ||
/// \return reference to your sprite | ||
SpriteAnimator &setFPS(float fps); | ||
|
||
/// \brief Define the type of your animation. See AnimationType | ||
/// | ||
/// \param type Type of your animation | ||
/// \return reference to your sprite | ||
SpriteAnimator &setAnimType(AnimType type); | ||
|
||
/// \brief Define if the Animation will play on start | ||
/// | ||
/// \param value boolean | ||
/// \return reference to your sprite | ||
SpriteAnimator &setPlayOnStart(bool value); | ||
|
||
/// \brief Get if the animation loop | ||
/// | ||
/// \return bool | ||
[[nodiscard]]const bool &isLoop() const; | ||
|
||
/// \brief Get the displayed rect | ||
/// | ||
/// \return sw::Rect | ||
[[nodiscard]]const sw::Vector2u &getRect() const; | ||
|
||
/// \brief Get the delay of your animation loop | ||
/// | ||
/// \return float | ||
[[nodiscard]]const float &getLoopDelay() const; | ||
|
||
/// \brief Get the speed of your animation | ||
/// | ||
/// \return float Frame per second | ||
[[nodiscard]]const float &getFPS() const; | ||
|
||
/// \brief Get the type of your animation | ||
/// | ||
/// \return AnimType | ||
[[nodiscard]]AnimType &getAnimType(); | ||
|
||
/// \brief Get if the animation will play on start | ||
/// | ||
/// \return bool | ||
[[nodiscard]]const bool &getPlayOnStart(); | ||
|
||
private: | ||
sw::Vector2u m_rect; | ||
sw::FloatRect m_displayRect; | ||
double m_lastFrame; | ||
int m_endFrame; | ||
float m_loopDelay; | ||
bool m_isPlaying; | ||
bool m_loop; | ||
bool m_playOnStart; | ||
float m_framePerSecond; | ||
AnimType m_type; | ||
|
||
Sprite &m_spr; | ||
|
||
void playOnStart(); | ||
|
||
friend SpriteAnimatorManager; | ||
|
||
}; // class SpriteAnimator | ||
} // SW | ||
|
||
#endif //SHIPWRECK_ENGINE_SPRITEANIMATOR_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
** ShipWreck Engine v0.1, graphical library made to create games | ||
** Current file: SpriteManagerAnimator.hpp | ||
*/ | ||
|
||
#ifndef SHIPWRECK_ENGINE_SPRITEANIMATORMANAGER_HPP | ||
#define SHIPWRECK_ENGINE_SPRITEANIMATORMANAGER_HPP | ||
|
||
#include "base/AManager.hpp" | ||
#include "components/SpriteAnimator.hpp" | ||
|
||
namespace sw { | ||
|
||
class SW_MODULE_EXPORT SpriteAnimatorManager : public AManager<SpriteAnimator>{ | ||
public: | ||
/// \brief Default constructor of the AManager class | ||
using sw::AManager<SpriteAnimator>::AManager; | ||
|
||
/// \brief Default destructor | ||
~SpriteAnimatorManager() override = default; | ||
|
||
/// \brief This function is called one time per frame and update all component \n | ||
/// stored according their data | ||
void onUpdate() override; | ||
|
||
private: | ||
|
||
/// \brief Update Animation following rules defined for the Type | ||
/// sw::SpriteAnimator::ANIM_LINE | ||
/// | ||
/// \param animator Reference to the animator | ||
/// \param current_time get total time | ||
static void animLine(SpriteAnimator &animator, double ¤t_time); | ||
|
||
/// \brief Update Animation following rules defined for the Type | ||
/// sw::SpriteAnimator::ANIM_SPRITE | ||
/// | ||
/// \param animator Reference to the animator | ||
/// \param current_time get total time | ||
static void animSprite(SpriteAnimator &animator, double ¤t_time); | ||
|
||
|
||
}; // class SpriteManagerAnimator | ||
|
||
} // sw | ||
|
||
#endif //SHIPWRECK_ENGINE_SPRITEANIMATORMANAGER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
** ShipWreck Engine v0.1, graphical library made to create games | ||
** Current file: SpriteManager.hpp | ||
*/ | ||
|
||
#ifndef SHIPWRECK_ENGINE_SPRITEMANAGER_HPP | ||
#define SHIPWRECK_ENGINE_SPRITEMANAGER_HPP | ||
|
||
#include "SWEngine.hpp" | ||
#include "base/AManager.hpp" | ||
#include "components/Sprite.hpp" | ||
|
||
namespace sw { | ||
class SW_MODULE_EXPORT SpriteManager : public AManager<Sprite> { | ||
public: | ||
/// \brief Default constructor | ||
using sw::AManager<Sprite>::AManager; | ||
|
||
/// \brief Default destructor | ||
~SpriteManager() override = default; | ||
|
||
/// \brief Function called each frame to update all Sprite | ||
void onUpdate() override; | ||
|
||
private: | ||
/// \brief Manage orientation | ||
/// | ||
/// \param sprite Current updated Sprite | ||
static void updateInvert(sw::Sprite &sprite); | ||
|
||
/// \brief Update the display rect | ||
/// | ||
/// \param sprite Current updated Sprite | ||
static void defineRect(sw::Sprite &sprite); | ||
|
||
}; // class SpriteManager | ||
} // SW | ||
|
||
#endif //SHIPWRECK_ENGINE_SPRITEMANAGER_HPP |
Oops, something went wrong.