blob: 8ee8db720880543941d27f44df12b484c9e05958 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
/* Starshatter: The Open Source Project
Copyright (c) 2021-2024, 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 <List.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;
}
|