Persisting data
If you want to persist your data, you should use the Preferences class. It is merely
a dictionary or a hash map data type which stores multiple key-value pairs in a
file. Libgdx will create a new preferences file on the fly if it does not exist yet. You
can have several preference files using unique names in order to split up data into
categories. To get access to a preference file, you need to request a Preferences
instance by its filename as follows:
Preferences prefs = Gdx.app.getPreferences("settings.prefs");
To write a (new) value, you have to choose a key under which the value should be
stored. If this key already exists in a preferences file, it will be overwritten. Do not
forget to call flush() afterwards to persist the data, or else all the changes will
be lost.
prefs.putInteger("sound_volume", 100); // volume @ 100%
prefs.flush();
Persisting data needs a lot more time than just modifying values in
memory (without flushing). Therefore, it is always better to modify as
many values as possible before a final flush() method is executed
To read back a certain value from a preferences file, you need to know the
corresponding key. If this key does not exist, it will be set to the default value.
You can optionally pass your own default value as the second argument
(for example, in the following listing, 50 is for the default sound volume):
int soundVolume = prefs.getInteger("sound_volume", 50);
Querying the Android API Level
On Android, you can query the Android API Level, which allows you to handle
things differently for certain versions of the Android OS. Use the following listing
to find out the version:
Gdx.app.getVersion();
On platforms other than Android, the version returned is always 0.
Querying memory usage
You can query the system to find out its current memory footprint of your
application. This may help you find excessive memory allocations that could lead to
application crashes. The following functions return the amount of memory (in bytes)
that is in use by the corresponding heap:
long memUsageJavaHeap = Gdx.app.getJavaHeap();
long memUsageNativeHeap = Gdx.app.getNativeHeap();
Graphics module
The graphics module can be accessed either through Gdx.getGraphics() or by
using the shortcut variable Gdx.graphics.
Querying delta time
Query Libgdx for the time span between the current and the last frame in seconds by
calling Gdx.graphics.getDeltaTime().
Querying display size
Query the device's display size returned in pixels by calling Gdx.graphics.
getWidth() and Gdx.graphics.getHeight().
Querying the FPS (frames per second) counter
Query a built-in frame counter provided by Libgdx to find the average number of
frames per second by calling Gdx.graphics.getFramesPerSecond().
Audio module
The audio module can be accessed either through Gdx.getAudio() or by using the
shortcut variable Gdx.audio.
Sound playback
To load sounds for playback, call Gdx.audio.newSound().
The supported file formats are WAV, MP3, and OGG.
There is an upper limit of 1 MB for decoded audio data. Consider the sounds to be
short effects like bullets or explosions so that the size limitation is not really an issue.
Music streaming
To stream music for playback, call Gdx.audio.newMusic().
The supported file formats are WAV, MP3, and OGG.
If you want to persist your data, you should use the Preferences class. It is merely
a dictionary or a hash map data type which stores multiple key-value pairs in a
file. Libgdx will create a new preferences file on the fly if it does not exist yet. You
can have several preference files using unique names in order to split up data into
categories. To get access to a preference file, you need to request a Preferences
instance by its filename as follows:
Preferences prefs = Gdx.app.getPreferences("settings.prefs");
To write a (new) value, you have to choose a key under which the value should be
stored. If this key already exists in a preferences file, it will be overwritten. Do not
forget to call flush() afterwards to persist the data, or else all the changes will
be lost.
prefs.putInteger("sound_volume", 100); // volume @ 100%
prefs.flush();
Persisting data needs a lot more time than just modifying values in
memory (without flushing). Therefore, it is always better to modify as
many values as possible before a final flush() method is executed
To read back a certain value from a preferences file, you need to know the
corresponding key. If this key does not exist, it will be set to the default value.
You can optionally pass your own default value as the second argument
(for example, in the following listing, 50 is for the default sound volume):
int soundVolume = prefs.getInteger("sound_volume", 50);
Querying the Android API Level
On Android, you can query the Android API Level, which allows you to handle
things differently for certain versions of the Android OS. Use the following listing
to find out the version:
Gdx.app.getVersion();
On platforms other than Android, the version returned is always 0.
Querying memory usage
You can query the system to find out its current memory footprint of your
application. This may help you find excessive memory allocations that could lead to
application crashes. The following functions return the amount of memory (in bytes)
that is in use by the corresponding heap:
long memUsageJavaHeap = Gdx.app.getJavaHeap();
long memUsageNativeHeap = Gdx.app.getNativeHeap();
Graphics module
The graphics module can be accessed either through Gdx.getGraphics() or by
using the shortcut variable Gdx.graphics.
Querying delta time
Query Libgdx for the time span between the current and the last frame in seconds by
calling Gdx.graphics.getDeltaTime().
Querying display size
Query the device's display size returned in pixels by calling Gdx.graphics.
getWidth() and Gdx.graphics.getHeight().
Querying the FPS (frames per second) counter
Query a built-in frame counter provided by Libgdx to find the average number of
frames per second by calling Gdx.graphics.getFramesPerSecond().
Audio module
The audio module can be accessed either through Gdx.getAudio() or by using the
shortcut variable Gdx.audio.
Sound playback
To load sounds for playback, call Gdx.audio.newSound().
The supported file formats are WAV, MP3, and OGG.
There is an upper limit of 1 MB for decoded audio data. Consider the sounds to be
short effects like bullets or explosions so that the size limitation is not really an issue.
Music streaming
To stream music for playback, call Gdx.audio.newMusic().
The supported file formats are WAV, MP3, and OGG.
No comments:
Post a Comment