CUGL 1.3
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 // 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 
382 
383 #pragma mark -
384 #pragma mark Constructors
385 
394  Display();
395 
416  bool init(std::string title, Rect bounds, Uint32 flags);
417 
427  void dispose();
428 
438  ~Display() { dispose(); }
439 
440 #pragma mark -
441 #pragma mark Static Accessors
442 public:
463  static bool start(std::string title, Rect bounds, Uint32 flags);
464 
475  static void stop();
476 
486  static Display* get() { return _thedisplay; }
487 
488 #pragma mark -
489 #pragma mark Window Management
490 
497  std::string getTitle() const { return _title; }
498 
506  void setTitle(const std::string& title) {
507  setTitle(title.c_str());
508  }
509 
517  void setTitle(const char* title);
518 
524  void show();
525 
531  void hide();
532 
533 #pragma mark -
534 #pragma mark Attributes
535 
552  Rect getBounds() const { return _bounds; }
553 
569 
595  Rect getUsableBounds(bool display=true);
596 
614  Vec2 getPixelDensity() const { return _scale; }
615 
634  Aspect getAspect() const { return _aspect; }
635 
641  bool isLandscape() const {
642  return (int)_aspect >= (int)Aspect::LANDSCAPE_4_3;
643  }
644 
650  bool isPortrait() const {
651  return ((int)_aspect < (int)Aspect::LANDSCAPE_4_3 &&
653  }
654 
669  bool hasNotch() const {
670  return _notched;
671  }
672 
673 #pragma mark -
674 #pragma mark Orientation
675 
695 
716 
735 
757 
776  bool hasOrientationListener() const { return _orientationListener != nullptr; }
777 
798 
819  void setOrientationListener(Listener listener) { _orientationListener = listener; }
820 
841 
842 #pragma mark -
843 #pragma mark Aspect Utilities
844 
851  float getAspectRatio() const { return getAspectRatio(_aspect); }
852 
861  const std::string getAspectName() const { return getAspectName(_aspect); }
862 
874  int widthForHeight(int height) const {
875  return (int)(ceilf(getAspectRatio(_aspect)/height));
876  }
877 
889  int heightForWidth(int width) const {
890  return (int)(ceilf(width/getAspectRatio(_aspect)));
891  }
892 
913  static Aspect getAspect(float ratio);
914 
925  static float getAspectRatio(Aspect aspect);
926 
937  static const std::string getAspectName(Aspect aspect);
938 
951  static int widthForHeight(int height, Aspect aspect) {
952  return (int)(ceilf(getAspectRatio(aspect)/height));
953  }
954 
967  static int heightForWidth(int width, Aspect aspect) {
968  return (int)(ceilf(width/getAspectRatio(aspect)));
969  }
970 
971 #pragma mark -
972 #pragma mark OpenGL Management
973 //private:
982  void refresh();
983 
993  bool prepareOpenGL(bool multisample);
994 
1004  bool initOpenGL(bool multisample);
1005 
1007  friend class Application;
1008 };
1009 
1010 }
1011 
1012 #endif /* __CU_DISPLAY_H__ */
cugl::Display::setOrientationListener
void setOrientationListener(Listener listener)
Definition: CUDisplay.h:819
cugl::Display::Orientation::FIXED
cugl::Display::getDefaultOrientation
Orientation getDefaultOrientation() const
Definition: CUDisplay.h:756
cugl::Display::Aspect::PORTRAIT_IPAD_PRO_LARGE
cugl::Display::heightForWidth
static int heightForWidth(int width, Aspect aspect)
Definition: CUDisplay.h:967
cugl::Display::_deviceOrientation
Orientation _deviceOrientation
Definition: CUDisplay.h:379
cugl::Display::isPortrait
bool isPortrait() const
Definition: CUDisplay.h:650
cugl::Display::_title
std::string _title
Definition: CUDisplay.h:352
cugl::Display::hide
void hide()
cugl::Display::Aspect::PORTRAIT_9_16
cugl::Display::hasNotch
bool hasNotch() const
Definition: CUDisplay.h:669
cugl::Display::INIT_MULTISAMPLED
static Uint32 INIT_MULTISAMPLED
Definition: CUDisplay.h:342
cugl::Display::isLandscape
bool isLandscape() const
Definition: CUDisplay.h:641
cugl::Display::getDisplayOrientation
Orientation getDisplayOrientation() const
Definition: CUDisplay.h:715
cugl::Display::Listener
std::function< void(Orientation previous, Orientation current, bool display)> Listener
Definition: CUDisplay.h:334
cugl::Application
Definition: CUApplication.h:83
cugl::Display::Orientation::FACE_DOWN
cugl::Display::Display
Display()
cugl::Display::getAspect
Aspect getAspect() const
Definition: CUDisplay.h:634
cugl::Display::initOpenGL
bool initOpenGL(bool multisample)
cugl::Display::Orientation::UPSIDE_DOWN
cugl::Display::_thedisplay
static Display * _thedisplay
Definition: CUDisplay.h:349
cugl::Display::_window
SDL_Window * _window
Definition: CUDisplay.h:355
cugl::Display::Aspect::LANDSCAPE_3_2
cugl::Display::Aspect::PORTRAIT_9_19p5
cugl::Display::widthForHeight
static int widthForHeight(int height, Aspect aspect)
Definition: CUDisplay.h:951
cugl::Display::_orientationListener
Listener _orientationListener
Definition: CUDisplay.h:373
cugl::Display::Orientation::FACE_UP
cugl::Display::Orientation::UNKNOWN
cugl::Display::Aspect::LANDSCAPE_IPAD_PRO_SMALL_2018
cugl::Display::~Display
~Display()
Definition: CUDisplay.h:438
cugl::Display::Aspect::LANDSCAPE_1024_600
cugl::Display::Aspect::LANDSCAPE_19p5_19
cugl::Display::getPixelBounds
Rect getPixelBounds() const
Definition: CUDisplay.h:568
cugl::Display::Orientation::PORTRAIT
cugl::Display::refresh
void refresh()
cugl::Display::Orientation::LANDSCAPE_REVERSED
cugl::Display::Aspect::UNKNOWN
cugl::Display::get
static Display * get()
Definition: CUDisplay.h:486
cugl::Display::getTitle
std::string getTitle() const
Definition: CUDisplay.h:497
cugl::Display::Aspect::PORTRAIT_3_5
cugl::Rect
Definition: CURect.h:45
cugl::Display::Aspect::LANDSCAPE_16_10
cugl::Display::getAspectRatio
float getAspectRatio() const
Definition: CUDisplay.h:851
cugl::Display::Aspect::LANDSCAPE_IPAD_PRO_SMALL_2017
cugl::Display::Aspect::PORTRAIT_10_16
cugl::Rect::origin
Vec2 origin
Definition: CURect.h:49
cugl::Display::prepareOpenGL
bool prepareOpenGL(bool multisample)
cugl::Display::Aspect::PORTRAIT_IPAD_PRO_SMALL_2017
cugl::Display::Orientation::LANDSCAPE
cugl::Display::INIT_HIGH_DPI
static Uint32 INIT_HIGH_DPI
Definition: CUDisplay.h:340
cugl::Display::start
static bool start(std::string title, Rect bounds, Uint32 flags)
cugl::Display::getPixelDensity
Vec2 getPixelDensity() const
Definition: CUDisplay.h:614
cugl::Display::getBounds
Rect getBounds() const
Definition: CUDisplay.h:552
cugl::Display::dispose
void dispose()
cugl::Display::_notched
bool _notched
Definition: CUDisplay.h:370
cugl::Display::_bounds
Rect _bounds
Definition: CUDisplay.h:363
cugl::Display::Orientation
Orientation
Definition: CUDisplay.h:244
cugl::Display::INIT_CENTERED
static Uint32 INIT_CENTERED
Definition: CUDisplay.h:344
cugl::Vec2
Definition: CUVec2.h:61
cugl::Display::Aspect
Aspect
Definition: CUDisplay.h:84
cugl::Display::getDeviceOrientation
Orientation getDeviceOrientation() const
Definition: CUDisplay.h:734
cugl::Display::widthForHeight
int widthForHeight(int height) const
Definition: CUDisplay.h:874
cugl::Display::getAspectName
const std::string getAspectName() const
Definition: CUDisplay.h:861
cugl::Display::Aspect::PORTRAIT_IPAD_PRO_SMALL_2018
cugl::Display::stop
static void stop()
cugl::Display::getInitialOrientation
Orientation getInitialOrientation() const
Definition: CUDisplay.h:694
cugl::Display::Aspect::LANDSCAPE_16_9
cugl::Display::show
void show()
cugl::Display::Aspect::SQUARE
cugl::Display::_displayOrientation
Orientation _displayOrientation
Definition: CUDisplay.h:377
cugl::Display::_glContext
SDL_GLContext _glContext
Definition: CUDisplay.h:357
cugl::Display::Aspect::LANDSCAPE_4_3
cugl::Display::Aspect::LANDSCAPE_5_3
cugl::Display::getOrientationListener
const Listener getOrientationListener() const
Definition: CUDisplay.h:797
cugl::Display::Aspect::PORTRAIT_600_1024
cugl::Display::_usable
Rect _usable
Definition: CUDisplay.h:365
cugl::Display::setTitle
void setTitle(const std::string &title)
Definition: CUDisplay.h:506
cugl::Display::_aspect
Aspect _aspect
Definition: CUDisplay.h:360
cugl::Rect::size
Size size
Definition: CURect.h:51
cugl::Display::init
bool init(std::string title, Rect bounds, Uint32 flags)
cugl::Display::hasOrientationListener
bool hasOrientationListener() const
Definition: CUDisplay.h:776
cugl::Display::_scale
Vec2 _scale
Definition: CUDisplay.h:367
cugl::Display::Aspect::PORTRAIT_3_4
cugl::Display::INIT_FULLSCREEN
static Uint32 INIT_FULLSCREEN
Definition: CUDisplay.h:338
cugl::Display::Aspect::LANDSCAPE_IPAD_PRO_LARGE
cugl::Display::removeOrientationListener
bool removeOrientationListener()
cugl::Display::Aspect::PORTRAIT_2_3
cugl::Display::_defaultOrientation
Orientation _defaultOrientation
Definition: CUDisplay.h:381
cugl::Display::_initialOrientation
Orientation _initialOrientation
Definition: CUDisplay.h:375
cugl::Display::heightForWidth
int heightForWidth(int width) const
Definition: CUDisplay.h:889
cugl::Display::getUsableBounds
Rect getUsableBounds(bool display=true)
cugl::Display
Definition: CUDisplay.h:71