CUGL 2.1
Cornell University Game Library
|
#include <CUDisplay.h>
Public Types | |
enum | Orientation : unsigned int { Orientation::FIXED = 0, Orientation::LANDSCAPE = 1, Orientation::PORTRAIT = 2, Orientation::LANDSCAPE_REVERSED = 3, Orientation::UPSIDE_DOWN = 4, Orientation::FACE_UP = 5, Orientation::FACE_DOWN = 6, Orientation::UNKNOWN = 7 } |
typedef std::function< void(Orientation previous, Orientation current, bool display)> | Listener |
Public Member Functions | |
std::string | getTitle () const |
void | setTitle (const std::string &title) |
void | setTitle (const char *title) |
void | show () |
void | hide () |
Rect | getBounds () const |
Rect | getPixelBounds () const |
Rect | getSafeBounds (bool display=true) |
Vec2 | getPixelDensity () const |
bool | isLandscape () const |
bool | isPortrait () const |
bool | hasNotch () const |
Orientation | getInitialOrientation () const |
Orientation | getDisplayOrientation () const |
Orientation | getDeviceOrientation () const |
Orientation | getDefaultOrientation () const |
bool | hasOrientationListener () const |
const Listener | getOrientationListener () const |
void | setOrientationListener (Listener listener) |
bool | removeOrientationListener () |
void | refresh () |
void | restoreRenderTarget () |
void | queryRenderTarget () |
Static Public Member Functions | |
static bool | start (std::string title, Rect bounds, Uint32 flags) |
static void | stop () |
static Display * | get () |
Static Public Attributes | |
static Uint32 | INIT_FULLSCREEN |
static Uint32 | INIT_HIGH_DPI |
static Uint32 | INIT_MULTISAMPLED |
static Uint32 | INIT_CENTERED |
Protected Member Functions | |
Display () | |
bool | init (std::string title, Rect bounds, Uint32 flags) |
void | dispose () |
~Display () | |
Protected Attributes | |
std::string | _title |
SDL_Window * | _window |
SDL_GLContext | _glContext |
int | _framebuffer |
int | _rendbuffer |
Rect | _bounds |
Rect | _usable |
Vec2 | _scale |
bool | _notched |
Listener | _orientationListener |
Orientation | _initialOrientation |
Orientation | _displayOrientation |
Orientation | _deviceOrientation |
Orientation | _defaultOrientation |
Static Protected Attributes | |
static Display * | _thedisplay |
Friends | |
class | Application |
This class is a singleton representing the native display.
The static methods of this class start() and stop() the SDL video system. Without it, you cannot draw anything. This should be the first and last methods called in any application. The Application class does this for you automatically.
The primary purpose of the display object is to initialize (and dispose) the OpenGL context. Any start-up features for OpenGL should go in this class. That are set via static attributes (like Application#setFullscreen(bool) that are read by the method start(). Setting these attributes after the display has started will have no effect (though this may be a feature for later releases).
The singleton display object also has several methods to get the (current) screen resolution and aspect ratio. The most important of these two is the the aspect ratio. Aspect ratio is one of the most unportable parts of cross-platform development. Everything else can be scaled to the screen, but the aspect ratio is fixed from the very beginning.
The singleton display also has information about the display and device orientation for mobile devices. In fact, is is possible to assign a listener to the object to respond to changes in device orientation.
If the device has multiple displays, this singleton will only refer to the main display.
This type represents a listener for an orientation change.
In CUGL, listeners are implemented as a set of callback functions, not as objects. For simplicity, Displays only have a single listener that handles both display and device changes (see getDisplayOrientation() and getDeviceOrientation().) If you wish for more than one listener, then your listener should handle its own dispatch.
Since the device orientation will always change when the display orientation does, this callback can easily safely handle both. The boolean parameter in the callback indicates whether or not a display orientation change has happened as well.
Unlike other events, this callback will be invoked at the end of an animation frame, after the screen has been drawn. So it will be processed before any input events waiting for the next frame.
The function type is equivalent to
std::function<void(Orientation previous, Orientation current, bool display)>
previous | The previous device orientation (before the change) |
current | The current device orientation (after the change) |
display | Whether the display orientation has changed as well |
|
strong |
The possible device/display orientations.
We use the same orientations for device and display even though these may not always agree (such as when the user has locked the display)
|
protected |
|
inlineprotected |
Deletes this object, releasing all resources.
This method quits the SDL video system and disposes the OpenGL context, effectively exitting and shutting down the entire program.
WARNING: This class is a singleton. You should never access this destructor directly. Use the stop() method instead.
|
protected |
Uninitializes this object, releasing all resources.
This method quits the SDL video system and disposes the OpenGL context, effectively exitting and shutting down the entire program.
WARNING: This class is a singleton. You should never access this method directly. Use the stop() method instead.
|
inlinestatic |
Returns the singleton instance for the display
You must call this static method first to get information about your specific display. This method will return nullptr until start() is called first.
|
inline |
Returns the full screen resolution for this display in points.
This method returns the bounds for the current resolution, not the maximum resolution. You should never change the resolution of a display. Allow the user to have their preferred resolution. Instead, you should adjust your camera to scale the viewport.
The value returned represents points, not pixels. If you are using a traditional display, these are the same. However, on Retina displays and other high DPI monitors, these may be different. Regardless, you should always work with points, not pixels, when computing the screen size. In particular, this is what you should assign the OpenGL viewport when using fullscreen.
|
inline |
Returns the default orientation of this device.
The default orientation corresponds to the intended orientiation that this mobile device should be held. For devices with home buttons, this home button is always expected at the bottom. For the vast majority of devices, this means the intended orientation is Portrait. However, some Samsung tablets have the home button oriented for Landscape.
This is important because the accelerometer axis is oriented relative to the default orientation. So a default landscape device will have a different accelerometer orientation than a portrait device.
If this display is not a mobile device, it will always return Orientation::FIXED.
|
inline |
Returns the current device orientation.
The device orientation is the orientation of a mobile device, as held by the user. It may or may not agree with the display orientation, which is how the screen is drawn. For example, if the screen is locked to landscape orientation, it is still possible for the device to have portrait orientation when it is held on its side.
This method is useful when you want to switch game modes for a different orientation (e.g. Powerpuff Girls Flipped Out).
If this display is not a mobile device, this method will always return Orientation::FIXED.
|
inline |
Returns the current display orientation.
This value is the current display orientation. The display orientation is the orientation of the coordinate space for drawing on a mobile device. In other words, the origin is at the bottom left of the screen in this device orientation.
This display orientation may or may not agree with the device orientation. In particular, it will not agree if the display orientation is locked (to say portrait or landscape only). However, this is the orientation that is important for drawing to the screen. To get the device orientation, call getDeviceOrientation().
If this display is not a mobile device, this method will always return Orientation::FIXED.
|
inline |
Returns the initial display orientation.
This value is the display orientation at startup. The display orientation is the orientation of the coordinate space for drawing on a mobile device. In other words, the origin is at the bottom left of the screen in this device orientation.
This display orientation may or may not agree with the device orientation. In particular, it will not agree if the display orientation is locked (to say portrait or landscape only). However, this is the orientation that is important for drawing to the screen. To get the device orientation, call getDeviceOrientation().
If this display is not a mobile device, this method will always return Orientation::FIXED.
|
inline |
Returns the listener for the display orientation.
This listener handles changes in either the device orientation (see getDeviceOrientation() or the display orientation (see getDeviceOrientation(). Since the device orientation will always change when the display orientation does, this callback can easily safely handle both. The boolean parameter in the callback indicates whether or not a display orientation change has happened as well.
Unlike other events, this listener will be invoked at the end of an animation frame, after the screen has been drawn. So it will be processed before any input events waiting for the next frame.
The display may only have one orientation listener at a time. If there is no listener, this method returns nullptr.
|
inline |
Returns the full screen resolution for this display in pixels.
This method returns the bounds for the current resolution, not the maximum resolution. You should never change the resolution of a display. Allow the user to have their preferred resolution. Instead, you should adjust your camera to scale the viewport.
The value returned represents the value pixels, not points. This is to help align the results with input devices on Retina displays and other high DPI monitors.
|
inline |
Returns the number of pixels for each point.
A point is a logical screen pixel. If you are using a traditional display, points and pixels are the same. However, on Retina displays and other high dpi monitors, they may be different. In particular, the number of pixels per point is a scaling factor times the point.
You should never need to use these scaling factor for anything, as it is not useful for determining anything other than whether a high DPI display is present. It does not necessarily refer to physical pixel on the screen. In some cases (OS X Retina displays), it refers to the pixel density of the backing framebuffer, which may be different from the physical framebuffer.
Rect cugl::Display::getSafeBounds | ( | bool | display = true | ) |
Returns the usable full screen resolution for this display in points.
Usable is a subjective term defined by the operating system. In general, it means the full screen minus any space used by important user interface elements, like a status bar (iPhone), menu bar (OS X), or task bar (Windows), or a notch (iPhone X). In the case of the latter, you can specify whether you want to use the display orientation or the device orientation.
This method computes the bounds for the current resolution, not the maximum resolution. You should never change the resolution of a display. Allow the user to have their preferred resolution. Instead, you should adjust your camera to scale the viewport.
The value returned represents points, not pixels. If you are using a traditional display, these are the same. However, on Retina displays and other high DPI monitors, these may be different. Regardless, you should always work with points, not pixels, when computing the screen size.
display | Whether to use the display (as opposed to the device) orientation |
|
inline |
Returns the title of this display
On a desktop, this title will be displayed at the top of the window.
|
inline |
Returns true if this device has a notch.
Notched devices are edgeless smartphones or tablets that include at dedicated area in the screen for a camera. Examples include the iPhone X.
If a device is notched you should call getSafeBounds() before laying out UI elements. It is acceptable to animate and draw backgrounds behind the notch, but it is not acceptable to place UI elements outside of these bounds.
|
inline |
Returns true if this display has an orientation listener
This listener handles changes in either the device orientation (see getDeviceOrientation() or the display orientation (see getDeviceOrientation(). Since the device orientation will always change when the display orientation does, this callback can easily safely handle both. The boolean parameter in the callback indicates whether or not a display orientation change has happened as well.
Unlike other events, this listener will be invoked at the end of an animation frame, after the screen has been drawn. So it will be processed before any input events waiting for the next frame.
The display may only have one orientation listener at a time.
void cugl::Display::hide | ( | ) |
Hides the window for this display (assuming it was visible).
This method does nothing if the window was not visible.
|
protected |
Initializes the display with the current screen information.
This method creates a display with the given title and bounds. As part of this initialization, it will create the OpenGL context, using the flags provided. The bounds are ignored if the display is fullscreen. In that case, it will use the bounds of the display.
This method gathers the native resolution bounds, pixel density, and orientation using platform-specific tools.
WARNING: This class is a singleton. You should never access this initializer directly. Use the start() method instead.
title | The window/display title |
bounds | The window/display bounds |
flags | The initialization flags |
bool cugl::Display::isLandscape | ( | ) | const |
Returns true if this device has a landscape orientation
bool cugl::Display::isPortrait | ( | ) | const |
Returns true if this device has a portrait orientation
void cugl::Display::queryRenderTarget | ( | ) |
Queries the identity of the default frame/render buffer.
This is necessary when you are using a RenderTarget and want to restore control the frame buffer. It is necessary because 0 is NOT necessarily the correct id of the default framebuffer (particularly on iOS).
void cugl::Display::refresh | ( | ) |
Refreshes the display.
This method will swap the OpenGL framebuffers, drawing the screen.
It will also reassess the orientation state and call the listener as necessary
bool cugl::Display::removeOrientationListener | ( | ) |
Removes the display orientation listener for this display.
This listener handles changes in either the device orientation (see getDeviceOrientation() or the display orientation (see getDeviceOrientation(). Since the device orientation will always change when the display orientation does, this callback can easily safely handle both. The boolean parameter in the callback indicates whether or not a display orientation change has happened as well.
Unlike other events, this listener will be invoked at the end of an animation frame, after the screen has been drawn. So it will be processed before any input events waiting for the next frame.
A display may only have one orientation listener at a time. If this display does not have an orientation listener, this method will fail.
void cugl::Display::restoreRenderTarget | ( | ) |
Restores the default frame/render buffer.
This is necessary when you are using a RenderTarget and want to restore control the frame buffer. It is necessary because 0 is NOT necessarily the correct id of the default framebuffer (particularly on iOS).
|
inline |
Sets the orientation listener for this display.
This listener handles changes in either the device orientation (see getDeviceOrientation() or the display orientation (see getDeviceOrientation(). Since the device orientation will always change when the display orientation does, this callback can easily safely handle both. The boolean parameter in the callback indicates whether or not a display orientation change has happened as well.
A display may only have one orientation listener at a time. If this display already has an orientation listener, this method will replace it for the once specified.
Unlike other events, this listener will be invoked at the end of an animation frame, after the screen has been drawn. So it will be processed before any input events waiting for the next frame.
listener | The listener to use |
void cugl::Display::setTitle | ( | const char * | title | ) |
Sets the title of this display
On a desktop, the title will be displayed at the top of the window.
title | The title of this display |
|
inline |
Sets the title of this display
On a desktop, the title will be displayed at the top of the window.
title | The title of this display |
void cugl::Display::show | ( | ) |
Shows the window for this display (assuming it was hidden).
This method does nothing if the window was not hidden.
|
static |
Starts up the SDL display and video system.
This static method needs to be the first line of any application, though it is handled automatically in the Application class.
This method creates the display with the given title and bounds. As part of this initialization, it will create the OpenGL context, using the flags provided. The bounds are ignored if the display is fullscreen. In that case, it will use the bounds of the display.
Once this method is called, the get() method will no longer return a null value.
title | The window/display title |
bounds | The window/display bounds |
flags | The initialization flags |
|
static |
Shuts down the SDL display and video system.
This static method needs to be the last line of any application, though it is handled automatically in the Application class. It will dipose of the display and the OpenGL context.
Once this method is called, the get() method will return nullptr. More importantly, no SDL function calls will work anymore.
|
friend |
This is called by the application loop
|
protected |
The full screen resolution of this device
|
protected |
The value of the default orientation
|
protected |
The value of the device orientation
|
protected |
The value of the display orientation
|
protected |
The default framebuffer associated with the display
|
protected |
The associated OpenGL drawing context
|
protected |
The value of the initial orientation
|
protected |
Whether this device has a notch in it
|
protected |
A listener for the orientation
|
protected |
The default renderbuffer associated with the display
|
protected |
The pixel density of the device
|
staticprotected |
The display singleton
|
protected |
The title (Window name) of the display
|
protected |
The full screen resolution minus menu bars and other features
|
protected |
The SDL window, which provides the OpenGL drawing context
|
static |
Whether this display should be centered (on windowed screens)
|
static |
Whether this display should use the fullscreen
|
static |
Whether this display should support a High DPI screen
|
static |
Whether this display should be multisampled