25
25
#include " math/random.hpp"
26
26
#include " math/util.hpp"
27
27
#include " object/player.hpp"
28
+ #include " supertux/constants.hpp"
28
29
#include " supertux/gameconfig.hpp"
29
30
#include " supertux/globals.hpp"
30
31
#include " supertux/level.hpp"
@@ -82,7 +83,9 @@ Camera::Camera(const std::string& name) :
82
83
m_scale_easing(),
83
84
m_scale_anchor(),
84
85
m_minimum_scale(1 .f),
85
- m_enfore_minimum_scale(false )
86
+ m_enfore_minimum_scale(false ),
87
+ m_last_translation(0 .0f , 0 .0f ),
88
+ m_last_scale(1 .0f )
86
89
{
87
90
}
88
91
@@ -118,7 +121,9 @@ Camera::Camera(const ReaderMapping& reader) :
118
121
m_scale_easing(),
119
122
m_scale_anchor(),
120
123
m_minimum_scale(1 .f),
121
- m_enfore_minimum_scale(false )
124
+ m_enfore_minimum_scale(false ),
125
+ m_last_translation(0 .0f , 0 .0f ),
126
+ m_last_scale(1 .0f )
122
127
{
123
128
std::string modename;
124
129
@@ -207,6 +212,25 @@ Camera::check_state()
207
212
PathObject::check_state ();
208
213
}
209
214
215
+ void Camera::reset_prediction_state () {
216
+ m_last_translation = get_translation ();
217
+ m_last_scale = get_current_scale ();
218
+ }
219
+
220
+ Vector
221
+ Camera::get_predicted_translation (float time_offset) const {
222
+ // TODO: extrapolate forwards instead of interpolating over the last frame
223
+ float x = time_offset * LOGICAL_FPS;
224
+ return get_translation () * x + (1 - x) * m_last_translation;
225
+ }
226
+
227
+ float
228
+ Camera::get_predicted_scale (float time_offset) const {
229
+ // TODO: extrapolate forwards instead of interpolating over the last frame
230
+ float x = time_offset * LOGICAL_FPS;
231
+ return get_current_scale () * x + (1 - x) * m_last_scale;
232
+ }
233
+
210
234
const Vector
211
235
Camera::get_translation () const
212
236
{
218
242
Camera::set_translation_centered (const Vector& translation)
219
243
{
220
244
m_translation = translation - m_screen_size.as_vector () / 2 ;
245
+ reset_prediction_state ();
221
246
}
222
247
223
248
Rectf
@@ -237,6 +262,7 @@ Camera::reset(const Vector& tuxpos)
237
262
keep_in_bounds (m_translation);
238
263
239
264
m_cached_translation = m_translation;
265
+ reset_prediction_state ();
240
266
}
241
267
242
268
void
@@ -286,6 +312,7 @@ Camera::scroll_to(const Vector& goal, float scrolltime)
286
312
m_translation.x = goal.x ;
287
313
m_translation.y = goal.y ;
288
314
m_mode = Mode::MANUAL;
315
+ reset_prediction_state ();
289
316
return ;
290
317
}
291
318
@@ -313,6 +340,10 @@ Camera::draw(DrawingContext& context)
313
340
void
314
341
Camera::update (float dt_sec)
315
342
{
343
+ // For use by camera position prediction
344
+ m_last_scale = get_current_scale ();
345
+ m_last_translation = get_translation ();
346
+
316
347
// Minimum scale should be set during the update sequence; else, reset it.
317
348
m_enfore_minimum_scale = false ;
318
349
@@ -361,6 +392,8 @@ Camera::keep_in_bounds(const Rectf& bounds)
361
392
362
393
// Remove any scale factor we may have added in the checks above.
363
394
m_translation -= scale_factor;
395
+
396
+ reset_prediction_state ();
364
397
}
365
398
366
399
void
@@ -754,6 +787,8 @@ Camera::ease_scale(float scale, float time, easing ease, AnchorPoint anchor)
754
787
m_scale = scale;
755
788
if (m_mode == Mode::MANUAL)
756
789
m_translation = m_scale_target_translation;
790
+
791
+ reset_prediction_state ();
757
792
}
758
793
}
759
794
0 commit comments