Interface Application
-
public interface ApplicationAn
Applicationis the main entry point of your project. It sets up a window and rendering surface and manages the different aspects of your application, namelyGraphics,Audio,InputandFiles. Think of an Application being equivalent to Swing'sJFrameor Android'sActivity.An application can be an instance of any of the following:
- a desktop application (see
JglfwApplicationfound in gdx-backends-jglfw.jar) - an Android application (see
AndroidApplicationfound in gdx-backends-android.jar) - a HTML5 application (see
GwtApplicationfound in gdx-backends-gwt.jar) - an iOS application (see
IOSApplicationfound in gdx-backends-robovm.jar)
While game programmers are used to having a main loop, libGDX employs a different concept to accommodate the event based nature of Android applications a little more. You application logic must be implemented in a
ApplicationListenerwhich has methods that get called by the Application when the application is created, resumed, paused, disposed or rendered. As a developer you will simply implement the ApplicationListener interface and fill in the functionality accordingly. The ApplicationListener is provided to a concrete Application instance as a parameter to the constructor or another initialization method. Please refer to the documentation of the Application implementations for more information. Note that the ApplicationListener can be provided to any Application implementation. This means that you only need to write your program logic once and have it run on different platforms by passing it to a concrete Application implementation.The Application interface provides you with a set of modules for graphics, audio, input and file i/o.
Graphicsoffers you various methods to output visuals to the screen. This is achieved via OpenGL ES 2.0 or 3.0 depending on what's available an the platform. On the desktop the features of OpenGL ES 2.0 and 3.0 are emulated via desktop OpenGL. On Android the functionality of the Java OpenGL ES bindings is used.Audiooffers you various methods to output and record sound and music. This is achieved via the Java Sound API on the desktop. On Android the Android media framework is used.Inputoffers you various methods to poll user input from the keyboard, touch screen, mouse and accelerometer. Additionally you can implement anInputProcessorand use it withInput.setInputProcessor(InputProcessor)to receive input events.Filesoffers you various methods to access internal and external files. An internal file is a file that is stored near your application. On Android internal files are equivalent to assets. On the desktop the classpath is first scanned for the specified file. If that fails then the root directory of your application is used for a look up. External files are resources you create in your application and write to an external storage. On Android external files reside on the SD-card, on the desktop external files are written to a users home directory. If you know what you are doing you can also specify absolute file names. Absolute filenames are not portable, so take great care when using this feature.Netoffers you various methods to perform network operations, such as performing HTTP requests, or creating server and client sockets for more elaborate network programming.The
Applicationalso has a set of methods that you can use to query specific information such as the operating system the application is currently running on and so forth. This allows you to have operating system dependent code paths. It is however not recommended to use this facilities.The
Applicationalso has a simple logging method which will print to standard out on the desktop and to logcat on Android. - a desktop application (see
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classApplication.ApplicationTypeEnumeration of possibleApplicationtypes
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidaddLifecycleListener(LifecycleListener listener)Adds a newLifecycleListenerto the application.voiddebug(java.lang.String tag, java.lang.String message)Logs a debug message to the console or logcatvoiddebug(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs a debug message to the console or logcatvoiderror(java.lang.String tag, java.lang.String message)Logs an error message to the console or logcatvoiderror(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs an error message to the console or logcatvoidexit()Schedule an exit from the application.ApplicationListenergetApplicationListener()ApplicationLoggergetApplicationLogger()AudiogetAudio()ClipboardgetClipboard()FilesgetFiles()GraphicsgetGraphics()InputgetInput()longgetJavaHeap()intgetLogLevel()Gets the log level.longgetNativeHeap()NetgetNet()PreferencesgetPreferences(java.lang.String name)Returns thePreferencesinstance of this Application.Application.ApplicationTypegetType()intgetVersion()voidlog(java.lang.String tag, java.lang.String message)Logs a message to the console or logcatvoidlog(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs a message to the console or logcatvoidpostRunnable(java.lang.Runnable runnable)Posts aRunnableon the main loop thread.voidremoveLifecycleListener(LifecycleListener listener)Removes theLifecycleListener.voidsetApplicationLogger(ApplicationLogger applicationLogger)Sets the current Application logger.voidsetLogLevel(int logLevel)Sets the log level.
-
-
-
Field Detail
-
LOG_NONE
static final int LOG_NONE
- See Also:
- Constant Field Values
-
LOG_DEBUG
static final int LOG_DEBUG
- See Also:
- Constant Field Values
-
LOG_INFO
static final int LOG_INFO
- See Also:
- Constant Field Values
-
LOG_ERROR
static final int LOG_ERROR
- See Also:
- Constant Field Values
-
-
Method Detail
-
getApplicationListener
ApplicationListener getApplicationListener()
- Returns:
- the
ApplicationListenerinstance
-
log
void log(java.lang.String tag, java.lang.String message)Logs a message to the console or logcat
-
log
void log(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs a message to the console or logcat
-
error
void error(java.lang.String tag, java.lang.String message)Logs an error message to the console or logcat
-
error
void error(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs an error message to the console or logcat
-
debug
void debug(java.lang.String tag, java.lang.String message)Logs a debug message to the console or logcat
-
debug
void debug(java.lang.String tag, java.lang.String message, java.lang.Throwable exception)Logs a debug message to the console or logcat
-
setLogLevel
void setLogLevel(int logLevel)
-
getLogLevel
int getLogLevel()
Gets the log level.
-
setApplicationLogger
void setApplicationLogger(ApplicationLogger applicationLogger)
Sets the current Application logger. Calls tolog(String, String)are delegated to thisApplicationLogger
-
getApplicationLogger
ApplicationLogger getApplicationLogger()
- Returns:
- the current
ApplicationLogger
-
getType
Application.ApplicationType getType()
- Returns:
- what
Application.ApplicationTypethis application has, e.g. Android or Desktop
-
getVersion
int getVersion()
- Returns:
- the Android API level on Android, the major OS version on iOS (5, 6, 7, ..), or 0 on the desktop.
-
getJavaHeap
long getJavaHeap()
- Returns:
- the Java heap memory use in bytes
-
getNativeHeap
long getNativeHeap()
- Returns:
- the Native heap memory use in bytes
-
getPreferences
Preferences getPreferences(java.lang.String name)
Returns thePreferencesinstance of this Application. It can be used to store application settings across runs.- Parameters:
name- the name of the preferences, must be useable as a file name.- Returns:
- the preferences.
-
getClipboard
Clipboard getClipboard()
-
postRunnable
void postRunnable(java.lang.Runnable runnable)
Posts aRunnableon the main loop thread. In a multi-window application, the Gdx.graphics and Gdx.input values may be unpredictable at the time the Runnable is executed. If graphics or input are needed, they can be copied to a variable to be used in the Runnable. For example:final Graphics graphics = Gdx.graphics;- Parameters:
runnable- the runnable.
-
exit
void exit()
Schedule an exit from the application. On android, this will cause a call to pause() and dispose() some time in the future, it will not immediately finish your application. On iOS this should be avoided in production as it breaks Apples guidelines
-
addLifecycleListener
void addLifecycleListener(LifecycleListener listener)
Adds a newLifecycleListenerto the application. This can be used by extensions to hook into the lifecycle more easily. TheApplicationListenermethods are sufficient for application level development.- Parameters:
listener-
-
removeLifecycleListener
void removeLifecycleListener(LifecycleListener listener)
Removes theLifecycleListener.- Parameters:
listener-
-
-