absence_thelab
view src/demosystem/demosys.cpp @ 1:4d5933c261c3
todo and .hgignore
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Oct 2014 02:18:43 +0300 |
parents | |
children |
line source
1 #include "demosys.h"
3 /////////////// Part base class implementation ///////////////
5 Part::Part() {
6 gc = 0;
8 rmode = RenderModeNormal;
9 RenderTexture = 0;
11 SetTimingAbs(0, 0);
12 paused = false;
13 }
15 void Part::SetGraphicsContext(GraphicsContext *gc) {
16 this->gc = gc;
17 }
19 GraphicsContext *Part::GetGraphicsContext() {
20 return gc;
21 }
23 void Part::SetTimingAbs(dword start, dword end) {
24 StartTime = start;
25 EndTime = end;
26 Duration = EndTime - StartTime;
27 }
29 void Part::SetTimingRel(dword start, dword dur) {
30 StartTime = start;
31 Duration = dur;
32 EndTime = StartTime + Duration;
33 }
35 void Part::Pause() {
36 if(paused) return;
37 timer.Stop();
38 paused = true;
39 }
41 void Part::Resume() {
42 if(!paused) return;
43 timer.Resume();
44 paused = false;
45 }
47 dword Part::GetStartTime() const {
48 return StartTime;
49 }
51 dword Part::GetEndTime() const {
52 return EndTime;
53 }
55 dword Part::GetDuration() const {
56 return Duration;
57 }
59 dword Part::GetTimePosition() const {
60 return timer.GetMilliSec();
61 }
63 float Part::GetParametricPosition() const {
64 return (float)timer.GetMilliSec() / (float)Duration;
65 }
67 void Part::SetRenderMode(RenderMode rmode) {
68 this->rmode = rmode;
69 }
71 void Part::SetRenderTexture(Texture *tex) {
72 RenderTexture = tex;
73 }
75 RenderMode Part::GetRenderMode() const {
76 return rmode;
77 }
79 Texture *Part::GetRenderTexture() const {
80 return RenderTexture;
81 }
84 void Part::Launch() {
85 timer.Start();
86 }
88 void Part::ShutDown() {
89 // do nothing
90 }
92 Scene *Part::GetScene() {
93 return scene;
94 }
97 /////////////// main Demo System class implementation ///////////////
99 DemoSystem::DemoSystem(GraphicsContext *gc) {
100 this->gc = gc;
101 state = DemoStateStopped;
102 }
104 void DemoSystem::AddPart(Part *part) {
106 if(state != DemoStateStopped) return;
108 if(!part->GetGraphicsContext()) part->SetGraphicsContext(gc);
110 parts.push_back(part);
111 inactive.push_back(part);
112 }
114 Part *DemoSystem::GetActivePart() {
115 return *active.begin();
116 }
118 //// demo flow control ////
120 // Run demo from the beggining
121 void DemoSystem::Run() {
123 if(state != DemoStateStopped) return;
125 state = DemoStateRunning;
126 timer.Start();
127 }
129 // Stop the execution of the demo, discard any sequencing information
130 void DemoSystem::Stop() {
132 if(state == DemoStateStopped) return;
134 while(active.size()) {
135 active.erase(active.begin());
136 }
138 inactive = parts;
140 state = DemoStateStopped;
141 }
143 // Pause the demo (freeze the timers)
144 void DemoSystem::Pause() {
146 if(state != DemoStateRunning) return;
148 std::list<Part*>::iterator iter = active.begin();
149 while(iter != active.end()) {
150 (*iter++)->Pause();
151 }
152 timer.Stop();
154 state = DemoStatePaused;
155 }
157 void DemoSystem::Resume() {
159 if(state != DemoStatePaused) return;
161 std::list<Part*>::iterator iter = active.begin();
162 while(iter != active.end()) {
163 (*iter++)->Resume();
164 }
165 timer.Resume();
167 state = DemoStateRunning;
168 }
171 void DemoSystem::Update() {
173 if(state != DemoStateRunning) return;
175 dword time = timer.GetMilliSec();
177 // Check if there are any inactive parts to launch
178 std::list<Part*>::iterator iter = inactive.begin();
179 while(iter != inactive.end()) {
180 if(time >= (*iter)->GetStartTime()) {
181 (*iter)->Launch();
182 active.push_back(*iter);
183 iter = inactive.erase(iter);
184 } else {
185 iter++;
186 }
187 }
189 // check if there are any parts to close and close them, and run the valid ones
190 iter = active.begin();
191 while(iter != active.end()) {
192 if(time >= (*iter)->GetEndTime()) {
193 (*iter)->ShutDown();
194 iter = active.erase(iter);
195 } else {
196 // run the part
197 if((*iter)->GetRenderMode() == RenderModeTexture) {
198 gc->SetRenderTarget((*iter)->GetRenderTexture(), (Texture*)0);
199 gc->Clear(0);
200 gc->ClearZBufferStencil(1.0f, 0);
201 }
203 (*iter)->MainLoop();
205 if((*iter)->GetRenderMode() == RenderModeTexture) {
206 gc->ResetRenderTarget();
207 }
209 iter++;
210 }
211 }
212 }