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