From 6bffea953dcd596f71c696fedf906e85e4b1315d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Wed, 4 Mar 2020 15:00:57 +0100 Subject: [PATCH] Implements extern growthrate that uses function pointers. --- CInterface/Library/GrowthRate.cpp | 6 ++++++ src/Library/GrowthRates/ExternGrowthRate.hpp | 19 +++++++++++++++++++ src/Library/GrowthRates/GrowthRate.cpp | 1 - 3 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 src/Library/GrowthRates/ExternGrowthRate.hpp delete mode 100644 src/Library/GrowthRates/GrowthRate.cpp diff --git a/CInterface/Library/GrowthRate.cpp b/CInterface/Library/GrowthRate.cpp index a361108..ebb0065 100644 --- a/CInterface/Library/GrowthRate.cpp +++ b/CInterface/Library/GrowthRate.cpp @@ -1,4 +1,5 @@ #include "../../src/Library/GrowthRates/GrowthRate.hpp" +#include "../../src/Library/GrowthRates/ExternGrowthRate.hpp" #include "../../src/Library/GrowthRates/LookupGrowthRate.hpp" #define export extern "C" using namespace CreatureLib::Library; @@ -8,6 +9,11 @@ export GrowthRate* CreatureLib_LookupGrowthRate_Construct(uint32_t experiencePer return new LookupGrowthRate(exp); }; +export GrowthRate* CreatureLib_ExternGrowthRate_Construct(uint8_t (*calcLevel)(uint32_t), + uint32_t (*calcExperience)(uint8_t)) { + return new ExternGrowthRate(calcLevel, calcExperience); +}; + export void CreatureLib_GrowthRate_Destruct(const GrowthRate* p) { delete p; } export uint8_t CreatureLib_GrowthRate_CalculateLevel(const GrowthRate* p, uint32_t experience) { diff --git a/src/Library/GrowthRates/ExternGrowthRate.hpp b/src/Library/GrowthRates/ExternGrowthRate.hpp new file mode 100644 index 0000000..fd534ae --- /dev/null +++ b/src/Library/GrowthRates/ExternGrowthRate.hpp @@ -0,0 +1,19 @@ +#ifndef CREATURELIB_EXTERNGROWTHRATE_HPP +#define CREATURELIB_EXTERNGROWTHRATE_HPP + +#include "GrowthRate.hpp" +namespace CreatureLib::Library { + class ExternGrowthRate : public GrowthRate { + uint8_t (*_calcLevel)(uint32_t experience); + uint32_t (*_calcExperience)(uint8_t level); + + public: + ExternGrowthRate(uint8_t (*calcLevel)(uint32_t), uint32_t (*calcExperience)(uint8_t level)) + : _calcLevel(calcLevel), _calcExperience(calcExperience) {} + + uint8_t CalculateLevel(uint32_t experience) const override { return _calcLevel(experience); } + uint32_t CalculateExperience(uint8_t level) const override { return _calcExperience(level); } + }; +} + +#endif // CREATURELIB_EXTERNGROWTHRATE_HPP diff --git a/src/Library/GrowthRates/GrowthRate.cpp b/src/Library/GrowthRates/GrowthRate.cpp deleted file mode 100644 index 4341d03..0000000 --- a/src/Library/GrowthRates/GrowthRate.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "GrowthRate.hpp"