FPLBase
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Utilities

General utility functions, used by FPLBase. More...

Detailed Description

General utility functions, used by FPLBase.

Classes

struct  fplbase::HighPerformanceParams
 HighPerformanceParams are used on Android to configure simulated key presses, in order to stop the CPU governor (see https://www.kernel.org/doc/Documentation/cpu-freq/governors.txt) from reducing the CPU frequency when a user is not providing tactile input. More...
 

Typedefs

typedef std::function< bool(const
char *filename, std::string
*dest)> 
fplbase::LoadFileFunction
 Called by LoadFile().
 
typedef void(* fplbase::VsyncCallback )(void)
 Used for Android to represent a Vsync callback function.
 

Enumerations

enum  fplbase::LogCategory {
  kApplication = 0, kError = 1, kSystem = 3, kAudio = 4,
  kVideo = 5, kRender = 6, kInput = 7, kCustom = 19
}
 Constants for use with LogInfo, LogError, etc.
 
enum  fplbase::PerformanceMode { kNormalPerformance = 0, kHighPerformance }
 Enum for use with the Set/GetPerformanceMode() functions.
 

Functions

bool fplbase::LoadFileRaw (const char *filename, std::string *dest)
 Loads a file and returns its contents via string pointer. More...
 
bool fplbase::LoadFile (const char *filename, std::string *dest)
 Loads a file and returns its contents via string pointer. More...
 
LoadFileFunction fplbase::SetLoadFileFunction (LoadFileFunction load_file_function)
 Set the function called by LoadFile(). More...
 
bool fplbase::SaveFile (const char *filename, const std::string &data)
 Save a string to a file, overwriting the existing contents. More...
 
bool fplbase::SaveFile (const char *filename, const void *data, size_t size)
 Save a string to a file, overwriting the existing contents. More...
 
bool fplbase::LoadPreferences (const char *filename, std::string *dest)
 Load preference settings. More...
 
bool fplbase::SavePreferences (const char *filename, const void *data, size_t size)
 Save preference settings. More...
 
int32_t fplbase::LoadPreference (const char *key, int32_t initial_value)
 Load a single integer value to a preference. More...
 
bool fplbase::SavePreference (const char *key, int32_t value)
 Save a single integer value to a preference. More...
 
const void * fplbase::MapFile (const char *filename, int32_t offset, int32_t *size)
 Map a file into memory and returns its contents via pointer. More...
 
void fplbase::UnmapFile (const void *file, int32_t size)
 Unmap a pointer that is mapped via MapFile() API. More...
 
bool fplbase::ChangeToUpstreamDir (const char *const binary_dir, const char *const target_dir)
 Search and change to a given directory. More...
 
bool fplbase::MipmapGeneration16bppSupported ()
 check if 16bpp MipMap is supported. More...
 
int32_t fplbase::GetSystemRamSize ()
 Get the system's RAM size. More...
 
void fplbase::LogInfo (const char *fmt, va_list args)
 Log a format string with Info priority to the console. More...
 
void fplbase::LogError (const char *fmt, va_list args)
 Log a format string with Error priority to the console. More...
 
void fplbase::LogInfo (LogCategory category, const char *fmt, va_list args)
 Log a format string with Info priority to the console. More...
 
void fplbase::LogError (LogCategory category, const char *fmt, va_list args)
 Log a format string with Error priority to the console. More...
 
void fplbase::LogInfo (const char *fmt,...)
 Log a format string with Info priority to the console. More...
 
void fplbase::LogError (const char *fmt,...)
 Log a format string with Error priority to the console. More...
 
void fplbase::LogInfo (LogCategory category, const char *fmt,...)
 Log a format string with Info priority to the console. More...
 
void fplbase::LogError (LogCategory category, const char *fmt,...)
 Log a format string with Error priority to the console. More...
 
jobject fplbase::AndroidGetActivity ()
 Get the Android activity class. More...
 
JNIEnv * fplbase::AndroidGetJNIEnv ()
 Get the Java native interface object (JNIEnv). More...
 
VsyncCallback fplbase::RegisterVsyncCallback (VsyncCallback callback)
 Register for handling vsync callbacks on android. More...
 
void fplbase::WaitForVsync ()
 Blocks until the next time a VSync happens.
 
int fplbase::GetVsyncFrameId ()
 Get Vsync frame id. More...
 
void fplbase::SendKeypressEventToAndroid (int android_keycode)
 Triggers a keypress event on an Android device. More...
 
std::string fplbase::AndroidGetActivityName ()
 Get the name of the current activity class. More...
 
std::string fplbase::AndroidGetViewIntentData ()
 Determine whether the activity was started with Intent.ACTION_VIEW and, if so, return the data the user wants to "view" in the application. More...
 
bool fplbase::GetStoragePath (const char *app_name, std::string *path)
 Retrieve a path where an app can store data files. More...
 
bool fplbase::SupportsHeadMountedDisplay ()
 Checks whether Head Mounted Displays, such as Cardboard, are supported by the system being run on. More...
 
bool fplbase::TouchScreenDevice ()
 Checks if the device has a touchscreen. More...
 
bool fplbase::IsTvDevice ()
 Checks whether the device we are running on is an Android TV device. More...
 
void fplbase::SetPerformanceMode (PerformanceMode new_mode)
 Sets the performance mode. More...
 
PerformanceMode fplbase::GetPerformanceMode ()
 Get the current performance mode. More...
 
void fplbase::RelaunchApplication ()
 Relaunch the application.
 
void fplbase::SetHighPerformanceParameters (const HighPerformanceParams &params)
 Sets the specific parameters used by high-performance mode on Android. More...
 
const HighPerformanceParams & fplbase::GetHighPerformanceParameters ()
 Get the high performance parameters. More...
 
std::string fplbase::DeviceModel ()
 Get the Android device's model. More...
 
int32_t fplbase::AndroidGetApiLevel ()
 Get the Android device's API level. More...
 

Variables

const int fplbase::kDefaultAndroidKeycode = 115
 Used for Android to simulate a keypress. Corresponds to F24, which is unavailable on most keyboards.
 
const double fplbase::kDefaultTimeBetweenPresses = 1.0
 Used for Android. Corresponds to the time, in seconds, between simulated keypresses.
 

Function Documentation

jobject fplbase::AndroidGetActivity ( )

Get the Android activity class.

Returns
Returns a pointer to the Java instance of the activity class in an Android application.
std::string fplbase::AndroidGetActivityName ( )

Get the name of the current activity class.

Note
This can be used by C++ code to determine how the application was started.
Returns
Returns a std::string containing the Android activity name.
int32_t fplbase::AndroidGetApiLevel ( )

Get the Android device's API level.

Returns
Returns the API level of the Android device the app is currently being run on.
JNIEnv* fplbase::AndroidGetJNIEnv ( )

Get the Java native interface object (JNIEnv).

Returns
Returns a pointer to the Java native interface object (JNIEnv) of the current thread on an Android application.
std::string fplbase::AndroidGetViewIntentData ( )

Determine whether the activity was started with Intent.ACTION_VIEW and, if so, return the data the user wants to "view" in the application.

Returns
Returns a std::string contianing the View intent data.
bool fplbase::ChangeToUpstreamDir ( const char *const  binary_dir,
const char *const  target_dir 
)

Search and change to a given directory.

Parameters
binary_dirA C-string corresponding to the current directory to start searching from.
target_dirA C-string corresponding to the desired directory that should be changed to, if found.
Returns
Returns true if it's found, false otherwise.
std::string fplbase::DeviceModel ( )

Get the Android device's model.

Returns
Returns the model of the Android device the app is currently being run on.
const HighPerformanceParams& fplbase::GetHighPerformanceParameters ( )

Get the high performance parameters.

Returns
Returns the current performance parameters, in the form of a struct.
PerformanceMode fplbase::GetPerformanceMode ( )

Get the current performance mode.

Returns
Returns the current PerformanceMode.
bool fplbase::GetStoragePath ( const char *  app_name,
std::string *  path 
)

Retrieve a path where an app can store data files.

Parameters
[in]app_nameA C-string corresponding to the name of the application.
[out]pathA std::string to capture the storage path. Contains nullptr if the function returns false.
Returns
Returns true if a storage path was found. Otherwise it returns false.
int32_t fplbase::GetSystemRamSize ( )

Get the system's RAM size.

Returns
Returns the system RAM size in MB.
int fplbase::GetVsyncFrameId ( )

Get Vsync frame id.

Returns
Returns a unique ID representing the frame. Guaranteed to change every time the frame increments.
Warning
May eventually wrap.
bool fplbase::IsTvDevice ( )

Checks whether the device we are running on is an Android TV device.

Returns
Returns true if the device is an Android TV device.
Note
Always returns false when not running on Android.
bool fplbase::LoadFile ( const char *  filename,
std::string *  dest 
)

Loads a file and returns its contents via string pointer.

In contrast to LoadFileRaw(), this method simply calls the function set by SetLoadFileFunction() to read the specified file.

Parameters
[in]filenameA UTF-8 C-string representing the file to load.
[out]destA pointer to a std::string to capture the output of the file.
Returns
Returns false if the file couldn't be loaded (usually means it's not present, but can also mean there was a read error).
bool fplbase::LoadFileRaw ( const char *  filename,
std::string *  dest 
)

