CUGL 1.2
Cornell University Game Library
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 // This singleton is also responsible for initializing (and disposing) the
10 // OpenGL context. That is because that context is tightly coupled to the
11 // orientation information, which is provided by this class.
12 //
13 // Because this is a singleton, there are no publicly accessible constructors
14 // or intializers. Use the static methods instead.
15 //
16 // CUGL MIT License:
17 // This software is provided 'as-is', without any express or implied
18 // warranty. In no event will the authors be held liable for any damages
19 // arising from the use of this software.
20 //
21 // Permission is granted to anyone to use this software for any purpose,
22 // including commercial applications, and to alter it and redistribute it
23 // freely, subject to the following restrictions:
24 //
25 // 1. The origin of this software must not be misrepresented; you must not
26 // claim that you wrote the original software. If you use this software
27 // in a product, an acknowledgment in the product documentation would be
28 // appreciated but is not required.
29 //
30 // 2. Altered source versions must be plainly marked as such, and must not
31 // be misrepresented as being the original software.
32 //
33 // 3. This notice may not be removed or altered from any source distribution.
34 //
35 // Author: Walker White
36 // Version: 12/12/18
37 #ifndef __CU_DISPLAY_H__
38 #define __CU_DISPLAY_H__
39 #include <cugl/math/CURect.h>
40 
41 namespace cugl {
42 
71 class Display {
72 public:
84  enum class Aspect : unsigned int {
90  SQUARE = 0,
97  PORTRAIT_3_4 = 1,
104  PORTRAIT_2_3 = 2,
110  PORTRAIT_10_16 = 3,
116  PORTRAIT_3_5 = 4,
123  PORTRAIT_9_16 = 5,
130  PORTRAIT_9_19p5 = 6,
137  PORTRAIT_600_1024 = 7,
165  LANDSCAPE_4_3 = 11,
172  LANDSCAPE_3_2 = 12,
179  LANDSCAPE_16_10 = 13,
185  LANDSCAPE_5_3 = 14,
193  LANDSCAPE_16_9 = 15,
200  LANDSCAPE_19p5_19 = 16,
207  LANDSCAPE_1024_600 = 17,
235  UNKNOWN = 21
236  };
237 
244  enum class Orientation : unsigned int {
251  FIXED = 0,
258  LANDSCAPE = 1,
265  PORTRAIT = 2,
272  LANDSCAPE_REVERSED = 3,
282  UPSIDE_DOWN = 4,
289  FACE_UP = 5,
296  FACE_DOWN = 6,
303  UNKNOWN = 7,
304  };
305 
334  typedef std::function<void(Orientation previous, Orientation current, bool display)> Listener;
335 
336  // Flags for the device initialization
338  static Uint32 INIT_FULLSCREEN;
340  static Uint32 INIT_HIGH_DPI;
342  static Uint32 INIT_MULTISAMPLED;
344  static Uint32 INIT_CENTERED;
345 
346 #pragma mark Values
347 protected:
350 
352  std::string _title;
353 
355  SDL_Window* _window;
357  SDL_GLContext _glContext;
358 
361 
368 
370  bool _notched;
371 
380 
381 #pragma mark -
382 #pragma mark Constructors
383 
392  Display();
393 
414  bool init(std::string title, Rect bounds, Uint32 flags);
415 
425  void dispose();
426 
436  ~Display() { dispose(); }
437 
438 #pragma mark -
439 #pragma mark Static Accessors
440 public:
461  static bool start(std::string title, Rect bounds, Uint32 flags);
462 
473  static void stop();
474 
484  static Display* get() { return _thedisplay; }
485 
486 #pragma mark -
487 #pragma mark Window Management
488 
495  std::string getTitle() const { return _title; }
496 
504  void setTitle(const std::string& title) {
505  setTitle(title.c_str());
506  }
507 
515  void setTitle(const char* title);
516 
522  void show();
523 
529  void hide();
530 
531 #pragma mark -
532 #pragma mark Attributes
533 
550  Rect getBounds() const { return _bounds; }
551 
566  Rect getPixelBounds() const { return Rect(_bounds.origin*_scale,_bounds.size*_scale); }
567 
593  Rect getUsableBounds(bool display=true);
594 
612  Vec2 getPixelDensity() const { return _scale; }
613 
632  Aspect getAspect() const { return _aspect; }
633 
639  bool isLandscape() const {
640  return (int)_aspect >= (int)Aspect::LANDSCAPE_4_3;
641  }
642 
648  bool isPortrait() const {
649  return ((int)_aspect < (int)Aspect::LANDSCAPE_4_3 &&
650  _aspect != Aspect::SQUARE);
651  }
652 
667  bool hasNotch() const {
668  return _notched;
669  }
670 
671 #pragma mark -
672 #pragma mark Orientation
673 
689 
705 
721 
740  bool hasOrientationListener() const { return _orientationListener != nullptr; }
741 
761  const Listener getOrientationListener() const { return _orientationListener; }
762 
783  void setOrientationListener(Listener listener) { _orientationListener = listener; }
784 
805 
806 #pragma mark -
807 #pragma mark Aspect Utilities
808 
815  float getAspectRatio() const { return getAspectRatio(_aspect); }
816 
825  const std::string getAspectName() const { return getAspectName(_aspect); }
826 
838  int widthForHeight(int height) const {
839  return (int)(ceilf(getAspectRatio(_aspect)/height));
840  }
841 
853  int heightForWidth(int width) const {
854  return (int)(ceilf(width/getAspectRatio(_aspect)));
855  }
856 
877  static Aspect getAspect(float ratio);
878 
889  static float getAspectRatio(Aspect aspect);
890 
901  static const std::string getAspectName(Aspect aspect);
902 
915  static int widthForHeight(int height, Aspect aspect) {
916  return (int)(ceilf(getAspectRatio(aspect)/height));
917  }
918 
931  static int heightForWidth(int width, Aspect aspect) {
932  return (int)(ceilf(width/getAspectRatio(aspect)));
933  }
934 
935 #pragma mark -
936 #pragma mark OpenGL Management
937 //private:
946  void refresh();
947 
957  bool prepareOpenGL(bool multisample);
958 
968  bool initOpenGL(bool multisample);
969 
971  friend class Application;
972 };
973 
974 }
975 
976 #endif /* __CU_DISPLAY_H__ */
Orientation getInitialOrientation() const
Definition: CUDisplay.h:688
Rect getBounds() const
Definition: CUDisplay.h:550
Aspect
Definition: CUDisplay.h:84
void setTitle(const std::string &title)
Definition: CUDisplay.h:504
std::string getTitle() const
Definition: CUDisplay.h:495
bool isLandscape() const
Definition: CUDisplay.h:639
static Uint32 INIT_FULLSCREEN
Definition: CUDisplay.h:338
bool removeOrientationListener()
Definition: CUVec2.h:61
Aspect _aspect
Definition: CUDisplay.h:360
static bool start(std::string title, Rect bounds, Uint32 flags)
Aspect getAspect() const
Definition: CUDisplay.h:632
float getAspectRatio() const
Definition: CUDisplay.h:815
Rect getUsableBounds(bool display=true)
Vec2 _scale
Definition: CUDisplay.h:367
Rect _bounds
Definition: CUDisplay.h:363
static Uint32 INIT_HIGH_DPI
Definition: CUDisplay.h:340
static void stop()
Orientation getDisplayOrientation() const
Definition: CUDisplay.h:704
Orientation getDeviceOrientation() const
Definition: CUDisplay.h:720
Vec2 getPixelDensity() const
Definition: CUDisplay.h:612
Orientation
Definition: CUDisplay.h:244
Definition: CUDisplay.h:71
int heightForWidth(int width) const
Definition: CUDisplay.h:853
SDL_GLContext _glContext
Definition: CUDisplay.h:357
SDL_Window * _window
Definition: CUDisplay.h:355
Orientation _displayOrientation
Definition: CUDisplay.h:377
static Display * _thedisplay
Definition: CUDisplay.h:349
bool hasOrientationListener() const
Definition: CUDisplay.h:740
Rect _usable
Definition: CUDisplay.h:365
bool hasNotch() const
Definition: CUDisplay.h:667
bool init(std::string title, Rect bounds, Uint32 flags)
static int widthForHeight(int height, Aspect aspect)
Definition: CUDisplay.h:915
~Display()
Definition: CUDisplay.h:436
void setOrientationListener(Listener listener)
Definition: CUDisplay.h:783
int widthForHeight(int height) const
Definition: CUDisplay.h:838
Definition: CURect.h:45
Vec2 origin
Definition: CURect.h:49
Orientation _initialOrientation
Definition: CUDisplay.h:375
Definition: CUApplication.h:84
const std::string getAspectName() const
Definition: CUDisplay.h:825
std::string _title
Definition: CUDisplay.h:352
const Listener getOrientationListener() const
Definition: CUDisplay.h:761
Orientation _deviceOrientation
Definition: CUDisplay.h:379
static int heightForWidth(int width, Aspect aspect)
Definition: CUDisplay.h:931
static Uint32 INIT_MULTISAMPLED
Definition: CUDisplay.h:342
Size size
Definition: CURect.h:51
bool isPortrait() const
Definition: CUDisplay.h:648
bool initOpenGL(bool multisample)
bool prepareOpenGL(bool multisample)
std::function< void(Orientation previous, Orientation current, bool display)> Listener
Definition: CUDisplay.h:334
Definition: CUAction.h:51
static Uint32 INIT_CENTERED
Definition: CUDisplay.h:344
Listener _orientationListener
Definition: CUDisplay.h:373
bool _notched
Definition: CUDisplay.h:370
Rect getPixelBounds() const
Definition: CUDisplay.h:566