Skip to content

Commit

Permalink
Merge pull request #5 from Creative-Rift/feature/sprite
Browse files Browse the repository at this point in the history
Add Sprite and SpriteAnimator component
  • Loading branch information
Alvarwow69 authored Nov 15, 2023
2 parents 9622820 + a5c679c commit 104c743
Show file tree
Hide file tree
Showing 34 changed files with 8,384 additions and 4 deletions.
5,909 changes: 5,909 additions & 0 deletions external/GLFW/glfw3.h

Large diffs are not rendered by default.

594 changes: 594 additions & 0 deletions external/GLFW/glfw3native.h

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions includes/components/Sprite.hpp
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
137 changes: 137 additions & 0 deletions includes/components/SpriteAnimator.hpp
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
8 changes: 7 additions & 1 deletion includes/core/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@
#include "core/window/Window.hpp"
#include "scene/sceneManager/SceneManager.hpp"
#include "resources/ResourcesManager.hpp"
#include "utils/Chrono.hpp"

namespace sw {

class SW_MODULE_EXPORT Core {
public:
static Chrono m_chronos;

/// @brief Start the Engine, call it before any other action
static void Start();

Expand All @@ -37,10 +40,13 @@ namespace sw {
/// \return sw::SceneManager&
[[nodiscard]] static sw::ResourcesManager &GetResourceManager();

static void setFrameRateLimit(unsigned int frameRate);

private:
static SceneManager m_sceneManager;
static ResourcesManager m_resourceManager;

static Chrono m_chronosWindow;
static double m_frameRate;
};

} // sw
Expand Down
1 change: 1 addition & 0 deletions includes/core/monitor/Monitor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef SHIPWRECK_ENGINE_MONITOR_HPP
#define SHIPWRECK_ENGINE_MONITOR_HPP

#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#include "utils/Vector2.hpp"

Expand Down
1 change: 1 addition & 0 deletions includes/core/window/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>

#include "SWEngine.hpp"
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#include "utils/Vector2.hpp"

Expand Down
2 changes: 1 addition & 1 deletion includes/gameobject/GameObject.inl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ inline Cpt& sw::GameObject::getComponent()
try
{
auto managerName = std::string(typeid(Cpt).name());
return (m_scene.getManager<sw::AManager<Cpt>>(managerName.append("Manager"))[m_name]);
return (m_scene.getManager<sw::AManager<Cpt>>(managerName.append("Manager")).getComponent(m_id));
}
catch (sw::Error& err) {
throw sw::Error("Component not found");
Expand Down
47 changes: 47 additions & 0 deletions includes/managers/SpriteAnimatorManager.hpp
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 &current_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 &current_time);


}; // class SpriteManagerAnimator

} // sw

#endif //SHIPWRECK_ENGINE_SPRITEANIMATORMANAGER_HPP
39 changes: 39 additions & 0 deletions includes/managers/SpriteManager.hpp
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
Loading

0 comments on commit 104c743

Please sign in to comment.