Loads a file and returns its contents via string pointer.

Parameters
[in]filenameA UTF-8 C-string representing the file to load.
[out]destA pointer to a std::string to capture the output of the file.
Returns
Returns false if the file couldn't be loaded (usually means it's not present, but can also mean there was a read error).
int32_t fplbase::LoadPreference ( const char *  key,
int32_t  initial_value 
)

Load a single integer value to a preference.

Parameters
[in]keyThe UTF-8 key for the preference.
[in]initial_valueThe default value to use if the preference is not found.
Returns
Returns the preference value.
bool fplbase::LoadPreferences ( const char *  filename,
std::string *  dest 
)

Load preference settings.

The API uses a dedicated API when an optimal API is available instead of regular file IO APIs.

Parameters
[in]filenameA UTF-8 C-string representing the file to load preferences from.
[out]destA pointer to a std::string to capture the preferences output.
Returns
Returns false if the file couldn't be loaded (usually means it's not present, but can also mean there was a read error).
void fplbase::LogError ( const char *  fmt,
va_list  args 
)

Log a format string with Error priority to the console.

Parameters
[in]fmtA C-string format string.
[in]argsA variable length argument list for the format string fmt.
void fplbase::LogError ( LogCategory  category,
const char *  fmt,
va_list  args 
)

