Gorgon Game Engine
ControlledTimer.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "../Animation.h"
4 
5 namespace Gorgon { namespace Animation {
6 
20  public:
21  virtual bool IsControlled() const override {
22  return true;
23  }
24 
25  virtual void Progress(unsigned int) override {
26  for(auto &anim : animations) {
27  unsigned int left = 0;
28 
29  anim.Progress(left);
30  curind++;
31  }
32 
33  curind = 0;
34  }
35 
36  virtual void Reset() override {
37  progress = 0.f;
38  }
39 
40  virtual unsigned int GetProgress() const override {
41  float v = progress - floor(progress);
42 
43  if(curind < animations.GetSize()) {
44  auto dur = animations[curind].GetDuration();
45  if(dur == -1)
46  return 0;
47 
48  return int(dur * v);
49  }
50  else
51  return 0;
52  }
53 
56  unsigned int GetProgress(int ind) const {
57  float v = progress - floor(progress);
58 
59  if(ind < animations.GetSize() && ind >= 0)
60  return int(animations[ind].GetDuration() * v);
61  else
62  return (unsigned)-1;
63  }
64 
73  void SetProgress(float value) {
74  progress = value;
75  }
76 
78  float GetProgressRate() const {
79  return progress;
80  }
81 
82  protected:
83  float progress = 0.f;
84  int curind = 0;
85  };
86 
87 } }
Gorgon::Animation::ControlledTimer::IsControlled
virtual bool IsControlled() const override
This method allows clients to determine if the progress is controlled.
Definition: ControlledTimer.h:21
Gorgon::Animation::Animate
void Animate()
Definition: Animation.cpp:31
Gorgon::Animation::ControlledTimer::Progress
virtual void Progress(unsigned int) override
Definition: ControlledTimer.h:25
Gorgon::Animation::basic_Storage
This class stores animations as a part of itself so that it can be moved around as a value rather tha...
Definition: Storage.h:21
Gorgon::Animation::ControllerBase::~ControllerBase
virtual ~ControllerBase()
Destructor.
Definition: Animation.cpp:54
Gorgon::Animation::Governor::~Governor
virtual ~Governor()
Destroys this governor. If it is the active governor, default governor will be activated.
Definition: Animation.cpp:21
File.h
Gorgon::Animation::Controller::islooping
bool islooping
Looping state.
Definition: Animation.h:267
Gorgon::Animation::ControllerBase::governor
Governor * governor
Definition: Animation.h:117
Gorgon::Animation::Controller::progress
double progress
Floating point progress to avoid precision loss due to speed.
Definition: Animation.h:276
Gorgon::Graphics::basic_TextureAnimationProvider
Definition: TextureAnimation.h:137
Gorgon::Graphics::BitmapAnimationProvider
basic_TextureAnimationProvider< Bitmap, basic_TextureAnimation, basic_AnimationFrame< Bitmap > > BitmapAnimationProvider
Definition: TextureAnimation.h:520
Gorgon::Resource::Base::GetGID
virtual GID::Type GetGID() const =0
This function shall return Gorgon ID of this resource.
Gorgon::Resource::GID::Null
constexpr Type Null
Null resource.
Definition: GID.h:105
Gorgon::Resource::Animation::save
void save(Writer &writer) const override
Definition: Animation.cpp:92
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Animation::Controller::FinishedEvent
Event< Controller > FinishedEvent
Will be fired when the controller reaches the finished state.
Definition: Animation.h:259
Gorgon::Animation::Controller::Controller
Controller(double progress=0.0)
Default constructor.
Definition: Animation.cpp:114
Gorgon::Resource::Writer::WriteChunkHeader
void WriteChunkHeader(GID::Type type, unsigned long size)
Writes the header of a chunk.
Definition: Writer.h:364
Gorgon::Resource::Image::SaveThis
static void SaveThis(Writer &writer, const Graphics::Bitmap &bmp, GID::Type type=GID::Image)
This function can be used to save a bitmap as image resource without modifying it.
Definition: Image.cpp:171
Gorgon::Animation::ControllerBase::Remove
virtual void Remove(Base &animation)
Removes the given animation.
Definition: Animation.cpp:78
Gorgon::Animation::Controller::ispaused
bool ispaused
Paused state.
Definition: Animation.h:264
Gorgon::Resource::Animation::SaveThis
static void SaveThis(Writer &writer, const Graphics::BitmapAnimationProvider &anim, GID::Type type=GID::Animation, std::function< void(Writer &writer)> extra={})
Saves the given animation as a resource.
Definition: Animation.cpp:100
Gorgon::Animation::ControlledTimer::GetProgress
virtual unsigned int GetProgress() const override
Returns the current progress of the timer.
Definition: ControlledTimer.h:40
Gorgon::Animation::Controller::length
unsigned length
Length of the animations controlled by this controller.
Definition: Animation.h:279
Gorgon::Resource::GID::Image
constexpr Type Image
Image resource.
Definition: GID.h:149
Gorgon::Resource::Writer::WriteObjectStart
Marker WriteObjectStart(const Base &base)
Writes the start of an object. Should have a matching WriteEnd with the returned marker.
Definition: File.cpp:211
Gorgon::Animation::ControlledTimer::SetProgress
void SetProgress(float value)
Sets the progress of this controlled timer.
Definition: ControlledTimer.h:73
Gorgon::Animation::Controller::Progress
virtual void Progress(unsigned timepassed) override final
Progresses this controller by the given time.
Definition: Animation.cpp:118
Gorgon::Time::DeltaTime
unsigned long DeltaTime()
Returns the time passed since the last frame.
Definition: Time.h:258
Gorgon::Animation::log
Utils::Logger log
Definition: Animation.cpp:11
Gorgon::Animation::ControllerBase::Delete
virtual void Delete(Base &animation)
Deletes the given animation.
Definition: Animation.cpp:90
Gorgon::Animation::ControlledTimer
This class is a timer that has its progression strictly controlled.
Definition: ControlledTimer.h:19
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Animation::Timer::Progress
virtual void Progress(unsigned timepassed) override final
Progresses this timer by moving the timer timepassed milliseconds forwards.
Definition: Animation.cpp:98
Gorgon::Resource::Writer::WriteChunkStart
Marker WriteChunkStart(GID::Type type)
Writes the start of a chunk. Should have a matching WriteEnd.
Definition: Writer.h:351
Gorgon::Animation::Base
This is the base class for all animations.
Definition: Animation.h:306
Gorgon::Animation::Controller::speed
float speed
Current speed.
Definition: Animation.h:270
LOG
#define LOG
Definition: Animation.cpp:14
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Utils::Logger
Eases logging procedure by appending necessary information to the given data and streams to a standar...
Definition: Logging.h:19
Gorgon::Animation::Controller::Pause
virtual void Pause()
Pauses the controller, until a Resume or Reset is issued.
Definition: Animation.cpp:212
Gorgon::Animation::ControlledTimer::curind
int curind
Definition: ControlledTimer.h:84
Gorgon::Animation::ControlledTimer::progress
float progress
Definition: ControlledTimer.h:83
Gorgon::Animation::Governor
This class governs the progress of animations.
Definition: Animation.h:26
Gorgon::Animation::Timer::progress
unsigned progress
Amount of time passed since the start of the animation.
Definition: Animation.h:160
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Resource::Base
This class is the base for all Gorgon Resources.
Definition: Base.h:20
Gorgon::Animation::ControllerBase::collectable
bool collectable
Whether this controller should be collected by the garbage collector when its task is finished.
Definition: Animation.h:115
Gorgon::Animation::ControlledTimer::GetProgress
unsigned int GetProgress(int ind) const
Returns the progress of the given animation.
Definition: ControlledTimer.h:56
Gorgon::Animation::Governor::Animate
virtual void Animate() const
Animates the animations within this governor.
Definition: Animation.cpp:35
Gorgon::Resource::Animation::LoadResourceWith
static bool LoadResourceWith(Animation &anim, std::weak_ptr< File > file, std::shared_ptr< Reader > reader, unsigned long size, std::function< Base *(std::weak_ptr< File > file, std::shared_ptr< Reader > reader, GID::Type, unsigned long)> loadfn)
This function allows loading animation with a function to load unknown resources.
Definition: Animation.cpp:8
Gorgon::Resource::Animation::MoveOutProvider
Graphics::BitmapAnimationProvider & MoveOutProvider() override
Definition: Animation.cpp:150
Gorgon::Animation::Controller::Reset
virtual void Reset() override
Resets the controller to start from the beginning.
Definition: Animation.cpp:204
Gorgon::Resource::Writer
This class allows resource objects to save their data to a stream.
Definition: Writer.h:59
Gorgon::Animation::Base::~Base
virtual ~Base()
Virtual destructor.
Definition: Animation.cpp:216
Gorgon::Resource::Base::cbegin
const Containers::Collection< Base >::ConstIterator cbegin() const
Beginning of children.
Definition: Base.h:110
Gorgon::Resource::GID::Animation_Durations
constexpr Type Animation_Durations
Definition: GID.h:189
Gorgon::Animation::Controller::Play
virtual void Play()
Starts this controller to run once.
Definition: Animation.cpp:172
Gorgon::Animation::ControllerBase::Add
virtual void Add(Base &animation)
This function attaches the given animation to this controller.
Definition: Animation.cpp:73
Gorgon::Resource::Writer::WriteEnd
void WriteEnd(Marker &marker)
This function performs writes necessary to end a chunk that is represented by the marker.
Definition: Writer.h:373
Gorgon::Animation::ControllerBase::ControllerBase
ControllerBase()
Default constructor.
Definition: Animation.cpp:44
Gorgon::Graphics::RectangularAnimationStorage
Gorgon::Animation::basic_Storage< RectangularAnimationProvider > RectangularAnimationStorage
Definition: Animations.h:374
Gorgon::Resource::Writer::WriteInt32
void WriteInt32(long value)
Writes a 32-bit integer to the stream.
Definition: Writer.h:149
Gorgon::Animation::Governor::Active
static Governor & Active()
Returns the current governor.
Definition: Animation.h:52
Gorgon::Animation::Base::SetController
virtual void SetController(ControllerBase &controller)
Sets the controller to the given controller.
Definition: Animation.h:334
Gorgon::Animation::ControlledTimer::Reset
virtual void Reset() override
Resets the animation to the start.
Definition: ControlledTimer.h:36
Gorgon::Resource::GID::Type
Type to store GID information.
Definition: GID.h:23
Gorgon::Resource::Animation::savedata
void savedata(Writer &writer) const
Definition: Animation.cpp:79
Gorgon::Resource::Base::children
Containers::Collection< Base > children
Child objects that this resource object have.
Definition: Base.h:162
Gorgon::Animation::ControllerBase::SetGovernor
virtual void SetGovernor(Governor &governor)
Changes the governor of this controller.
Definition: Animation.cpp:63
Gorgon::Animation::Controller::SetProgress
virtual void SetProgress(unsigned progress)
Sets the current progress of the controller.
Definition: Animation.h:184
Gorgon::Resource::Animation::MoveOutAsBitmap
Graphics::BitmapAnimationProvider MoveOutAsBitmap()
Moves the animation out of the resource system. Use Prepare and Discard before moving out to avoid co...
Definition: Animation.cpp:124
Gorgon::Animation::ControllerBase::animations
Containers::Collection< Base > animations
List of animations this controller holds.
Definition: Animation.h:120
Gorgon::Animation::Base::controller
ControllerBase * controller
Controller of this animation.
Definition: Animation.h:388
Gorgon::Graphics::basic_TextureAnimationProvider::GetCount
int GetCount() const override
Returns number of frames.
Definition: TextureAnimation.h:209
Gorgon::Graphics::basic_TextureAnimationProvider::duration
unsigned duration
Definition: TextureAnimation.h:446
Gorgon::Graphics::basic_TextureAnimationProvider::frames
std::vector< basic_AnimationFrame< T_ > > frames
Definition: TextureAnimation.h:445
Gorgon::Animation::ControlledTimer::GetProgressRate
float GetProgressRate() const
Returns the progress of this controlled timer.
Definition: ControlledTimer.h:78
Gorgon::Resource::Base::cend
const Containers::Collection< Base >::ConstIterator cend() const
End of children.
Definition: Base.h:115
Gorgon::Resource::Image
This resource contains images.
Definition: Image.h:23
Gorgon::Graphics::basic_TextureAnimationProvider::Add
void Add(T_ &image, unsigned duration=42, bool own=false)
Adds the given image to the end of the animation.
Definition: TextureAnimation.h:281
Gorgon::Resource::Animation::animmoveout
virtual Graphics::RectangularAnimationStorage animmoveout() override
Definition: Animation.cpp:154
Gorgon::Animation::Controller::isfinished
bool isfinished
Whether the controller is finished.
Definition: Animation.h:273
Gorgon::Resource::Animation
This class represents an animation resource.
Definition: Animation.h:19
Animation.h