CUGL
Cornell University Game Library
All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends
CUDisplay.h
1 //
2 // CUDisplay.h
3 // Cornell University Game Library (CUGL)
4 //
5 // This module is a singleton providing display information about the device.
6 // Originally, we had made this part of Application. However, we discovered
7 // that we needed platform specfic code for this, so we factored it out.
8 //
9 // Because this is a singleton, there are no publicly accessible constructors
10 // or intializers. Use the static methods instead.
11 //
12 // CUGL zlib License:
13 // This software is provided 'as-is', without any express or implied
14 // warranty. In no event will the authors be held liable for any damages
15 // arising from the use of this software.
16 //
17 // Permission is granted to anyone to use this software for any purpose,
18 // including commercial applications, and to alter it and redistribute it
19 // freely, subject to the following restrictions:
20 //
21 // 1. The origin of this software must not be misrepresented; you must not
22 // claim that you wrote the original software. If you use this software
23 // in a product, an acknowledgment in the product documentation would be
24 // appreciated but is not required.
25 //
26 // 2. Altered source versions must be plainly marked as such, and must not
27 // be misrepresented as being the original software.
28 //
29 // 3. This notice may not be removed or altered from any source distribution.
30 //
31 // Author: Walker White
32 // Version: 7/12/16
33 // TODO: Orientation detection for mobile devices
34 #ifndef __CU_DISPLAY_H__
35 #define __CU_DISPLAY_H__
36 #include <cugl/math/CURect.h>
37 
38 namespace cugl {
39 
57 class Display {
58 public:
67  enum class Aspect : unsigned int {
73  SQUARE = 0,
80  PORTRAIT_3_4 = 1,
87  PORTRAIT_2_3 = 2,
93  PORTRAIT_10_16 = 3,
99  PORTRAIT_3_5 = 4,
106  PORTRAIT_9_16 = 5,
113  PORTRAIT_600_1024 = 6,
119  PORTRAIT_IPAD_PRO = 7,
126  LANDSCAPE_4_3 = 8,
133  LANDSCAPE_3_2 = 9,
140  LANDSCAPE_16_10 = 10,
146  LANDSCAPE_5_3 = 11,
154  LANDSCAPE_16_9 = 12,
161  LANDSCAPE_1024_600 = 13,
167  LANDSCAPE_IPAD_PRO = 14,
174  UKNOWN = 15
175  };
176 
177 #pragma mark Values
178 protected:
181 
184 
191 
192 
193 #pragma mark -
194 #pragma mark Constructors
195 
204  Display();
205 
217  bool init();
218 
228  void dispose();
229 
239  ~Display() { dispose(); }
240 
241 #pragma mark -
242 #pragma mark Static Accessors
243 public:
253  static bool start();
254 
264  static void stop();
265 
275  static Display* get() { return _thedisplay; }
276 
277 #pragma mark -
278 #pragma mark Attributes
279 
294  const Rect& getBounds() const { return _bounds; }
295 
308  Rect getPixelBounds() const { return Rect(_bounds.origin*_scale,_bounds.size*_scale); }
309 
329  const Rect& getUsableBounds() const { return _usable; }
330 
348  const Vec2& getPixelDensity() const { return _scale; }
349 
368  Aspect getAspect() const { return _aspect; }
369 
375  bool isLandscape() const {
376  return (int)_aspect >= (int)Aspect::LANDSCAPE_4_3;
377  }
378 
384  bool isPortrait() const {
385  return ((int)_aspect < (int)Aspect::LANDSCAPE_4_3 &&
386  _aspect != Aspect::SQUARE);
387  }
388 
389 #pragma mark -
390 #pragma mark Aspect Utilities
391 
398  float getAspectRatio() const { return getAspectRatio(_aspect); }
399 
408  const std::string getAspectName() const { return getAspectName(_aspect); }
409 
421  int widthForHeight(int height) const {
422  return (int)(ceilf(getAspectRatio(_aspect)/height));
423  }
424 
436  int heightForWidth(int width) const {
437  return (int)(ceilf(width/getAspectRatio(_aspect)));
438  }
439 
460  static Aspect getAspect(float ratio);
461 
472  static float getAspectRatio(Aspect aspect);
473 
484  static const std::string getAspectName(Aspect aspect);
485 
498  static int widthForHeight(int height, Aspect aspect) {
499  return (int)(ceilf(getAspectRatio(aspect)/height));
500  }
501 
514  static int heightForWidth(int width, Aspect aspect) {
515  return (int)(ceilf(width/getAspectRatio(aspect)));
516  }
517 
518 };
519 
520 }
521 
522 #endif /* __CU_DISPLAY_H__ */
Aspect
Definition: CUDisplay.h:67
bool isLandscape() const
Definition: CUDisplay.h:375
Definition: CUVec2.h:61
Aspect _aspect
Definition: CUDisplay.h:183
Aspect getAspect() const
Definition: CUDisplay.h:368
float getAspectRatio() const
Definition: CUDisplay.h:398
Vec2 _scale
Definition: CUDisplay.h:190
Rect _bounds
Definition: CUDisplay.h:186
static void stop()
const Vec2 & getPixelDensity() const
Definition: CUDisplay.h:348
static bool start()
Definition: CUDisplay.h:57
int heightForWidth(int width) const
Definition: CUDisplay.h:436
static Display * _thedisplay
Definition: CUDisplay.h:180
Rect _usable
Definition: CUDisplay.h:188
static int widthForHeight(int height, Aspect aspect)
Definition: CUDisplay.h:498
~Display()
Definition: CUDisplay.h:239
int widthForHeight(int height) const
Definition: CUDisplay.h:421
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
const std::string getAspectName() const
Definition: CUDisplay.h:408
static int heightForWidth(int width, Aspect aspect)
Definition: CUDisplay.h:514
Size size
Definition: CURect.h:51
bool isPortrait() const
Definition: CUDisplay.h:384
const Rect & getBounds() const
Definition: CUDisplay.h:294
Definition: CUAnimationNode.h:52
const Rect & getUsableBounds() const
Definition: CUDisplay.h:329
Rect getPixelBounds() const
Definition: CUDisplay.h:308