From d61bbbaa3cd623489b902bb4e0bc503db4769003 Mon Sep 17 00:00:00 2001 From: Linus Probert Date: Tue, 20 Feb 2018 22:37:48 +0100 Subject: [PATCH] Updated docs --- docs/_algorithm_8h.html | 11 ++++--- docs/_algorithm_8h_source.html | 11 ++++--- docs/_animation_8cpp.html | 11 ++++--- docs/_animation_8cpp_source.html | 11 ++++--- docs/_animation_8h.html | 11 ++++--- docs/_animation_8h_source.html | 13 +++++--- docs/_button_8cpp.html | 11 ++++--- docs/_button_8cpp_source.html | 11 ++++--- docs/_button_8h.html | 11 ++++--- docs/_button_8h_source.html | 11 ++++--- docs/_camera_8cpp.html | 11 ++++--- docs/_camera_8cpp_source.html | 13 +++++--- docs/_camera_8h.html | 11 ++++--- docs/_camera_8h_source.html | 13 +++++--- docs/_collision_detector_8cpp.html | 11 ++++--- docs/_collision_detector_8cpp_source.html | 25 +++++--------- docs/_collision_detector_8h.html | 11 ++++--- docs/_collision_detector_8h_source.html | 11 ++++--- docs/_collision_property_8h.html | 11 ++++--- docs/_collision_property_8h_source.html | 11 ++++--- docs/_deltatime_monitor_8cpp.html | 11 ++++--- docs/_deltatime_monitor_8cpp_source.html | 11 ++++--- docs/_deltatime_monitor_8h.html | 11 ++++--- docs/_deltatime_monitor_8h_source.html | 11 ++++--- docs/_entity_8cpp.html | 11 ++++--- docs/_entity_8cpp_source.html | 11 ++++--- docs/_entity_8h.html | 11 ++++--- docs/_entity_8h_source.html | 13 +++++--- docs/_entity_container_8cpp.html | 11 ++++--- docs/_entity_container_8cpp_source.html | 14 ++++---- docs/_entity_container_8h.html | 11 ++++--- docs/_entity_container_8h_source.html | 13 +++++--- docs/_entity_properties_8cpp.html | 11 ++++--- docs/_entity_properties_8cpp_source.html | 13 +++++--- docs/_entity_properties_8h.html | 11 ++++--- docs/_entity_properties_8h_source.html | 13 +++++--- docs/_entity_shape_8h.html | 11 ++++--- docs/_entity_shape_8h_source.html | 11 ++++--- docs/_flat_builder_8cpp.html | 11 ++++--- docs/_flat_builder_8cpp_source.html | 21 +++++------- docs/_flat_builder_8h.html | 11 ++++--- docs/_flat_builder_8h_source.html | 11 ++++--- docs/_game_controller_8cpp.html | 11 ++++--- docs/_game_controller_8cpp_source.html | 11 ++++--- docs/_game_controller_8h.html | 11 ++++--- docs/_game_controller_8h_source.html | 11 ++++--- docs/_game_controller_container_8cpp.html | 11 ++++--- ...game_controller_container_8cpp_source.html | 14 ++++---- docs/_game_controller_container_8h.html | 11 ++++--- .../_game_controller_container_8h_source.html | 11 ++++--- docs/_game_data_8h.html | 11 ++++--- docs/_game_data_8h_source.html | 11 ++++--- docs/_game_engine_8cpp.html | 11 ++++--- docs/_game_engine_8cpp_source.html | 11 ++++--- docs/_game_engine_8h.html | 11 ++++--- docs/_game_engine_8h_source.html | 13 +++++--- docs/_map_area_8h.html | 11 ++++--- docs/_map_area_8h_source.html | 11 ++++--- docs/_media_util_8cpp.html | 11 ++++--- docs/_media_util_8cpp_source.html | 11 ++++--- docs/_media_util_8h.html | 11 ++++--- docs/_media_util_8h_source.html | 11 ++++--- docs/_mixer_8cpp.html | 11 ++++--- docs/_mixer_8cpp_source.html | 11 ++++--- docs/_mixer_8h.html | 11 ++++--- docs/_mixer_8h_source.html | 11 ++++--- docs/_quad_tree_8cpp.html | 11 ++++--- docs/_quad_tree_8cpp_source.html | 11 ++++--- docs/_quad_tree_8h.html | 11 ++++--- docs/_quad_tree_8h_source.html | 11 ++++--- docs/_render_data_8h.html | 11 ++++--- docs/_render_data_8h_source.html | 11 ++++--- docs/_runtime_analyzer_8cpp.html | 11 ++++--- docs/_runtime_analyzer_8cpp_source.html | 11 ++++--- docs/_runtime_analyzer_8h.html | 11 ++++--- docs/_runtime_analyzer_8h_source.html | 11 ++++--- docs/_spin_lock_8h.html | 11 ++++--- docs/_spin_lock_8h_source.html | 11 ++++--- docs/_square_8cpp.html | 11 ++++--- docs/_square_8cpp_source.html | 11 ++++--- docs/_square_8h.html | 11 ++++--- docs/_square_8h_source.html | 13 +++++--- docs/_texture_8cpp.html | 11 ++++--- docs/_texture_8cpp_source.html | 11 ++++--- docs/_texture_8h.html | 11 ++++--- docs/_texture_8h_source.html | 13 +++++--- docs/_timer_8cpp.html | 11 ++++--- docs/_timer_8cpp_source.html | 11 ++++--- docs/_timer_8h.html | 11 ++++--- docs/_timer_8h_source.html | 11 ++++--- docs/_u_i_d_8cpp.html | 11 ++++--- docs/_u_i_d_8cpp_source.html | 11 ++++--- docs/_u_i_d_8h.html | 11 ++++--- docs/_u_i_d_8h_source.html | 11 ++++--- docs/_window_8cpp.html | 11 ++++--- docs/_window_8cpp_source.html | 11 ++++--- docs/_window_8h.html | 11 ++++--- docs/_window_8h_source.html | 11 ++++--- docs/annotated.html | 11 ++++--- docs/classes.html | 11 ++++--- docs/classflat2d_1_1_algorithm-members.html | 11 ++++--- docs/classflat2d_1_1_algorithm.html | 11 ++++--- docs/classflat2d_1_1_animation-members.html | 11 ++++--- docs/classflat2d_1_1_animation.html | 11 ++++--- docs/classflat2d_1_1_camera-members.html | 11 ++++--- docs/classflat2d_1_1_camera.html | 11 ++++--- ...flat2d_1_1_collision_detector-members.html | 11 ++++--- docs/classflat2d_1_1_collision_detector.html | 11 ++++--- ...sflat2d_1_1_deltatime_monitor-members.html | 11 ++++--- docs/classflat2d_1_1_deltatime_monitor.html | 11 ++++--- docs/classflat2d_1_1_entity-members.html | 11 ++++--- docs/classflat2d_1_1_entity.html | 13 +++++--- ...ssflat2d_1_1_entity_container-members.html | 11 ++++--- docs/classflat2d_1_1_entity_container.html | 11 ++++--- ...sflat2d_1_1_entity_properties-members.html | 11 ++++--- docs/classflat2d_1_1_entity_properties.html | 11 ++++--- ...assflat2d_1_1_execution_timer-members.html | 11 ++++--- docs/classflat2d_1_1_execution_timer.html | 11 ++++--- .../classflat2d_1_1_flat_builder-members.html | 11 ++++--- docs/classflat2d_1_1_flat_builder.html | 11 ++++--- ...assflat2d_1_1_game_controller-members.html | 11 ++++--- docs/classflat2d_1_1_game_controller.html | 11 ++++--- ...1_1_game_controller_container-members.html | 11 ++++--- ...sflat2d_1_1_game_controller_container.html | 11 ++++--- docs/classflat2d_1_1_game_data-members.html | 11 ++++--- docs/classflat2d_1_1_game_data.html | 11 ++++--- docs/classflat2d_1_1_game_engine-members.html | 11 ++++--- docs/classflat2d_1_1_game_engine.html | 11 ++++--- docs/classflat2d_1_1_map_area-members.html | 11 ++++--- docs/classflat2d_1_1_map_area.html | 11 ++++--- docs/classflat2d_1_1_media_util-members.html | 11 ++++--- docs/classflat2d_1_1_media_util.html | 11 ++++--- docs/classflat2d_1_1_mixer-members.html | 11 ++++--- docs/classflat2d_1_1_mixer.html | 11 ++++--- docs/classflat2d_1_1_quad_tree-members.html | 11 ++++--- docs/classflat2d_1_1_quad_tree.html | 11 ++++--- docs/classflat2d_1_1_render_data-members.html | 11 ++++--- docs/classflat2d_1_1_render_data.html | 11 ++++--- ...ssflat2d_1_1_runtime_analyzer-members.html | 11 ++++--- docs/classflat2d_1_1_runtime_analyzer.html | 11 ++++--- docs/classflat2d_1_1_spin_lock-members.html | 11 ++++--- docs/classflat2d_1_1_spin_lock.html | 11 ++++--- docs/classflat2d_1_1_square-members.html | 11 ++++--- docs/classflat2d_1_1_square.html | 13 +++++--- docs/classflat2d_1_1_texture-members.html | 11 ++++--- docs/classflat2d_1_1_texture.html | 11 ++++--- docs/classflat2d_1_1_timer-members.html | 11 ++++--- docs/classflat2d_1_1_timer.html | 11 ++++--- docs/classflat2d_1_1_u_i_d-members.html | 11 ++++--- docs/classflat2d_1_1_u_i_d.html | 11 ++++--- docs/classflat2d_1_1_window-members.html | 11 ++++--- docs/classflat2d_1_1_window.html | 11 ++++--- .../classflat2d_1_1ui_1_1_button-members.html | 11 ++++--- docs/classflat2d_1_1ui_1_1_button.html | 11 ++++--- .../dir_68267d1309a1af8e8297ef4c3efbcdba.html | 11 ++++--- docs/doxygen.css | 2 +- docs/dynsections.js | 33 ++++++++++++++++--- docs/files.html | 11 ++++--- docs/flat_8h.html | 11 ++++--- docs/flat_8h_source.html | 11 ++++--- docs/functions.html | 11 ++++--- docs/functions_0x7e.html | 11 ++++--- docs/functions_b.html | 11 ++++--- docs/functions_c.html | 11 ++++--- docs/functions_d.html | 11 ++++--- docs/functions_e.html | 11 ++++--- docs/functions_f.html | 11 ++++--- docs/functions_func.html | 11 ++++--- docs/functions_func_0x7e.html | 11 ++++--- docs/functions_func_b.html | 11 ++++--- docs/functions_func_c.html | 11 ++++--- docs/functions_func_e.html | 11 ++++--- docs/functions_func_g.html | 11 ++++--- docs/functions_func_h.html | 11 ++++--- docs/functions_func_i.html | 11 ++++--- docs/functions_func_l.html | 11 ++++--- docs/functions_func_m.html | 11 ++++--- docs/functions_func_o.html | 11 ++++--- docs/functions_func_p.html | 11 ++++--- docs/functions_func_q.html | 11 ++++--- docs/functions_func_r.html | 11 ++++--- docs/functions_func_s.html | 11 ++++--- docs/functions_func_t.html | 11 ++++--- docs/functions_func_u.html | 11 ++++--- docs/functions_func_w.html | 11 ++++--- docs/functions_g.html | 11 ++++--- docs/functions_h.html | 11 ++++--- docs/functions_i.html | 11 ++++--- docs/functions_l.html | 11 ++++--- docs/functions_m.html | 11 ++++--- docs/functions_o.html | 11 ++++--- docs/functions_p.html | 11 ++++--- docs/functions_q.html | 11 ++++--- docs/functions_r.html | 11 ++++--- docs/functions_rela.html | 11 ++++--- docs/functions_s.html | 11 ++++--- docs/functions_t.html | 11 ++++--- docs/functions_type.html | 11 ++++--- docs/functions_u.html | 11 ++++--- docs/functions_vars.html | 11 ++++--- docs/functions_w.html | 11 ++++--- docs/functions_x.html | 11 ++++--- docs/functions_y.html | 11 ++++--- docs/globals.html | 11 ++++--- docs/globals_defs.html | 11 ++++--- docs/hierarchy.html | 11 ++++--- docs/index.html | 11 ++++--- docs/jquery.js | 32 ++++++++++++++++-- docs/menu.js | 24 ++++++++++++++ docs/menudata.js | 23 +++++++++++++ docs/namespaceflat2d.html | 11 ++++--- docs/namespaceflat2d_1_1ui.html | 11 ++++--- docs/namespacemembers.html | 11 ++++--- docs/namespacemembers_enum.html | 11 ++++--- docs/namespacemembers_eval.html | 11 ++++--- docs/namespacemembers_type.html | 11 ++++--- docs/namespaces.html | 11 ++++--- docs/search/all_0.html | 6 +++- docs/search/all_1.html | 6 +++- docs/search/all_10.html | 6 +++- docs/search/all_11.html | 6 +++- docs/search/all_12.html | 6 +++- docs/search/all_13.html | 6 +++- docs/search/all_14.html | 6 +++- docs/search/all_15.html | 6 +++- docs/search/all_16.html | 6 +++- docs/search/all_2.html | 6 +++- docs/search/all_3.html | 6 +++- docs/search/all_4.html | 6 +++- docs/search/all_5.html | 6 +++- docs/search/all_6.html | 6 +++- docs/search/all_7.html | 6 +++- docs/search/all_8.html | 6 +++- docs/search/all_9.html | 6 +++- docs/search/all_a.html | 6 +++- docs/search/all_b.html | 6 +++- docs/search/all_c.html | 6 +++- docs/search/all_d.html | 6 +++- docs/search/all_e.html | 6 +++- docs/search/all_f.html | 6 +++- docs/search/classes_0.html | 6 +++- docs/search/classes_1.html | 6 +++- docs/search/classes_2.html | 6 +++- docs/search/classes_3.html | 6 +++- docs/search/classes_4.html | 6 +++- docs/search/classes_5.html | 6 +++- docs/search/classes_6.html | 6 +++- docs/search/classes_7.html | 6 +++- docs/search/classes_8.html | 6 +++- docs/search/classes_9.html | 6 +++- docs/search/classes_a.html | 6 +++- docs/search/classes_b.html | 6 +++- docs/search/classes_c.html | 6 +++- docs/search/classes_d.html | 6 +++- docs/search/defines_0.html | 6 +++- docs/search/enums_0.html | 6 +++- docs/search/enums_1.html | 6 +++- docs/search/enumvalues_0.html | 6 +++- docs/search/enumvalues_1.html | 6 +++- docs/search/enumvalues_2.html | 6 +++- docs/search/enumvalues_3.html | 6 +++- docs/search/enumvalues_4.html | 6 +++- docs/search/enumvalues_5.html | 6 +++- docs/search/files_0.html | 6 +++- docs/search/files_1.html | 6 +++- docs/search/files_2.html | 6 +++- docs/search/files_3.html | 6 +++- docs/search/files_4.html | 6 +++- docs/search/files_5.html | 6 +++- docs/search/files_6.html | 6 +++- docs/search/files_7.html | 6 +++- docs/search/files_8.html | 6 +++- docs/search/files_9.html | 6 +++- docs/search/files_a.html | 6 +++- docs/search/files_b.html | 6 +++- docs/search/files_c.html | 6 +++- docs/search/files_d.html | 6 +++- docs/search/functions_0.html | 6 +++- docs/search/functions_1.html | 6 +++- docs/search/functions_10.html | 6 +++- docs/search/functions_11.html | 6 +++- docs/search/functions_2.html | 6 +++- docs/search/functions_3.html | 6 +++- docs/search/functions_4.html | 6 +++- docs/search/functions_5.html | 6 +++- docs/search/functions_6.html | 6 +++- docs/search/functions_7.html | 6 +++- docs/search/functions_8.html | 6 +++- docs/search/functions_9.html | 6 +++- docs/search/functions_a.html | 6 +++- docs/search/functions_b.html | 6 +++- docs/search/functions_c.html | 6 +++- docs/search/functions_d.html | 6 +++- docs/search/functions_e.html | 6 +++- docs/search/functions_f.html | 6 +++- docs/search/namespaces_0.html | 6 +++- docs/search/pages_0.html | 6 +++- docs/search/related_0.html | 6 +++- docs/search/search.js | 25 +++++++++++++- docs/search/typedefs_0.html | 6 +++- docs/search/typedefs_1.html | 6 +++- docs/search/typedefs_2.html | 6 +++- docs/search/typedefs_3.html | 6 +++- docs/search/typedefs_4.html | 6 +++- docs/search/typedefs_5.html | 6 +++- docs/search/typedefs_6.html | 6 +++- docs/search/typedefs_7.html | 6 +++- docs/search/typedefs_8.html | 6 +++- docs/search/variables_0.html | 6 +++- docs/search/variables_1.html | 6 +++- docs/search/variables_2.html | 6 +++- docs/search/variables_3.html | 6 +++- docs/search/variables_4.html | 6 +++- docs/search/variables_5.html | 6 +++- docs/search/variables_6.html | 6 +++- docs/search/variables_7.html | 6 +++- ...structflat2d_1_1_entity_shape-members.html | 11 ++++--- docs/structflat2d_1_1_entity_shape.html | 11 ++++--- 318 files changed, 2134 insertions(+), 1001 deletions(-) diff --git a/docs/_algorithm_8h.html b/docs/_algorithm_8h.html index f210658..659acfc 100644 --- a/docs/_algorithm_8h.html +++ b/docs/_algorithm_8h.html @@ -3,7 +3,7 @@ - + flat: src/Algorithm.h File Reference @@ -31,18 +31,21 @@ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
Animation.h
-Go to the documentation of this file.
1 #ifndef ANIMATION_H_
2 #define ANIMATION_H_
3 
4 #include <SDL2/SDL.h>
5 #include <vector>
6 #include "Timer.h"
7 
8 
9 namespace flat2d
10 {
11  typedef std::vector<SDL_Rect> Clips;
12 
19  class Animation
20  {
21  private:
22  Timer animationTimer;
23 
24  Clips clips;
25  uint32_t timestep;
26  int clipIndex = 0;
27 
28  public:
29  Animation(Clips c, uint32_t t) : clips(c), timestep(t) { }
30 
35  const SDL_Rect* run();
36 
40  void start();
41 
45  void stop();
46 
51  bool isRunning() const;
52  };
53 } // namespace flat2d
54 
55 #endif // ANIMATION_H_
const SDL_Rect * run()
Definition: Animation.cpp:21
+Go to the documentation of this file.
1 #ifndef ANIMATION_H_
2 #define ANIMATION_H_
3 
4 #include <SDL2/SDL.h>
5 #include <vector>
6 #include "Timer.h"
7 
8 
9 namespace flat2d
10 {
11  typedef std::vector<SDL_Rect> Clips;
12 
19  class Animation
20  {
21  private:
22  Timer animationTimer;
23 
24  Clips clips;
25  uint32_t timestep;
26  int clipIndex = 0;
27 
28  public:
29  Animation(Clips c, uint32_t t) : clips(c), timestep(t) { }
30 
35  const SDL_Rect* run();
36 
40  void start();
41 
45  void stop();
46 
51  bool isRunning() const;
52  };
53 } // namespace flat2d
54 
55 #endif // ANIMATION_H_
const SDL_Rect * run()
Definition: Animation.cpp:21
bool isRunning() const
Definition: Animation.cpp:16
@@ -83,7 +86,7 @@ diff --git a/docs/_button_8cpp.html b/docs/_button_8cpp.html index 75f645e..f496d04 100644 --- a/docs/_button_8cpp.html +++ b/docs/_button_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/Button.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
Camera.cpp
-Go to the documentation of this file.
1 #include "Camera.h"
2 
3 namespace flat2d
4 {
5  void Camera::setMapDimensions(int w, int h)
6  {
7  mapWidth = w;
8  mapHeight = h;
9  }
10 
12  {
13  return mapHeight;
14  }
15 
17  {
18  return mapWidth;
19  }
20 
21  SDL_Rect Camera::getBox()
22  {
23  SDL_Rect box = { x, y, w, h};
24  return box;
25  }
26 
27  void Camera::centerOn(int xpos, int ypos) {
28  x = xpos - (w/2);
29  y = ypos - (h/2);
30 
31  if (x > mapWidth - w) {
32  x = mapWidth - w;
33  } else if (x < 0) {
34  x = 0;
35  }
36  if (y > mapHeight - h) {
37  y = mapHeight - h;
38  } else if (y < 0) {
39  y = 0;
40  }
41  }
42 
43  bool Camera::isVisibleOnCamera(const SDL_Rect& box, int depth)
44  {
45  int bx = getScreenXposFor(box.x, depth);
46  int by = getScreenYposFor(box.y, depth);
47 
48  if (bx > w) {
49  return false;
50  } else if (bx + box.w < 0) {
51  return false;
52  } else if (by > h) {
53  return false;
54  } else if (by + box.h < 0) {
55  return false;
56  }
57  return true;
58  }
59 
60  bool Camera::isOutOfMapBounds(const SDL_Rect& rect)
61  {
62  return rect.x + rect.w < 0
63  || rect.y + rect.h < 0
64  || rect.y > mapHeight
65  || rect.x > mapWidth;
66  }
67 
68  int Camera::getScreenXposFor(int xpos, int depth) const
69  {
70  if (depth > 0) {
71  return xpos - (x / (1.5 * depth));
72  }
73  return xpos - x;
74  }
75 
76  int Camera::getScreenYposFor(int ypos, int depth) const
77  {
78  if (depth > 0) {
79  return ypos - (y / (5 * depth));
80  }
81  return ypos - y;
82  }
83 } // namespace flat2d
bool isVisibleOnCamera(const SDL_Rect &box, int depth=0)
Definition: Camera.cpp:43
+Go to the documentation of this file.
1 #include "Camera.h"
2 
3 namespace flat2d
4 {
5  void Camera::setMapDimensions(int w, int h)
6  {
7  mapWidth = w;
8  mapHeight = h;
9  }
10 
12  {
13  return mapHeight;
14  }
15 
17  {
18  return mapWidth;
19  }
20 
21  SDL_Rect Camera::getBox()
22  {
23  SDL_Rect box = { x, y, w, h};
24  return box;
25  }
26 
27  void Camera::centerOn(int xpos, int ypos) {
28  x = xpos - (w/2);
29  y = ypos - (h/2);
30 
31  if (x > mapWidth - w) {
32  x = mapWidth - w;
33  } else if (x < 0) {
34  x = 0;
35  }
36  if (y > mapHeight - h) {
37  y = mapHeight - h;
38  } else if (y < 0) {
39  y = 0;
40  }
41  }
42 
43  bool Camera::isVisibleOnCamera(const SDL_Rect& box, int depth)
44  {
45  int bx = getScreenXposFor(box.x, depth);
46  int by = getScreenYposFor(box.y, depth);
47 
48  if (bx > w) {
49  return false;
50  } else if (bx + box.w < 0) {
51  return false;
52  } else if (by > h) {
53  return false;
54  } else if (by + box.h < 0) {
55  return false;
56  }
57  return true;
58  }
59 
60  bool Camera::isOutOfMapBounds(const SDL_Rect& rect)
61  {
62  return rect.x + rect.w < 0
63  || rect.y + rect.h < 0
64  || rect.y > mapHeight
65  || rect.x > mapWidth;
66  }
67 
68  int Camera::getScreenXposFor(int xpos, int depth) const
69  {
70  if (depth > 0) {
71  return xpos - (x / (1.5 * depth));
72  }
73  return xpos - x;
74  }
75 
76  int Camera::getScreenYposFor(int ypos, int depth) const
77  {
78  if (depth > 0) {
79  return ypos - (y / (5 * depth));
80  }
81  return ypos - y;
82  }
83 } // namespace flat2d
bool isVisibleOnCamera(const SDL_Rect &box, int depth=0)
Definition: Camera.cpp:43
bool isOutOfMapBounds(const SDL_Rect &rect)
Definition: Camera.cpp:60
@@ -88,7 +91,7 @@ diff --git a/docs/_camera_8h.html b/docs/_camera_8h.html index 88903c9..5280cc0 100644 --- a/docs/_camera_8h.html +++ b/docs/_camera_8h.html @@ -3,7 +3,7 @@ - + flat: src/Camera.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
Camera.h
-Go to the documentation of this file.
1 #ifndef CAMERA_H_
2 #define CAMERA_H_
3 
4 #include <SDL2/SDL.h>
5 #include "Square.h"
6 
7 namespace flat2d
8 {
17  class Camera : public Square
18  {
19  private:
20  int mapWidth, mapHeight;
21 
22  public:
23  Camera(int width, int height) :
24  Square(0, 0, width, height),
25  mapWidth(1920),
26  mapHeight(1080) { }
27 
28  virtual ~Camera() { }
29 
35  void setMapDimensions(int, int);
36 
41  int getMapHeight();
42 
47  int getMapWidth();
48 
53  SDL_Rect getBox();
54 
60  void centerOn(int x, int y);
61 
68  bool isVisibleOnCamera(const SDL_Rect& box, int depth = 0);
69 
75  bool isOutOfMapBounds(const SDL_Rect& rect);
76 
83  int getScreenXposFor(int x, int depth = 0) const;
84 
91  int getScreenYposFor(int y, int depth = 0) const;
92  };
93 } // namespace flat2d
94 
95 #endif // CAMERA_H_
bool isVisibleOnCamera(const SDL_Rect &box, int depth=0)
Definition: Camera.cpp:43
+Go to the documentation of this file.
1 #ifndef CAMERA_H_
2 #define CAMERA_H_
3 
4 #include <SDL2/SDL.h>
5 #include "Square.h"
6 
7 namespace flat2d
8 {
17  class Camera : public Square
18  {
19  private:
20  int mapWidth, mapHeight;
21 
22  public:
23  Camera(int width, int height) :
24  Square(0, 0, width, height),
25  mapWidth(1920),
26  mapHeight(1080) { }
27 
28  virtual ~Camera() { }
29 
35  void setMapDimensions(int, int);
36 
41  int getMapHeight();
42 
47  int getMapWidth();
48 
53  SDL_Rect getBox();
54 
60  void centerOn(int x, int y);
61 
68  bool isVisibleOnCamera(const SDL_Rect& box, int depth = 0);
69 
75  bool isOutOfMapBounds(const SDL_Rect& rect);
76 
83  int getScreenXposFor(int x, int depth = 0) const;
84 
91  int getScreenYposFor(int y, int depth = 0) const;
92  };
93 } // namespace flat2d
94 
95 #endif // CAMERA_H_
bool isVisibleOnCamera(const SDL_Rect &box, int depth=0)
Definition: Camera.cpp:43
bool isOutOfMapBounds(const SDL_Rect &rect)
Definition: Camera.cpp:60
@@ -90,7 +93,7 @@ diff --git a/docs/_collision_detector_8cpp.html b/docs/_collision_detector_8cpp.html index 5bfc3ff..0fdd910 100644 --- a/docs/_collision_detector_8cpp.html +++ b/docs/_collision_detector_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/CollisionDetector.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
CollisionDetector.cpp
-Go to the documentation of this file.
1 #include <limits>
2 #include <algorithm>
3 #include <vector>
4 #include <cassert>
5 
6 #include "Entity.h"
7 #include "EntityShape.h"
8 #include "CollisionDetector.h"
9 #include "EntityContainer.h"
10 #include "DeltatimeMonitor.h"
11 #include "GameData.h"
12 #include "Algorithm.h"
13 
14 namespace flat2d
15 {
17  {
18  entityContainer->iterateCollidablesFor(e,
19  [this, e, data](Entity* o)
20  {
22  dtMonitor->getDeltaTime());
23  if (this->AABB(broadphaseShape, o->getEntityProperties().getColliderShape())) {
24  this->handlePossibleCollision(e, o, data);
25  }
26  return false;
27  });
28  }
29 
30  bool CollisionDetector::handlePossibleCollision(Entity* o1, Entity* o2, const GameData *data)
31  {
32  if (*o1 == *o2) {
33  return false;
34  }
35 
36  EntityProperties &props1 = o1->getEntityProperties();
37  EntityProperties &props2 = o2->getEntityProperties();
38 
39  bool collided = false;
40  float deltatime = dtMonitor->getDeltaTime();
41  EntityShape colliderShape = props2.getColliderShape();
42 
43  float xvel = props1.getXvel() * deltatime;
44  if (xvel != 0 && AABB(props1.getXVelocityColliderShape(deltatime), colliderShape)) {
45  if (!o1->onCollision(o2, data) && !o1->onHorizontalCollision(o2, data)) {
46  handleHorizontalCollisions(&props1, &props2);
47  }
48  o2->onCollision(o1, data);
49  o2->onHorizontalCollision(o1, data);
50  collided = true;
51  }
52 
53  float yvel = props1.getYvel() * deltatime;
54  if (yvel != 0 && AABB(props1.getYVelocityColliderShape(deltatime), colliderShape)) {
55  if (!o1->onCollision(o2, data) && !o1->onVerticalCollision(o2, data)) {
56  handleVerticalCollisions(&props1, &props2);
57  }
58  o2->onCollision(o1, data);
59  o2->onVerticalCollision(o1, data);
60  collided = true;
61  }
62 
63  return collided;
64  }
65 
66  bool CollisionDetector::AABB(const EntityShape& b1, const EntityShape& b2) const
67  {
68  return !(b1.x > b2.x + b2.w)
69  && !(b1.x + b1.w < b2.x)
70  && !(b1.y > b2.y + b2.h)
71  && !(b1.y + b1.h < b2.y);
72  }
73 
75  float *normalx, float *normaly) const
76  {
77  EntityShape b1 = p1->getColliderShape();
78  EntityShape b2 = p2->getColliderShape();
79 
80  float xvel = p1->getXvel() * dtMonitor->getDeltaTime();
81  float yvel = p1->getYvel() * dtMonitor->getDeltaTime();
82 
83 
84  // Find the distances to the blocking object
85  float xInvEntry, xInvExit;
86  float yInvEntry, yInvExit;
87 
88  if (xvel > 0.0f) {
89  xInvEntry = b2.x - (b1.x + b1.w + 2);
90  xInvExit = (b2.x + b2.w) - (b1.x - 2);
91  } else {
92  xInvEntry = (b2.x + b2.w) - (b1.x - 2);
93  xInvExit = b2.x - (b1.x + b1.w + 2);
94  }
95 
96  if (yvel > 0.0f) {
97  yInvEntry = b2.y - (b1.y + b1.w + 2);
98  yInvExit = (b2.y + b2.w) - (b1.y - 2);
99  } else {
100  yInvEntry = (b2.y + b2.w) - (b1.y - 2);
101  yInvExit = b2.y - (b1.y + b1.w + 2);
102  }
103 
104  // Find the impact time
105  float xEntry, xExit;
106  float yEntry, yExit;
107 
108  if (xvel == 0.0f) {
109  xEntry = -std::numeric_limits<float>::infinity();
110  xExit = std::numeric_limits<float>::infinity();
111  } else {
112  xEntry = xInvEntry / xvel;
113  xExit = xInvExit / xvel;
114  }
115 
116  if (yvel == 0.0f) {
117  yEntry = -std::numeric_limits<float>::infinity();
118  yExit = std::numeric_limits<float>::infinity();
119  } else {
120  yEntry = yInvEntry / yvel;
121  yExit = yInvExit / yvel;
122  }
123 
124  // Find earliest/latest times of collision
125  float entryTime = std::max(xEntry, yEntry);
126  float exitTime = std::min(xExit, yExit);
127 
128  if (entryTime > exitTime || (xEntry < 0.0f && yEntry < 0.0f) || xEntry > 1.0f || yEntry > 1.0f ) {
129  // No collision
130  *normalx = 0.0f;
131  *normaly = 0.0f;
132  return 1.0f;
133 
134  } else {
135  if (xEntry > yEntry) {
136  if (xInvEntry < 0.0f) {
137  *normalx = 1.0f;
138  *normaly = 0.0f;
139  } else {
140  *normalx = -1.0f;
141  *normaly = 0.0f;
142  }
143  } else {
144  if (yInvEntry < 0.0f) {
145  *normalx = 0.0f;
146  *normaly = 1.0f;
147  } else {
148  *normalx = 0.0f;
149  *normaly = -1.0f;
150  }
151  }
152  }
153  return entryTime;
154  }
155 
156  void CollisionDetector::handleHorizontalCollisions(EntityProperties* props1, EntityProperties* props2) const
157  {
158  assert(props1->getXvel() != 0);
159 
160  switch (props1->getCollisionProperty()) {
162  handleHorizontalBouncyCollision(props1, props2);
163  break;
165  handleHorizontalStickyCollision(props1, props2);
166  break;
168  break;
170  default:
171  handleHorizontalSolidCollision(props1, props2);
172  }
173  }
174 
175  void CollisionDetector::handleVerticalCollisions(EntityProperties* props1, EntityProperties* props2) const
176  {
177  assert(props1->getYvel() != 0);
178 
179  switch (props1->getCollisionProperty()) {
181  handleVerticalBouncyCollision(props1, props2);
182  break;
184  handleVerticalStickyCollision(props1, props2);
185  break;
187  break;
189  default:
190  handleVerticalSolidCollision(props1, props2);
191  }
192  }
193 
194  void CollisionDetector::handleVerticalSolidCollision(EntityProperties* props1, EntityProperties* props2) const
195  {
196  EntityShape colliderShape = props2->getColliderShape();
197 
198  if (props1->getYvel() > 0) {
199  props1->setYpos(colliderShape.y - props1->getHeight() - 1 + props1->getColliderBottomOffset());
200  } else {
201  props1->setYpos(colliderShape.y + colliderShape.h + 1 - props1->getColliderTopOffset());
202  }
203  props1->setYvel(0);
204  }
205 
206  void CollisionDetector::handleHorizontalSolidCollision(EntityProperties* props1, EntityProperties* props2) const
207  {
208  EntityShape colliderShape = props2->getColliderShape();
209 
210  if (props1->getXvel() > 0) {
211  props1->setXpos(colliderShape.x - props1->getWidth() - 1 + props1->getColliderRightOffset());
212  } else {
213  props1->setXpos(colliderShape.x + colliderShape.w + 1 - props1->getColliderLeftOffset());
214  }
215  props1->setXvel(0);
216  }
217 
218 
219  void CollisionDetector::handleVerticalBouncyCollision(EntityProperties* props1, EntityProperties* props2) const
220  {
221  float yvel = props1->getYvel();
222  handleVerticalSolidCollision(props1, props2);
223 
224  if (yvel > 0) {
225  yvel -= 50;
226  }
227  if (std::abs(yvel) <= 300) {
228  yvel = 0;
229  }
230 
231  props1->setYvel(-1 * yvel);
232  }
233 
234  void CollisionDetector::handleHorizontalBouncyCollision(EntityProperties* props1, EntityProperties* props2) const
235  {
236  float xvel = props1->getXvel();
237  handleHorizontalSolidCollision(props1, props2);
238 
239  if (xvel > 0) {
240  xvel -= 50;
241  }
242  if (std::abs(xvel) <= 300) {
243  xvel = 0;
244  }
245 
246  props1->setYvel(-1 * xvel);
247  }
248 
249 
250  void CollisionDetector::handleVerticalStickyCollision(EntityProperties* props1, EntityProperties* props2) const
251  {
252  EntityShape colliderShape = props2->getColliderShape();
253 
254  if (props1->getYvel() > 0) {
255  props1->setYpos(colliderShape.y);
256  } else {
257  props1->setYpos(colliderShape.y + colliderShape.h - props1->getHeight());
258  }
259  props1->setXvel(0);
260  props1->setYvel(0);
261  }
262 
263  void CollisionDetector::handleHorizontalStickyCollision(EntityProperties* props1, EntityProperties* props2) const
264  {
265  EntityShape colliderShape = props2->getColliderShape();
266 
267  if (props1->getXvel() > 0) {
268  props1->setXpos(colliderShape.x);
269  } else {
270  props1->setXpos(colliderShape.x + colliderShape.w - props1->getWidth());
271  }
272  props1->setXvel(0);
273  props1->setYvel(0);
274  }
275 
276 } // namespace flat2d
int getWidth() const
Definition: Square.h:48
- - +Go to the documentation of this file.
1 #include <limits>
2 #include <algorithm>
3 #include <vector>
4 #include <cassert>
5 
6 #include "Entity.h"
7 #include "EntityShape.h"
8 #include "CollisionDetector.h"
9 #include "EntityContainer.h"
10 #include "DeltatimeMonitor.h"
11 #include "GameData.h"
12 #include "Algorithm.h"
13 
14 namespace flat2d
15 {
17  {
18  entityContainer->iterateCollidablesFor(e,
19  [this, e, data](Entity* o)
20  {
22  dtMonitor->getDeltaTime());
23  if (this->AABB(broadphaseShape, o->getEntityProperties().getColliderShape())) {
24  this->handlePossibleCollision(e, o, data);
25  }
26  return false;
27  });
28  }
29 
30  bool CollisionDetector::handlePossibleCollision(Entity* o1, Entity* o2, const GameData *data)
31  {
32  if (*o1 == *o2) {
33  return false;
34  }
35 
36  EntityProperties &props1 = o1->getEntityProperties();
37  EntityProperties &props2 = o2->getEntityProperties();
38 
39  bool collided = false;
40  float deltatime = dtMonitor->getDeltaTime();
41  EntityShape colliderShape = props2.getColliderShape();
42 
43  float xvel = props1.getXvel() * deltatime;
44  if (xvel != 0 && AABB(props1.getXVelocityColliderShape(deltatime), colliderShape)) {
45  if (!o1->onCollision(o2, data) && !o1->onHorizontalCollision(o2, data)) {
46  handleHorizontalCollisions(&props1, &props2);
47  }
48  o2->onCollision(o1, data);
49  o2->onHorizontalCollision(o1, data);
50  collided = true;
51  }
52 
53  float yvel = props1.getYvel() * deltatime;
54  if (yvel != 0 && AABB(props1.getYVelocityColliderShape(deltatime), colliderShape)) {
55  if (!o1->onCollision(o2, data) && !o1->onVerticalCollision(o2, data)) {
56  handleVerticalCollisions(&props1, &props2);
57  }
58  o2->onCollision(o1, data);
59  o2->onVerticalCollision(o1, data);
60  collided = true;
61  }
62 
63  return collided;
64  }
65 
66  bool CollisionDetector::AABB(const EntityShape& b1, const EntityShape& b2) const
67  {
68  return !(b1.x > b2.x + b2.w)
69  && !(b1.x + b1.w < b2.x)
70  && !(b1.y > b2.y + b2.h)
71  && !(b1.y + b1.h < b2.y);
72  }
73 
75  float *normalx, float *normaly) const
76  {
77  EntityShape b1 = p1->getColliderShape();
78  EntityShape b2 = p2->getColliderShape();
79 
80  float xvel = p1->getXvel() * dtMonitor->getDeltaTime();
81  float yvel = p1->getYvel() * dtMonitor->getDeltaTime();
82 
83 
84  // Find the distances to the blocking object
85  float xInvEntry, xInvExit;
86  float yInvEntry, yInvExit;
87 
88  if (xvel > 0.0f) {
89  xInvEntry = b2.x - (b1.x + b1.w + 2);
90  xInvExit = (b2.x + b2.w) - (b1.x - 2);
91  } else {
92  xInvEntry = (b2.x + b2.w) - (b1.x - 2);
93  xInvExit = b2.x - (b1.x + b1.w + 2);
94  }
95 
96  if (yvel > 0.0f) {
97  yInvEntry = b2.y - (b1.y + b1.w + 2);
98  yInvExit = (b2.y + b2.w) - (b1.y - 2);
99  } else {
100  yInvEntry = (b2.y + b2.w) - (b1.y - 2);
101  yInvExit = b2.y - (b1.y + b1.w + 2);
102  }
103 
104  // Find the impact time
105  float xEntry, xExit;
106  float yEntry, yExit;
107 
108  if (xvel == 0.0f) {
109  xEntry = -std::numeric_limits<float>::infinity();
110  xExit = std::numeric_limits<float>::infinity();
111  } else {
112  xEntry = xInvEntry / xvel;
113  xExit = xInvExit / xvel;
114  }
115 
116  if (yvel == 0.0f) {
117  yEntry = -std::numeric_limits<float>::infinity();
118  yExit = std::numeric_limits<float>::infinity();
119  } else {
120  yEntry = yInvEntry / yvel;
121  yExit = yInvExit / yvel;
122  }
123 
124  // Find earliest/latest times of collision
125  float entryTime = std::max(xEntry, yEntry);
126  float exitTime = std::min(xExit, yExit);
127 
128  if (entryTime > exitTime || (xEntry < 0.0f && yEntry < 0.0f) || xEntry > 1.0f || yEntry > 1.0f ) {
129  // No collision
130  *normalx = 0.0f;
131  *normaly = 0.0f;
132  return 1.0f;
133 
134  } else {
135  if (xEntry > yEntry) {
136  if (xInvEntry < 0.0f) {
137  *normalx = 1.0f;
138  *normaly = 0.0f;
139  } else {
140  *normalx = -1.0f;
141  *normaly = 0.0f;
142  }
143  } else {
144  if (yInvEntry < 0.0f) {
145  *normalx = 0.0f;
146  *normaly = 1.0f;
147  } else {
148  *normalx = 0.0f;
149  *normaly = -1.0f;
150  }
151  }
152  }
153  return entryTime;
154  }
155 
156  void CollisionDetector::handleHorizontalCollisions(EntityProperties* props1, EntityProperties* props2) const
157  {
158  assert(props1->getXvel() != 0);
159 
160  switch (props1->getCollisionProperty()) {
162  handleHorizontalBouncyCollision(props1, props2);
163  break;
165  handleHorizontalStickyCollision(props1, props2);
166  break;
168  break;
170  default:
171  handleHorizontalSolidCollision(props1, props2);
172  }
173  }
174 
175  void CollisionDetector::handleVerticalCollisions(EntityProperties* props1, EntityProperties* props2) const
176  {
177  assert(props1->getYvel() != 0);
178 
179  switch (props1->getCollisionProperty()) {
181  handleVerticalBouncyCollision(props1, props2);
182  break;
184  handleVerticalStickyCollision(props1, props2);
185  break;
187  break;
189  default:
190  handleVerticalSolidCollision(props1, props2);
191  }
192  }
193 
194  void CollisionDetector::handleVerticalSolidCollision(EntityProperties* props1, EntityProperties* props2) const
195  {
196  EntityShape colliderShape = props2->getColliderShape();
197 
198  if (props1->getYvel() > 0) {
199  props1->setYpos(colliderShape.y - props1->getHeight() - 1 + props1->getColliderBottomOffset());
200  } else {
201  props1->setYpos(colliderShape.y + colliderShape.h + 1 - props1->getColliderTopOffset());
202  }
203  props1->setYvel(0);
204  }
205 
206  void CollisionDetector::handleHorizontalSolidCollision(EntityProperties* props1, EntityProperties* props2) const
207  {
208  EntityShape colliderShape = props2->getColliderShape();
209 
210  if (props1->getXvel() > 0) {
211  props1->setXpos(colliderShape.x - props1->getWidth() - 1 + props1->getColliderRightOffset());
212  } else {
213  props1->setXpos(colliderShape.x + colliderShape.w + 1 - props1->getColliderLeftOffset());
214  }
215  props1->setXvel(0);
216  }
217 
218 
219  void CollisionDetector::handleVerticalBouncyCollision(EntityProperties* props1, EntityProperties* props2) const
220  {
221  float yvel = props1->getYvel();
222  handleVerticalSolidCollision(props1, props2);
223 
224  if (yvel > 0) {
225  yvel -= 50;
226  }
227  if (std::abs(yvel) <= 300) {
228  yvel = 0;
229  }
230 
231  props1->setYvel(-1 * yvel);
232  }
233 
234  void CollisionDetector::handleHorizontalBouncyCollision(EntityProperties* props1, EntityProperties* props2) const
235  {
236  float xvel = props1->getXvel();
237  handleHorizontalSolidCollision(props1, props2);
238 
239  if (xvel > 0) {
240  xvel -= 50;
241  }
242  if (std::abs(xvel) <= 300) {
243  xvel = 0;
244  }
245 
246  props1->setYvel(-1 * xvel);
247  }
248 
249 
250  void CollisionDetector::handleVerticalStickyCollision(EntityProperties* props1, EntityProperties* props2) const
251  {
252  EntityShape colliderShape = props2->getColliderShape();
253 
254  if (props1->getYvel() > 0) {
255  props1->setYpos(colliderShape.y);
256  } else {
257  props1->setYpos(colliderShape.y + colliderShape.h - props1->getHeight());
258  }
259  props1->setXvel(0);
260  props1->setYvel(0);
261  }
262 
263  void CollisionDetector::handleHorizontalStickyCollision(EntityProperties* props1, EntityProperties* props2) const
264  {
265  EntityShape colliderShape = props2->getColliderShape();
266 
267  if (props1->getXvel() > 0) {
268  props1->setXpos(colliderShape.x);
269  } else {
270  props1->setXpos(colliderShape.x + colliderShape.w - props1->getWidth());
271  }
272  props1->setXvel(0);
273  props1->setYvel(0);
274  }
275 
276 } // namespace flat2d
void iterateCollidablesFor(const Entity *, EntityIter)
virtual bool onHorizontalCollision(Entity *collider, const GameData *)
Definition: Entity.cpp:164
-
EntityShape getVelocityColliderShape(float deltatime) const
-
virtual bool onVerticalCollision(Entity *collider, const GameData *)
Definition: Entity.cpp:159
-
float sweptAABB(EntityProperties *props1, EntityProperties *props2, float *normalx, float *normaly) const
virtual bool onCollision(Entity *collider, const GameData *)
Definition: Entity.cpp:154
@@ -93,10 +91,7 @@
void handlePossibleCollisionsFor(Entity *entity, const GameData *data)
- -
bool AABB(const EntityShape &, const EntityShape &) const
-
int getHeight() const
Definition: Square.h:54
@@ -104,22 +99,18 @@ - -
EntityShape getYVelocityColliderShape(float deltatime) const
-
EntityShape getXVelocityColliderShape(float deltatime) const
EntityShape getColliderShape() const
EntityProperties & getEntityProperties()
Definition: Entity.cpp:149
-
diff --git a/docs/_collision_detector_8h.html b/docs/_collision_detector_8h.html index f65699f..0992637 100644 --- a/docs/_collision_detector_8h.html +++ b/docs/_collision_detector_8h.html @@ -3,7 +3,7 @@ - + flat: src/CollisionDetector.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
Entity.h
-Go to the documentation of this file.
1 #ifndef ENTITY_H_
2 #define ENTITY_H_
3 
4 #include <SDL2/SDL.h>
5 #include <string>
6 #include <sstream>
7 #include <memory>
8 #include <map>
9 
10 #include "EntityProperties.h"
11 #include "Animation.h"
12 #include "UID.h"
13 
14 namespace flat2d
15 {
16  class Camera;
17  class RenderData;
18  class GameData;
19  class Texture;
20 
29  class Entity
30  {
31  private:
32  size_t id;
33  bool fixedPosition = false;
34  bool inputHandler = false;
35  SDL_Rect clip;
36  std::shared_ptr<Texture> texture = nullptr;
37 
38  protected:
40 
41  std::string currentAnimation = "";
42  std::map<std::string, Animation*> animations;
43 
44  bool dead = false;
45 
46  public:
47  Entity(int x, int y, int w, int h) :
48  entityProperties(x, y, w, h),
49  dead(false) {
50  id = UID::generate();
51  clip = { 0, 0, w, h };
52  }
53 
54  virtual ~Entity() {
55  for (auto it = animations.begin(); it != animations.end(); it++) {
56  delete it->second;
57  }
58  }
59 
60  /* Operators */
61  virtual bool operator==(const Entity& o) const {
62  return id == o.id;
63  }
64 
65  virtual bool operator!=(const Entity& o) const {
66  return id != o.id;
67  }
68 
69  virtual Entity& operator=(const Entity& o) {
70  // TODO(Linus): This doesn't look right...
71  id = o.id;
72  return *this;
73  }
74 
79  virtual int getId() const {
80  return static_cast<int>(id);
81  }
82 
87  virtual std::string getStringId() const {
88  std::string number;
89  std::stringstream ss;
90  ss << id;
91  ss >> number;
92  return number;
93  }
94 
95  virtual bool operator<(const Entity& o) const {
96  return id < o.id;
97  }
98 
105  virtual int getType() const {
106  return -1;
107  }
108 
113  void setClip(SDL_Rect&);
114 
118  void setDead(bool isDead);
119 
124  bool isFixedPosition();
125 
131  void setFixedPosition(bool isFixed);
132 
137  bool isInputHandler();
138 
145  void setInputHandler(bool inputHandler);
146 
155  void addAnimation(std::string id, Animation* animation);
156 
163  void startAnimation(std::string id);
164 
168  void stopAnimations();
169 
175 
180  const EntityProperties& getEntityProperties() const;
181 
186  const std::weak_ptr<Texture> getTexture() const;
187 
191  void setSharedTexture(std::shared_ptr<Texture> texture);
192 
197  void setTexture(Texture *texture);
198 
204  virtual bool isDead() const;
205 
210  virtual void render(const RenderData*) const;
211 
219  virtual bool onCollision(Entity *collider, const GameData*);
220 
227  virtual bool onVerticalCollision(Entity *collider, const GameData*);
228 
235  virtual bool onHorizontalCollision(Entity *collider, const GameData*);
236 
241  virtual void init(const GameData* gameData) { }
242 
247  virtual void preHandle(const GameData* gameData) { }
248 
253  virtual void handle(const SDL_Event& event) { }
254 
259  virtual void postHandle(const GameData* gameData) { }
260 
265  virtual void preRender(const GameData* gameData) { }
266 
271  virtual void postRender(const GameData* gameData) { }
272 
277  virtual void preMove(const GameData* gameData) { }
278 
283  virtual void postMove(const GameData* gameData) { }
284  };
285 } // namespace flat2d
286 
287 #endif // ENTITY_H_
virtual bool operator<(const Entity &o) const
Definition: Entity.h:95
+Go to the documentation of this file.
1 #ifndef ENTITY_H_
2 #define ENTITY_H_
3 
4 #include <SDL2/SDL.h>
5 #include <string>
6 #include <sstream>
7 #include <memory>
8 #include <map>
9 
10 #include "EntityProperties.h"
11 #include "Animation.h"
12 #include "UID.h"
13 
14 namespace flat2d
15 {
16  class Camera;
17  class RenderData;
18  class GameData;
19  class Texture;
20 
29  class Entity
30  {
31  private:
32  size_t id;
33  bool fixedPosition = false;
34  bool inputHandler = false;
35  SDL_Rect clip;
36  std::shared_ptr<Texture> texture = nullptr;
37 
38  protected:
40 
41  std::string currentAnimation = "";
42  std::map<std::string, Animation*> animations;
43 
44  bool dead = false;
45 
46  public:
47  Entity(int x, int y, int w, int h) :
48  entityProperties(x, y, w, h),
49  dead(false) {
50  id = UID::generate();
51  clip = { 0, 0, w, h };
52  }
53 
54  virtual ~Entity() {
55  for (auto it = animations.begin(); it != animations.end(); it++) {
56  delete it->second;
57  }
58  }
59 
60  /* Operators */
61  virtual bool operator==(const Entity& o) const {
62  return id == o.id;
63  }
64 
65  virtual bool operator!=(const Entity& o) const {
66  return id != o.id;
67  }
68 
69  virtual Entity& operator=(const Entity& o) {
70  // TODO(Linus): This doesn't look right...
71  id = o.id;
72  return *this;
73  }
74 
79  virtual int getId() const {
80  return static_cast<int>(id);
81  }
82 
87  virtual std::string getStringId() const {
88  std::string number;
89  std::stringstream ss;
90  ss << id;
91  ss >> number;
92  return number;
93  }
94 
95  virtual bool operator<(const Entity& o) const {
96  return id < o.id;
97  }
98 
105  virtual int getType() const {
106  return -1;
107  }
108 
113  void setClip(SDL_Rect&);
114 
118  void setDead(bool isDead);
119 
124  bool isFixedPosition();
125 
131  void setFixedPosition(bool isFixed);
132 
137  bool isInputHandler();
138 
145  void setInputHandler(bool inputHandler);
146 
155  void addAnimation(std::string id, Animation* animation);
156 
163  void startAnimation(std::string id);
164 
168  void stopAnimations();
169 
175 
180  const EntityProperties& getEntityProperties() const;
181 
186  const std::weak_ptr<Texture> getTexture() const;
187 
191  void setSharedTexture(std::shared_ptr<Texture> texture);
192 
197  void setTexture(Texture *texture);
198 
204  virtual bool isDead() const;
205 
210  virtual void render(const RenderData*) const;
211 
219  virtual bool onCollision(Entity *collider, const GameData*);
220 
227  virtual bool onVerticalCollision(Entity *collider, const GameData*);
228 
235  virtual bool onHorizontalCollision(Entity *collider, const GameData*);
236 
241  virtual void init(const GameData* gameData) { }
242 
247  virtual void preHandle(const GameData* gameData) { }
248 
253  virtual void handle(const SDL_Event& event) { }
254 
259  virtual void postHandle(const GameData* gameData) { }
260 
265  virtual void preRender(const GameData* gameData) { }
266 
271  virtual void postRender(const GameData* gameData) { }
272 
277  virtual void preMove(const GameData* gameData) { }
278 
283  virtual void postMove(const GameData* gameData) { }
284  };
285 } // namespace flat2d
286 
287 #endif // ENTITY_H_
virtual bool operator<(const Entity &o) const
Definition: Entity.h:95
virtual void postRender(const GameData *gameData)
Definition: Entity.h:271
void setClip(SDL_Rect &)
Definition: Entity.cpp:85
virtual Entity & operator=(const Entity &o)
Definition: Entity.h:69
@@ -123,7 +126,7 @@ diff --git a/docs/_entity_container_8cpp.html b/docs/_entity_container_8cpp.html index 02c89cb..f3d1702 100644 --- a/docs/_entity_container_8cpp.html +++ b/docs/_entity_container_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/EntityContainer.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
EntityContainer.cpp
-Go to the documentation of this file.
1 #include <functional>
2 #include <cassert>
3 #include <string>
4 #include <map>
5 #include <vector>
6 #include <cmath>
7 
8 #include "Entity.h"
9 #include "EntityContainer.h"
10 #include "GameData.h"
11 #include "RenderData.h"
12 #include "EntityProperties.h"
13 #include "DeltatimeMonitor.h"
14 #include "CollisionDetector.h"
15 #include "RuntimeAnalyzer.h"
16 
17 namespace flat2d
18 {
20  {
22  }
23 
24  void EntityContainer::addLayer(unsigned int layer)
25  {
26  Layer newLayer = static_cast<Layer>(layer);
27  if (layeredObjects.find(newLayer) != layeredObjects.end()) {
28  return;
29  }
30  ObjectList list;
31  layeredObjects[newLayer] = list;
32  }
33 
34  std::vector<int> EntityContainer::getLayerKeys() const
35  {
36  std::vector<int> keys;
37  for (auto& it : layeredObjects) {
38  keys.push_back(it.first);
39  }
40  return keys;
41  }
42 
44  {
45  std::string objId = object->getStringId();
46  if (objects.find(objId) != objects.end()) {
47  return;
48  }
49 
50  // Make sure this layer exists
51  if (layeredObjects.find(layer) == layeredObjects.end()) {
52  return;
53  }
54 
55  objects[objId] = object;
56  uninitiatedEntities[objId] = object;
57  layeredObjects[layer][objId] = object;
58  registerObjectToSpatialPartitions(object);
59  if (object->isInputHandler()) {
60  inputHandlers[objId] = object;
61  }
62  if (object->getEntityProperties().isCollidable()) {
63  collidableObjects[objId] = object;
64  }
65  }
66 
68  {
69  collidableObjects.clear();
70  for (auto& it : objects) {
71  if (it.second->getEntityProperties().isCollidable()) {
72  collidableObjects[it.first] = it.second;
73  }
74  }
75  }
76 
77  EntityShape EntityContainer::createBoundingBoxFor(const EntityProperties& props) const
78  {
79  EntityShape vShape = props.getVelocityColliderShape(dtMonitor->getDeltaTime());
80  return {
81  vShape.x - spatialPartitionExpansion,
82  vShape.y - spatialPartitionExpansion,
83  vShape.w + (2 * spatialPartitionExpansion),
84  vShape.h + (2 * spatialPartitionExpansion)
85  };
86  }
87 
88  void EntityContainer::registerObjectToSpatialPartitions(Entity *o)
89  {
90  if (!o->getEntityProperties().isCollidable()) {
91  return;
92  }
93 
95  EntityShape boundingBox = createBoundingBoxFor(props);
96 
97  addObjectToSpatialPartitionFor(o, boundingBox.x, boundingBox.y);
98  addObjectToSpatialPartitionFor(o, boundingBox.x, boundingBox.y + boundingBox.h);
99  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, boundingBox.y);
100  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, boundingBox.y + boundingBox.h);
101 
102  if (boundingBox.w > static_cast<int>(spatialPartitionDimension)) {
103  for (int i = boundingBox.x + spatialPartitionDimension;
104  i < boundingBox.x + boundingBox.w;
105  i += spatialPartitionDimension)
106  {
107  addObjectToSpatialPartitionFor(o, i, boundingBox.y);
108  addObjectToSpatialPartitionFor(o, i, boundingBox.y + boundingBox.h);
109  }
110  }
111 
112  if (boundingBox.h > static_cast<int>(spatialPartitionDimension)) {
113  for (int i = boundingBox.y + spatialPartitionDimension;
114  i < boundingBox.h + boundingBox.h;
115  i += spatialPartitionDimension)
116  {
117  addObjectToSpatialPartitionFor(o, boundingBox.x, i);
118  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, i);
119  }
120  }
121 
122  props.setLocationChanged(false);
123  }
124 
125  void EntityContainer::clearObjectFromCurrentPartitions(Entity *o)
126  {
128  for (auto it = currentAreas.begin(); it != currentAreas.end(); it++) {
129  spatialPartitionMap[*it].erase(o->getStringId());
130  }
131 
132  currentAreas.clear();
133  }
134 
135  void EntityContainer::clearObjectFromUnattachedPartitions(Entity *o)
136  {
137  // TODO(Linus): This is bugging out. Fix later for optimization.
139  EntityShape bounder = createBoundingBoxFor(o->getEntityProperties());
140 
141  std::vector<int> indexes;
142  int index = 0;
143  for (auto it = currentAreas.begin(); it != currentAreas.end(); it++) {
144  if (!it->containsPoint(bounder.x, bounder.y)
145  && !it->containsPoint(bounder.x, bounder.y + bounder.h)
146  && !it->containsPoint(bounder.x + bounder.w, bounder.y)
147  && !it->containsPoint(bounder.x + bounder.w, bounder.y + bounder.h))
148  {
149  spatialPartitionMap[*it].erase(o->getStringId());
150  indexes.push_back(index++);
151  }
152  }
153 
154  for (auto it = indexes.begin(); it != indexes.end(); it++) {
155  currentAreas.erase(currentAreas.begin() + *it);
156  }
157  }
158 
159  void EntityContainer::addObjectToSpatialPartitionFor(Entity* o, int x, int y)
160  {
161  std::string objId = o->getStringId();
162 
163  unsigned int xcord = (x - (x % spatialPartitionDimension));
164  unsigned int ycord = (y - (y % spatialPartitionDimension));
165 
166  // Find and make sure partition exists
167  MapArea area(xcord, ycord, spatialPartitionDimension);
168  if (spatialPartitionMap.find(area) == spatialPartitionMap.end()) {
169  spatialPartitionMap[area] = ObjectList();
170  }
171 
172  // The object is already in this partition
173  if (spatialPartitionMap[area].find(objId) != spatialPartitionMap[area].end()) {
174  return;
175  }
176 
177  EntityProperties& objEntityProperties = o->getEntityProperties();
178  objEntityProperties.getCurrentAreas().push_back(area);
179 
180  spatialPartitionMap[area][objId] = o;
181  }
182 
184  {
185  spatialPartitionDimension = i;
186  }
187 
189  {
190  std::string objId = object->getStringId();
191  objects.erase(objId);
192  inputHandlers.erase(objId);
193  uninitiatedEntities.erase(objId);
194  if (object->getEntityProperties().isCollidable()) {
195  collidableObjects.erase(objId);
196  }
197 
198  clearObjectFromCurrentPartitions(object);
199 
200  for (auto it = layeredObjects.begin(); it != layeredObjects.end(); it++) {
201  it->second.erase(objId);
202  }
203  }
204 
205  void EntityContainer::reinitLayerMap()
206  {
207  layeredObjects.clear();
208  ObjectList list;
209  layeredObjects[-1] = list;
210  }
211 
212  bool EntityContainer::isUninitiated(const std::string& id) const
213  {
214  return uninitiatedEntities.find(id) != uninitiatedEntities.end();
215  }
216 
218  {
219  for(auto it = objects.begin(); it != objects.end(); it++) {
220  delete it->second;
221  }
222  uninitiatedEntities.clear();
223  objects.clear();
224  collidableObjects.clear();
225  spatialPartitionMap.clear();
226  inputHandlers.clear();
227  reinitLayerMap();
228  for (auto it = renderAreas.begin(); it != renderAreas.end(); it++) {
229  delete it->second;
230  }
231  renderAreas.clear();
232  }
233 
235  {
236  if (layeredObjects.find(layer) == layeredObjects.end()) {
237  return;
238  }
239 
240  for (auto it = layeredObjects[layer].begin(); it != layeredObjects[layer].end(); it++) {
241  std::string objId = it->second->getStringId();
242  objects.erase(objId);
243  inputHandlers.erase(objId);
244  uninitiatedEntities.erase(objId);
245  if (it->second->getEntityProperties().isCollidable()) {
246  collidableObjects.erase(objId);
247  }
248  delete it->second;
249  }
250 
251  layeredObjects[layer].clear();
252  }
253 
255  {
256 #ifdef FPS_DBG
258 #endif
259  for (auto it = uninitiatedEntities.begin(); it != uninitiatedEntities.end(); it++) {
260  it->second->init(gameData);
261  }
262  uninitiatedEntities.clear();
263  }
264 
265  void EntityContainer::handleObjects(const SDL_Event& event, const GameData* gameData)
266  {
267 #ifdef FPS_DBG
269 #endif
270  for (auto it = inputHandlers.begin(); it != inputHandlers.end(); it++) {
271  if (isUninitiated(it->first)) {
272  continue;
273  }
274  it->second->preHandle(gameData);
275  it->second->handle(event);
276  it->second->postHandle(gameData);
277  }
278  }
279 
280  void EntityContainer::renderObjects(const GameData* data) const
281  {
282 #ifdef FPS_DBG
284 #endif
285  for (auto it1 = layeredObjects.begin(); it1 != layeredObjects.end(); it1++) {
286  for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
287  if (isUninitiated(it2->first)) {
288  continue;
289  }
290  it2->second->preRender(data);
291  if (isInRenderArea(it2->second, data)) {
292  it2->second->render(data->getRenderData());
293  }
294  it2->second->postRender(data);
295  }
296  }
297  }
298 
299  bool EntityContainer::isInRenderArea(Entity *e, const GameData* data) const
300  {
301  if (renderAreas.empty()) {
302  return true;
303  }
304 
305  for (const auto& it : renderAreas) {
306  if (data->getCollisionDetector()->AABB(it.second->asEntityShape(),
308  return true;
309  }
310  }
311  }
312 
314  {
315 #ifdef FPS_DBG
317 #endif
318  float deltatime = dtMonitor->getDeltaTime();
319  CollisionDetector *coldetector = data->getCollisionDetector();
320 
321  for (auto& object : objects) {
322  if (isUninitiated(object.first)) {
323  continue;
324  }
325  object.second->preMove(data);
326  handlePossibleObjectMovement(object.second);
327 
328  EntityProperties& props = object.second->getEntityProperties();
329  if (props.isMoving()) {
330  if (props.isCollidable()) {
331  coldetector->handlePossibleCollisionsFor(object.second, data);
332  }
333  props.move(deltatime);
334  handlePossibleObjectMovement(object.second);
335  }
336 
337  object.second->postMove(data);
338  handlePossibleObjectMovement(object.second);
339  }
340 
341  clearDeadObjects();
342  }
343 
344  void EntityContainer::handlePossibleObjectMovement(Entity* entity)
345  {
346  // TODO(Linus): Maybe this should be replaced by the previous callback function that was
347  // injected into the objects entity properties???
348  // Multiple calls to this function seems wasteful although it filters nicely with the
349  // hasLocationChanged flag.
350  if (entity->getEntityProperties().hasLocationChanged()) {
351  clearObjectFromCurrentPartitions(entity);
352  registerObjectToSpatialPartitions(entity);
353  entity->getEntityProperties().setLocationChanged(false);
354  }
355  }
356 
358  {
359  return objects.size();
360  }
361 
363  {
364  if (layeredObjects.find(layer) == layeredObjects.end()) {
365  return 0;
366  }
367 
368  return layeredObjects[layer].size();
369  }
370 
372  {
373  return collidableObjects.size();
374  }
375 
376  void EntityContainer::clearDeadObjects()
377  {
378  std::vector<std::string> objectsToErase;
379  for (auto it = objects.begin(); it != objects.end(); it++) {
380  if (!it->second->isDead()) {
381  continue;
382  }
383  std::string objId = it->first;
384  collidableObjects.erase(objId);
385  for (auto layerIt = layeredObjects.begin(); layerIt != layeredObjects.end(); layerIt++) {
386  layerIt->second.erase(objId);
387  }
388  clearObjectFromCurrentPartitions(it->second);
389  objectsToErase.push_back(objId);
390  delete it->second;
391  }
392 
393  for (auto it = objectsToErase.begin(); it != objectsToErase.end(); it++) {
394  objects.erase(*it);
395  }
396  }
397 
399  {
400  return spatialPartitionMap.size();
401  }
402 
403  void EntityContainer::iterateAllMovingObjects(EntityIter func) const
404  {
405  for (auto it = objects.begin(); it != objects.end(); it++) {
406  if (it->second->getEntityProperties().isMoving()) {
407  func(it->second);
408  }
409  }
410  }
411 
412  void EntityContainer::iterateCollidablesFor(const Entity* source, EntityIter func)
413  {
414  const EntityProperties::Areas& currentAreas = source->getEntityProperties().getCurrentAreas();
415  std::map<float, Entity*> sortedMap;
416  EntityShape colliderShape = source->getEntityProperties().getColliderShape();
417  float sx = colliderShape.x + (colliderShape.w / 2);
418  float sy = colliderShape.y + (colliderShape.h / 2);
419 
420  // Itterate the objects and sort them according to distance
421  for (auto& area : currentAreas) {
422  for (auto& object : spatialPartitionMap[area]) {
423  if (!object.second->getEntityProperties().isCollidable()) {
424  continue;
425  }
426  if (*source == *object.second) {
427  continue;
428  }
429 
430  const EntityShape& targetShape = object.second->getEntityProperties().getColliderShape();
431  float tx = targetShape.x + (targetShape.w/2);
432  float ty = targetShape.y + (targetShape.h/2);
433 
434  float distance;
435  if (sx == tx) {
436  distance = std::abs(sy - ty);
437  } else if (sy == ty) {
438  distance = std::abs(sx - tx);
439  } else {
440  distance = sqrt(pow(sx - tx, 2) + pow(sy - ty, 2));
441  }
442 
443  if (sortedMap.find(distance) != sortedMap.end()) {
444  if (*sortedMap[distance] == *object.second) {
445  continue;
446  }
447  }
448 
449  while (sortedMap.find(distance) != sortedMap.end()) {
450  distance += 0.00001f;
451  }
452 
453  sortedMap[distance] = object.second;
454  }
455  }
456 
457  // Itterate the sorted objects and call the cb function
458  for (auto objectIter = sortedMap.begin(); objectIter != sortedMap.end(); objectIter++) {
459  func(objectIter->second);
460  }
461  }
462 
463  Entity* EntityContainer::checkAllObjects(EntityProcessor func) const
464  {
465  for (auto it = objects.begin(); it != objects.end(); it++) {
466  if (func(it->second)) {
467  return it->second;
468  }
469  }
470  return nullptr;
471  }
472 
474  {
475  for (auto it = collidableObjects.begin(); it != collidableObjects.end(); it++) {
476  if (func(it->second)) {
477  return it->second;
478  }
479  }
480  return nullptr;
481  }
482 
483  Entity* EntityContainer::checkCollidablesFor(const Entity* source, EntityProcessor func)
484  {
485  const EntityProperties::Areas& currentAreas = source->getEntityProperties().getCurrentAreas();
486  for (auto areaIter = currentAreas.begin(); areaIter != currentAreas.end(); areaIter++) {
487  for (auto objectIter = spatialPartitionMap[*areaIter].begin();
488  objectIter != spatialPartitionMap[*areaIter].end();
489  objectIter++)
490  {
491  if (objectIter->second->getEntityProperties().isCollidable() && func(objectIter->second)) {
492  return objectIter->second;
493  }
494  }
495  }
496  return nullptr;
497  }
498 
499  void EntityContainer::iterateCollidablesIn(Layer layer, EntityIter func)
500  {
501  if (layeredObjects.find(layer) == layeredObjects.end()) {
502  return;
503  }
504 
505  for (auto it = layeredObjects[layer].begin(); it != layeredObjects[layer].end(); it++) {
506  if (it->second->getEntityProperties().isCollidable()) {
507  func(it->second);
508  }
509  }
510  }
511 
512  void EntityContainer::addRenderArea(const std::string& key, MapArea *area)
513  {
514  renderAreas[key] = area;
515  }
516 
517  MapArea* EntityContainer::getRenderArea(const std::string& key)
518  {
519  return renderAreas[key];
520  }
521 
522  void EntityContainer::removeRenderArea(const std::string& key)
523  {
524  MapArea *area = renderAreas[key];
525  if (area != nullptr) {
526  delete area;
527  }
528  }
529 } // namespace flat2d
void registerObject(Entity *, Layer=DEFAULT_LAYER)
+Go to the documentation of this file.
1 #include <functional>
2 #include <cassert>
3 #include <string>
4 #include <map>
5 #include <vector>
6 #include <cmath>
7 
8 #include "Entity.h"
9 #include "EntityContainer.h"
10 #include "GameData.h"
11 #include "RenderData.h"
12 #include "EntityProperties.h"
13 #include "DeltatimeMonitor.h"
14 #include "CollisionDetector.h"
15 #include "RuntimeAnalyzer.h"
16 
17 namespace flat2d
18 {
20  {
22  }
23 
24  void EntityContainer::addLayer(unsigned int layer)
25  {
26  Layer newLayer = static_cast<Layer>(layer);
27  if (layeredObjects.find(newLayer) != layeredObjects.end()) {
28  return;
29  }
30  ObjectList list;
31  layeredObjects[newLayer] = list;
32  }
33 
34  std::vector<int> EntityContainer::getLayerKeys() const
35  {
36  std::vector<int> keys;
37  for (auto& it : layeredObjects) {
38  keys.push_back(it.first);
39  }
40  return keys;
41  }
42 
44  {
45  std::string objId = object->getStringId();
46  if (objects.find(objId) != objects.end()) {
47  return;
48  }
49 
50  // Make sure this layer exists
51  if (layeredObjects.find(layer) == layeredObjects.end()) {
52  return;
53  }
54 
55  objects[objId] = object;
56  uninitiatedEntities[objId] = object;
57  layeredObjects[layer][objId] = object;
58  registerObjectToSpatialPartitions(object);
59  if (object->isInputHandler()) {
60  inputHandlers[objId] = object;
61  }
62  if (object->getEntityProperties().isCollidable()) {
63  collidableObjects[objId] = object;
64  }
65  }
66 
68  {
69  collidableObjects.clear();
70  for (auto& it : objects) {
71  if (it.second->getEntityProperties().isCollidable()) {
72  collidableObjects[it.first] = it.second;
73  }
74  }
75  }
76 
77  EntityShape EntityContainer::createBoundingBoxFor(const EntityProperties& props) const
78  {
79  EntityShape vShape = props.getVelocityColliderShape(dtMonitor->getDeltaTime());
80  return {
81  vShape.x - spatialPartitionExpansion,
82  vShape.y - spatialPartitionExpansion,
83  vShape.w + (2 * spatialPartitionExpansion),
84  vShape.h + (2 * spatialPartitionExpansion)
85  };
86  }
87 
88  void EntityContainer::registerObjectToSpatialPartitions(Entity *o)
89  {
90  if (!o->getEntityProperties().isCollidable()) {
91  return;
92  }
93 
94  EntityProperties& props = o->getEntityProperties();
95  EntityShape boundingBox = createBoundingBoxFor(props);
96 
97  addObjectToSpatialPartitionFor(o, boundingBox.x, boundingBox.y);
98  addObjectToSpatialPartitionFor(o, boundingBox.x, boundingBox.y + boundingBox.h);
99  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, boundingBox.y);
100  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, boundingBox.y + boundingBox.h);
101 
102  if (boundingBox.w > static_cast<int>(spatialPartitionDimension)) {
103  for (int i = boundingBox.x + spatialPartitionDimension;
104  i < boundingBox.x + boundingBox.w;
105  i += spatialPartitionDimension)
106  {
107  addObjectToSpatialPartitionFor(o, i, boundingBox.y);
108  addObjectToSpatialPartitionFor(o, i, boundingBox.y + boundingBox.h);
109  }
110  }
111 
112  if (boundingBox.h > static_cast<int>(spatialPartitionDimension)) {
113  for (int i = boundingBox.y + spatialPartitionDimension;
114  i < boundingBox.h + boundingBox.h;
115  i += spatialPartitionDimension)
116  {
117  addObjectToSpatialPartitionFor(o, boundingBox.x, i);
118  addObjectToSpatialPartitionFor(o, boundingBox.x + boundingBox.w, i);
119  }
120  }
121 
122  props.setLocationChanged(false);
123  }
124 
125  void EntityContainer::clearObjectFromCurrentPartitions(Entity *o)
126  {
127  EntityProperties::Areas& currentAreas = o->getEntityProperties().getCurrentAreas();
128  for (auto it = currentAreas.begin(); it != currentAreas.end(); it++) {
129  spatialPartitionMap[*it].erase(o->getStringId());
130  }
131 
132  currentAreas.clear();
133  }
134 
135  void EntityContainer::clearObjectFromUnattachedPartitions(Entity *o)
136  {
137  // TODO(Linus): This is bugging out. Fix later for optimization.
138  EntityProperties::Areas& currentAreas = o->getEntityProperties().getCurrentAreas();
139  EntityShape bounder = createBoundingBoxFor(o->getEntityProperties());
140 
141  std::vector<int> indexes;
142  int index = 0;
143  for (auto it = currentAreas.begin(); it != currentAreas.end(); it++) {
144  if (!it->containsPoint(bounder.x, bounder.y)
145  && !it->containsPoint(bounder.x, bounder.y + bounder.h)
146  && !it->containsPoint(bounder.x + bounder.w, bounder.y)
147  && !it->containsPoint(bounder.x + bounder.w, bounder.y + bounder.h))
148  {
149  spatialPartitionMap[*it].erase(o->getStringId());
150  indexes.push_back(index++);
151  }
152  }
153 
154  for (auto it = indexes.begin(); it != indexes.end(); it++) {
155  currentAreas.erase(currentAreas.begin() + *it);
156  }
157  }
158 
159  void EntityContainer::addObjectToSpatialPartitionFor(Entity* o, int x, int y)
160  {
161  std::string objId = o->getStringId();
162 
163  unsigned int xcord = (x - (x % spatialPartitionDimension));
164  unsigned int ycord = (y - (y % spatialPartitionDimension));
165 
166  // Find and make sure partition exists
167  MapArea area(xcord, ycord, spatialPartitionDimension);
168  if (spatialPartitionMap.find(area) == spatialPartitionMap.end()) {
169  spatialPartitionMap[area] = ObjectList();
170  }
171 
172  // The object is already in this partition
173  if (spatialPartitionMap[area].find(objId) != spatialPartitionMap[area].end()) {
174  return;
175  }
176 
177  EntityProperties& objEntityProperties = o->getEntityProperties();
178  objEntityProperties.getCurrentAreas().push_back(area);
179 
180  spatialPartitionMap[area][objId] = o;
181  }
182 
184  {
185  spatialPartitionDimension = i;
186  }
187 
189  {
190  std::string objId = object->getStringId();
191  objects.erase(objId);
192  inputHandlers.erase(objId);
193  uninitiatedEntities.erase(objId);
194  if (object->getEntityProperties().isCollidable()) {
195  collidableObjects.erase(objId);
196  }
197 
198  clearObjectFromCurrentPartitions(object);
199 
200  for (auto it = layeredObjects.begin(); it != layeredObjects.end(); it++) {
201  it->second.erase(objId);
202  }
203  }
204 
205  void EntityContainer::reinitLayerMap()
206  {
207  layeredObjects.clear();
208  ObjectList list;
209  layeredObjects[-1] = list;
210  }
211 
212  bool EntityContainer::isUninitiated(const std::string& id) const
213  {
214  return uninitiatedEntities.find(id) != uninitiatedEntities.end();
215  }
216 
218  {
219  for(auto it = objects.begin(); it != objects.end(); it++) {
220  delete it->second;
221  }
222  uninitiatedEntities.clear();
223  objects.clear();
224  collidableObjects.clear();
225  spatialPartitionMap.clear();
226  inputHandlers.clear();
227  reinitLayerMap();
228  for (auto it = renderAreas.begin(); it != renderAreas.end(); it++) {
229  delete it->second;
230  }
231  renderAreas.clear();
232  }
233 
235  {
236  if (layeredObjects.find(layer) == layeredObjects.end()) {
237  return;
238  }
239 
240  for (auto it = layeredObjects[layer].begin(); it != layeredObjects[layer].end(); it++) {
241  std::string objId = it->second->getStringId();
242  objects.erase(objId);
243  inputHandlers.erase(objId);
244  uninitiatedEntities.erase(objId);
245  if (it->second->getEntityProperties().isCollidable()) {
246  collidableObjects.erase(objId);
247  }
248  delete it->second;
249  }
250 
251  layeredObjects[layer].clear();
252  }
253 
255  {
256 #ifdef FPS_DBG
258 #endif
259  for (auto it = uninitiatedEntities.begin(); it != uninitiatedEntities.end(); it++) {
260  it->second->init(gameData);
261  }
262  uninitiatedEntities.clear();
263  }
264 
265  void EntityContainer::handleObjects(const SDL_Event& event, const GameData* gameData)
266  {
267 #ifdef FPS_DBG
269 #endif
270  for (auto it = inputHandlers.begin(); it != inputHandlers.end(); it++) {
271  if (isUninitiated(it->first)) {
272  continue;
273  }
274  it->second->preHandle(gameData);
275  it->second->handle(event);
276  it->second->postHandle(gameData);
277  }
278  }
279 
280  void EntityContainer::renderObjects(const GameData* data) const
281  {
282 #ifdef FPS_DBG
284 #endif
285  for (auto it1 = layeredObjects.begin(); it1 != layeredObjects.end(); it1++) {
286  for (auto it2 = it1->second.begin(); it2 != it1->second.end(); it2++) {
287  if (isUninitiated(it2->first)) {
288  continue;
289  }
290  it2->second->preRender(data);
291  if (isInRenderArea(it2->second, data)) {
292  it2->second->render(data->getRenderData());
293  }
294  it2->second->postRender(data);
295  }
296  }
297  }
298 
299  bool EntityContainer::isInRenderArea(Entity *e, const GameData* data) const
300  {
301  if (renderAreas.empty()) {
302  return true;
303  }
304 
305  for (const auto& it : renderAreas) {
306  if (data->getCollisionDetector()->AABB(it.second->asEntityShape(),
308  return true;
309  }
310  }
311  }
312 
314  {
315 #ifdef FPS_DBG
317 #endif
318  float deltatime = dtMonitor->getDeltaTime();
319  CollisionDetector *coldetector = data->getCollisionDetector();
320 
321  for (auto& object : objects) {
322  if (isUninitiated(object.first)) {
323  continue;
324  }
325  object.second->preMove(data);
326  handlePossibleObjectMovement(object.second);
327 
328  EntityProperties& props = object.second->getEntityProperties();
329  if (props.isMoving()) {
330  if (props.isCollidable()) {
331  coldetector->handlePossibleCollisionsFor(object.second, data);
332  }
333  props.move(deltatime);
334  handlePossibleObjectMovement(object.second);
335  }
336 
337  object.second->postMove(data);
338  handlePossibleObjectMovement(object.second);
339  }
340 
341  clearDeadObjects();
342  }
343 
344  void EntityContainer::handlePossibleObjectMovement(Entity* entity)
345  {
346  // TODO(Linus): Maybe this should be replaced by the previous callback function that was
347  // injected into the objects entity properties???
348  // Multiple calls to this function seems wasteful although it filters nicely with the
349  // hasLocationChanged flag.
350  if (entity->getEntityProperties().hasLocationChanged()) {
351  clearObjectFromCurrentPartitions(entity);
352  registerObjectToSpatialPartitions(entity);
353  entity->getEntityProperties().setLocationChanged(false);
354  }
355  }
356 
358  {
359  return objects.size();
360  }
361 
363  {
364  if (layeredObjects.find(layer) == layeredObjects.end()) {
365  return 0;
366  }
367 
368  return layeredObjects[layer].size();
369  }
370 
372  {
373  return collidableObjects.size();
374  }
375 
376  void EntityContainer::clearDeadObjects()
377  {
378  std::vector<std::string> objectsToErase;
379  for (auto it = objects.begin(); it != objects.end(); it++) {
380  if (!it->second->isDead()) {
381  continue;
382  }
383  std::string objId = it->first;
384  collidableObjects.erase(objId);
385  for (auto layerIt = layeredObjects.begin(); layerIt != layeredObjects.end(); layerIt++) {
386  layerIt->second.erase(objId);
387  }
388  clearObjectFromCurrentPartitions(it->second);
389  objectsToErase.push_back(objId);
390  delete it->second;
391  }
392 
393  for (auto it = objectsToErase.begin(); it != objectsToErase.end(); it++) {
394  objects.erase(*it);
395  }
396  }
397 
399  {
400  return spatialPartitionMap.size();
401  }
402 
403  void EntityContainer::iterateAllMovingObjects(EntityIter func) const
404  {
405  for (auto it = objects.begin(); it != objects.end(); it++) {
406  if (it->second->getEntityProperties().isMoving()) {
407  func(it->second);
408  }
409  }
410  }
411 
412  void EntityContainer::iterateCollidablesFor(const Entity* source, EntityIter func)
413  {
414  const EntityProperties::Areas& currentAreas = source->getEntityProperties().getCurrentAreas();
415  std::map<float, Entity*> sortedMap;
416  EntityShape colliderShape = source->getEntityProperties().getColliderShape();
417  float sx = colliderShape.x + (colliderShape.w / 2);
418  float sy = colliderShape.y + (colliderShape.h / 2);
419 
420  // Itterate the objects and sort them according to distance
421  for (auto& area : currentAreas) {
422  for (auto& object : spatialPartitionMap[area]) {
423  if (!object.second->getEntityProperties().isCollidable()) {
424  continue;
425  }
426  if (*source == *object.second) {
427  continue;
428  }
429 
430  const EntityShape& targetShape = object.second->getEntityProperties().getColliderShape();
431  float tx = targetShape.x + (targetShape.w/2);
432  float ty = targetShape.y + (targetShape.h/2);
433 
434  float distance;
435  if (sx == tx) {
436  distance = std::abs(sy - ty);
437  } else if (sy == ty) {
438  distance = std::abs(sx - tx);
439  } else {
440  distance = sqrt(pow(sx - tx, 2) + pow(sy - ty, 2));
441  }
442 
443  if (sortedMap.find(distance) != sortedMap.end()) {
444  if (*sortedMap[distance] == *object.second) {
445  continue;
446  }
447  }
448 
449  while (sortedMap.find(distance) != sortedMap.end()) {
450  distance += 0.00001f;
451  }
452 
453  sortedMap[distance] = object.second;
454  }
455  }
456 
457  // Itterate the sorted objects and call the cb function
458  for (auto objectIter = sortedMap.begin(); objectIter != sortedMap.end(); objectIter++) {
459  func(objectIter->second);
460  }
461  }
462 
463  Entity* EntityContainer::checkAllObjects(EntityProcessor func) const
464  {
465  for (auto it = objects.begin(); it != objects.end(); it++) {
466  if (func(it->second)) {
467  return it->second;
468  }
469  }
470  return nullptr;
471  }
472 
474  {
475  for (auto it = collidableObjects.begin(); it != collidableObjects.end(); it++) {
476  if (func(it->second)) {
477  return it->second;
478  }
479  }
480  return nullptr;
481  }
482 
483  Entity* EntityContainer::checkCollidablesFor(const Entity* source, EntityProcessor func)
484  {
485  const EntityProperties::Areas& currentAreas = source->getEntityProperties().getCurrentAreas();
486  for (auto areaIter = currentAreas.begin(); areaIter != currentAreas.end(); areaIter++) {
487  for (auto objectIter = spatialPartitionMap[*areaIter].begin();
488  objectIter != spatialPartitionMap[*areaIter].end();
489  objectIter++)
490  {
491  if (objectIter->second->getEntityProperties().isCollidable() && func(objectIter->second)) {
492  return objectIter->second;
493  }
494  }
495  }
496  return nullptr;
497  }
498 
499  void EntityContainer::iterateCollidablesIn(Layer layer, EntityIter func)
500  {
501  if (layeredObjects.find(layer) == layeredObjects.end()) {
502  return;
503  }
504 
505  for (auto it = layeredObjects[layer].begin(); it != layeredObjects[layer].end(); it++) {
506  if (it->second->getEntityProperties().isCollidable()) {
507  func(it->second);
508  }
509  }
510  }
511 
512  void EntityContainer::addRenderArea(const std::string& key, MapArea *area)
513  {
514  renderAreas[key] = area;
515  }
516 
517  MapArea* EntityContainer::getRenderArea(const std::string& key)
518  {
519  return renderAreas[key];
520  }
521 
522  void EntityContainer::removeRenderArea(const std::string& key)
523  {
524  MapArea *area = renderAreas[key];
525  if (area != nullptr) {
526  delete area;
527  }
528  }
529 } // namespace flat2d
void registerObject(Entity *, Layer=DEFAULT_LAYER)
void iterateCollidablesFor(const Entity *, EntityIter)
@@ -110,7 +113,6 @@ -
virtual std::string getStringId() const
Definition: Entity.h:87
std::vector< MapArea > Areas
@@ -138,7 +140,7 @@ diff --git a/docs/_entity_container_8h.html b/docs/_entity_container_8h.html index cc3b776..870bd1f 100644 --- a/docs/_entity_container_8h.html +++ b/docs/_entity_container_8h.html @@ -3,7 +3,7 @@ - + flat: src/EntityContainer.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
EntityContainer.h
-Go to the documentation of this file.
1 #ifndef ENTITYCONTAINER_H_
2 #define ENTITYCONTAINER_H_
3 
4 #include <SDL2/SDL.h>
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 #include <string>
9 #include <functional>
10 
11 #include "MapArea.h"
12 #include "EntityShape.h"
13 
14 namespace flat2d
15 {
16  // Forward declarations
17  class Entity;
18  class GameData;
19  class RenderData;
20  class DeltatimeMonitor;
22 
23  typedef int Layer;
24  typedef std::map<std::string, Entity*> ObjectList;
25  typedef std::map<Layer, ObjectList> LayerMap;
26  typedef std::map<MapArea, ObjectList> SpatialPartitionMap;
27  typedef std::map<std::string, MapArea*> RenderAreas;
28 
40  {
41  private:
42  // TODO(Linus): Maybe make this editable in the future?
43  const int spatialPartitionExpansion = 0;
44  unsigned int spatialPartitionDimension = 100;
45 
46  DeltatimeMonitor *dtMonitor = nullptr;
47 
48  ObjectList objects;
49  ObjectList collidableObjects;
50  ObjectList inputHandlers;
51  LayerMap layeredObjects;
52  SpatialPartitionMap spatialPartitionMap;
53  ObjectList uninitiatedEntities;
54  RenderAreas renderAreas;
55 
56  typedef std::function<bool (Entity*)> EntityProcessor;
57  typedef std::function<void (Entity*)> EntityIter;
58 
59  private:
60  EntityContainer(const EntityContainer&); // Don't implement
61  void operator=(const EntityContainer&); // Don't implement
62 
63  void clearDeadObjects();
64  void registerObjectToSpatialPartitions(Entity *entity);
65  void addObjectToSpatialPartitionFor(Entity *entity, int x, int y);
66  void clearObjectFromCurrentPartitions(Entity *entity);
67  void clearObjectFromUnattachedPartitions(Entity *entity);
68  EntityShape createBoundingBoxFor(const EntityProperties& props) const;
69  void handlePossibleObjectMovement(Entity* entity);
70 
71  void reinitLayerMap();
72  bool isUninitiated(const std::string& id) const;
73  bool isInRenderArea(Entity*, const GameData*) const;
74 
75  public:
76  static const int DEFAULT_LAYER = -1;
77 
78  explicit EntityContainer(DeltatimeMonitor* dtm) : dtMonitor(dtm) {
79  reinitLayerMap();
80  }
81 
83 
93  void addLayer(unsigned int);
94 
99  std::vector<int> getLayerKeys() const;
100 
108  void registerObject(Entity*, Layer = DEFAULT_LAYER);
109 
114  void unregisterObject(Entity *entity);
115 
119  void unregisterAllObjects();
120 
125  void unregisterAllObjectsFor(Layer);
126 
131  size_t getObjectCount() const;
132 
138  size_t getObjectCountFor(Layer);
139 
144  size_t getCollidablesCount() const;
145 
150  size_t getSpatialPartitionCount() const;
151 
156  void repopulateCollidables();
157 
164  void initiateEntities(const GameData *gameData);
165 
171  void handleObjects(const SDL_Event&, const GameData*);
172 
178  void moveObjects(const GameData*);
179 
185  void renderObjects(const GameData*) const;
186 
194  void setSpatialPartitionDimension(unsigned int);
195 
203  void iterateCollidablesIn(Layer, EntityIter);
204 
210  void iterateAllMovingObjects(EntityIter) const;
211 
219  void iterateCollidablesFor(const Entity*, EntityIter);
220 
228  Entity* checkAllCollidableObjects(EntityProcessor) const;
229 
237  Entity* checkAllObjects(EntityProcessor) const;
238 
247  Entity* checkCollidablesFor(const Entity*, EntityProcessor);
248 
255  void addRenderArea(const std::string& key, MapArea*);
256 
262  MapArea* getRenderArea(const std::string& key);
263 
268  void removeRenderArea(const std::string& key);
269  };
270 } // namespace flat2d
271 
272 #endif // ENTITYCONTAINER_H_
void registerObject(Entity *, Layer=DEFAULT_LAYER)
+Go to the documentation of this file.
1 #ifndef ENTITYCONTAINER_H_
2 #define ENTITYCONTAINER_H_
3 
4 #include <SDL2/SDL.h>
5 #include <iostream>
6 #include <map>
7 #include <vector>
8 #include <string>
9 #include <functional>
10 
11 #include "MapArea.h"
12 #include "EntityShape.h"
13 
14 namespace flat2d
15 {
16  // Forward declarations
17  class Entity;
18  class GameData;
19  class RenderData;
20  class DeltatimeMonitor;
22 
23  typedef int Layer;
24  typedef std::map<std::string, Entity*> ObjectList;
25  typedef std::map<Layer, ObjectList> LayerMap;
26  typedef std::map<MapArea, ObjectList> SpatialPartitionMap;
27  typedef std::map<std::string, MapArea*> RenderAreas;
28 
40  {
41  private:
42  // TODO(Linus): Maybe make this editable in the future?
43  const int spatialPartitionExpansion = 0;
44  unsigned int spatialPartitionDimension = 100;
45 
46  DeltatimeMonitor *dtMonitor = nullptr;
47 
48  ObjectList objects;
49  ObjectList collidableObjects;
50  ObjectList inputHandlers;
51  LayerMap layeredObjects;
52  SpatialPartitionMap spatialPartitionMap;
53  ObjectList uninitiatedEntities;
54  RenderAreas renderAreas;
55 
56  typedef std::function<bool (Entity*)> EntityProcessor;
57  typedef std::function<void (Entity*)> EntityIter;
58 
59  private:
60  EntityContainer(const EntityContainer&); // Don't implement
61  void operator=(const EntityContainer&); // Don't implement
62 
63  void clearDeadObjects();
64  void registerObjectToSpatialPartitions(Entity *entity);
65  void addObjectToSpatialPartitionFor(Entity *entity, int x, int y);
66  void clearObjectFromCurrentPartitions(Entity *entity);
67  void clearObjectFromUnattachedPartitions(Entity *entity);
68  EntityShape createBoundingBoxFor(const EntityProperties& props) const;
69  void handlePossibleObjectMovement(Entity* entity);
70 
71  void reinitLayerMap();
72  bool isUninitiated(const std::string& id) const;
73  bool isInRenderArea(Entity*, const GameData*) const;
74 
75  public:
76  static const int DEFAULT_LAYER = -1;
77 
78  explicit EntityContainer(DeltatimeMonitor* dtm) : dtMonitor(dtm) {
79  reinitLayerMap();
80  }
81 
83 
93  void addLayer(unsigned int);
94 
99  std::vector<int> getLayerKeys() const;
100 
109 
114  void unregisterObject(Entity *entity);
115 
119  void unregisterAllObjects();
120 
126 
131  size_t getObjectCount() const;
132 
138  size_t getObjectCountFor(Layer);
139 
144  size_t getCollidablesCount() const;
145 
150  size_t getSpatialPartitionCount() const;
151 
156  void repopulateCollidables();
157 
164  void initiateEntities(const GameData *gameData);
165 
171  void handleObjects(const SDL_Event&, const GameData*);
172 
178  void moveObjects(const GameData*);
179 
185  void renderObjects(const GameData*) const;
186 
194  void setSpatialPartitionDimension(unsigned int);
195 
203  void iterateCollidablesIn(Layer, EntityIter);
204 
210  void iterateAllMovingObjects(EntityIter) const;
211 
219  void iterateCollidablesFor(const Entity*, EntityIter);
220 
228  Entity* checkAllCollidableObjects(EntityProcessor) const;
229 
237  Entity* checkAllObjects(EntityProcessor) const;
238 
247  Entity* checkCollidablesFor(const Entity*, EntityProcessor);
248 
255  void addRenderArea(const std::string& key, MapArea*);
256 
262  MapArea* getRenderArea(const std::string& key);
263 
268  void removeRenderArea(const std::string& key);
269  };
270 } // namespace flat2d
271 
272 #endif // ENTITYCONTAINER_H_
void registerObject(Entity *, Layer=DEFAULT_LAYER)
void iterateCollidablesFor(const Entity *, EntityIter)
std::map< std::string, Entity * > ObjectList
@@ -116,7 +119,7 @@ diff --git a/docs/_entity_properties_8cpp.html b/docs/_entity_properties_8cpp.html index e9bc611..55723df 100644 --- a/docs/_entity_properties_8cpp.html +++ b/docs/_entity_properties_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/EntityProperties.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
EntityProperties.cpp
-Go to the documentation of this file.
1 #include <iostream>
2 #include "EntityProperties.h"
3 
4 
5 namespace flat2d
6 {
8  {
9  this->collisionProperty = prop;
10  }
11 
13  {
14  return collisionProperty;
15  }
16 
18  {
19  this->x += x;
20  if (x != 0) {
21  setLocationChanged(true);
22  }
23  }
24 
26  {
27  x = pos;
28  if (pos != 0) {
29  setLocationChanged(true);
30  }
31  }
32 
34  {
35  this->y += y;
36  if (y != 0) {
37  setLocationChanged(true);
38  }
39  }
40 
42  {
43  y = pos;
44  if (pos != 0) {
45  setLocationChanged(true);
46  }
47  }
48 
50  {
51  xvel = v;
52  if (v != 0) {
53  setLocationChanged(true);
54  }
55  }
56 
58  {
59  return xvel;
60  }
61 
63  {
64  yvel = v;
65  if (v != 0) {
66  setLocationChanged(true);
67  }
68  }
69 
71  {
72  this->z = depth;
73  }
74 
76  {
77  return z;
78  }
79 
81  {
82  return yvel;
83  }
84 
86  {
87  return xvel != 0 || yvel != 0;
88  }
89 
91  {
92  return { x, y, w, h };
93  }
94 
95  void EntityProperties::setCollidable(bool collidable)
96  {
97  this->collidable = collidable;
98  }
99 
101  {
102  return collidable;
103  }
104 
105  void EntityProperties::setVisible(bool visible)
106  {
107  this->visible = visible;
108  }
109 
111  {
112  return visible;
113  }
114 
116  {
117  return {
118  x + colliderShape.x,
119  y + colliderShape.y,
120  colliderShape.w,
121  colliderShape.h
122  };
123  }
124 
125  EntityShape EntityProperties::getCustomVelocityColliderShape(float dx, float dy) const
126  {
127  EntityShape eShape = getColliderShape();
128  EntityShape vShape;
129  if (dx > 0) {
130  vShape.x = eShape.x;
131  vShape.w = eShape.w + dx;
132  } else {
133  vShape.x = eShape.x + dx;
134  vShape.w = eShape.w - dx;
135  }
136  if (dy > 0) {
137  vShape.y = eShape.y;
138  vShape.h = eShape.h + dy;
139  } else {
140  vShape.y = eShape.y + dy;
141  vShape.h = eShape.h - dy;
142  }
143  return vShape;
144  }
145 
147  {
148  return getCustomVelocityColliderShape(xvel * deltatime, yvel * deltatime);
149  }
150 
152  {
153  return getCustomVelocityColliderShape(xvel * deltatime, 0);
154  }
155 
157  {
158  return getCustomVelocityColliderShape(0, yvel * deltatime);
159  }
160 
162  {
163  return colliderShape.x;
164  }
165 
167  {
168  return w - (colliderShape.x + colliderShape.w);
169  }
170 
172  {
173  return colliderShape.y;
174  }
175 
177  {
178  return h - (colliderShape.y + colliderShape.h);
179  }
180 
181 
183  {
184  this->colliderShape = shape;
185  }
186 
187  bool EntityProperties::containsPoint(int px, int py) const
188  {
189  return px >= x
190  && px <= x + w
191  && py >= y
192  && py <= y + h;
193  }
194 
196  {
197  locationChanged = changed;
198  }
199 
201  {
202  return locationChanged;
203  }
204 
206  {
207  return currentAreas;
208  }
209 
211  {
212  return currentAreas;
213  }
214 
215  void EntityProperties::move(float deltatime)
216  {
217  int dx = xvel * deltatime;
218  int dy = yvel * deltatime;
219 
220  incrementXpos(dx);
221  incrementYpos(dy);
222  }
223 } // namespace flat2d
void setCollidable(bool collidable)
+Go to the documentation of this file.
1 #include <iostream>
2 #include "EntityProperties.h"
3 
4 
5 namespace flat2d
6 {
8  {
9  this->collisionProperty = prop;
10  }
11 
13  {
14  return collisionProperty;
15  }
16 
18  {
19  this->x += x;
20  if (x != 0) {
21  setLocationChanged(true);
22  }
23  }
24 
26  {
27  x = pos;
28  if (pos != 0) {
29  setLocationChanged(true);
30  }
31  }
32 
34  {
35  this->y += y;
36  if (y != 0) {
37  setLocationChanged(true);
38  }
39  }
40 
42  {
43  y = pos;
44  if (pos != 0) {
45  setLocationChanged(true);
46  }
47  }
48 
50  {
51  xvel = v;
52  if (v != 0) {
53  setLocationChanged(true);
54  }
55  }
56 
58  {
59  return xvel;
60  }
61 
63  {
64  yvel = v;
65  if (v != 0) {
66  setLocationChanged(true);
67  }
68  }
69 
71  {
72  this->z = depth;
73  }
74 
76  {
77  return z;
78  }
79 
81  {
82  return yvel;
83  }
84 
86  {
87  return xvel != 0 || yvel != 0;
88  }
89 
91  {
92  return { x, y, w, h };
93  }
94 
95  void EntityProperties::setCollidable(bool collidable)
96  {
97  this->collidable = collidable;
98  }
99 
101  {
102  return collidable;
103  }
104 
105  void EntityProperties::setVisible(bool visible)
106  {
107  this->visible = visible;
108  }
109 
111  {
112  return visible;
113  }
114 
116  {
117  return {
118  x + colliderShape.x,
119  y + colliderShape.y,
120  colliderShape.w,
121  colliderShape.h
122  };
123  }
124 
125  EntityShape EntityProperties::getCustomVelocityColliderShape(float dx, float dy) const
126  {
127  EntityShape eShape = getColliderShape();
128  EntityShape vShape;
129  if (dx > 0) {
130  vShape.x = eShape.x;
131  vShape.w = eShape.w + dx;
132  } else {
133  vShape.x = eShape.x + dx;
134  vShape.w = eShape.w - dx;
135  }
136  if (dy > 0) {
137  vShape.y = eShape.y;
138  vShape.h = eShape.h + dy;
139  } else {
140  vShape.y = eShape.y + dy;
141  vShape.h = eShape.h - dy;
142  }
143  return vShape;
144  }
145 
147  {
148  return getCustomVelocityColliderShape(xvel * deltatime, yvel * deltatime);
149  }
150 
152  {
153  return getCustomVelocityColliderShape(xvel * deltatime, 0);
154  }
155 
157  {
158  return getCustomVelocityColliderShape(0, yvel * deltatime);
159  }
160 
162  {
163  return colliderShape.x;
164  }
165 
167  {
168  return w - (colliderShape.x + colliderShape.w);
169  }
170 
172  {
173  return colliderShape.y;
174  }
175 
177  {
178  return h - (colliderShape.y + colliderShape.h);
179  }
180 
181 
183  {
184  this->colliderShape = shape;
185  }
186 
187  bool EntityProperties::containsPoint(int px, int py) const
188  {
189  return px >= x
190  && px <= x + w
191  && py >= y
192  && py <= y + h;
193  }
194 
196  {
197  locationChanged = changed;
198  }
199 
201  {
202  return locationChanged;
203  }
204 
206  {
207  return currentAreas;
208  }
209 
211  {
212  return currentAreas;
213  }
214 
215  void EntityProperties::move(float deltatime)
216  {
217  int dx = xvel * deltatime;
218  int dy = yvel * deltatime;
219 
220  incrementXpos(dx);
221  incrementYpos(dy);
222  }
223 } // namespace flat2d
void setCollidable(bool collidable)
void move(float deltatime)
@@ -118,7 +121,7 @@ diff --git a/docs/_entity_properties_8h.html b/docs/_entity_properties_8h.html index c7ebfc6..5d54f54 100644 --- a/docs/_entity_properties_8h.html +++ b/docs/_entity_properties_8h.html @@ -3,7 +3,7 @@ - + flat: src/EntityProperties.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
EntityProperties.h
-Go to the documentation of this file.
1 #ifndef ENTITYPROPERTIES_H_
2 #define ENTITYPROPERTIES_H_
3 
4 #include <SDL2/SDL.h>
5 #include <vector>
6 #include <functional>
7 
8 #include "Square.h"
9 #include "MapArea.h"
10 #include "EntityShape.h"
11 #include "CollisionProperty.h"
12 
13 namespace flat2d
14 {
15  class EntityContainer;
16 
24  class EntityProperties : public Square
25  {
26  public:
27  typedef std::vector<MapArea> Areas;
28 
29  private:
30  int z = 0;
31 
32  float xvel = 0.0f;
33  float yvel = 0.0f;
34 
35  bool collidable = false;
36  bool locationChanged = false;
37  bool visible = true;
38 
39  CollisionProperty collisionProperty = CollisionProperty::SOLID;
40 
41  EntityShape colliderShape = { 0, 0, 0, 0 };
42  Areas currentAreas;
43 
44  EntityShape getCustomVelocityColliderShape(float dx, float dy) const;
45 
46  public:
54  EntityProperties(int x, int y, int w, int h) : Square(x, y, w, h) {
55  setColliderShape({ 0, 0, w, h });
56  }
57 
64  EntityProperties(int x, int y, int dim) : EntityProperties(x, y, dim, dim) { }
65 
70  SDL_Rect getBoundingBox() const;
71 
78 
83  void setColliderShape(EntityShape shape);
84 
90  EntityShape getVelocityColliderShape(float deltatime) const;
91 
96  EntityShape getXVelocityColliderShape(float deltatime) const;
97 
102  EntityShape getYVelocityColliderShape(float deltatime) const;
103 
108  void setCollidable(bool collidable);
109 
114  bool isCollidable() const;
115 
120  void setVisible(bool visible);
121 
126  bool isVisible() const;
127 
132  void incrementXpos(int x);
133 
138  void setXpos(int pos);
139 
144  void setXvel(float xvel);
145 
150  float getXvel() const;
151 
156  void incrementYpos(int y);
157 
162  void setYpos(int pos);
163 
168  void setYvel(float yvel);
169 
174  float getYvel() const;
175 
180  void setDepth(int depth);
181 
186  int getDepth() const;
187 
194 
200 
205  int getColliderLeftOffset() const;
206 
211  int getColliderRightOffset() const;
212 
217  int getColliderTopOffset() const;
218 
223  int getColliderBottomOffset() const;
224 
229  bool isMoving() const;
230 
235  void move(float deltatime);
236 
241  bool containsPoint(int, int) const;
242 
247  Areas& getCurrentAreas();
248 
253  const Areas& getCurrentAreas() const;
254 
258  void setLocationChanged(bool changed);
259 
264  bool hasLocationChanged() const;
265  };
266 } // namespace flat2d
267 
268 #endif // ENTITYPROPERTIES_H_
void setCollidable(bool collidable)
+Go to the documentation of this file.
1 #ifndef ENTITYPROPERTIES_H_
2 #define ENTITYPROPERTIES_H_
3 
4 #include <SDL2/SDL.h>
5 #include <vector>
6 #include <functional>
7 
8 #include "Square.h"
9 #include "MapArea.h"
10 #include "EntityShape.h"
11 #include "CollisionProperty.h"
12 
13 namespace flat2d
14 {
15  class EntityContainer;
16 
24  class EntityProperties : public Square
25  {
26  public:
27  typedef std::vector<MapArea> Areas;
28 
29  private:
30  int z = 0;
31 
32  float xvel = 0.0f;
33  float yvel = 0.0f;
34 
35  bool collidable = false;
36  bool locationChanged = false;
37  bool visible = true;
38 
39  CollisionProperty collisionProperty = CollisionProperty::SOLID;
40 
41  EntityShape colliderShape = { 0, 0, 0, 0 };
42  Areas currentAreas;
43 
44  EntityShape getCustomVelocityColliderShape(float dx, float dy) const;
45 
46  public:
54  EntityProperties(int x, int y, int w, int h) : Square(x, y, w, h) {
55  setColliderShape({ 0, 0, w, h });
56  }
57 
64  EntityProperties(int x, int y, int dim) : EntityProperties(x, y, dim, dim) { }
65 
70  SDL_Rect getBoundingBox() const;
71 
78 
83  void setColliderShape(EntityShape shape);
84 
90  EntityShape getVelocityColliderShape(float deltatime) const;
91 
96  EntityShape getXVelocityColliderShape(float deltatime) const;
97 
102  EntityShape getYVelocityColliderShape(float deltatime) const;
103 
108  void setCollidable(bool collidable);
109 
114  bool isCollidable() const;
115 
120  void setVisible(bool visible);
121 
126  bool isVisible() const;
127 
132  void incrementXpos(int x);
133 
138  void setXpos(int pos);
139 
144  void setXvel(float xvel);
145 
150  float getXvel() const;
151 
156  void incrementYpos(int y);
157 
162  void setYpos(int pos);
163 
168  void setYvel(float yvel);
169 
174  float getYvel() const;
175 
180  void setDepth(int depth);
181 
186  int getDepth() const;
187 
194 
200 
205  int getColliderLeftOffset() const;
206 
211  int getColliderRightOffset() const;
212 
217  int getColliderTopOffset() const;
218 
223  int getColliderBottomOffset() const;
224 
229  bool isMoving() const;
230 
235  void move(float deltatime);
236 
241  bool containsPoint(int, int) const;
242 
248 
253  const Areas& getCurrentAreas() const;
254 
258  void setLocationChanged(bool changed);
259 
264  bool hasLocationChanged() const;
265  };
266 } // namespace flat2d
267 
268 #endif // ENTITYPROPERTIES_H_
void setCollidable(bool collidable)
void move(float deltatime)
@@ -122,7 +125,7 @@ diff --git a/docs/_entity_shape_8h.html b/docs/_entity_shape_8h.html index 28aae70..dee0d07 100644 --- a/docs/_entity_shape_8h.html +++ b/docs/_entity_shape_8h.html @@ -3,7 +3,7 @@ - + flat: src/EntityShape.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
FlatBuilder.cpp
-Go to the documentation of this file.
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_image.h>
3 #include <SDL2/SDL_ttf.h>
4 #include <string>
5 
6 #include "FlatBuilder.h"
7 #include "GameData.h"
8 #include "RenderData.h"
9 #include "EntityContainer.h"
10 #include "CollisionDetector.h"
11 #include "Camera.h"
12 #include "Window.h"
13 #include "Mixer.h"
14 #include "DeltatimeMonitor.h"
16 #include "GameEngine.h"
17 
18 
19 namespace flat2d
20 {
22  {
23  delete gameEngine;
24  delete renderData;
25  delete gameData;
26  delete collisionDetector;
27  delete entityContainer;
28  delete window;
29  delete camera;
30  delete mixer;
31  delete deltatimeMonitor;
32  delete controllerContainer;
33 
34  IMG_Quit();
35  SDL_Quit();
36  TTF_Quit();
37  Mix_Quit();
38  }
39 
40  bool FlatBuilder::initSDL(std::string title, int screenWidth, int screenHeight)
41  {
42  if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER ) < 0) {
43  std::cerr << "Failed to init video: " << SDL_GetError() << std::endl;
44  return false;
45  }
46 
47  if ( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) {
48  std::cerr << "Warning: Linear texture filtering not enabled" << std::endl;
49  }
50 
51  if (hidpi) {
52  window = new Window(title, screenWidth*2, screenHeight*2);
53  } else {
54  window = new Window(title, screenWidth, screenHeight);
55  }
56  if (!window->init()) {
57  return false;
58  }
59 
60  if (hidpi) {
61  if (SDL_RenderSetLogicalSize(window->getRenderer(), screenWidth, screenHeight)) {
62  std::cerr << "Unable to change render logical size: " << IMG_GetError() << std::endl;
63  return -1;
64  }
65  }
66 
67  int imgFlags = IMG_INIT_PNG;
68  if (!(IMG_Init( imgFlags ) & imgFlags )) {
69  std::cerr << "Unable to initialize SDL_image: " << IMG_GetError() << std::endl;
70  return false;
71  }
72 
73  if (TTF_Init() == -1) {
74  std::cerr << "Unable to initiate SDL2_ttf: " << TTF_GetError() << std::endl;
75  return false;
76  }
77 
78  if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0) {
79  std::cerr << "Could not initialize SDL_Mixer: " << Mix_GetError() << std::endl;
80  return false;
81  }
82 
83  camera = new Camera(screenWidth, screenHeight);
84  return true;
85  }
86 
87  bool FlatBuilder::initContainers()
88  {
89  deltatimeMonitor = new DeltatimeMonitor();
90  entityContainer = new EntityContainer(deltatimeMonitor);
91  collisionDetector = new CollisionDetector(entityContainer, deltatimeMonitor);
92  renderData = new RenderData(window->getRenderer(), camera);
93  mixer = new Mixer();
94  controllerContainer = new GameControllerContainer();
95  gameData = new GameData(entityContainer, collisionDetector, mixer, renderData, deltatimeMonitor);
96  gameEngine = new GameEngine(gameData);
97 
98  return true;
99  }
100 
102  {
103  return gameData;
104  }
105 
107  {
108  return gameEngine;
109  }
110 
111  int FlatBuilder::loadSDL(const std::string& name, int fps, int screenWidth, int screenHeight)
112  {
113  if (!initSDL(name, screenWidth, screenHeight)) {
114  return -1;
115  }
116 
117  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
118  if (!initContainers()) {
119  return -1;
120  }
121 
122  gameEngine->init(fps);
123 
124  return 0;
125  }
126 } // namespace flat2d
+Go to the documentation of this file.
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_image.h>
3 #include <SDL2/SDL_ttf.h>
4 #include <string>
5 
6 #include "FlatBuilder.h"
7 #include "GameData.h"
8 #include "RenderData.h"
9 #include "EntityContainer.h"
10 #include "CollisionDetector.h"
11 #include "Camera.h"
12 #include "Window.h"
13 #include "Mixer.h"
14 #include "DeltatimeMonitor.h"
16 #include "GameEngine.h"
17 
18 
19 namespace flat2d
20 {
22  {
23  delete gameEngine;
24  delete renderData;
25  delete gameData;
26  delete collisionDetector;
27  delete entityContainer;
28  delete window;
29  delete camera;
30  delete mixer;
31  delete deltatimeMonitor;
32  delete controllerContainer;
33 
34  IMG_Quit();
35  SDL_Quit();
36  TTF_Quit();
37  Mix_Quit();
38  }
39 
40  bool FlatBuilder::initSDL(std::string title, int screenWidth, int screenHeight)
41  {
42  if (SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_GAMECONTROLLER ) < 0) {
43  std::cerr << "Failed to init video: " << SDL_GetError() << std::endl;
44  return false;
45  }
46 
47  if ( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) ) {
48  std::cerr << "Warning: Linear texture filtering not enabled" << std::endl;
49  }
50 
51  if (hidpi) {
52  window = new Window(title, screenWidth*2, screenHeight*2);
53  } else {
54  window = new Window(title, screenWidth, screenHeight);
55  }
56  if (!window->init()) {
57  return false;
58  }
59 
60  if (hidpi) {
61  if (SDL_RenderSetLogicalSize(window->getRenderer(), screenWidth, screenHeight)) {
62  std::cerr << "Unable to change render logical size: " << IMG_GetError() << std::endl;
63  return -1;
64  }
65  }
66 
67  int imgFlags = IMG_INIT_PNG;
68  if (!(IMG_Init( imgFlags ) & imgFlags )) {
69  std::cerr << "Unable to initialize SDL_image: " << IMG_GetError() << std::endl;
70  return false;
71  }
72 
73  if (TTF_Init() == -1) {
74  std::cerr << "Unable to initiate SDL2_ttf: " << TTF_GetError() << std::endl;
75  return false;
76  }
77 
78  if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 ) < 0) {
79  std::cerr << "Could not initialize SDL_Mixer: " << Mix_GetError() << std::endl;
80  return false;
81  }
82 
83  camera = new Camera(screenWidth, screenHeight);
84  return true;
85  }
86 
87  bool FlatBuilder::initContainers()
88  {
89  deltatimeMonitor = new DeltatimeMonitor();
90  entityContainer = new EntityContainer(deltatimeMonitor);
91  collisionDetector = new CollisionDetector(entityContainer, deltatimeMonitor);
92  renderData = new RenderData(window->getRenderer(), camera);
93  mixer = new Mixer();
94  controllerContainer = new GameControllerContainer();
95  gameData = new GameData(entityContainer, collisionDetector, mixer, renderData, deltatimeMonitor);
96  gameEngine = new GameEngine(gameData);
97 
98  return true;
99  }
100 
102  {
103  return gameData;
104  }
105 
107  {
108  return gameEngine;
109  }
110 
111  int FlatBuilder::loadSDL(const std::string& name, int fps, int screenWidth, int screenHeight)
112  {
113  if (!initSDL(name, screenWidth, screenHeight)) {
114  return -1;
115  }
116 
117  SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "nearest");
118  if (!initContainers()) {
119  return -1;
120  }
121 
122  gameEngine->init(fps);
123 
124  return 0;
125  }
126 } // namespace flat2d
- -
GameData * getGameData() const
bool init()
Definition: Window.cpp:7
int loadSDL(const std::string &name, int fps, int screenWidth, int screenHeight)
-
void init(int fps)
Definition: GameEngine.cpp:13
@@ -87,22 +87,17 @@ - - - -
SDL_Renderer * getRenderer()
Definition: Window.h:52
-
diff --git a/docs/_flat_builder_8h.html b/docs/_flat_builder_8h.html index 6763f0a..8912f0d 100644 --- a/docs/_flat_builder_8h.html +++ b/docs/_flat_builder_8h.html @@ -3,7 +3,7 @@ - + flat: src/FlatBuilder.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
GameControllerContainer.cpp
-Go to the documentation of this file.
1 #include <iostream>
2 
4 #include "GameController.h"
5 
6 namespace flat2d
7 {
9  {
10  loadControllers();
11  }
12 
14  {
15  for (auto it = controllers.begin(); it != controllers.end(); it++) {
16  delete *it;
17  }
18  }
19 
20  int GameControllerContainer::loadControllers()
21  {
22  int controllerCount = SDL_NumJoysticks();
23  if (controllerCount < 1) {
24  std::cout << "No game controller available" << std::endl;
25  return controllerCount;
26  }
27 
28  for (int i = 0; i < controllerCount; i++) {
29  GameController *controller = new GameController(i);
30 
31  if (controller->getSDLController() != nullptr) {
32  controllers.push_back(controller);
33  } else {
34  delete controller;
35  }
36  }
37 
38  return controllerCount;
39  }
40 
42  {
43  return !controllers.empty();
44  }
45 
47  {
48  return controllers[index];
49  }
50 } // namespace flat2d
+Go to the documentation of this file.
1 #include <iostream>
2 
4 #include "GameController.h"
5 
6 namespace flat2d
7 {
9  {
10  loadControllers();
11  }
12 
14  {
15  for (auto it = controllers.begin(); it != controllers.end(); it++) {
16  delete *it;
17  }
18  }
19 
20  int GameControllerContainer::loadControllers()
21  {
22  int controllerCount = SDL_NumJoysticks();
23  if (controllerCount < 1) {
24  std::cout << "No game controller available" << std::endl;
25  return controllerCount;
26  }
27 
28  for (int i = 0; i < controllerCount; i++) {
29  GameController *controller = new GameController(i);
30 
31  if (controller->getSDLController() != nullptr) {
32  controllers.push_back(controller);
33  } else {
34  delete controller;
35  }
36  }
37 
38  return controllerCount;
39  }
40 
42  {
43  return !controllers.empty();
44  }
45 
47  {
48  return controllers[index];
49  }
50 } // namespace flat2d
-
SDL_GameController * getSDLController()
@@ -82,7 +84,7 @@ diff --git a/docs/_game_controller_container_8h.html b/docs/_game_controller_container_8h.html index 13b0cac..fa82205 100644 --- a/docs/_game_controller_container_8h.html +++ b/docs/_game_controller_container_8h.html @@ -3,7 +3,7 @@ - + flat: src/GameControllerContainer.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
GameEngine.h
-Go to the documentation of this file.
1 #ifndef GAMEENGINE_H_
2 #define GAMEENGINE_H_
3 
4 #include <functional>
5 
6 namespace flat2d
7 {
8  class GameData;
9  class RenderData;
10  class EntityContainer;
11 
19  };
20 
21  typedef std::function<void (const SDL_Event&)> HandleCallback;
22  typedef std::function<GameStateAction (GameData*)> StateCallback;
23 
27  class GameEngine
28  {
29  private:
30  GameData *gameData;
31 
32  int screenTicksPerFrame = 1000/60;
33 
34  GameEngine(const GameEngine&); // Don't implement
35  void operator=(const GameEngine&); // Don't implement
36 
37  public:
42  explicit GameEngine(GameData *data) : gameData(data) { }
43 
49 
54  void init(int fps);
55 
62  void run(StateCallback = nullptr, HandleCallback = nullptr) const;
63  };
64 } // namespace flat2d
65 
66 #endif // GAMEENGINE_H_
+Go to the documentation of this file.
1 #ifndef GAMEENGINE_H_
2 #define GAMEENGINE_H_
3 
4 #include <functional>
5 
6 namespace flat2d
7 {
8  class GameData;
9  class RenderData;
10  class EntityContainer;
11 
19  };
20 
21  typedef std::function<void (const SDL_Event&)> HandleCallback;
22  typedef std::function<GameStateAction (GameData*)> StateCallback;
23 
27  class GameEngine
28  {
29  private:
30  GameData *gameData;
31 
32  int screenTicksPerFrame = 1000/60;
33 
34  GameEngine(const GameEngine&); // Don't implement
35  void operator=(const GameEngine&); // Don't implement
36 
37  public:
42  explicit GameEngine(GameData *data) : gameData(data) { }
43 
49 
54  void init(int fps);
55 
62  void run(StateCallback = nullptr, HandleCallback = nullptr) const;
63  };
64 } // namespace flat2d
65 
66 #endif // GAMEENGINE_H_
@@ -86,7 +89,7 @@ diff --git a/docs/_map_area_8h.html b/docs/_map_area_8h.html index 01afee9..a8d325d 100644 --- a/docs/_map_area_8h.html +++ b/docs/_map_area_8h.html @@ -3,7 +3,7 @@ - + flat: src/MapArea.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/_runtime_analyzer_8h_source.html b/docs/_runtime_analyzer_8h_source.html index 54b1442..ca3c180 100644 --- a/docs/_runtime_analyzer_8h_source.html +++ b/docs/_runtime_analyzer_8h_source.html @@ -3,7 +3,7 @@ - + flat: src/RuntimeAnalyzer.h Source File @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
Square.h
-Go to the documentation of this file.
1 #ifndef SQUARE_H_
2 #define SQUARE_H_
3 
4 namespace flat2d
5 {
10  class Square
11  {
12  protected:
13  int x, y, w, h;
14 
15  public:
16  Square() : x(0), y(0), w(0), h(0) { }
17  Square(int px, int py, int dim) : x(px), y(py), w(dim), h(dim) { }
18  Square(int px, int py, int pw, int ph) : x(px), y(py), w(pw), h(ph) { }
19 
26  bool containsPoint(int, int) const;
27 
28  bool operator<(const Square&) const;
29  bool operator==(const Square&) const;
30  bool operator!=(const Square&) const;
31 
36  int getXpos() const { return x; }
37 
42  int getYpos() const { return y; }
43 
48  int getWidth() const { return w; }
49 
54  int getHeight() const { return h; }
55 
60  virtual void setXpos(int pos) { x = pos; }
61 
66  virtual void setYpos(int pos) { y = pos; }
67 
72  virtual void setWidth(int width) { w = width; }
73 
78  virtual void setHeight(int height) { h = height; }
79  };
80 } // namespace flat2d
81 #endif // SQUARE_H_
int getWidth() const
Definition: Square.h:48
+Go to the documentation of this file.
1 #ifndef SQUARE_H_
2 #define SQUARE_H_
3 
4 namespace flat2d
5 {
10  class Square
11  {
12  protected:
13  int x, y, w, h;
14 
15  public:
16  Square() : x(0), y(0), w(0), h(0) { }
17  Square(int px, int py, int dim) : x(px), y(py), w(dim), h(dim) { }
18  Square(int px, int py, int pw, int ph) : x(px), y(py), w(pw), h(ph) { }
19 
26  bool containsPoint(int, int) const;
27 
28  bool operator<(const Square&) const;
29  bool operator==(const Square&) const;
30  bool operator!=(const Square&) const;
31 
36  int getXpos() const { return x; }
37 
42  int getYpos() const { return y; }
43 
48  int getWidth() const { return w; }
49 
54  int getHeight() const { return h; }
55 
60  virtual void setXpos(int pos) { x = pos; }
61 
66  virtual void setYpos(int pos) { y = pos; }
67 
72  virtual void setWidth(int width) { w = width; }
73 
78  virtual void setHeight(int height) { h = height; }
79  };
80 } // namespace flat2d
81 #endif // SQUARE_H_
int getWidth() const
Definition: Square.h:48
@@ -94,7 +97,7 @@ diff --git a/docs/_texture_8cpp.html b/docs/_texture_8cpp.html index b0c8baf..65ee4d9 100644 --- a/docs/_texture_8cpp.html +++ b/docs/_texture_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/Texture.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
Texture.h
-Go to the documentation of this file.
1 #ifndef TEXTURE_H_
2 #define TEXTURE_H_
3 
4 #include <SDL2/SDL.h>
5 #include <SDL2/SDL_ttf.h>
6 #include <string>
7 #include "Square.h"
8 
9 namespace flat2d
10 {
15  class Texture : public Square
16  {
17  private:
18  SDL_Texture *texture = nullptr;
19  TTF_Font *font = nullptr;
20 
21  void freeTexture();
22  void freeFont();
23 
24  public:
25  Texture() : Square() { }
26  Texture(int x, int y) : Square(x, y, 0, 0) { }
27  explicit Texture(SDL_Texture* t) : Square(x, y, 0, 0), texture(t) { }
28  virtual ~Texture();
29 
36  void render(SDL_Renderer *renderer,
37  const SDL_Rect* clip = nullptr,
38  const SDL_Rect* pos = nullptr
39  ) const;
40 
47  bool loadFromFile(std::string path, SDL_Renderer *renderer);
48 
55  bool loadFont(std::string path, size_t size);
56 
63  bool loadFromRenderedText(std::string text, SDL_Color color, SDL_Renderer *renderer);
64  };
65 } // namespace flat2d
66 
67 #endif // TEXTURE_H_
bool loadFont(std::string path, size_t size)
Definition: Texture.cpp:53
+Go to the documentation of this file.
1 #ifndef TEXTURE_H_
2 #define TEXTURE_H_
3 
4 #include <SDL2/SDL.h>
5 #include <SDL2/SDL_ttf.h>
6 #include <string>
7 #include "Square.h"
8 
9 namespace flat2d
10 {
15  class Texture : public Square
16  {
17  private:
18  SDL_Texture *texture = nullptr;
19  TTF_Font *font = nullptr;
20 
21  void freeTexture();
22  void freeFont();
23 
24  public:
25  Texture() : Square() { }
26  Texture(int x, int y) : Square(x, y, 0, 0) { }
27  explicit Texture(SDL_Texture* t) : Square(x, y, 0, 0), texture(t) { }
28  virtual ~Texture();
29 
36  void render(SDL_Renderer *renderer,
37  const SDL_Rect* clip = nullptr,
38  const SDL_Rect* pos = nullptr
39  ) const;
40 
47  bool loadFromFile(std::string path, SDL_Renderer *renderer);
48 
55  bool loadFont(std::string path, size_t size);
56 
63  bool loadFromRenderedText(std::string text, SDL_Color color, SDL_Renderer *renderer);
64  };
65 } // namespace flat2d
66 
67 #endif // TEXTURE_H_
bool loadFont(std::string path, size_t size)
Definition: Texture.cpp:53
virtual ~Texture()
Definition: Texture.cpp:8
@@ -87,7 +90,7 @@ diff --git a/docs/_timer_8cpp.html b/docs/_timer_8cpp.html index 32e89e9..bf30596 100644 --- a/docs/_timer_8cpp.html +++ b/docs/_timer_8cpp.html @@ -3,7 +3,7 @@ - + flat: src/Timer.cpp File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
@@ -103,7 +106,7 @@ diff --git a/docs/classes.html b/docs/classes.html index 8d07d0d..cc1a7d2 100644 --- a/docs/classes.html +++ b/docs/classes.html @@ -3,7 +3,7 @@ - + flat: Class Index @@ -31,18 +31,21 @@
- + +/* @license-end */ @@ -102,7 +105,7 @@ diff --git a/docs/classflat2d_1_1_algorithm-members.html b/docs/classflat2d_1_1_algorithm-members.html index 6f157a6..f070c27 100644 --- a/docs/classflat2d_1_1_algorithm-members.html +++ b/docs/classflat2d_1_1_algorithm-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@ - + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_animation-members.html b/docs/classflat2d_1_1_animation-members.html index c33a789..3a27748 100644 --- a/docs/classflat2d_1_1_animation-members.html +++ b/docs/classflat2d_1_1_animation-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_camera-members.html b/docs/classflat2d_1_1_camera-members.html index 5c2248e..2c637cf 100644 --- a/docs/classflat2d_1_1_camera-members.html +++ b/docs/classflat2d_1_1_camera-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_collision_detector-members.html b/docs/classflat2d_1_1_collision_detector-members.html index 8ce8f1c..8ed4d65 100644 --- a/docs/classflat2d_1_1_collision_detector-members.html +++ b/docs/classflat2d_1_1_collision_detector-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_deltatime_monitor-members.html b/docs/classflat2d_1_1_deltatime_monitor-members.html index 0ca756b..d3d28aa 100644 --- a/docs/classflat2d_1_1_deltatime_monitor-members.html +++ b/docs/classflat2d_1_1_deltatime_monitor-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_entity-members.html b/docs/classflat2d_1_1_entity-members.html index 8ddf040..8414369 100644 --- a/docs/classflat2d_1_1_entity-members.html +++ b/docs/classflat2d_1_1_entity-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
-