Log a format string with Error priority to the console.

Parameters
[in]categoryThe LogCategory for the message.
[in]fmtA C-string format string.
[in]argsA variable length argument list for the format string fmt.
void fplbase::LogError ( const char *  fmt,
  ... 
)

Log a format string with Error priority to the console.

Parameters
[in]fmtA C-string format string.
void fplbase::LogError ( LogCategory  category,
const char *  fmt,
  ... 
)

Log a format string with Error priority to the console.

Parameters
[in]categoryThe LogCategory for the message.
[in]fmtA C-string format string.
void fplbase::LogInfo ( const char *  fmt,
va_list  args 
)

Log a format string with Info priority to the console.

Parameters
[in]fmtA C-string format string.
[in]argsA variable length argument list for the format string fmt.
void fplbase::LogInfo ( LogCategory  category,
const char *  fmt,
va_list  args 
)

Log a format string with Info priority to the console.

Parameters
[in]categoryThe LogCategory for the message.
[in]fmtA C-string format string.
[in]argsA variable length argument list for the format string fmt.
void fplbase::LogInfo ( const char *  fmt,
  ... 
)

Log a format string with Info priority to the console.

Parameters
[in]fmtA C-string format string.
void fplbase::LogInfo ( LogCategory  category,
const char *  fmt,
  ... 
)

Log a format string with Info priority to the console.

Parameters
[in]categoryThe LogCategory for the message.
[in]fmtA C-string format string.
const void* fplbase::MapFile ( const char *  filename,
int32_t  offset,
int32_t *  size 
)

