Claw  1.7.3
single_tweener.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  contact: julien.jorge@gamned.org
23 */
30 
31 #include <algorithm>
32 #include <boost/bind.hpp>
33 
50 static void local_swap( double& a, double& b )
51 {
52  const double t(a);
53  a = b;
54  b = t;
55 } // local_swap()
56 
57 #ifndef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
58 
59 #ifndef WORKAROUND_BOOST_THROW_EXCEPTION
60 #define WORKAROUND_BOOST_THROW_EXCEPTION
61 
62 #include "boost/throw_exception.hpp"
63 
67 namespace boost {
68 
76  void throw_exception( std::exception const& exc ) {
77  // nothing
78  } // throw_exception()
79 
80 } // namespace boost
81 
82 #endif // WORKAROUND_BOOST_THROW_EXCEPTION
83 #endif // ifdef CLAW_TWEENER_DEFINE_BOOST_THROW_EXCEPTION
84 
85 /*----------------------------------------------------------------------------*/
90  : m_date(0), m_easing( easing_none::ease_in_out )
91 {
92 
93 } // single_tweener::single_tweener()
94 
95 /*----------------------------------------------------------------------------*/
105 ( double init, double end, double duration, update_function callback,
106  easing_function e )
107  : m_init(init), m_end(end), m_date(0), m_duration(duration),
108  m_callback(callback), m_easing(e)
109 {
110 
111 } // single_tweener::single_tweener()
112 
113 /*----------------------------------------------------------------------------*/
122 ( double& val, double end, double duration, easing_function e )
123  : m_init(val), m_end(end), m_date(0), m_duration(duration), m_easing(e)
124 {
125  m_callback = boost::bind( &local_swap, boost::ref(val), _1 );
126 } // single_tweener::single_tweener()
127 
128 /*----------------------------------------------------------------------------*/
134 {
135  m_init = v;
136 } // single_tweener::set_init()
137 
138 /*----------------------------------------------------------------------------*/
144 {
145  m_end = v;
146 } // single_tweener::set_end()
147 
148 /*----------------------------------------------------------------------------*/
154 {
155  m_duration = v;
156 } // single_tweener::set_duration()
157 
158 /*----------------------------------------------------------------------------*/
164 {
165  m_callback = f;
166 } // single_tweener::set_callback()
167 
168 /*----------------------------------------------------------------------------*/
174 {
175  m_easing = f;
176 } // single_tweener::set_easing()
177 
178 /*----------------------------------------------------------------------------*/
182 claw::tween::single_tweener* claw::tween::single_tweener::do_clone() const
183 {
184  return new single_tweener(*this);
185 } // single_tweener::do_clone()
186 
187 /*----------------------------------------------------------------------------*/
191 bool claw::tween::single_tweener::do_is_finished() const
192 {
193  return m_date >= m_duration;
194 } // single_tweener::do_is_finished()
195 
196 /*----------------------------------------------------------------------------*/
201 double claw::tween::single_tweener::do_update( double dt )
202 {
203  const double t( std::min(m_duration - m_date, dt) );
204  const double result = dt - t;
205  m_date += t;
206 
207  const double coeff = m_easing( m_date / m_duration );
208  const double val = m_init + coeff * (m_end - m_init);
209 
210  m_callback(val);
211 
212  return result;
213 } // single_tweener::do_update()