engine.app Functions
The App class contains functions to control the Unigine application graphic context, which includes:
- Update parameters
- Events of the controls (keyboard, mouse)
- FPS
- Window parameters (height, width, name, etc.)
The class is used, for example, when you need to integrate the Unigine engine into a custom application window or get and manage user input.
Usage Example
The following examples perform:
- Changing parameters of the application window (such as title, icon). The specified changes are applied after application start-up. It means that the window with default settings will appear first. If you need the custom settings are applied before the application window is displayed, you should implement your own class inherited from the App class.
- Processing user inputs.
- Updating the application in backgroud.
Changing Window Parameters
#include <core/unigine.h>
int init() {
// set a custom icon for the application window
Image icon = new Image("textures/window_icon.png");
engine.app.setIcon(icon);
// set a custom title for the application window
engine.app.setTitle("Custom Window Title");
return 1;
}
int update() {
Image mouse = new Image();
mouse.load("textures/mouse_pointer.png");
if (mouse.isLoaded() && mouse.getFormat() == IMAGE_FORMAT_RGBA8)
{
// set the image for the OS mouse pointer
engine.app.setMouseCursor(mouse);
// show the OS mouse pointer
engine.app.setMouseShow(1);
}
return 1;
}
Processing User Inputs
#include <core/unigine.h>
// declare a persecutor
PlayerPersecutor player;
int init() {
// update the application constantly
engine.app.setUpdate(1);
// get a node
Node node = engine.editor.getNodeByName("material_ball");
// create a new persecutor
player = new PlayerPersecutor();
// set the obtainde node as the target one
player.setTarget(node);
// set player's settings
player.setAnchor(Vec3(0,0,0.45f));
player.setPhiAngle(-130.0f);
player.setMinDistance(0.3f);
player.setMaxDistance(20.0f);
player.setDistance(5.0f);
player.setFov(35.0f);
// set the player to the Game Camera viewport
engine.game.setPlayer(player);
return 1;
}
int update() {
// change the distance between the player and the target node on mouse wheel scrolling
int scroll_delta = engine.app.getMouseAxis(APP_AXIS_Y);
if(scroll_delta != 0) {
float f_scroll_delta = scroll_delta / 30.0f;
player.setDistance(player.getDistance() - f_scroll_delta);
}
// zoom the camera in or out when the right mouse button is pressed and the mouse is moved backward/forward
if(engine.app.getMouseButton() == APP_BUTTON_RIGHT) {
float mouse_delta = (engine.gui.getMouseDY() + engine.gui.getMouseDX()) / 500.0f;
player.setDistance(player.getDistance() - mouse_delta);
}
return 1;
}
App Class
Members
int engine.app.isActive()
Returns the value indicating if the current application is active (i.e. in focus). The function will always return 1, if the window update mode is set to 1.The sounds will be automatically disabled, if the application is not active.
Return value
1 if application is active; otherwise, 0.void engine.app.setClipboard(string str)
Updates the contents of the system clipboard.Arguments
- string str - Contents to set.
string engine.app.getClipboard()
Retrieves the contents of the system clipboard.Return value
Contents of the system clipboard.int engine.app.isDone()
Returns a value indicating if the application is closed.Return value
1 if the application is closed; otherwise, 0.void engine.app.setFilter(float filter)
Filters the FPS counter value. By the value of 0, filtering is disabled and the current FPS value is always shown. The default is 0.9f (10 percent of the current FPS is taken into account).Arguments
- float filter - Filter value in range [0;1). The default value is 0.9f (10 percent of the current FPS is taken into account).
int engine.app.getFlags()
Returns the video mode flags set for the application.Return value
Video mode flags: APP_DESTROY, APP_RESIZABLE, APP_FULLSCREEN, etc.float engine.app.getFps()
Returns the application FPS counter value.Return value
Application FPS counter value.float engine.app.getMinFps()
Returns the minimum FPS counter value for the last 600 frames.Return value
Minimum FPS counter value for the last 600 frames.float engine.app.getMaxFps()
Returns the maximum FPS counter value for the last 600 frames.Return value
Maximum FPS counter value for the last 600 frames.float engine.app.getMeanFps()
Returns the average FPS counter value for the last 600 frames.Return value
Average FPS counter value for the last 600 frames.float engine.app.getFTime()
Returns time spent between the previous update and the current one.Return value
Application frame duration in seconds.int engine.app.setGamma(float gamma)
Sets the gamma correction value.Arguments
- float gamma - Gamma correction value. If the 1.0f value is provided, no gamma correction is applied.
Return value
1 if the gamma correction value is set successfully; otherwise, 0.engine.app.getHandle()
Returns a pointer to the application handle.Return value
Pointer to the application handle.void engine.app.setHeight(int value)
Sets the new height for the application window. The height is specified for the window inside and doesn't include the window borders.Arguments
- int value - Application window height, in pixels.
int engine.app.getHeight()
Returns the current height of the application window. The returned value is the window inside height without borders.Return value
Current height of the application window, in pixels.int engine.app.setIcon(Image data)
Sets an icon for the application window. The image must be of the square size and RGBA8 format.engine.app.setIcon(new Image("icons/my_icon.png"));
Arguments
- Image data - Icon to be set.
Return value
1 if the icon is set successfully; otherwise, 0.float engine.app.getIFps()
Returns an inverse value of the application FPS counter.Return value
Inverse value of the application FPS counter (1/FPS).string engine.app.getKeyName(int key)
Returns a name of the given key.log.message("engine.app.getKeyName() returned: %s\n", engine.app.getKeyName(APP_KEY_ALT));
log.message("engine.app.getKeyName() returned: %s\n", engine.app.getKeyName(97));
getKeyName() returned: ALT
getKeyName() returned: a
Arguments
- int key - Key in one of the following formats:
- Character format (for example, 'a')
- Standard ASCII key code (for example, 97)
- One of APP_KEY_* variables
Return value
Key name.void engine.app.setKeyPressFunc(int ( * func)(unsigned int) key)
Application event which is called on pressing the key. The engine will stop event processing if the event function returns 1.Arguments
- int ( * func)(unsigned int) key - Event function pointer.
void engine.app.setKeyPressUnicodeFunc(int ( * func)(unsigned int) key)
Application event which is called on pressing the unicode key. The engine will stop event processing if the event function returns 1.Arguments
- int ( * func)(unsigned int) key - Event function pointer.
void engine.app.setKeyReleaseFunc(int ( * func)(unsigned int) key)
Application event which is called on releasing the key. The engine will stop event processing if the event function returns 1.Arguments
- int ( * func)(unsigned int) key - Event function pointer.
void engine.app.setKeyState(int key, int state)
Sets the keyboard key state (pressed or not pressed).// set the ALT button pressed
engine.app.setKeyState(APP_KEY_ALT, 1);
// set the 'a' button pressed
engine.app.setKeyState(97, 1);
Arguments
- int key - Key ID. Possible values can be in range [0;255], which are standard ASCII code symbols, or in range [APP_KEY_ESC;APP_KEY_F12], which are used for control buttons. For full list of control buttons see APP_KEY_* variables.
- int state - Key state: 1 to set the button pressed; 0 to set the button not pressed.
int engine.app.getKeyState(int key)
Returns the keyboard key state (pressed or not pressed).if (engine.app.getKeyState(APP_KEY_ALT)) log.message("ALT has been pressed\n");
if (engine.app.getKeyState(97)) log.message("'a' has been pressed\n");
Arguments
- int key - Key ID. Possible values can be in range [0;255], which are standard ASCII code symbols, or in range [APP_KEY_ESC;APP_KEY_F12], which are used for control buttons. For full list of control button see APP_KEY_* variables.
Return value
1 if the key is pressed; otherwise, 0.void engine.app.setMouse(int x, int y)
Sets a mouse pointer to the specified position in the coordinate system of the application window.// set a position of the mouse pointer
int mouse_x = engine.app.getWidth() / 2;
int mouse_y = engine.app.getHeight() / 2;
// set the mouse pointer to the position on pressing Esc
if (engine.app.clearKeyState(APP_KEY_ESC))
{
// check if the mouse pointer is bound to the application window
if (engine.app.getMouseGrab())
// set the pointer to the position
engine.app.setMouse(mouse_x, mouse_y);
}
Arguments
- int x - X coordinate of the mouse pointer.
- int y - Y coordinate of the mouse pointer.
void engine.app.setMouseAxis(int axis, int value)
Sets the mouse wheel delta along the specified axis. The delta value is changed when scrolling the mouse wheel horizontally (APP_AXIS_X) or vertically (APP_AXIS_Y).Arguments
- int axis - Mouse axis. Possible values are APP_AXIS_X, APP_AXIS_Y.
- int value - Mouse wheel delta.
int engine.app.getMouseAxis(int axis)
Returns the current mouse position delta along the specified axis. The delta value is changed when scrolling the mouse wheel horizontally (APP_AXIS_X) or vertically (APP_AXIS_Y).int wheel = 0;
int axis_x = engine.app.getMouseAxis(APP_AXIS_X);
int axis_y = engine.app.getMouseAxis(APP_AXIS_Y);
if(axis_x > 0) log.message("Mouse wheel has been scrolled right\n");
if(axis_x < 0) log.message("Mouse wheel has been scrolled left\n");
if(axis_y > 0) log.message("Mouse wheel has been scrolled up\n");
if(axis_y < 0) log.message("Mouse wheel has been scrolled down\n");
Arguments
- int axis - Mouse axis. Possible values are APP_AXIS_X, APP_AXIS_Y.
Return value
Mouse wheel delta regarding the last wheel position.void engine.app.setMouseButton(int arg1)
Simulates pressing of the specified mouse buttons.// set the right and middle mouse buttons pressed in addition to already pressed buttons
engine.app.setMouseButton(engine.app.getMouseButton() | APP_BUTTON_RIGHT | APP_BUTTON_MIDDLE);
Arguments
- int arg1 - Mouse buttons to be pressed. Possible values are APP_BUTTON_*.
int engine.app.getMouseButton()
Returns the current states of all mouse buttons. Each bit of the returned bit mask will specify the state of the buttons in the following order:- APP_BUTTON_LEFT = 1 << 0
- APP_BUTTON_MIDDLE
- APP_BUTTON_RIGHT
- APP_BUTTON_DCLICK
- APP_BUTTON_AUX_0
- APP_BUTTON_AUX_1
- APP_BUTTON_AUX_2
- APP_BUTTON_AUX_3
Return value
A bit mask that stores current states of all mouse buttons (pressed or not pressed).string engine.app.getMouseButtonName(int button)
Returns a name of the given button.Arguments
- int button - One of APP_BUTTON_* variables.
Return value
Button name.int engine.app.getMouseButtonState(int button)
Returns a state of the specified button.if (engine.app.getMouseButtonState(APP_BUTTON_LEFT | APP_BUTTON_DCLICK)) log.message("Left mouse button has been clicked\n");
if (engine.app.getMouseButtonState(APP_BUTTON_MIDDLE)) log.message("Middle mouse button has been clicked\n");
Arguments
- int button - One of APP_BUTTON_* variables.
Return value
1 if al least one of the specified buttons is pressed; otherwise, 0.int engine.app.setMouseCursor(Image data, int x = 0, int y = 0)
Sets a custom image for the OS mouse cursor. The image must be of the square size and RGBA8 format.engine.app.setMouseCursor(new Image("textures/my_cursor.png"));
// show the OS mouse pointer
engine.app.setMouseShow(1);
Arguments
- Image data - Cursor image.
- int x - X coordinate of the cursor's hot spot.
- int y - Y coordinate of the cursor's hot spot.
Return value
1 if the image is set successfully; otherwise, 0.void engine.app.setMouseGrab(int arg1)
Sets a value indicating if the mouse pointer is bound to the application window.Arguments
- int arg1 - 1 if the mouse cannot leave the application window; otherwise, 0.
int engine.app.getMouseGrab()
Returns a value indicating if the mouse pointer is bound to the application window.Return value
1 if the mouse pointer cannot leave the application window; otherwise, 0.void engine.app.setMouseShow(int arg1)
Sets a value indicating if the OS mouse pointer should be displayed.Arguments
- int arg1 - 1 if the OS mouse pointer should be displayed; 0 if the application cursor is used only.
int engine.app.getMouseShow()
Returns a value indicating if the OS mouse pointer is displayed.Return value
1 if the OS mouse pointer is displayed; otherwise, 0.int engine.app.getMouseX()
Returns the current X coordinate of the mouse pointer in the coordinate system of the application window.Return value
X coordinate of the mouse pointer.int engine.app.getMouseY()
Returns the current Y coordinate of the mouse pointer in the coordinate system of the application window.Return value
Y coordinate of the mouse pointer.string engine.app.getName()
Returns a name of the graphics API used for application rendering.Return value
One of the following values:- opengl for OpenGL
- direct3d11 for DirectX 11
- NULL if no graphics API is used for application rendering
int engine.app.isNULLInitialized()
Returns a value indicating whether the NULL renderer is initialized.Return value
1 if the renderer is initialized; otherwise, 0.int engine.app.getNumTouches()
Returns the number of touches. The maximum number of touches is 16.Return value
Number of touches.int engine.app.setPosition(int x, int y)
Sets the window position.Arguments
- int x - X coordinate of the upper-left corner of the window.
- int y - Y coordinate of the upper-left corner of the window.
Return value
1 if the position of the window is set successfully; otherwise, 0.int engine.app.getPositionX()
Returns the X coordinate of the upper-left corner of the window.Return value
X coordinate of the upper-left corner of the window.int engine.app.getPositionY()
Returns the Y coordinate of the upper-left corner of the window.Return value
Y coordinate of the upper-left corner of the window.int engine.app.setTitle(string title)
Sets a title for the application window.Arguments
- string title
Return value
1 if the title is set successfully; otherwise, 0.int engine.app.getTouchX(int touch)
Returns the X coordinate of the touch in the coordinate system of the application window.Arguments
- int touch - The touch number.
Return value
X coordinate of the touch.int engine.app.getTouchY(int touch)
Returns the Y coordinate of the touch in the coordinate system of the application window.Arguments
- int touch - The toucn number.
Return value
Y coordinate of the touch.int engine.app.setUpdate(int update)
Sets the value indicating whether the application is updated when the window is hidden.Arguments
- int update - Window update mode: 1 for constantly repeating update cycle (i.e. the application is updated if the window is hidden); otherwise, 0.
Return value
1 if the window update mode is set successfully; otherwise, 0.int engine.app.getUpdate()
Returns the current window update mode (i.e. a value indicating whether the application is updated when the window is hidden).Return value
1 if the update cycle is constantly repeated; otherwise, 0.int engine.app.setVideoMode(int width, int height, int flags = 0, int refresh = 0)
Sets a video mode and initializes the application.Arguments
- int width - Video width in pixels.
- int height - Video height in pixels.
- int flags - Video mode flags: APP_DESTROY, APP_RESIZABLE, APP_FULLSCREEN, APP_FULLWINDOW, APP_VSYNC, APP_DEBUG_CONTEXT, APP_BREAK_ON_ERROR.
- int refresh - Force the refresh rate in Hz; if 0 is provided, the default refresh rate is used.
Return value
1 if the video mode is set and the application is initialized successfully; otherwise, 0.string engine.app.getVideoModeName()
Returns the name of the current video mode.Return value
Returns the name of the current video mode.void engine.app.setWidth(int value)
Sets the new width of the application window. The width is specified for the window inside part and doesn't include the window borders.Arguments
- int value - Window width, in pixels.
int engine.app.getWidth()
Returns the current application window width. The returned value is the window inside part width without borders.Return value
Window width, in pixels.int engine.app.clearKeyState(int key)
Returns the key state and clears it to 0 (not pressed). This function allows returning the key state only once even if it is kept pressed over several frames.Arguments
- int key - Key ID. Possible values can be in range [0;255], which are standard ASCII code symbols, or in range [APP_KEY_ESC;APP_KEY_F12], which are used for control buttons. For full list of control buttons see APP_KEY_* variables.
Return value
1 if the key is pressed, and this function was not called previously in the current frame; otherwise, 0.int engine.app.clearMouseAxis(int axis)
Returns the current mouse wheel position delta and clears it to 0.Arguments
- int axis - Mouse wheel axis. Possible values are APP_AXIS_X, APP_AXIS_Y.
Return value
Mouse wheel delta regarding the last mouse wheel position.int engine.app.clearMouseButtonState(int button)
Returns the button state and clears it to 0 (not pressed). This function allows returning the button state only once even if it is kept pressed over several frames. The function will return 1 if at least one of the specified buttons is pressed.// the function will return 1 if one of the buttons is pressed;
// the button state will be set to 0
engine.app.clearMouseButtonState(APP_BUTTON_LEFT | APP_BUTTON_RIGHT);
Arguments
- int button - Button. Possible values are APP_BUTTON_*.
Return value
1 if at least one of the buttons is pressed, and this function was not called previously in the current frame; otherwise, 0.void engine.app.destroy()
Application requests the engine to destroy.int engine.app.dialogMessage(string title, string str, string flags = 0)
Creates a system modal message dialog. Since the dialog is modal, all other actions are stopped until this function returns a value.If you don't specify flags, the Oc combination will be used by default.
Arguments
- string title - Title of the dialog window.
- string str - Content of the dialog window.
- string flags - Dialog window flags. The argument accepts a string with names of buttons. The following combinations are available:
- Oc or oC - OK, Cancel.
- Yn or yN - Yes, No
- ynC, yNc or Ync - Yes, No, Cancel
If you send any other combination, only the OK button will appear in the window.The uppercase letter indicates which button will be in focus.
Return value
The function returns one of the following values:- 1 if the OK button has been pressed.
- 2 if the Yes button has been pressed.
- -1 if the No button has been pressed.
- 0 if the Cancel button has been pressed.
void engine.app.exit()
The engine requests to exit the application.int engine.app.restoreVideoMode()
Restores the video mode of the operating system.Return value
1 if the video mode is restored successfully; otherwise, 0.void engine.app.startFps()
Starts the FPS counter if it was stopped. All function calls are placed into a stack, so the number of calls to this function should correspond to the number of calls to the stopFps() function.void engine.app.stopFps()
Stops the FPS counter. This function should be called if application window is hidden or some heavy non-rendering tasks are processing. All function calls are placed into a stack, so the number of calls to this function should correspond to the number of calls to the startFps() function.void engine.app.updateFps()
Updates all FPS statistics.int APP_AXIS_X
Description
A mouse wheel is scrolled horizontally (along the X axis; a horizontal mouse wheel is required).int APP_AXIS_Y
Description
A mouse wheel is scrolled vertically (along the Y axis; a vertical mouse wheel is used).int APP_BREAK_ON_ERROR
Description
Break if an error occurs.int APP_BUTTON_AUX_0
Description
Detected activity of the first auxiliary mouse button.int APP_BUTTON_AUX_1
Description
Detected activity of the second auxiliary mouse button.int APP_BUTTON_AUX_2
Description
Detected activity of the third auxiliary mouse button.int APP_BUTTON_AUX_3
Description
Detected activity of the fourth auxiliary mouse button.int APP_BUTTON_DCLICK
Description
Left mouse button is double-clicked.int APP_BUTTON_LEFT
Description
Detected activity of the left mouse button.int APP_BUTTON_MIDDLE
Description
Detected activity of the middle mouse button (scrolling wheel).int APP_BUTTON_RIGHT
Description
Detected activity of the right mouse button.int APP_DEBUG_CONTEXT
Description
Debug context is enabled.int APP_DESTROY
Description
Fullscreen mode is destroyed.int APP_FULLSCREEN
Description
Fullscreen mode.int APP_FULLWINDOW
Description
Full window mode.int APP_KEY_ALT
Description
The ALT key is pressed.int APP_KEY_BACKSPACE
Description
The Backspace key is pressed.int APP_KEY_CAPS
Description
The Caps Lock key is pressed.int APP_KEY_CMD
Description
The Command key is pressed (for OS X).int APP_KEY_CTRL
Description
The Ctrl key is pressed.int APP_KEY_DELETE
Description
The Delete key is pressed.int APP_KEY_DOWN
Description
The Down Arrow key is pressed.int APP_KEY_END
Description
The End key is pressed.int APP_KEY_ESC
Description
The Esc key is pressed.int APP_KEY_F1
Description
The F1 key is pressed.int APP_KEY_F10
Description
The F10 key is pressed.int APP_KEY_F11
Description
The F11 key is pressed.int APP_KEY_F12
Description
The F12 key is pressed.int APP_KEY_F2
Description
The F2 key is pressed.int APP_KEY_F3
Description
The F3 key is pressed.int APP_KEY_F4
Description
The F4 key is pressed.int APP_KEY_F5
Description
The F5 key is pressed.int APP_KEY_F6
Description
The F6 key is pressed.int APP_KEY_F7
Description
The F7 key is pressed.int APP_KEY_F8
Description
The F8 key is pressed.int APP_KEY_F9
Description
The F9 key is pressed.int APP_KEY_HOME
Description
The Home key is pressed.int APP_KEY_INSERT
Description
The Insert key is pressed.int APP_KEY_LEFT
Description
The Left Arrow key is pressed.int APP_KEY_NUM
Description
The Num Lock key is pressed.int APP_KEY_PGDOWN
Description
The Page Down key is pressed.int APP_KEY_PGUP
Description
The Page Up key is pressed.int APP_KEY_RETURN
Description
The Enter (Return) key is pressed.int APP_KEY_RIGHT
Description
The Right Arrow key is pressed.int APP_KEY_SCROLL
Description
The Scroll Lock key is pressed.int APP_KEY_SHIFT
Description
The Shift key is pressed.int APP_KEY_TAB
Description
The Tab key is pressed.int APP_KEY_UP
Description
The Page Up key is pressed.int APP_NUM_AXES
Description
Number of axes, along which a mouse wheel can be scrolled.int APP_NUM_KEYS
Description
Detected number of keyboard keys.int APP_NUM_TOUCHES
Description
Number of the detached touches (for touch-screen controls).int APP_RESIZABLE
Description
Resizable window.int APP_VSYNC
Description
Vertical syncronization.Last update: 2017-12-29
Help improve this article
Was this article helpful?
(or select a word/phrase and press Ctrl+Enter)