Map a file into memory and returns its contents via pointer.

In contrast to LoadFile(), this method calls mmap API to map the whole or a part of the file.

Parameters
[in]filenameA UTF-8 C-string representing the file to load.
[in]offsetAn offset of the file contents to map.
in/out]size A size to map. A size of 0 indicates to map whole file. returns a mapped size of the file.
Returns
Returns a mapped pointer. nullptr when failed to map the file.
bool fplbase::MipmapGeneration16bppSupported ( )

check if 16bpp MipMap is supported.

Returns
Return true if 16bpp MipMap generation is supported.
Note
Basically always true, except on certain android devices.
VsyncCallback fplbase::RegisterVsyncCallback ( VsyncCallback  callback)

Register for handling vsync callbacks on android.

Note
As with most callbacks, this will normally be called on a separate thread, so you'll need to be careful about thread-safety with anything you do during the callback.
Parameters
callbackThe VSync callback function to register.
Returns
Return value is whatever callback was previously registered. (Or nullptr if there was none.)
bool fplbase::SaveFile ( const char *  filename,
const std::string &  data 
)

Save a string to a file, overwriting the existing contents.

Parameters
[in]filenameA UTF-8 C-string representing the file to save to.
[in]dataA const reference to a std::string containing the data that should be written to the file specified by filename.
Returns
Returns false if the file could not be written.
bool fplbase::SaveFile ( const char *  filename,
const void *  data,
size_t  size 
)

Save a string to a file, overwriting the existing contents.

Parameters
[in]filenameA UTF-8 C-string representing the file to save to.
[in]dataA const void pointer to the data that should be written to the file specified by filename.
[in]sizeThe size of the data array to write to the file specified by filename.
Returns
Returns false if the file could not be written.
bool fplbase::SavePreference ( const char *  key,
int32_t  value 
)

Save a single integer value to a preference.

Parameters
[in]keyThe UTF-8 key for the preference.
[in]valueThe value to save for the preference.
bool fplbase::SavePreferences ( const char *  filename,
const void *  data,
size_t  size 
)

Save preference settings.

The API uses a dedicated API when an optimal API is available instead of regular file IO APIs.

Parameters
[in]filenameA UTF-8 C-string representing the file to save preferences to.
[in]dataA const void pointer to the data that should be written to the file specified by filename.
[in]sizeThe size of the data array to write to the file specified by filename.
Returns
Returns false if the file couldn't be loaded (usually means it's not present, but can also mean there was a read error).
void fplbase::SendKeypressEventToAndroid ( int  android_keycode)

Triggers a keypress event on an Android device.

Parameters
[in]android_keycodeThe key code corresponding to the keypress that should be triggered.
void fplbase::SetHighPerformanceParameters ( const HighPerformanceParams &  params)

Sets the specific parameters used by high-performance mode on Android.

Parameters
[in]paramsA const reference to a HighPerformanceParams struct to set for use with high-performance mode.
LoadFileFunction fplbase::SetLoadFileFunction ( LoadFileFunction  load_file_function)

Set the function called by LoadFile().

Parameters
[in]load_file_functionThe function to be used by LoadFile() to read files.
Note
If the specified function is nullptr, LoadFileRaw() is set as the default function.
Returns
Returns the function previously set by LoadFileFunction().
void fplbase::SetPerformanceMode ( PerformanceMode  new_mode)

Sets the performance mode.

Parameters
[in]new_modeThe PerformanceMode to set.
bool fplbase::SupportsHeadMountedDisplay ( )

Checks whether Head Mounted Displays, such as Cardboard, are supported by the system being run on.

Returns
Returns true if Head Mounted Displays are supported.
bool fplbase::TouchScreenDevice ( )

Checks if the device has a touchscreen.

Returns
Returns true if the device has a touchscreen.
void fplbase::UnmapFile ( const void *  file,
int32_t  size 
)

Unmap a pointer that is mapped via MapFile() API.

Parameters
[in]fileA pointer to the file, returned via MapFile API.
[in]sizeA size to unmap.