Skip to content

Commit

Permalink
[Enitty] Implemebnted unique IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
PanosK92 committed Oct 22, 2023
1 parent 8cc457d commit 9a4b6a8
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 16 deletions.
33 changes: 31 additions & 2 deletions runtime/Core/SpObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

//= INCLUDES =========
//= INCLUDES ========
#include "pch.h"
#include "SpObject.h"
//====================
//===================

//= NAMESPACES =====
using namespace std;
//==================

namespace Spartan
{
Expand All @@ -32,4 +36,29 @@ namespace Spartan
{
m_object_id = GenerateObjectId();
}

uint64_t SpObject::GenerateObjectId()
{
static mt19937_64 eng{ random_device{}() };

auto time_now = chrono::high_resolution_clock::now().time_since_epoch().count();
auto thread_id = hash<thread::id>()(this_thread::get_id());
auto random_value = eng();

uint64_t a = static_cast<uint64_t>(time_now);
uint64_t b = static_cast<uint64_t>(thread_id);
uint64_t c = random_value;

// mix function based on Knuth's multiplicative method
// https://gist.github.com/badboy/6267743
a *= 2654435761u;
b *= 2654435761u;
c *= 2654435761u;

a ^= (a >> 16);
b ^= (b >> 16);
c ^= (c >> 16);

return a + b + c;
}
}
20 changes: 8 additions & 12 deletions runtime/Core/SpObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,24 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

namespace Spartan
{
//= FORWARD DECLARATIONS =
class Context;
//========================

// Globals
// globals
extern uint64_t g_id;

class SP_CLASS SpObject
{
public:
SpObject();

// Name
const std::string& GetObjectName() const { return m_object_name; }
// name
const std::string& GetObjectName() const { return m_object_name; }
void SetObjectName(const std::string& name) { m_object_name = name; }

// Id
const uint64_t GetObjectId() const { return m_object_id; }
void SetObjectId(const uint64_t id) { m_object_id = id; }
static uint64_t GenerateObjectId() { return ++g_id; }
// id
const uint64_t GetObjectId() const { return m_object_id; }
void SetObjectId(const uint64_t id) { m_object_id = id; }
static uint64_t GenerateObjectId();

// CPU & GPU sizes
// cpu & gpu sizes
const uint64_t GetObjectSizeCpu() const { return m_object_size_cpu; }
const uint64_t GetObjectSizeGpu() const { return m_object_size_gpu; }

Expand Down
4 changes: 3 additions & 1 deletion runtime/Core/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

// Engine macros
// engine macros
#include "Definitions.h"

//= STD =====================
Expand Down Expand Up @@ -51,6 +51,8 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <variant>
#include <cstring>
#include <unordered_set>
#include <chrono>
#include <random>
//===========================

//= RUNTIME ====================
Expand Down
2 changes: 1 addition & 1 deletion runtime/World/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ namespace Spartan
light->SetColor(Color(1.0f, 0.0f, 0.0f, 1.0f));
light->SetIntensity(LightIntensity::bulb_500_watt);
light->SetShadowsEnabled(false);
light->SetRange(5.0f);
light->SetRange(3.0f);
light->SetAngle(145.0f * Math::Helper::DEG_TO_RAD);
}
}
Expand Down

0 comments on commit 9a4b6a8

Please sign in to comment.