CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
common_services.h
Go to the documentation of this file.
1 // Copyright 2015 Google Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef CORGI_COMPONENT_LIBRARY_COMMON_SERVICES_H_
16 #define CORGI_COMPONENT_LIBRARY_COMMON_SERVICES_H_
17 
18 #include "breadboard/graph_factory.h"
19 #include "corgi/component.h"
21 #include "fplbase/asset_manager.h"
22 #include "fplbase/input.h"
23 #include "fplbase/renderer.h"
24 #include "fplbase/utilities.h"
25 
26 namespace corgi {
27 namespace component_library {
28 
29 /// @file
30 /// @addtogroup corgi_component_library
31 /// @{
32 ///
33 /// @struct CommonServicesData
34 ///
35 /// @brief Holds the data that Components need (e.g. the input system,
36 /// renderer, etc.).
38 
39 /// @class CommonServicesComponent
40 ///
41 /// @brief This is a unique Component, as no Entities will register with it and
42 /// it contains no per-entity data. Its use is to provide a central location for
43 /// other Components to easily access game services and managers (since
44 /// Components themselves do not have direct access to the game state, but do
45 /// have access to other Components).
46 class CommonServicesComponent : public corgi::Component<CommonServicesData> {
47  public:
48  /// @brief The default constructor to create an empty CommonServicesComponent.
49  CommonServicesComponent() : export_force_defaults_(false) {}
50 
51  /// @brief Destructor for CommonServicesComponent.
53 
54  /// @brief Initializes the CommonServicesComponent with pointers to the
55  /// various game services and managers.
56  ///
57  /// @param[in] asset_manager A pointer to the game's AssetManager.
58  /// @param[in] entity_factory A pointer to the game's EntityFactory.
59  /// @param[in] graph_factory A pointer to the game's breadboard::GraphFactory.
60  /// @param[in] input_system A pointer to the game's input_system.
61  /// @param[in] renderer A pointer to the game's Renderer.
62  void Initialize(fplbase::AssetManager* asset_manager,
64  breadboard::GraphFactory* graph_factory,
65  fplbase::InputSystem* input_system,
66  fplbase::Renderer* renderer) {
67  asset_manager_ = asset_manager;
68  entity_factory_ = entity_factory;
69  graph_factory_ = graph_factory;
70  input_system_ = input_system;
71  renderer_ = renderer;
72  }
73 
74  /// @return Returns a pointer to the AssetManager.
75  fplbase::AssetManager* asset_manager() { return asset_manager_; }
76 
77  /// @return Returns a pointer to the breadboard::GraphFactory.
78  breadboard::GraphFactory* graph_factory() { return graph_factory_; }
79 
80  /// @return Returns a pointer to the InputSystem.
81  fplbase::InputSystem* input_system() { return input_system_; }
82 
83  /// @return Returns a pointer to the EntityFactory.
84  EntityFactory* entity_factory() { return entity_factory_; }
85 
86  /// @return Returns a pointer to the Renderer.
87  fplbase::Renderer* renderer() { return renderer_; }
88 
89  /// @brief This component should never be added to an Entity. It is only
90  /// provided as an interface for other components to access common game
91  /// resources.
92  ///
93  /// @warning Asserts when called.
94  void AddFromRawData(corgi::EntityRef& /*entity*/, const void* /*raw_data*/) {
95  assert(false);
96  }
97 
98  /// @brief This should be called when Components are exporting their data to a
99  /// FlatBuffer. It indicates if the FlatBuffer should include default values
100  /// in the serialized data.
101  ///
102  /// @return Returns `true` if the FlatBuffer serialized data should include
103  /// default values. Otherwise returns `false`, indicating that default values
104  /// should be excluded from the FlatBuffer serialized data.
105  bool export_force_defaults() const { return export_force_defaults_; }
106 
107  /// @brief Set a flag to determine if Components, when exporting their data
108  /// to FlatBuffers, should include default values in the serialized data.
109  ///
110  /// @param[in] b A `bool` indicating if the FlatBuffer serialized data should
111  /// include default values.
112  void set_export_force_defaults(bool b) { export_force_defaults_ = b; }
113 
114  private:
115  fplbase::AssetManager* asset_manager_;
116  EntityFactory* entity_factory_;
117  breadboard::GraphFactory* graph_factory_;
118  fplbase::InputSystem* input_system_;
119  fplbase::Renderer* renderer_;
120  bool export_force_defaults_;
121 };
122 /// @}
123 
124 } // component_library
125 } // corgi
126 
129 
130 #endif // CORGI_COMPONENT_LIBRARY_COMMON_SERVICES_H_
bool export_force_defaults() const
This should be called when Components are exporting their data to a FlatBuffer. It indicates if the F...
Definition: common_services.h:105
A Component is an object that encapsulates all data and logic for Entities of a particular type...
Definition: component.h:43
void Initialize(fplbase::AssetManager *asset_manager, EntityFactory *entity_factory, breadboard::GraphFactory *graph_factory, fplbase::InputSystem *input_system, fplbase::Renderer *renderer)
Initializes the CommonServicesComponent with pointers to the various game services and managers...
Definition: common_services.h:62
Holds the data that Components need (e.g. the input system, renderer, etc.).
Definition: common_services.h:37
breadboard::GraphFactory * graph_factory()
Definition: common_services.h:78
CommonServicesComponent()
The default constructor to create an empty CommonServicesComponent.
Definition: common_services.h:49
fplbase::Renderer * renderer()
Definition: common_services.h:87
fplbase::AssetManager * asset_manager()
Definition: common_services.h:75
EntityFactory * entity_factory()
Definition: common_services.h:84
void AddFromRawData(corgi::EntityRef &, const void *)
This component should never be added to an Entity. It is only provided as an interface for other comp...
Definition: common_services.h:94
void set_export_force_defaults(bool b)
Set a flag to determine if Components, when exporting their data to FlatBuffers, should include defau...
Definition: common_services.h:112
A reference object for pointing into the vector pool. It acts as a pointer for vector pool elements a...
Definition: vector_pool.h:72
An EntityFactory builds Entities based on prototypes, using FlatBuffers to specify the raw data for E...
Definition: entity_factory.h:37
virtual ~CommonServicesComponent()
Destructor for CommonServicesComponent.
Definition: common_services.h:52
This is a unique Component, as no Entities will register with it and it contains no per-entity data...
Definition: common_services.h:46
#define CORGI_REGISTER_COMPONENT(ComponentType, DataType)
Definition: component_id_lookup.h:48
fplbase::InputSystem * input_system()
Definition: common_services.h:81