From 3a6e631261975084ff8c80f6d68b5a02b77b4b95 Mon Sep 17 00:00:00 2001 From: obligaron Date: Sat, 22 May 2021 14:01:51 +0200 Subject: [PATCH] Remove memset for PlayerStruct --- Source/player.cpp | 9 ++++++++- Source/player.h | 8 ++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Source/player.cpp b/Source/player.cpp index 7d514a34a..8370cf721 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -367,6 +367,13 @@ bool PlayerStruct::IsWalking() const } } +void PlayerStruct::Reset() +{ + // Create empty default initialized PlayerStruct on heap to avoid excessive stack usage + auto emptyPlayer = std::make_unique(); + *this = std::move(*emptyPlayer); +} + void SetPlayerGPtrs(byte *pData, byte **pAnim) { int i; @@ -772,7 +779,7 @@ void CreatePlayer(int pnum, HeroClass c) } auto &player = plr[pnum]; - memset(&player, 0, sizeof(PlayerStruct)); + player.Reset(); SetRndSeed(SDL_GetTicks()); player._pClass = c; diff --git a/Source/player.h b/Source/player.h index 3d36761ff..599c21874 100644 --- a/Source/player.h +++ b/Source/player.h @@ -150,6 +150,9 @@ enum player_weapon_type : uint8_t { struct PlayerStruct { PlayerStruct() = default; + PlayerStruct(PlayerStruct &&) noexcept = default; + PlayerStruct &operator=(PlayerStruct &&) noexcept = default; + PLR_MODE _pmode; int8_t walkpath[MAX_PATH_LENGTH]; bool plractive; @@ -395,6 +398,11 @@ struct PlayerStruct { * @brief Is the player currently walking? */ bool IsWalking() const; + + /** + * @brief Resets all Data of the current PlayerStruct + */ + void Reset(); }; extern int myplr;