Skip to content

Commit

Permalink
🚧 Base files
Browse files Browse the repository at this point in the history
  • Loading branch information
zschzen committed Aug 29, 2024
1 parent cd0a386 commit 8ba9850
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 0 deletions.
111 changes: 111 additions & 0 deletions include/lugl/lu_math.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#ifndef LUGL_MATH_HPP
#define LUGL_MATH_HPP

#include "lu_settings.hpp"
#include "luapi.hpp"

#include <cfloat>
#include <cmath> /** 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 */
34 changes: 34 additions & 0 deletions include/lugl/lu_platform.hpp
Original file line number Diff line number Diff line change
@@ -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
25 changes: 25 additions & 0 deletions include/lugl/lu_settings.hpp
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions include/lugl/lu_types.hpp
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions src/base.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef LUGL_BASE_HPP
#define LUGL_BASE_HPP

#include <memory>

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 */
23 changes: 23 additions & 0 deletions src/graphics-wrapper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef LUGL_GRAPHICS_WRAPPER_HPP
#define LUGL_GRAPHICS_WRAPPER_HPP

#if defined( __EMSCRIPTEN__ )
# include <GLES2/gl2.h>
#elif __APPLE__
# ifndef GL_SILENCE_DEPRECATION
# define GL_SILENCE_DEPRECATION
# endif
# include "TargetConditionals.h"
# if TARGET_OS_IPHONE
# include <OpenGLES/ES2/gl.h>
# else
# include <OpenGL/gl3.h>
# endif
#elif __ANDROID__
# include <GLES2/gl2.h>
#elif WIN32
# define GLEW_STATIC
# include <GL/glew.h>
#endif

#endif /** LUGL_GRAPHICS_WRAPPER_HPP */

0 comments on commit 8ba9850

Please sign in to comment.