Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SimObject.cpp
Go to the documentation of this file.
1 /* Project Starshatter 4.5
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: Stars.exe
6  FILE: SimObject.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Simulation Object and Observer classes
13 */
14 
15 #include "MemDebug.h"
16 #include "SimObject.h"
17 
18 #include "Graphic.h"
19 #include "Light.h"
20 #include "Scene.h"
21 
22 // +--------------------------------------------------------------------+
23 
25 {
27  while (++observed)
28  observed->Unregister(this);
29 }
30 
31 void
33 {
34  if (obj) {
35  obj->Register(this);
36 
37  if (!observe_list.contains(obj))
38  observe_list.append(obj);
39  }
40 }
41 
42 void
44 {
45  if (obj) {
46  obj->Unregister(this);
47  observe_list.remove(obj);
48  }
49 }
50 
51 bool
53 {
54  if (obj)
55  observe_list.remove(obj);
56 
57  return true;
58 }
59 
60 const char*
62 {
63  static char name[32];
64  sprintf_s(name, "SimObserver 0x%08x", (DWORD) this);
65  return name;
66 }
67 
68 // +--------------------------------------------------------------------+
69 
71 {
72  Notify();
73 }
74 
75 // +--------------------------------------------------------------------+
76 
77 void
79 {
80  if (!notifying) {
81  notifying = true;
82 
83  int nobservers = observers.size();
84  int nupdate = 0;
85 
86  if (nobservers > 0) {
88  while (++iter) {
89  SimObserver* observer = iter.value();
90  observer->Update(this);
91  nupdate++;
92  }
93 
94  observers.clear();
95  }
96 
97  if (nobservers != nupdate) {
98  ::Print("WARNING: incomplete notify sim object '%s' - %d of %d notified\n",
99  Name(), nupdate, nobservers);
100  }
101 
102  notifying = false;
103  }
104  else {
105  ::Print("WARNING: double notify on sim object '%s'\n", Name());
106  }
107 }
108 
109 // +--------------------------------------------------------------------+
110 
111 void
113 {
114  if (!notifying && !observers.contains(observer))
115  observers.append(observer);
116 }
117 
118 // +--------------------------------------------------------------------+
119 
120 void
122 {
123  if (!notifying)
124  observers.remove(observer);
125 }
126 
127 // +--------------------------------------------------------------------+
128 
129 void
131 {
132  if (rep)
133  scene.AddGraphic(rep);
134  if (light)
135  scene.AddLight(light);
136 
137  active = true;
138 }
139 
140 void
142 {
143  if (rep)
144  scene.DelGraphic(rep);
145  if (light)
146  scene.DelLight(light);
147 
148  active = false;
149 }
150