◆ operator<()

+

◆ operator<()

@@ -1360,7 +1363,7 @@

diff --git a/docs/classflat2d_1_1_entity_container-members.html b/docs/classflat2d_1_1_entity_container-members.html index b380868..4de9251 100644 --- a/docs/classflat2d_1_1_entity_container-members.html +++ b/docs/classflat2d_1_1_entity_container-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@

- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_entity_properties-members.html b/docs/classflat2d_1_1_entity_properties-members.html index 1600918..89c0016 100644 --- a/docs/classflat2d_1_1_entity_properties-members.html +++ b/docs/classflat2d_1_1_entity_properties-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_execution_timer-members.html b/docs/classflat2d_1_1_execution_timer-members.html index b80d0a0..a38e91c 100644 --- a/docs/classflat2d_1_1_execution_timer-members.html +++ b/docs/classflat2d_1_1_execution_timer-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_flat_builder-members.html b/docs/classflat2d_1_1_flat_builder-members.html index 91ddff8..efd03c3 100644 --- a/docs/classflat2d_1_1_flat_builder-members.html +++ b/docs/classflat2d_1_1_flat_builder-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_game_controller-members.html b/docs/classflat2d_1_1_game_controller-members.html index 8b999fa..80deace 100644 --- a/docs/classflat2d_1_1_game_controller-members.html +++ b/docs/classflat2d_1_1_game_controller-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_game_controller_container-members.html b/docs/classflat2d_1_1_game_controller_container-members.html index d988779..e5002ca 100644 --- a/docs/classflat2d_1_1_game_controller_container-members.html +++ b/docs/classflat2d_1_1_game_controller_container-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_game_data-members.html b/docs/classflat2d_1_1_game_data-members.html index e440648..ae9640f 100644 --- a/docs/classflat2d_1_1_game_data-members.html +++ b/docs/classflat2d_1_1_game_data-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_game_engine-members.html b/docs/classflat2d_1_1_game_engine-members.html index 7c53620..2d23178 100644 --- a/docs/classflat2d_1_1_game_engine-members.html +++ b/docs/classflat2d_1_1_game_engine-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_map_area-members.html b/docs/classflat2d_1_1_map_area-members.html index b514946..b00a2de 100644 --- a/docs/classflat2d_1_1_map_area-members.html +++ b/docs/classflat2d_1_1_map_area-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_media_util-members.html b/docs/classflat2d_1_1_media_util-members.html index 8dd118d..2b13b3e 100644 --- a/docs/classflat2d_1_1_media_util-members.html +++ b/docs/classflat2d_1_1_media_util-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_mixer-members.html b/docs/classflat2d_1_1_mixer-members.html index 8348ebc..4cd17b6 100644 --- a/docs/classflat2d_1_1_mixer-members.html +++ b/docs/classflat2d_1_1_mixer-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_quad_tree-members.html b/docs/classflat2d_1_1_quad_tree-members.html index 23dfdbf..60ea13a 100644 --- a/docs/classflat2d_1_1_quad_tree-members.html +++ b/docs/classflat2d_1_1_quad_tree-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_render_data-members.html b/docs/classflat2d_1_1_render_data-members.html index 959971a..8088e44 100644 --- a/docs/classflat2d_1_1_render_data-members.html +++ b/docs/classflat2d_1_1_render_data-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_runtime_analyzer-members.html b/docs/classflat2d_1_1_runtime_analyzer-members.html index 4b038fc..c42aea1 100644 --- a/docs/classflat2d_1_1_runtime_analyzer-members.html +++ b/docs/classflat2d_1_1_runtime_analyzer-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_spin_lock-members.html b/docs/classflat2d_1_1_spin_lock-members.html index e08b4d3..ae3487f 100644 --- a/docs/classflat2d_1_1_spin_lock-members.html +++ b/docs/classflat2d_1_1_spin_lock-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_square-members.html b/docs/classflat2d_1_1_square-members.html index 367f8fc..354bcec 100644 --- a/docs/classflat2d_1_1_square-members.html +++ b/docs/classflat2d_1_1_square-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
-

