summaryrefslogtreecommitdiff
path: root/vfg/src/Puppet.cpp
blob: 90ba137efa310efd4f7cc5c521f390a9f2e80049 (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
#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]].getIsPlaying()) {
			playlist.erase(playlist.begin());
			if (playlist.size()>0) {
				clips[playlist[0]].play();
			}
		}
	}
	if (playlist.size()==0) {
		if (clips.find("base") != clips.end()) clips["base"].draw(x,y,scale);
	}
	else {
		clips[playlist[0]].update();
		clips[playlist[0]].draw(x,y,scale);
	}
}

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

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