From 8ba9850be3ce9328b3d8cbd890396f01990c6b6a Mon Sep 17 00:00:00 2001 From: Leandro Peres Date: Thu, 29 Aug 2024 11:51:57 -0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20Base=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/lugl/lu_math.hpp | 111 +++++++++++++++++++++++++++++++++++ include/lugl/lu_platform.hpp | 34 +++++++++++ include/lugl/lu_settings.hpp | 25 ++++++++ include/lugl/lu_types.hpp | 13 ++++ src/base.hpp | 36 ++++++++++++ src/graphics-wrapper.hpp | 23 ++++++++ 6 files changed, 242 insertions(+) create mode 100644 include/lugl/lu_math.hpp create mode 100644 include/lugl/lu_platform.hpp create mode 100644 include/lugl/lu_settings.hpp create mode 100644 include/lugl/lu_types.hpp create mode 100644 src/base.hpp create mode 100644 src/graphics-wrapper.hpp diff --git a/include/lugl/lu_math.hpp b/include/lugl/lu_math.hpp new file mode 100644 index 0000000..183b086 --- /dev/null +++ b/include/lugl/lu_math.hpp @@ -0,0 +1,111 @@ +#ifndef LUGL_MATH_HPP +#define LUGL_MATH_HPP + +#include "lu_settings.hpp" +#include "luapi.hpp" + +#include +#include /** sqrtf(), atan2f(), floor() */ + +//---------------------------------------------------------------------------------- +// Macros +//---------------------------------------------------------------------------------- +#ifndef MAX_FLOAT +# define MAX_FLOAT FLT_MAX +#endif + +#ifndef EPSILON +# define EPSILON FLT_EPSILON +#endif + +#ifndef SQRT +# define SQRT( x ) sqrtf( x ) +#endif + +#ifndef ATAN2 +# define ATAN2( y, x ) atan2f( y, x ) +#endif + +#ifndef ABS +# define ABS( x ) ( x ) > 0.0F ? ( x ) : -( x ) +#endif + +#ifndef PI +# define PI ( real )( 3.14159265358979323846 ) +#endif + +#ifndef TAU +# define TAU ( real )( 6.283185307179586476925 ) +#endif + +#ifndef EPSILON +# define EPSILON 0.000001 +#endif + +#ifndef DEG2RAD +# define DEG2RAD 0.01745329251 /** ( TAU / 360.0F ) */ +#endif + +#ifndef RAD2DEG +# define RAD2DEG 57.2957795131 /** ( 360.0F / TAU ) */ +#endif + +//---------------------------------------------------------------------------------- +// Structs +//---------------------------------------------------------------------------------- + + +//---------------------------------------------------------------------------------- +// Math Functions Definition +//---------------------------------------------------------------------------------- +template < typename T > +inline LU_API T +Min( T a, T b ) +{ + return a < b ? a : b; +} + +template < typename T > +inline LU_API T +Max( T a, T b ) +{ + return a > b ? a : b; +} + +template < typename T > +inline LU_API T +Clamp( T value, T lower, T upper ) +{ + return Max( lower, Min( value, upper ) ); +} + +/// Linear interpolated value. Unclamped +inline LU_API real +Lerp( real a, real b, real t ) +{ + return ( 1.0F - t ) * a + b * t; +} + +/// Inverse linear interpolated value. Unclamped +inline LU_API real +InvLerp( real a, real b, real v ) +{ + return ( v - a ) / ( b - a ); +} + +/// Remap a value from one range to another. Unclamped +inline LU_API real +Remap( real iMin, real iMax, real oMin, real oMax, real v ) +{ + const real t = InvLerp( iMin, iMax, v ); + return Lerp( oMin, oMax, t ); +} + +// Wrap input value from min to max +inline LU_API real +Wrap( real a, real b, real v ) +{ + return v - ( b - a ) * floorf( ( v - a ) / ( b - a ) ); +} + +#endif /* LUGL_MATH_HPP */ diff --git a/include/lugl/lu_platform.hpp b/include/lugl/lu_platform.hpp new file mode 100644 index 0000000..a285796 --- /dev/null +++ b/include/lugl/lu_platform.hpp @@ -0,0 +1,34 @@ +#ifndef LUGL_PLATFORM_HPP +#define LUGL_PLATFORM_HPP + +enum class Platform +{ + mac, + ios, + android, + emscripten, + windows, + linux +}; + +inline Platform +getCurrentPlatform() +{ +#if defined( __EMSCRIPTEN__ ) + return Platform::emscripten; +#elif __APPLE__ +# if TARGET_OS_IPHONE + return Platform::ios; +# else + return Platform::mac; +# endif +#elif __ANDROID__ + return Platform::android; +#elif WIN32 + return Platform::windows; +#elif __linux__ + return Platform::linux; +#endif +} + +#endif //LUGL_PLATFORM_HPP diff --git a/include/lugl/lu_settings.hpp b/include/lugl/lu_settings.hpp new file mode 100644 index 0000000..eb94bee --- /dev/null +++ b/include/lugl/lu_settings.hpp @@ -0,0 +1,25 @@ +#ifndef LUGL_SETTINGS_H +#define LUGL_SETTINGS_H + +#include "lu_types.hpp" +#include "luapi.hpp" + +/// Default allocation functions +LU_API void * luAlloc_Default( int32 size ); +LU_API void luFree_Default( void * mem ); + +/// Implement this function to use your own memory allocator. +inline void * +luAlloc( int32 size ) +{ + return luAlloc_Default( size ); +} + +/// If you implement luAlloc, you should also implement this function. +inline void +b2Free( void * mem ) +{ + luFree_Default( mem ); +} + +#endif // LUGL_SETTINGS_H diff --git a/include/lugl/lu_types.hpp b/include/lugl/lu_types.hpp new file mode 100644 index 0000000..386268a --- /dev/null +++ b/include/lugl/lu_types.hpp @@ -0,0 +1,13 @@ +#ifndef LUGL_TYPES_H +#define LUGL_TYPES_H + +typedef signed char int8; +typedef signed short int16; +typedef signed int int32; +typedef unsigned char uint8; +typedef unsigned short uint16; +typedef unsigned int uint32; + +typedef float real; + +#endif // LUGL_TYPES_H diff --git a/src/base.hpp b/src/base.hpp new file mode 100644 index 0000000..67ef557 --- /dev/null +++ b/src/base.hpp @@ -0,0 +1,36 @@ +#ifndef LUGL_BASE_HPP +#define LUGL_BASE_HPP + +#include + +template < typename T > using Scope = std::unique_ptr< T >; +/** + * Create a unique pointer (Scope) to an instance of type T. + * @tparam T The type of object to create. + * @tparam Args The argument types for constructing the object. + * @param args The arguments to forward to the constructor. + * @return A unique pointer (Scope) to the created object. + */ +template < typename T, typename... Args > +constexpr Scope< T > +CreateScope( Args &&... args ) +{ + return std::make_unique< T >( std::forward< Args >( args )... ); +} + +template < typename T > using Ref = std::shared_ptr< T >; +/** + * Create a shared pointer (Ref) to an instance of type T. + * @tparam T The type of object to create. + * @tparam Args The argument types for constructing the object. + * @param args The arguments to forward to the constructor. + * @return A shared pointer (Ref) to the created object. + */ +template < typename T, typename... Args > +constexpr Ref< T > +CreateRef( Args &&... args ) +{ + return std::make_shared< T >( std::forward< Args >( args )... ); +} + +#endif /** LUGL_BASE_HPP */ diff --git a/src/graphics-wrapper.hpp b/src/graphics-wrapper.hpp new file mode 100644 index 0000000..97e783b --- /dev/null +++ b/src/graphics-wrapper.hpp @@ -0,0 +1,23 @@ +#ifndef LUGL_GRAPHICS_WRAPPER_HPP +#define LUGL_GRAPHICS_WRAPPER_HPP + +#if defined( __EMSCRIPTEN__ ) +# include +#elif __APPLE__ +# ifndef GL_SILENCE_DEPRECATION +# define GL_SILENCE_DEPRECATION +# endif +# include "TargetConditionals.h" +# if TARGET_OS_IPHONE +# include +# else +# include +# endif +#elif __ANDROID__ +# include +#elif WIN32 +# define GLEW_STATIC +# include +#endif + +#endif /** LUGL_GRAPHICS_WRAPPER_HPP */