neoGFX
C++ GPU-oriented GUI library and app/game creation framework.
sprite_plane.hpp
Go to the documentation of this file.
1 // sprite_plane.hpp
2 /*
3  neogfx C++ GUI Library
4  Copyright (c) 2015 Leigh Johnston. All Rights Reserved.
5 
6  This program is free software: you can redistribute it and / or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #pragma once
20 
21 #include <neogfx/neogfx.hpp>
22 #include <unordered_set>
23 #include <mutex>
24 #include <boost/pool/pool_alloc.hpp>
25 #include <boost/functional/hash.hpp>
26 #include <neolib/timer.hpp>
28 #include <neogfx/game/chrono.hpp>
29 #include <neogfx/game/sprite.hpp>
32 
33 namespace neogfx
34 {
35  class sprite_plane : public widget, public i_shape_container
36  {
37  public:
39  public:
47  public:
51  public:
52  typedef std::shared_ptr<i_game_object> object_pointer;
53  typedef std::vector<object_pointer> object_list;
54  typedef std::vector<i_shape*> shape_list;
57  typedef std::pair<i_collidable_object*, i_collidable_object*> collision_pair;
58  typedef std::unordered_set<collision_pair, boost::hash<collision_pair>, std::equal_to<collision_pair>, boost::fast_pool_allocator<collision_pair>> collision_list;
59  private:
60  typedef std::list<sprite, boost::fast_pool_allocator<sprite>> simple_sprite_list;
61  typedef std::list<physical_object, boost::fast_pool_allocator<physical_object>> simple_object_list;
62  class physics_thread;
63  public:
64  sprite_plane();
65  sprite_plane(i_widget& aParent);
66  sprite_plane(i_layout& aLayout);
67  ~sprite_plane();
68  public:
70  virtual void paint(graphics_context& aGraphicsContext) const;
71  public:
72  virtual void mouse_button_pressed(mouse_button aButton, const point& aPosition, key_modifiers_e aKeyModifiers);
73  public:
74  virtual const i_widget& as_widget() const;
75  virtual i_widget& as_widget();
76  public:
77  bool dynamic_update_enabled() const;
78  void enable_dynamic_update(bool aEnableDynamicUpdate);
79  void enable_z_sorting(bool aEnableZSorting);
80  public:
81  void add_sprite(i_sprite& aObject);
82  void add_sprite(std::shared_ptr<i_sprite> aObject);
84  void add_physical_object(std::shared_ptr<i_physical_object> aObject);
85  void add_shape(i_shape& aObject);
86  void add_shape(std::shared_ptr<i_shape> aObject);
87  public:
89  i_sprite& create_sprite(const i_texture& aTexture);
90  i_sprite& create_sprite(const i_image& aImage);
91  i_sprite& create_sprite(const i_texture& aTexture, const rect& aTextureRect);
92  i_sprite& create_sprite(const i_image& aImage, const rect& aTextureRect);
93  public:
96  const optional_vec3& uniform_gravity() const;
97  void set_uniform_gravity(const optional_vec3& aUniformGravity = vec3{ 0.0, -9.80665, 0.0});
100  const optional_step_time_interval& physics_time() const;
101  void set_physics_time(const optional_step_time_interval& aTime);
102  step_time_interval physics_step_interval() const;
103  void set_physics_step_interval(step_time_interval aStepInterval);
104  public:
105  void reserve(std::size_t aCapacity);
106  const object_list& objects() const;
107  void add_object(std::shared_ptr<i_game_object> aObject);
108  public:
109  bool is_collision_tree_2d() const;
110  bool is_collision_tree_3d() const;
111  const broad_phase_collision_tree_2d& collision_tree_2d() const;
112  broad_phase_collision_tree_2d& collision_tree_2d();
113  const broad_phase_collision_tree_3d& collision_tree_3d() const;
114  broad_phase_collision_tree_3d& collision_tree_3d();
115  public:
116  double update_time() const;
117  private:
118  void do_add_object(std::shared_ptr<i_game_object> aObject);
119  void sort_shapes() const;
120  void sort_objects();
121  void update_objects();
122  bool snapshot();
123  private:
124  neolib::callback_timer iUpdater;
125  bool iEnableDynamicUpdate;
126  bool iEnableZSorting;
127  bool iNeedsSorting;
128  scalar iG;
129  optional_vec3 iUniformGravity;
130  optional_step_time_interval iPhysicsTime;
131  step_time_interval iStepInterval;
132  object_list iObjects;
133  object_list iNewObjects;
134  mutable shape_list iRenderBuffer;
135  simple_sprite_list iSimpleSprites;
136  simple_object_list iSimpleObjects;
137  object_list::iterator iLastCollidable;
138  mutable boost::optional<broad_phase_collision_tree_2d> iBroadPhaseCollisionTree2d;
139  mutable boost::optional<broad_phase_collision_tree_3d> iBroadPhaseCollisionTree3d;
140  chrono::flicks iUpdateTime;
141  std::atomic<bool> iUpdatedSinceLastSnapshot;
142  bool iTakingSnapshot;
143  mutable std::recursive_mutex iUpdateMutex;
144  std::unique_ptr<physics_thread> iPhysicsThread;
145  collision_list iCollisions;
146  };
147 }
boost::optional< time_interval > optional_time_interval
std::shared_ptr< i_game_object > object_pointer
void add_object(std::shared_ptr< i_game_object > aObject)
boost::optional< step_time_interval > optional_step_time_interval
virtual const i_widget & as_widget() const
event< graphics_context & > sprites_painted
double scalar
Definition: numerical.hpp:34
virtual void paint(graphics_context &aGraphicsContext) const
virtual neogfx::logical_coordinate_system logical_coordinate_system() const
void set_physics_time(const optional_step_time_interval &aTime)
event< step_time_interval > applying_physics
boost::optional< vec3 > optional_vec3
Definition: numerical.hpp:294
i_physical_object::step_time_interval step_time_interval
const broad_phase_collision_tree_3d & collision_tree_3d() const
step_time_interval physics_step_interval() const
double update_time() const
std::vector< i_shape * > shape_list
event< i_game_object & > object_clicked
i_physical_object & create_physical_object()
virtual void mouse_button_pressed(mouse_button aButton, const point &aPosition, key_modifiers_e aKeyModifiers)
i_physical_object::time_interval time_interval
std::vector< object_pointer > object_list
const optional_step_time_interval & physics_time() const
void set_physics_step_interval(step_time_interval aStepInterval)
bool dynamic_update_enabled() const
std::unordered_set< collision_pair, boost::hash< collision_pair >, std::equal_to< collision_pair >, boost::fast_pool_allocator< collision_pair > > collision_list
void add_shape(i_shape &aObject)
event< step_time_interval > physics_applied
aabb_quadtree broad_phase_collision_tree_2d
void reserve(std::size_t aCapacity)
bool is_collision_tree_3d() const
bool is_collision_tree_2d() const
const broad_phase_collision_tree_2d & collision_tree_2d() const
i_physical_object & create_earth()
adds gravity by simulating the earth, groundlevel at y = 0;
void enable_z_sorting(bool aEnableZSorting)
const object_list & objects() const
void enable_dynamic_update(bool aEnableDynamicUpdate)
event< i_game_object &, i_game_object & > object_collision
void add_sprite(i_sprite &aObject)
event< graphics_context & > painting_sprites
std::chrono::duration< std::chrono::nanoseconds::rep, std::ratio< 1, 705600000 > > flicks
Definition: flicks.h:60
mouse_button
Definition: mouse.hpp:27
i_physical_object::optional_step_time_interval optional_step_time_interval
void set_gravitational_constant(scalar aG)
void set_uniform_gravity(const optional_vec3 &aUniformGravity=vec3{ 0.0, -9.80665, 0.0})
const optional_vec3 & uniform_gravity() const
aabb_octree broad_phase_collision_tree_3d
event< step_time_interval > taking_snapshot
void add_physical_object(i_physical_object &aObject)
std::pair< i_collidable_object *, i_collidable_object * > collision_pair
scalar gravitational_constant() const
i_physical_object::optional_time_interval optional_time_interval
i_sprite & create_sprite()