Cinder  0.8.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Display.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2010, The Barbarian Group
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without modification, are permitted provided that
6  the following conditions are met:
7 
8  * Redistributions of source code must retain the above copyright notice, this list of conditions and
9  the following disclaimer.
10  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
11  the following disclaimer in the documentation and/or other materials provided with the distribution.
12 
13  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
14  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
15  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
16  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
17  TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
19  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
20  POSSIBILITY OF SUCH DAMAGE.
21 */
22 
23 #pragma once
24 
25 #include "cinder/Cinder.h"
26 #include "cinder/Area.h"
27 #include "cinder/Function.h"
28 
29 #if defined( CINDER_MAC )
30  #if defined( __OBJC__ )
31  @class NSScreen;
32  #else
33  class NSScreen;
34  #endif
35  typedef uint32_t CGDirectDisplayID;
36 #elif defined( CINDER_COCOA_TOUCH )
37  #if defined( __OBJC__ )
38  @class UIScreen;
39  #else
40  class UIScreen;
41  #endif
42 #elif defined( CINDER_MSW )
43  #include <windows.h>
44  #undef min
45  #undef max
46 #endif
47 
48 #include <map>
49 #include <vector>
50 
51 namespace cinder {
52 
53 typedef std::shared_ptr<class Display> DisplayRef;
54 
55 class Display {
56  public:
57  ~Display();
58 
60  int getWidth() const { return mArea.getWidth(); }
62  int getHeight() const { return mArea.getHeight(); }
64  Vec2i getSize() const { return Vec2i( getWidth(), getHeight() ); }
66  float getAspectRatio() const { return getWidth() / (float)getHeight(); }
68  Area getBounds() const { return mArea; }
69 
71  Vec2i getSystemCoordinate( const Vec2i &displayRelativeCoordinate ) const;
72 
74  int getBitsPerPixel() const { return mBitsPerPixel; }
76  float getContentScale() const { return mContentScale; }
77 
79  bool contains( const Vec2i &pt ) const { return mArea.contains( pt ); }
80 
81 #if defined( CINDER_MAC )
82  NSScreen* getNsScreen() const { return mScreen; }
83  CGDirectDisplayID getCgDirectDisplayId() const { return mDirectDisplayID; }
84 #elif defined( CINDER_COCOA_TOUCH )
85  UIScreen* getUiScreen() const { return mUiScreen; }
87  const std::vector<Vec2i>& getSupportedResolutions() const { return mSupportedResolutions; }
89  void setResolution( const Vec2i &resolution );
90 #endif
91 
93  static DisplayRef getMainDisplay();
95  static const std::vector<DisplayRef>& getDisplays();
97  static DisplayRef getDisplayForPoint( const Vec2i &pt );
99  static Area getSpanningArea();
100 
101 #if defined( CINDER_MAC )
102  static DisplayRef findFromCgDirectDisplayId( CGDirectDisplayID displayID );
103  static DisplayRef findFromNsScreen( NSScreen *nsScreen );
104 #elif defined( CINDER_MSW )
105  static DisplayRef findFromHmonitor( HMONITOR hMonitor );
107  static BOOL CALLBACK enumMonitorProc( HMONITOR hMonitor, HDC hdc, LPRECT rect, LPARAM lParam );
108 #endif
109 
110  friend std::ostream& operator<<( std::ostream &o, const Display &display )
111  {
112  return o << display.mArea << " @ " << display.mBitsPerPixel << "bpp @ scale " << display.mContentScale;
113  }
114 
115 #if defined( CINDER_COCOA_TOUCH )
116  signals::signal<void()>& getSignalDisplaysChanged() { return mSignalDisplaysChanged; }
118  template<typename T, typename Y>
119  void connectDisplaysChanged( T fn, Y *inst ) { getSignalDisplaysChanged().connect( std::bind( fn, inst ) ); }
120 #endif
121 
122  private:
123  Area mArea;
124  int mBitsPerPixel;
125  float mContentScale;
126 #if defined( CINDER_MAC )
127  NSScreen *mScreen;
128  CGDirectDisplayID mDirectDisplayID;
129 #elif defined( CINDER_COCOA_TOUCH )
130  UIScreen *mUiScreen;
131  std::vector<Vec2i> mSupportedResolutions;
132  signals::signal<void()> mSignalDisplaysChanged;
133 #elif defined( CINDER_MSW )
134  HMONITOR mMonitor;
135 #endif
136 
137  static void enumerateDisplays();
138 
139  static std::vector<DisplayRef> sDisplays;
140  static bool sDisplaysInitialized;
141 };
142 
143 } // namespace cinder
friend std::ostream & operator<<(std::ostream &o, const Display &display)
Definition: Display.h:110
float getAspectRatio() const
Returns the Display aspect ratio, which is its width / height.
Definition: Display.h:66
float getContentScale() const
Returns the factor which multiplies points to pixels. 2.0f for high-density (Retina) displays; 1...
Definition: Display.h:76
static Area getSpanningArea()
Returns the Area which spans all Displays.
Definition: Display.cpp:72
Definition: Area.h:37
static DisplayRef getMainDisplay()
Returns the system's primary display.
Definition: Display.cpp:302
bool contains(const Vec2i &offset) const
Definition: Area.cpp:112
static const std::vector< DisplayRef > & getDisplays()
Returns a vector of all displays connected to the system.
Definition: Display.cpp:54
Definition: Display.h:55
Vec2i getSize() const
Returns the size of the Display measured in points.
Definition: Display.h:64
Vec2i getSystemCoordinate(const Vec2i &displayRelativeCoordinate) const
Returns the system position (relative to the system's primary display's upper-left corner) of a Displ...
Definition: Display.cpp:297
int getBitsPerPixel() const
Returns the bits per pixel for the display. Typically 24 bits.
Definition: Display.h:74
int32_t getWidth() const
Definition: Area.h:47
Area getBounds() const
Returns the bounding Area of the Display in points, measured relative to primary display's upper-left...
Definition: Display.h:68
std::shared_ptr< class Display > DisplayRef
Definition: Display.h:53
bool contains(const Vec2i &pt) const
Returns whether the Display's coordinates contain pt.
Definition: Display.h:79
int getHeight() const
Returns the height of the screen measured in points.
Definition: Display.h:62
int getWidth() const
Returns the width of the screen measured in points.
Definition: Display.h:60
~Display()
Definition: Display.cpp:45
int32_t getHeight() const
Definition: Area.h:48
static DisplayRef getDisplayForPoint(const Vec2i &pt)
Returns the Display which contains a given point, measured relative to the upper-left corner of the p...
Definition: Display.cpp:61
Vec2< int > Vec2i
Definition: Vector.h:1313