Skip to content

Commit

Permalink
refactor: replace PACKED macros with LPL_PACKED macros for better str…
Browse files Browse the repository at this point in the history
…uct packing
  • Loading branch information
MasterLaplace committed Nov 8, 2024
1 parent 9718db5 commit c2a70c3
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include <string>

namespace Flakkari::Engine::ECS::Components::_2D {
PACKED_START
LPL_PACKED_START

/**
* @brief Collider component for ECS entities that have a script attached to them
Expand All @@ -39,7 +39,7 @@ struct Collider {
std::size_t size() const { return sizeof(_size); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::_2D

#endif /* !COLLIDER_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {
PACKED_START
LPL_PACKED_START

/**
* @brief Control component for 2D entities (player, enemies, etc...)
Expand Down Expand Up @@ -55,7 +55,7 @@ struct Control {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::_2D

#endif /* !FLAKKARI_CONTROL_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {
PACKED_START
LPL_PACKED_START

struct Movable {
Math::Vector2f velocity; // pixels / second
Expand All @@ -38,7 +38,7 @@ struct Movable {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::_2D

#endif /* !FLAKKARI_MOVABLE_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {
PACKED_START
LPL_PACKED_START

/**
* @brief RigidBody represent the physical properties of a rigid body in a game engine
Expand Down Expand Up @@ -53,7 +53,7 @@ struct RigidBody {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::_2D

#endif /* !RIGIDBODY_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#include "../../../Math/Vector.hpp"

namespace Flakkari::Engine::ECS::Components::_2D {
PACKED_START
LPL_PACKED_START

struct Transform {
Math::Vector2f position;
Expand All @@ -40,7 +40,7 @@ struct Transform {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::_2D

#endif /* !FLAKKARI_TRANSFORM_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
PACKED_START
LPL_PACKED_START

/**
* @brief Health is a structure that represents the life of an "living object"
Expand Down Expand Up @@ -50,7 +50,7 @@ struct Health {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !Health_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
PACKED_START
LPL_PACKED_START

struct Id {
std::size_t id;
Expand All @@ -35,7 +35,7 @@ struct Id {
std::size_t size() const { return sizeof(id); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !FLAKKARI_ID_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
PACKED_START
LPL_PACKED_START

/**
* @brief Parent component for ECS entities that have a parent entity attached to them
Expand All @@ -40,7 +40,7 @@ struct Parent {
std::size_t size() const { return sizeof(*this); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !FLAKKARI_PARENT_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
PACKED_START
LPL_PACKED_START

/**
* @brief Spawned component for ECS entities that have a script attached to them
Expand All @@ -41,7 +41,7 @@ struct Spawned {
std::size_t size() const { return sizeof(has_spawned); }
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !SPAWNED_HPP_ */
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#include "config.h.in"

namespace Flakkari::Engine::ECS::Components::Common {
PACKED_START
LPL_PACKED_START

/**
* @brief Weapon is a structure that defines the characteristics of a weapon.
Expand Down Expand Up @@ -58,7 +58,7 @@ struct Weapon {
std::size_t size() const { return sizeof(*this); };
};

PACKED_END
LPL_PACKED_END
} // namespace Flakkari::Engine::ECS::Components::Common

#endif /* !WEAPON_HPP_ */
4 changes: 2 additions & 2 deletions Flakkari/Engine/Math/Vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Flakkari::Engine::Math {

template <typename Type> struct Vector {

PACKED_START
LPL_PACKED_START
union {
struct {
Type x;
Expand Down Expand Up @@ -56,7 +56,7 @@ template <typename Type> struct Vector {
Vector(Type x, Type y) : v{x, y, 0, 1} {};
Vector(Type x) : v{x, 0, 0, 1} {};
Vector(const Vector<Type> &other) : v{other.v[0], other.v[1], other.v[2], other.v[3]} {};
PACKED_END
LPL_PACKED_END

Vector<Type> &operator=(const Vector<Type> &other)
{
Expand Down
41 changes: 41 additions & 0 deletions Flakkari/Network/Packed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**************************************************************************
* Flakkari Library v0.3.0
*
* Flakkari Library is a C++ Library for Network.
* @file Packed.hpp
* @brief Packed header. Contains PACKED macros.
* (LPL_PACKED_START, LPL_PACKED_END, PACKED)
*
* @details LPL_PACKED_START and LPL_PACKED_END macros are used to pack structs:
* PACKED_<_> macros are used to pack structs:
* LPL_PACKED_START struct _ {}; LPL_PACKED_END
* PACKED macros are used to pack structs:
* struct _ {} PACKED;
*
* Flakkari Library is under MIT License.
* https://opensource.org/licenses/MIT
* © 2023 @MasterLaplace
* @version 0.3.0
* @date 2023-01-10
**************************************************************************/

#ifndef PACKED_HPP_
#define PACKED_HPP_

#ifdef LPL_PACKED_START
# undef LPL_PACKED_START
#endif

#ifdef LPL_PACKED_END
# undef LPL_PACKED_END
#endif

#ifdef _MSC_VER
# define LPL_PACKED_START __pragma(pack(push, 1))
# define LPL_PACKED_END __pragma(pack(pop))
#else
# define LPL_PACKED_START _Pragma("pack(1)")
# define LPL_PACKED_END _Pragma("pack()")
#endif

#endif /* !PACKED_HPP_ */
4 changes: 2 additions & 2 deletions Flakkari/Protocol/Events.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ enum class EventState : uint8_t {
MAX_STATE
};

PACKED_START
LPL_PACKED_START

struct Event {
EventId id;
EventState state;
};

PACKED_END
LPL_PACKED_END

} /* namespace V_0 */

Expand Down
4 changes: 2 additions & 2 deletions Flakkari/Protocol/Header.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ enum class Priority : byte {
MAX_PRIORITY = 4
};

PACKED_START
LPL_PACKED_START

template <typename Id> struct Header {
Priority _priority : 4 = Priority::LOW;
Expand All @@ -69,7 +69,7 @@ template <typename Id> struct Header {
.count());
};

PACKED_END
LPL_PACKED_END

} // namespace V_0

Expand Down
60 changes: 30 additions & 30 deletions Flakkari/config.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,32 @@
#endif


#ifndef CONFIG_UTILS
#define CONFIG_UTILS
#ifndef LAPLACE_CONFIG_UTILS
#define LAPLACE_CONFIG_UTILS

////////////////////////////////////////////////////////////
// Define shared portable macros for various compilers
////////////////////////////////////////////////////////////
#define NEED_COMMA struct _
#define ATTRIBUTE(key) __attribute__((key))
#define UNUSED_ATTRIBUTE ATTRIBUTE(unused)
#define UNUSED(x) (void)(x)
#define LPL_NEED_COMMA struct _
#define LPL_ATTRIBUTE(key) __attribute__((key))
#define LPL_UNUSED_ATTRIBUTE LPL_ATTRIBUTE(unused)
#define LPL_UNUSED(x) (void)(x)

////////////////////////////////////////////////////////////
// Define portable NULL pointer using C++11 nullptr keyword
////////////////////////////////////////////////////////////
#if defined(__cplusplus) && __cplusplus >= 201103L
#define lpl_nullptr nullptr
#elif !defined(NULL)
#define nullptr ((void*)0)
#define lpl_nullptr ((void*)0)
#else
#define nullptr NULL
#define lpl_nullptr NULL
#endif

////////////////////////////////////////////////////////////
// Define boolean type and values
////////////////////////////////////////////////////////////
#if defined(__cplusplus)
#elif !defined(__bool_true_false_are_defined)
#if !defined(__bool_true_false_are_defined) && !defined(__cplusplus)
#define bool _Bool
#define true 1
#define false 0
Expand All @@ -71,7 +71,7 @@
#if defined __GNUC__ && defined __GNUC_MINOR__
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
#elif !defined(__GNUC_PREREQ)
# define __GNUC_PREREQ(maj, min) 0
#endif

Expand All @@ -80,34 +80,34 @@
////////////////////////////////////////////////////////////
/** Usage:
* @example
* PACKED(struct MyStruct
* LPL_PACKED(struct MyStruct
* {
* int a;
* char b;
* ...
* });
\**********************************************************/
#if defined(__GNUC__) || defined(__GNUG__)
#define PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#define PACKED_START _Pragma("pack(1)")
#define PACKED_END _Pragma("pack()")
#elif _MSC_VER
#define PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
#define PACKED_START __pragma(pack(push, 1))
#define PACKED_END __pragma(pack(pop))
#if defined(_MSC_VER) || defined(_MSVC_LANG)
#define LPL_PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
#define LPL_PACKED_START __pragma(pack(push, 1))
#define LPL_PACKED_END __pragma(pack(pop))
#elif defined(__GNUC__) || defined(__GNUG__)
#define LPL_PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#define LPL_PACKED_START _Pragma("pack(1)")
#define LPL_PACKED_END _Pragma("pack()")
#else
#define PACKED( __Declaration__ ) __Declaration__
#define PACKED_START _Pragma("pack(1)")
#define PACKED_END _Pragma("pack()")
#define LPL_PACKED( __Declaration__ ) __Declaration__
#define LPL_PACKED_START
#define LPL_PACKED_END
#endif

////////////////////////////////////////////////////////////
// Helper macro to convert a macro to a string
////////////////////////////////////////////////////////////
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define LPL_STRINGIFY(x) #x
#define LPL_TOSTRING(x) LPL_STRINGIFY(x)

#endif /* !CONFIG_UTILS */
#endif /* !LAPLACE_CONFIG_UTILS */


#ifndef FLAKKARI_DISTRIBUTION_H_
Expand Down Expand Up @@ -474,10 +474,10 @@
// Define the FLAKKARI version string
////////////////////////////////////////////////////////////
#define FLAKKARI_VERSION_STRING \
TOSTRING(FLAKKARI_VERSION_MAJOR) "." \
TOSTRING(FLAKKARI_VERSION_MINOR) "." \
TOSTRING(FLAKKARI_VERSION_PATCH) "." \
TOSTRING(FLAKKARI_VERSION_TWEAK)
LPL_TOSTRING(FLAKKARI_VERSION_MAJOR) "." \
LPL_TOSTRING(FLAKKARI_VERSION_MINOR) "." \
LPL_TOSTRING(FLAKKARI_VERSION_PATCH) "." \
LPL_TOSTRING(FLAKKARI_VERSION_TWEAK)

#endif /* !FLAKKARI_VERSION_H_ */

Expand Down

0 comments on commit c2a70c3

Please sign in to comment.