supertux
camera.hpp
1 // SuperTux
2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 // 2023 Vankata453
4 //
5 // This program is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18 #ifndef HEADER_SUPERTUX_OBJECT_CAMERA_HPP
19 #define HEADER_SUPERTUX_OBJECT_CAMERA_HPP
20 
21 #include <string>
22 
23 #include "math/anchor_point.hpp"
24 #include "math/size.hpp"
25 #include "math/vector.hpp"
26 #include "object/path_object.hpp"
27 #include "supertux/game_object.hpp"
28 #include "supertux/timer.hpp"
29 
30 class Path;
31 class PathWalker;
32 class ReaderMapping;
33 
40 class Camera final : public GameObject,
41  public PathObject
42 {
43 public:
44  static void register_class(ssq::VM& vm);
45 
46 public:
47  enum class Mode
48  {
49  NORMAL, MANUAL, AUTOSCROLL, SCROLLTO
50  };
51 
52 public:
53  Camera(const std::string& name);
54  Camera(const ReaderMapping& reader);
55  ~Camera() override;
56 
59  virtual void update(float dt_sec) override;
60  virtual void draw(DrawingContext& ) override;
61 
62  virtual bool is_singleton() const override { return true; }
63  virtual bool is_saveable() const override;
64 
65  static std::string class_name() { return "camera"; }
66  virtual std::string get_class_name() const override { return class_name(); }
67  virtual std::string get_exposed_class_name() const override { return "Camera"; }
68  static std::string display_name() { return _("Camera"); }
69  virtual std::string get_display_name() const override { return display_name(); }
70  virtual GameObjectClasses get_class_types() const override { return GameObject::get_class_types().add(typeid(PathObject)).add(typeid(Camera)); }
71 
72  virtual ObjectSettings get_settings() override;
73  virtual void after_editor_set() override;
74 
75  void save_state() override;
76  void check_state() override;
77 
78  virtual const std::string get_icon_path() const override { return "images/engine/editor/camera.png"; }
81  Rectf get_rect() const;
82 
87  void reset(const Vector& tuxpos);
88 
90  const Vector get_translation() const;
91  void set_translation(const Vector& translation) { m_translation = translation; }
92  void set_translation_centered(const Vector& translation);
93 
94  void keep_in_bounds(const Rectf& bounds);
95 
97  void shake(float duration, float x, float y);
98 
100  void start_earthquake(float strength, float delay);
101  void stop_earthquake();
102 
105  void scroll_to(const Vector& goal, float scrolltime);
106  void move(const Vector& offset);
107 
110  Vector get_center() const;
111 
114  const Vector& get_position() const;
115 
117  const Sizef& get_screen_size() const;
118 
119  void set_mode(Mode mode_) { m_mode = mode_; }
120 
121  Mode get_mode() const { return m_mode; }
122 
124  float get_current_scale() const { return m_enfore_minimum_scale ? std::min(m_minimum_scale, m_scale) : m_scale; }
125 
127  float get_target_scale() const { return m_scale_target; }
128 
130  void ease_scale(float scale, float time, easing ease, AnchorPoint anchor = AnchorPoint::ANCHOR_MIDDLE);
139  void set_pos(float x, float y);
146  void move(float x, float y);
152  void set_mode(const std::string& mode);
160  void scroll_to(float x, float y, float scrolltime);
166  void set_scale(float scale);
174  void set_scale_anchor(float scale, int anchor);
181  void scale(float scale, float time);
190  void scale_anchor(float scale, float time, int anchor);
198  void ease_scale(float scale, float time, const std::string& ease);
208  void ease_scale_anchor(float scale, float time, int anchor, const std::string& ease);
213  float get_screen_width() const;
218  float get_screen_height() const;
223  float get_x() const;
228  float get_y() const;
229 
230 private:
231  void keep_in_bounds(Vector& vector);
232 
233  void update_scroll_normal(float dt_sec);
234  void update_scroll_normal_multiplayer(float dt_sec);
235  void update_scroll_autoscroll(float dt_sec);
236  void update_scroll_to(float dt_sec);
237  void update_scale(float dt_sec);
238  void update_shake();
239  void update_earthquake();
240 
241  Vector get_scale_anchor_target() const;
242  void reload_scale();
243 
244 private:
245  Mode m_mode;
246  Mode m_defaultmode;
247 
248  Sizef m_screen_size;
249 
250  Vector m_translation;
251 
252  // normal mode
253  Vector m_lookahead_pos;
254  Vector m_peek_pos;
255  Vector m_cached_translation;
256 
257  // shaking
258  Timer m_shaketimer;
259  float m_shakespeed;
260  float m_shakedepth_x;
261  float m_shakedepth_y;
262 
263  // Earthquake
264  bool m_earthquake;
265  float m_earthquake_strength,
266  m_earthquake_delay,
267  m_earthquake_last_offset;
268  Timer m_earthquake_delay_timer;
269 
270  // scrollto mode
271  Vector m_scroll_from;
272  Vector m_scroll_goal;
273  float m_scroll_to_pos;
274  float m_scrollspeed;
275 
276  float m_scale,
277  m_scale_origin,
278  m_scale_target,
279  m_scale_time_total,
280  m_scale_time_remaining;
281  Vector m_scale_origin_translation,
282  m_scale_target_translation;
283  easing m_scale_easing;
284  AnchorPoint m_scale_anchor;
285 
286  // Minimum scale is used in certain circumstances where a fixed minimum scale
287  // should be used, regardless of the scriping-accessible `m_scale` property.
288  float m_minimum_scale;
289  bool m_enfore_minimum_scale;
290 
291 private:
292  Camera(const Camera&) = delete;
293  Camera& operator=(const Camera&) = delete;
294 };
295 
296 #endif
297 
298 /* EOF */
void start_earthquake(float strength, float delay)
Shake the camera vertically with a specified average strength, at a certain minimal delay...
Definition: camera.cpp:252
virtual std::string get_display_name() const override
Returns the display name of the object, translated to the user&#39;s locale.
Definition: camera.hpp:69
float get_screen_width() const
Gets the current width of the screen.
Definition: camera.cpp:834
float get_x() const
Gets the X coordinate of the top-left corner of the screen.
Definition: camera.cpp:846
A walker that travels along a path.
Definition: path_walker.hpp:31
void set_scale_anchor(float scale, int anchor)
Sets the scale factor and the target position anchor.
Definition: camera.cpp:804
void scale(float scale, float time)
Fades to a specified scale factor in ""time"" seconds.
Definition: camera.cpp:810
virtual bool is_singleton() const override
If true only a single object of this type is allowed in a given GameObjectManager.
Definition: camera.hpp:62
const Vector & get_position() const
get the coordinates of the point directly in the top left of this camera
Definition: camera.cpp:865
virtual bool is_saveable() const override
Indicates if the object will be saved.
Definition: camera.cpp:884
void save_state() override
Save/check the current state of the object.
Definition: camera.cpp:197
Vector get_center() const
get the coordinates of the point directly in the center of this camera
Definition: camera.cpp:858
void set_pos(float x, float y)
Moves the camera to the specified absolute position.
Definition: camera.cpp:769
void set_scale(float scale)
Sets the scale factor.
Definition: camera.cpp:798
virtual GameObjectClasses get_class_types() const override
List notable classes in inheritance hierarchy of class.
Definition: camera.hpp:70
void shake(float duration, float x, float y)
shake camera in a direction 1 time
Definition: camera.cpp:243
float get_target_scale() const
get the scale towards which the camera is moving
Definition: camera.hpp:127
void ease_scale(float scale, float time, easing ease, AnchorPoint anchor=AnchorPoint::ANCHOR_MIDDLE)
smoothly slide the scale and anchor position of the camera towards a new value
Definition: camera.cpp:748
Definition: object_settings.hpp:39
float get_y() const
Gets the Y coordinate of the top-left corner of the screen.
Definition: camera.cpp:852
const Sizef & get_screen_size() const
get the width and height of the screen
Definition: camera.cpp:871
Definition: path.hpp:48
void scale_anchor(float scale, float time, int anchor)
Fades to a specified scale factor and target position anchor in ""time"" seconds. ...
Definition: camera.cpp:816
Definition: rectf.hpp:31
This class is responsible for: Updating and drawing the object.
Definition: game_object.hpp:83
float get_screen_height() const
Gets the current height of the screen.
Definition: camera.cpp:840
float get_current_scale() const
get the exact scale at this exact moment
Definition: camera.hpp:124
virtual void draw(DrawingContext &) override
The GameObject should draw itself onto the provided DrawingContext if this function is called...
Definition: camera.cpp:302
const Vector get_translation() const
return camera position
Definition: camera.cpp:211
virtual GameObjectClasses get_class_types() const
List notable classes in inheritance hierarchy of class.
Definition: game_object.cpp:113
virtual void update(float dt_sec) override
This function is called once per frame and allows the object to update it&#39;s state.
Definition: camera.cpp:314
A base class for all objects that contain, or make use of a path.
Definition: path_object.hpp:36
A helper structure to list all the type_indexes of the classes in the type hierarchy of a given class...
Definition: game_object.hpp:57
void scroll_to(const Vector &goal, float scrolltime)
scroll the upper left edge of the camera in scrolltime seconds to the position goal ...
Definition: camera.cpp:282
Simple timer designed to be used in the update functions of objects.
Definition: timer.hpp:24
A ""Camera"" that was given a name can be manipulated by scripts.
Definition: camera.hpp:40
Definition: reader_mapping.hpp:32
void reset(const Vector &tuxpos)
reset camera position
Definition: camera.cpp:230
This class provides functions for drawing things on screen.
Definition: drawing_context.hpp:42
void ease_scale_anchor(float scale, float time, int anchor, const std::string &ease)
Fades to a specified scale factor and target position anchor in ""time"" seconds with easing (smooth ...
Definition: camera.cpp:828
Definition: sizef.hpp:26