Ocean
GLView.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_PLATFORM_ANDROID_APPLICATION_GL_VIEW_H
9 #define META_OCEAN_PLATFORM_ANDROID_APPLICATION_GL_VIEW_H
10 
12 
13 #include "ocean/base/Singleton.h"
14 
15 #include "ocean/math/Math.h"
16 
17 namespace Ocean
18 {
19 
20 namespace Platform
21 {
22 
23 namespace Android
24 {
25 
26 namespace Application
27 {
28 
29 /**
30  * This class implements the base class for all OpenGL-ES-based views.
31  * The view is implemented as singleton object.
32  * @ingroup platformandroidapplication
33  */
34 class OCEAN_PLATFORM_ANDROID_APPLICATION_EXPORT GLView
35 {
36  public:
37 
38  /**
39  * Definition of a function pointer creating an instance of the GLView object.
40  */
41  typedef GLView* (*InstanceFunction)();
42 
43  protected:
44 
45  /**
46  * This class implements a simple helper class providing the instance of this GLView object.
47  */
48  class Instance : public Singleton<Instance>
49  {
50  friend class Singleton<Instance>;
51 
52  public:
53 
54  /**
55  * Sets the instance function.
56  * @param instanceFunction The instance function to be set
57  * @param isBaseClass True, if the provided instance function is for the base classes GLView or GLFrameView; False, otherwise.
58  */
59  inline void setInstanceFunction(const InstanceFunction& instanceFunction, const bool isBaseClass);
60 
61  /**
62  * Returns the instance.
63  * @return The instance
64  */
66 
67  protected:
68 
69  /**
70  * Creates a new instance.
71  */
72  inline Instance();
73 
74  /**
75  * Destructs an instance.
76  */
77  virtual ~Instance();
78 
79  protected:
80 
81  /// The instance object.
83 
84  /// The instance function.
85  InstanceFunction baseInstanceFunction_;
86 
87  /// The instance function.
88  InstanceFunction derivedInstanceFunction_;
89 
90  /// The lock of this object.
92  };
93 
94  public:
95 
96  /**
97  * Initializes the view.
98  * @return True, if succeeded
99  */
100  virtual bool initialize();
101 
102  /**
103  * Releases the view.
104  * @return True, if succeeded
105  */
106  virtual bool release();
107 
108  /**
109  * View resize event function.
110  * @param width New view width, with range [1, infinity)
111  * @param height New view height, with range [1, infinity)
112  * @return True, if succeeded
113  */
114  virtual bool resize(const int width, const int height);
115 
116  /**
117  * Renders the next frame.
118  * @return True, if succeeded
119  */
120  virtual bool render();
121 
122  /**
123  * Converts the given screen positions into frame positions.
124  * @param xScreen Horizontal screen position, with range [0, infinity)
125  * @param yScreen Vertical screen position, with range [0, infinity)
126  * @param xFrame Resulting horizontal frame position, with range [0, infinity)
127  * @param yFrame Resulting vertical frame position, with range [0, infinity)
128  * @return True, if succeeded
129  */
130  virtual bool screen2frame(const Scalar xScreen, const Scalar yScreen, Scalar& xFrame, Scalar& yFrame);
131 
132  /**
133  * Touch down event function.
134  * @param x Horizontal touch position
135  * @param y Vertical touch position
136  */
137  virtual void onTouchDown(const float x, const float y);
138 
139  /**
140  * Touch move event function.
141  * @param x Horizontal touch position
142  * @param y Vertical touch position
143  */
144  virtual void onTouchMove(const float x, const float y);
145 
146  /**
147  * Touch move event function.
148  * @param x Horizontal touch position
149  * @param y Vertical touch position
150  */
151  virtual void onTouchUp(const float x, const float y);
152 
153  /**
154  * The resume event function for events sent from the owning activity.
155  */
156  virtual void onResume();
157 
158  /**
159  * The pause event function for events sent from the owning activity.
160  */
161  virtual void onPause();
162 
163  /**
164  * The stop event function for events sent from the owning activity.
165  */
166  virtual void onStop();
167 
168  /**
169  * The destroy event function for events sent from the owning activity.
170  */
171  virtual void onDestroy();
172 
173  /**
174  * The event function for granted permissions.
175  * @param permission The permission which has been granted
176  */
177  virtual void onPermissionGranted(const std::string& permission);
178 
179  /**
180  * Returns the instance of this frame view object.
181  * @return The frame view object
182  */
183  static GLView& get();
184 
185  /**
186  * Returns the instance of a derived object from this frame view object.
187  * @return The derived frame view object
188  * @tparam T The data type of the derived view object
189  */
190  template <typename T>
191  static T& get();
192 
193  /**
194  * Sets the instance function for an optional derived class.
195  * @param instanceFunction The instance function to set
196  * @param isBaseClass True, if the provided instance function is for the base classes GLView or GLFrameView; False, otherwise.
197  * @return Always True
198  */
199  static inline bool registerInstanceFunction(const InstanceFunction& instanceFunction, const bool isBaseClass = false);
200 
201  /**
202  * Creates an instance of this object.
203  * @return The new instance
204  */
205  static inline GLView* createInstance();
206 
207  protected:
208 
209  /**
210  * Creates a new view object.
211  */
212  GLView() = default;
213 
214  /**
215  * Destructs a view object.
216  */
217  virtual ~GLView() = default;
218 };
219 
221  view_(nullptr),
222  baseInstanceFunction_(nullptr),
223  derivedInstanceFunction_(nullptr)
224 {
225  // nothing to do here
226 }
227 
228 inline void GLView::Instance::setInstanceFunction(const InstanceFunction& instanceFunction, const bool isBaseClass)
229 {
230  if (isBaseClass)
231  {
232  baseInstanceFunction_ = instanceFunction;
233  }
234  else
235  {
236  derivedInstanceFunction_ = instanceFunction;
237  }
238 }
239 
240 template <typename T>
242 {
243  ocean_assert(dynamic_cast<T*>(&Instance::get().instance()) != nullptr);
244  return dynamic_cast<T&>(Instance::get().instance());
245 }
246 
247 inline bool GLView::registerInstanceFunction(const InstanceFunction& instanceFunction, const bool isBaseClass)
248 {
249  Instance::get().setInstanceFunction(instanceFunction, isBaseClass);
250 
251  return true;
252 }
253 
255 {
256  return new GLView();
257 }
258 
259 }
260 
261 }
262 
263 }
264 
265 }
266 
267 #endif // META_OCEAN_PLATFORM_ANDROID_APPLICATION_GL_VIEW_H
This class implements a recursive lock object.
Definition: Lock.h:31
This class implements a simple helper class providing the instance of this GLView object.
Definition: GLView.h:49
GLView * view_
The instance object.
Definition: GLView.h:82
InstanceFunction baseInstanceFunction_
The instance function.
Definition: GLView.h:85
Lock lock_
The lock of this object.
Definition: GLView.h:91
Instance()
Creates a new instance.
Definition: GLView.h:220
void setInstanceFunction(const InstanceFunction &instanceFunction, const bool isBaseClass)
Sets the instance function.
Definition: GLView.h:228
InstanceFunction derivedInstanceFunction_
The instance function.
Definition: GLView.h:88
This class implements the base class for all OpenGL-ES-based views.
Definition: GLView.h:35
virtual bool resize(const int width, const int height)
View resize event function.
virtual void onStop()
The stop event function for events sent from the owning activity.
static GLView * createInstance()
Creates an instance of this object.
Definition: GLView.h:254
virtual void onTouchDown(const float x, const float y)
Touch down event function.
virtual bool render()
Renders the next frame.
GLView *(* InstanceFunction)()
Definition of a function pointer creating an instance of the GLView object.
Definition: GLView.h:41
virtual bool screen2frame(const Scalar xScreen, const Scalar yScreen, Scalar &xFrame, Scalar &yFrame)
Converts the given screen positions into frame positions.
static GLView & get()
Returns the instance of this frame view object.
static bool registerInstanceFunction(const InstanceFunction &instanceFunction, const bool isBaseClass=false)
Sets the instance function for an optional derived class.
Definition: GLView.h:247
virtual void onPause()
The pause event function for events sent from the owning activity.
virtual void onTouchMove(const float x, const float y)
Touch move event function.
virtual void onDestroy()
The destroy event function for events sent from the owning activity.
virtual void onPermissionGranted(const std::string &permission)
The event function for granted permissions.
virtual void onTouchUp(const float x, const float y)
Touch move event function.
virtual bool initialize()
Initializes the view.
virtual bool release()
Releases the view.
virtual void onResume()
The resume event function for events sent from the owning activity.
GLView()=default
Creates a new view object.
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
static Instance & get()
Returns a reference to the unique object.
Definition: Singleton.h:115
float Scalar
Definition of a scalar type.
Definition: Math.h:128
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15