absence_thelab

diff src/demosystem/demosys.cpp @ 0:1cffe3409164

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Oct 2014 01:46:07 +0300
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/demosystem/demosys.cpp	Thu Oct 23 01:46:07 2014 +0300
     1.3 @@ -0,0 +1,212 @@
     1.4 +#include "demosys.h"
     1.5 +
     1.6 +/////////////// Part base class implementation ///////////////
     1.7 +
     1.8 +Part::Part() {
     1.9 +	gc = 0;
    1.10 +
    1.11 +	rmode = RenderModeNormal;
    1.12 +	RenderTexture = 0;
    1.13 +
    1.14 +	SetTimingAbs(0, 0);
    1.15 +	paused = false;
    1.16 +}
    1.17 +
    1.18 +void Part::SetGraphicsContext(GraphicsContext *gc) {
    1.19 +	this->gc = gc;
    1.20 +}
    1.21 +
    1.22 +GraphicsContext *Part::GetGraphicsContext() {
    1.23 +	return gc;
    1.24 +}
    1.25 +
    1.26 +void Part::SetTimingAbs(dword start, dword end) {
    1.27 +	StartTime = start;
    1.28 +	EndTime = end;
    1.29 +	Duration = EndTime - StartTime;
    1.30 +}
    1.31 +
    1.32 +void Part::SetTimingRel(dword start, dword dur) {
    1.33 +	StartTime = start;
    1.34 +	Duration = dur;
    1.35 +	EndTime = StartTime + Duration;
    1.36 +}
    1.37 +
    1.38 +void Part::Pause() {
    1.39 +	if(paused) return;
    1.40 +	timer.Stop();
    1.41 +	paused = true;
    1.42 +}
    1.43 +
    1.44 +void Part::Resume() {
    1.45 +	if(!paused) return;
    1.46 +	timer.Resume();
    1.47 +	paused = false;
    1.48 +}
    1.49 +
    1.50 +dword Part::GetStartTime() const {
    1.51 +	return StartTime;
    1.52 +}
    1.53 +
    1.54 +dword Part::GetEndTime() const {
    1.55 +	return EndTime;
    1.56 +}
    1.57 +
    1.58 +dword Part::GetDuration() const {
    1.59 +	return Duration;
    1.60 +}
    1.61 +
    1.62 +dword Part::GetTimePosition() const {
    1.63 +	return timer.GetMilliSec();
    1.64 +}
    1.65 +
    1.66 +float Part::GetParametricPosition() const {
    1.67 +	return (float)timer.GetMilliSec() / (float)Duration;
    1.68 +}
    1.69 +
    1.70 +void Part::SetRenderMode(RenderMode rmode) {
    1.71 +	this->rmode = rmode;
    1.72 +}
    1.73 +
    1.74 +void Part::SetRenderTexture(Texture *tex) {
    1.75 +	RenderTexture = tex;
    1.76 +}
    1.77 +
    1.78 +RenderMode Part::GetRenderMode() const {
    1.79 +	return rmode;
    1.80 +}
    1.81 +
    1.82 +Texture *Part::GetRenderTexture() const {
    1.83 +	return RenderTexture;
    1.84 +}
    1.85 +
    1.86 +
    1.87 +void Part::Launch() {
    1.88 +	timer.Start();
    1.89 +}
    1.90 +
    1.91 +void Part::ShutDown() {
    1.92 +	// do nothing
    1.93 +}
    1.94 +
    1.95 +Scene *Part::GetScene() {
    1.96 +	return scene;
    1.97 +}
    1.98 +
    1.99 +
   1.100 +/////////////// main Demo System class implementation ///////////////
   1.101 +
   1.102 +DemoSystem::DemoSystem(GraphicsContext *gc) {
   1.103 +	this->gc = gc;
   1.104 +	state = DemoStateStopped;
   1.105 +}
   1.106 +
   1.107 +void DemoSystem::AddPart(Part *part) {
   1.108 +
   1.109 +	if(state != DemoStateStopped) return;
   1.110 +
   1.111 +	if(!part->GetGraphicsContext()) part->SetGraphicsContext(gc);
   1.112 +
   1.113 +	parts.push_back(part);
   1.114 +	inactive.push_back(part);
   1.115 +}
   1.116 +
   1.117 +Part *DemoSystem::GetActivePart() {
   1.118 +	return *active.begin();
   1.119 +}
   1.120 +
   1.121 +//// demo flow control ////
   1.122 +
   1.123 +// Run demo from the beggining
   1.124 +void DemoSystem::Run() {
   1.125 +
   1.126 +	if(state != DemoStateStopped) return;
   1.127 +
   1.128 +	state = DemoStateRunning;
   1.129 +	timer.Start();
   1.130 +}
   1.131 +
   1.132 +// Stop the execution of the demo, discard any sequencing information
   1.133 +void DemoSystem::Stop() {
   1.134 +
   1.135 +	if(state == DemoStateStopped) return;
   1.136 +
   1.137 +	while(active.size()) {
   1.138 +		active.erase(active.begin());
   1.139 +	}
   1.140 +
   1.141 +	inactive = parts;
   1.142 +
   1.143 +	state = DemoStateStopped;
   1.144 +}
   1.145 +
   1.146 +// Pause the demo (freeze the timers)
   1.147 +void DemoSystem::Pause() {
   1.148 +
   1.149 +	if(state != DemoStateRunning) return;
   1.150 +
   1.151 +	std::list<Part*>::iterator iter = active.begin();
   1.152 +	while(iter != active.end()) {
   1.153 +		(*iter++)->Pause();
   1.154 +	}	
   1.155 +	timer.Stop();
   1.156 +	
   1.157 +	state = DemoStatePaused;
   1.158 +}
   1.159 +
   1.160 +void DemoSystem::Resume() {
   1.161 +
   1.162 +	if(state != DemoStatePaused) return;
   1.163 +
   1.164 +	std::list<Part*>::iterator iter = active.begin();
   1.165 +	while(iter != active.end()) {
   1.166 +		(*iter++)->Resume();
   1.167 +	}	
   1.168 +	timer.Resume();
   1.169 +
   1.170 +	state = DemoStateRunning;
   1.171 +}
   1.172 +
   1.173 +
   1.174 +void DemoSystem::Update() {
   1.175 +
   1.176 +	if(state != DemoStateRunning) return;
   1.177 +
   1.178 +	dword time = timer.GetMilliSec();
   1.179 +
   1.180 +	// Check if there are any inactive parts to launch
   1.181 +	std::list<Part*>::iterator iter = inactive.begin();
   1.182 +	while(iter != inactive.end()) {
   1.183 +		if(time >= (*iter)->GetStartTime()) {
   1.184 +			(*iter)->Launch();
   1.185 +			active.push_back(*iter);
   1.186 +			iter = inactive.erase(iter);
   1.187 +		} else {
   1.188 +			iter++;
   1.189 +		}
   1.190 +	}
   1.191 +
   1.192 +	// check if there are any parts to close and close them, and run the valid ones
   1.193 +	iter = active.begin();
   1.194 +	while(iter != active.end()) {
   1.195 +		if(time >= (*iter)->GetEndTime()) {
   1.196 +			(*iter)->ShutDown();
   1.197 +			iter = active.erase(iter);
   1.198 +		} else {
   1.199 +			// run the part
   1.200 +			if((*iter)->GetRenderMode() == RenderModeTexture) {
   1.201 +				gc->SetRenderTarget((*iter)->GetRenderTexture(), (Texture*)0);
   1.202 +				gc->Clear(0);
   1.203 +				gc->ClearZBufferStencil(1.0f, 0);
   1.204 +			}
   1.205 +
   1.206 +			(*iter)->MainLoop();
   1.207 +
   1.208 +			if((*iter)->GetRenderMode() == RenderModeTexture) {
   1.209 +				gc->ResetRenderTarget();
   1.210 +			}
   1.211 +
   1.212 +			iter++;
   1.213 +		}
   1.214 +	}
   1.215 +}
   1.216 \ No newline at end of file