◆ operator<()

+

◆ operator<()

@@ -714,7 +717,7 @@

diff --git a/docs/classflat2d_1_1_texture-members.html b/docs/classflat2d_1_1_texture-members.html index 853584d..8b447fb 100644 --- a/docs/classflat2d_1_1_texture-members.html +++ b/docs/classflat2d_1_1_texture-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@

- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_timer-members.html b/docs/classflat2d_1_1_timer-members.html index c646aef..94e9f50 100644 --- a/docs/classflat2d_1_1_timer-members.html +++ b/docs/classflat2d_1_1_timer-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_u_i_d-members.html b/docs/classflat2d_1_1_u_i_d-members.html index 6859458..6c952cd 100644 --- a/docs/classflat2d_1_1_u_i_d-members.html +++ b/docs/classflat2d_1_1_u_i_d-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1_window-members.html b/docs/classflat2d_1_1_window-members.html index bef5ea1..73a1007 100644 --- a/docs/classflat2d_1_1_window-members.html +++ b/docs/classflat2d_1_1_window-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/classflat2d_1_1ui_1_1_button-members.html b/docs/classflat2d_1_1ui_1_1_button-members.html index 6069e09..1c5ebe9 100644 --- a/docs/classflat2d_1_1ui_1_1_button-members.html +++ b/docs/classflat2d_1_1ui_1_1_button-members.html @@ -3,7 +3,7 @@ - + flat: Member List @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
diff --git a/docs/dir_68267d1309a1af8e8297ef4c3efbcdba.html b/docs/dir_68267d1309a1af8e8297ef4c3efbcdba.html index ccdfb97..aece307 100644 --- a/docs/dir_68267d1309a1af8e8297ef4c3efbcdba.html +++ b/docs/dir_68267d1309a1af8e8297ef4c3efbcdba.html @@ -3,7 +3,7 @@ - + flat: src Directory Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */
@@ -124,7 +127,7 @@ diff --git a/docs/flat_8h.html b/docs/flat_8h.html index 7434172..df43b7b 100644 --- a/docs/flat_8h.html +++ b/docs/flat_8h.html @@ -3,7 +3,7 @@ - + flat: src/flat.h File Reference @@ -31,18 +31,21 @@
- + +/* @license-end */ - + +/* @license-end */ - + +/* @license-end */
@@ -99,7 +102,7 @@

- a -

    diff --git a/docs/functions_0x7e.html b/docs/functions_0x7e.html index a13c70e..b859d3a 100644 --- a/docs/functions_0x7e.html +++ b/docs/functions_0x7e.html @@ -3,7 +3,7 @@ - + flat: Class Members @@ -31,18 +31,21 @@
- + +/* @license-end */ @@ -108,7 +111,7 @@

- ~ -