diff options
author | Aki <please@ignore.pl> | 2022-04-01 21:23:39 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2022-04-01 21:23:39 +0200 |
commit | 3c487c5cd69c53d6fea948643c0a76df03516605 (patch) | |
tree | 72730c7b8b26a5ef8fc9a987ec4c16129efd5aac /StarsEx/SimObject.cpp | |
parent | 8f353abd0bfe18baddd8a8250ab7c4f2d1c83a6e (diff) | |
download | starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.zip starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.gz starshatter-3c487c5cd69c53d6fea948643c0a76df03516605.tar.bz2 |
Moved Stars45 to StarsEx
Diffstat (limited to 'StarsEx/SimObject.cpp')
-rw-r--r-- | StarsEx/SimObject.cpp | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/StarsEx/SimObject.cpp b/StarsEx/SimObject.cpp new file mode 100644 index 0000000..24fac59 --- /dev/null +++ b/StarsEx/SimObject.cpp @@ -0,0 +1,148 @@ +/* Starshatter: The Open Source Project + Copyright (c) 2021-2022, Starshatter: The Open Source Project Contributors + Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors + Copyright (c) 1997-2006, Destroyer Studios LLC. + + AUTHOR: John DiCamillo + + + OVERVIEW + ======== + Simulation Object and Observer classes +*/ + +#include "SimObject.h" + +#include "Graphic.h" +#include "Light.h" +#include "Scene.h" + +// +--------------------------------------------------------------------+ + +SimObserver::~SimObserver() +{ + ListIter<SimObject> observed = observe_list; + while (++observed) + observed->Unregister(this); +} + +void +SimObserver::Observe(SimObject* obj) +{ + if (obj) { + obj->Register(this); + + if (!observe_list.contains(obj)) + observe_list.append(obj); + } +} + +void +SimObserver::Ignore(SimObject* obj) +{ + if (obj) { + obj->Unregister(this); + observe_list.remove(obj); + } +} + +bool +SimObserver::Update(SimObject* obj) +{ + if (obj) + observe_list.remove(obj); + + return true; +} + +const char* +SimObserver::GetObserverName() const +{ + static char name[32]; + sprintf_s(name, "SimObserver 0x%08x", (DWORD) this); + return name; +} + +// +--------------------------------------------------------------------+ + +SimObject::~SimObject() +{ + Notify(); +} + +// +--------------------------------------------------------------------+ + +void +SimObject::Notify() +{ + if (!notifying) { + notifying = true; + + int nobservers = observers.size(); + int nupdate = 0; + + if (nobservers > 0) { + ListIter<SimObserver> iter = observers; + while (++iter) { + SimObserver* observer = iter.value(); + observer->Update(this); + nupdate++; + } + + observers.clear(); + } + + if (nobservers != nupdate) { + ::Print("WARNING: incomplete notify sim object '%s' - %d of %d notified\n", + Name(), nupdate, nobservers); + } + + notifying = false; + } + else { + ::Print("WARNING: double notify on sim object '%s'\n", Name()); + } +} + +// +--------------------------------------------------------------------+ + +void +SimObject::Register(SimObserver* observer) +{ + if (!notifying && !observers.contains(observer)) + observers.append(observer); +} + +// +--------------------------------------------------------------------+ + +void +SimObject::Unregister(SimObserver* observer) +{ + if (!notifying) + observers.remove(observer); +} + +// +--------------------------------------------------------------------+ + +void +SimObject::Activate(Scene& scene) +{ + if (rep) + scene.AddGraphic(rep); + if (light) + scene.AddLight(light); + + active = true; +} + +void +SimObject::Deactivate(Scene& scene) +{ + if (rep) + scene.DelGraphic(rep); + if (light) + scene.DelLight(light); + + active = false; +} + |