summaryrefslogtreecommitdiff
path: root/vfg/src/Puppet.cpp
blob: 9d9a11eeaec106a0b7362775c0d068fac788f640 (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
#include "Puppet.h"

Puppet::Puppet()
{

}

Puppet::~Puppet()
{
    //dtor
}

void Puppet::load(string filename) {
	ofxXmlSettings XML;
	int frames=0;
	long t=ofGetElapsedTimeMillis();
	if( !XML.loadFile(filename) ){
		printf("unable to load %s check data/ folder\n",filename.c_str());
	}else{
		if(XML.pushTag("VFxmas")) {
			for (int i=0;i<XML.getNumTags("Clip");i++) {
				string name=XML.getAttribute("Clip", "name","",i);
				if (name!="") {
					clips[name]=puppetSprite();
					clips[name].load(XML.getAttribute("Clip", "files","",i),XML.getAttribute("Clip", "frames",0,i),XML.getAttribute("Clip", "start",0,i));
					clips[name].setAnchorPercent(XML.getAttribute("Clip", "xAnchorPct",0.5,i),XML.getAttribute("Clip", "yAnchorPct",0.5,i));
					clips[name].setFrameRate(XML.getAttribute("Clip", "rate",25,i));
					clips[name].setLoop(false);
					frames+=clips[name].getTotalFrames();
				}
			}
			printf("loaded %s: %i animation clips with %i frames in %2fs\n",filename.c_str(),clips.size(),frames,((float)(ofGetElapsedTimeMillis()-t))*.001f);
		}
	}
}

void Puppet::draw(float x, float y, float scale) {
	if (playlist.size()>0) {
		if (!clips[playlist[0].second].getIsPlaying()) {
		    if (ofGetElapsedTimeMillis()-playlist[0].first>clips[playlist[0].second].getDuration()) {
		        playlist.erase(playlist.begin());
		    }
			if (playlist.size()>0) {
				if (playlist[0].first<ofGetElapsedTimeMillis())
				clips[playlist[0].second].play();
			}
		}
	}
	if (playlist.size()==0) {
		if (clips.find("base") != clips.end()) clips["base"].draw(x,y,scale);
	}
	else if (clips[playlist[0].second].getIsPlaying()){
		clips[playlist[0].second].update();
		clips[playlist[0].second].draw(x,y,scale);
	}
}

void Puppet::play(string clip,int time){
	if (clips.find(clip) != clips.end()) {
		playlist.push_back(make_pair(time,clip));
		if (playlist.size()==1&&time<ofGetElapsedTimeMillis()) clips[playlist[0].second].play();
		//printf("playing %s, %i, %s\n",playlist[0].c_str(),playlist.size(),clips[playlist[0]].getIsPlaying()?"true":"false");
	}
}
void Puppet::playNow(string clip,int time){
    if (playlist.size()>0) {
        clips[playlist[0].second].stop();
        playlist.clear();
    }
    play(clip);
}

bool Puppet::isPlaying(){
	if (playlist.size()>0) return clips[playlist[0].second].getIsPlaying();
	else return false;
}

void Puppet::clear(){
    playlist.clear();
}