nomlib
Loading...
Searching...
No Matches
IActionObject.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_ACTIONS_IACTION_OBJECT_HPP
30#define NOMLIB_ACTIONS_IACTION_OBJECT_HPP
31
32#include <functional>
33#include <vector>
34
35#include "export.hpp"
36#include "nomlib/config.hpp"
37#include "nomlib/system/Timer.hpp"
38
39namespace nom {
40
42class NOM_EXPORT IActionObject
43{
44 public:
45 typedef IActionObject self_type;
46
51 typedef
52 std::function<real32(real32, real32, real32, real32)> timing_curve_func;
53
62
64
65 virtual ~IActionObject();
66
68 const std::string& name() const;
69
73 real32 duration() const;
74
81 real32 speed() const;
82
89
91 void set_name(const std::string& action_id);
92
94 virtual void set_speed(real32 speed);
95
100
107 virtual std::unique_ptr<IActionObject> clone() const = 0;
108
118 virtual IActionObject::FrameState next_frame(real32 delta_time) = 0;
119
132 virtual IActionObject::FrameState prev_frame(real32 delta_time) = 0;
133
143 virtual void pause(real32 delta_time) = 0;
144
154 virtual void resume(real32 delta_time) = 0;
155
166 virtual void rewind(real32 delta_time) = 0;
167
175 virtual void release() = 0;
176
177 protected:
182
186 void set_duration(real32 seconds);
187
192
196 real32 elapsed_frames_ = 0.0f;
197
205
206 private:
208 std::string name_;
209 real32 duration_ = 0.0f;
210 real32 speed_ = 1.0f;
211 timing_curve_func timing_curve_ = nullptr;
212};
213
217typedef std::vector<std::shared_ptr<IActionObject>> action_list;
218
224template<typename ObjectType, typename... ObjectArgs>
225NOM_EXPORT std::shared_ptr<ObjectType> create_action(ObjectArgs&&... args)
226{
227 // IMPORTANT: We risk object slicing if we use std::make_shared here! The
228 // problem occurs when the end-user tries to return the created action
229 // pointer by value.
230 return( std::shared_ptr<ObjectType>(
231 new ObjectType( std::forward<ObjectArgs>(args) ... ) ) );
232}
233
251template<typename ObjectType>
252NOM_EXPORT std::shared_ptr<ObjectType>
254{
255 // IMPORTANT: We risk object slicing if we use std::make_shared here! The
256 // problem occurs when the end-user tries to return the created action
257 // pointer by value.
258 return( std::shared_ptr<ObjectType>( new ObjectType(actions) ) );
259}
260
261} // namespace nom
262
263#endif // include guard defined
264
Pure virtual base class interface for actions.
void set_duration(real32 seconds)
Set the duration of the action.
virtual void set_speed(real32 speed)
Set the speed factor of the action.
real32 elapsed_frames_
Internal frames counter.
Timer timer_
Internal time clock (milliseconds resolution).
virtual void release()=0
Free externally referenced resources held by the action.
const std::string & name() const
Get the unique identifier of the action.
virtual IActionObject::FrameState prev_frame(real32 delta_time)=0
Play the action backwards in time by one time step.
NOM_EXPORT std::shared_ptr< ObjectType > create_action(ObjectArgs &&... args)
Constructor function for creating an action.
void set_status(FrameState state)
Set the state of the action.
virtual IActionObject::FrameState next_frame(real32 delta_time)=0
Play the action forward in time by one time step.
FrameState
The update status of the action.
@ COMPLETED
The action has finished its update loop.
@ PLAYING
The action is still updating; duration remains.
std::function< real32(real32, real32, real32, real32)> timing_curve_func
A function pointer to a timing mode.
virtual std::unique_ptr< IActionObject > clone() const =0
Create a deep copy instance of the action.
virtual void resume(real32 delta_time)=0
Resume the internal state of the action.
virtual void rewind(real32 delta_time)=0
Reset the internal state of the action back to its initial starting values.
IActionObject::FrameState status() const
Get the current state of the action.
void set_name(const std::string &action_id)
Set the unique identifier of the action.
real32 speed() const
Get the speed modifier of the action.
virtual void pause(real32 delta_time)=0
Freeze the action's internal state.
std::vector< std::shared_ptr< IActionObject > > action_list
A collection of actions.
real32 duration() const
Get the duration of the action.
virtual void set_timing_curve(const IActionObject::timing_curve_func &mode)
Set the timing mode of the action.
const IActionObject::timing_curve_func & timing_curve() const
Get the timing mode function used by the action.
NOM_EXPORT std::shared_ptr< ObjectType > create_action(const action_list &actions)
Constructor function for creating an action that uses a collection of actions.