2017-09-30 10:37:45 +02:00
|
|
|
#pragma once
|
|
|
|
#ifndef REAL_BUILD
|
|
|
|
# include "externals.hh"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*= Utilities ================================================================*/
|
|
|
|
|
|
|
|
// Disable next ImGui button(s)
|
|
|
|
void disableButton( );
|
|
|
|
// Re-enable ImGui buttons
|
|
|
|
inline void reenableButtons( )
|
|
|
|
{
|
|
|
|
ImGui::PopStyleColor( 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-10-02 13:51:08 +02:00
|
|
|
#define GL_CHECK( FAIL ) \
|
2017-10-01 18:51:02 +02:00
|
|
|
do { \
|
|
|
|
auto err_( glGetError( ) ); \
|
2017-10-02 13:51:08 +02:00
|
|
|
if ( err_ != GL_NO_ERROR ) FAIL; \
|
2017-10-01 18:51:02 +02:00
|
|
|
} while ( 0 )
|
|
|
|
|
2017-10-02 13:51:08 +02:00
|
|
|
#define GL_ASSERT( ) \
|
|
|
|
GL_CHECK({ \
|
|
|
|
fprintf( stderr , "GL error %x in %s:%d\n" , \
|
|
|
|
err_ , __FILE__ , __LINE__ ); \
|
|
|
|
abort( ); \
|
|
|
|
})
|
|
|
|
|
2017-10-01 18:51:02 +02:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
2017-09-30 10:37:45 +02:00
|
|
|
// Add some value to an angle, keeping it in [-180;180]
|
|
|
|
void updateAngle(
|
2017-11-03 09:08:19 +01:00
|
|
|
float& initial ,
|
|
|
|
const float delta
|
2017-09-30 10:37:45 +02:00
|
|
|
);
|
|
|
|
// Make a rotation matrix from three YPR angles (in degrees)
|
|
|
|
void anglesToMatrix(
|
2017-11-03 09:08:19 +01:00
|
|
|
float const* angles ,
|
|
|
|
float* matrix );
|
2017-09-30 10:37:45 +02:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// Helpers for finding entries in collections
|
|
|
|
template< typename T , typename I >
|
|
|
|
inline auto find(
|
2017-11-03 09:08:19 +01:00
|
|
|
T const& collection ,
|
|
|
|
I const& item )
|
2017-09-30 10:37:45 +02:00
|
|
|
{
|
|
|
|
return std::find( collection.begin( ) , collection.end( ) , item );
|
|
|
|
}
|
|
|
|
|
|
|
|
template< typename T , typename I >
|
|
|
|
inline auto find(
|
2017-11-03 09:08:19 +01:00
|
|
|
T& collection ,
|
|
|
|
I const& item )
|
2017-09-30 10:37:45 +02:00
|
|
|
{
|
|
|
|
return std::find( collection.begin( ) , collection.end( ) , item );
|
|
|
|
}
|
2017-10-31 09:22:38 +01:00
|
|
|
|
2017-11-01 12:51:44 +01:00
|
|
|
template< typename T , typename I >
|
|
|
|
inline bool Contains(
|
2017-11-03 09:08:19 +01:00
|
|
|
T const& collection ,
|
|
|
|
I const& item )
|
2017-11-01 12:51:44 +01:00
|
|
|
{
|
|
|
|
return std::find( collection.begin( ) , collection.end( ) , item ) != collection.end( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// Get the absolute path
|
|
|
|
std::string GetAbsolutePath(
|
2017-11-03 09:08:19 +01:00
|
|
|
std::string const& path );
|
2017-11-01 12:51:44 +01:00
|
|
|
|
|
|
|
// Get the absolute parent path for a (possibly relative) path
|
|
|
|
std::string GetParentPath(
|
2017-11-03 09:08:19 +01:00
|
|
|
std::string const& path );
|
2017-11-01 12:51:44 +01:00
|
|
|
|
2017-10-31 09:22:38 +01:00
|
|
|
|
|
|
|
/*= JSON utilities ===========================================================*/
|
|
|
|
|
2017-10-31 14:21:42 +01:00
|
|
|
using T_JSONObject = picojson::value::object;
|
|
|
|
using T_JSONArray = picojson::value::array;
|
|
|
|
|
2017-10-31 09:22:38 +01:00
|
|
|
// Convert a vector to a JSON array
|
|
|
|
picojson::value jsonVector(
|
2017-11-03 09:08:19 +01:00
|
|
|
float const* vector ,
|
|
|
|
const int nComponents );
|
2017-10-31 09:22:38 +01:00
|
|
|
|
|
|
|
// Convert a GLM vec3 to a JSON array
|
|
|
|
inline picojson::value jsonVector(
|
2017-11-03 09:08:19 +01:00
|
|
|
glm::vec3 const& vector )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
return jsonVector( &vector.x , 3 );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add an entry to a JSON object
|
|
|
|
template< typename T >
|
|
|
|
inline void jsonAdd(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
T const& v )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
using T_Entry = std::pair< std::string , picojson::value >;
|
|
|
|
object.insert( T_Entry( key , picojson::value( v ) ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
extern template void jsonAdd< double >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
double const& v );
|
2017-10-31 14:21:42 +01:00
|
|
|
extern template void jsonAdd< T_JSONObject >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
T_JSONObject const& v );
|
2017-10-31 14:21:42 +01:00
|
|
|
extern template void jsonAdd< T_JSONArray >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
T_JSONArray const& v );
|
2017-10-31 09:22:38 +01:00
|
|
|
extern template void jsonAdd< std::string >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
std::string const& v );
|
2017-10-31 09:22:38 +01:00
|
|
|
|
|
|
|
template< >
|
|
|
|
void jsonAdd< picojson::value >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
picojson::value const& v );
|
2017-10-31 09:22:38 +01:00
|
|
|
template< >
|
|
|
|
void jsonAdd< int >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
int const& v );
|
2017-10-31 09:22:38 +01:00
|
|
|
template< >
|
|
|
|
void jsonAdd< unsigned >(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject& object ,
|
|
|
|
std::string const& key ,
|
|
|
|
unsigned const& v );
|
2017-10-31 09:22:38 +01:00
|
|
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
// JSON read type error
|
|
|
|
class X_JsonGetFailed : public std::exception { };
|
|
|
|
|
2017-10-31 14:21:42 +01:00
|
|
|
// Get a JSON object field; throws X_JsonGetFailed if the type is incorrect or
|
|
|
|
// if the field is missing.
|
|
|
|
template< typename T >
|
|
|
|
inline T const& jsonGet(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 14:21:42 +01:00
|
|
|
{
|
|
|
|
const std::string k( key );
|
|
|
|
if ( object.find( k ) == object.end( ) ) {
|
|
|
|
throw X_JsonGetFailed( );
|
|
|
|
}
|
|
|
|
|
|
|
|
picojson::value const& v( object.at( k ) );
|
|
|
|
if ( !v.is< T >( ) ) {
|
|
|
|
throw X_JsonGetFailed( );
|
|
|
|
}
|
|
|
|
return v.get< T >( );
|
|
|
|
}
|
|
|
|
|
2017-10-31 09:22:38 +01:00
|
|
|
// Get a pointer to a JSON object field, or nullptr if the field doesn't exist.
|
|
|
|
// Throws X_JsonGetFailed if the type is incorrect.
|
|
|
|
template< typename T >
|
|
|
|
inline T const* jsonGetOpt(
|
2017-11-03 09:08:19 +01:00
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
const std::string k( key );
|
|
|
|
if ( object.find( k ) == object.end( ) ) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
picojson::value const& v( object.at( k ) );
|
|
|
|
if ( !v.is< T >( ) ) {
|
|
|
|
throw X_JsonGetFailed( );
|
|
|
|
}
|
|
|
|
return &v.get< T >( );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a value from a JSON object, if the value actually exists. Fails with
|
|
|
|
// X_JsonGetFailed if the type is wrong
|
|
|
|
template< typename T >
|
|
|
|
inline bool jsonSetFromOpt(
|
2017-11-03 09:08:19 +01:00
|
|
|
T& out ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
T const* ptr( jsonGetOpt< T >( object , key ) );
|
|
|
|
if ( ptr ) {
|
|
|
|
out = *ptr;
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integer specialization of the above
|
|
|
|
template< >
|
|
|
|
inline bool jsonSetFromOpt< int >(
|
2017-11-03 09:08:19 +01:00
|
|
|
int& out ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
double const* ptr( jsonGetOpt< double >( object , key ) );
|
|
|
|
if ( ptr ) {
|
|
|
|
out = int( *ptr );
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unsigned integer specialization of the above
|
|
|
|
template< >
|
|
|
|
inline bool jsonSetFromOpt< unsigned >(
|
2017-11-03 09:08:19 +01:00
|
|
|
unsigned& out ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
double const* ptr( jsonGetOpt< double >( object , key ) );
|
|
|
|
if ( ptr ) {
|
|
|
|
out = unsigned( *ptr );
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 32-bit float specialization of the above
|
|
|
|
template< >
|
|
|
|
inline bool jsonSetFromOpt< float >(
|
2017-11-03 09:08:19 +01:00
|
|
|
float& out ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* const key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
double const* ptr( jsonGetOpt< double >( object , key ) );
|
|
|
|
if ( ptr ) {
|
|
|
|
out = float( *ptr );
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a vector from a JSON array of numbers
|
|
|
|
void jsonGetVectorOpt(
|
2017-11-03 09:08:19 +01:00
|
|
|
float* vector ,
|
|
|
|
const size_t size ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* key );
|
2017-10-31 09:22:38 +01:00
|
|
|
|
|
|
|
// Read a GLM vec3 from a JSON array of numbers
|
|
|
|
inline void jsonGetVectorOpt(
|
2017-11-03 09:08:19 +01:00
|
|
|
glm::vec3& vector ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
jsonGetVectorOpt( &vector.x , 3 , object , key );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load some object using its "bool load( jsonobject )" method.
|
|
|
|
template< typename T >
|
|
|
|
inline bool jsonLoadOpt(
|
2017-11-03 09:08:19 +01:00
|
|
|
T& out ,
|
|
|
|
T_JSONObject const& object ,
|
|
|
|
char const* key )
|
2017-10-31 09:22:38 +01:00
|
|
|
{
|
|
|
|
try {
|
2017-10-31 14:21:42 +01:00
|
|
|
auto const* sub( jsonGetOpt< T_JSONObject >( object , key ) );
|
2017-10-31 09:22:38 +01:00
|
|
|
if ( sub ) {
|
|
|
|
return out.load( *sub );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
} catch ( X_JsonGetFailed const& ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|