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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
#include <stdio.h>
#include <string.h>
#include <expat.h>
#include "event.h"
#include "main/curve.h"
#include "main/linearcurve.h"
#include "main/autosplinecurve.h"
#include "main/linecurve.h"
#include "exception.h"
#include "demolib_prefs.h"
#if DEMOLIB_MAINLOOP
Event::Event(MainLoop *ml, const char *title, const char *elem, Hashtable *attr, char *curvenames)
{
int curve_type = CURVE_NONE;
this->start = -1.0f;
this->stop = -1.0f;
this->active = false;
this->title = strdup(title);
if (attr->exists("layer")) {
this->layer = attr->get_float("layer");
} else {
this->layer = 0.0f;
}
this->curves = NULL;
if (attr->exists("curve")) {
const char * const ctstr = (const char * const)(attr->lookup("curve"));
if (strcmp(ctstr, "linear") == 0) {
curve_type = CURVE_LINEAR;
}
if (strcmp(ctstr, "autospline") == 0) {
curve_type = CURVE_AUTOSPLINE;
}
if (strcmp(ctstr, "line") == 0) {
curve_type = CURVE_LINE;
}
if (curve_type == CURVE_NONE) {
throw new FatalException(ctstr, "Invalid curve type.");
}
}
if (curvenames == NULL || strcmp(curvenames, "") == 0) return;
if (curve_type == CURVE_NONE) {
throw new FatalException(elem, "No curve type specified.");
}
this->curves = new Hashtable(true);
/* now create a set of attribute curves */
char *cn = strdup(curvenames);
char *curve = strtok(cn, ":");
do {
Curve *c = NULL;
switch (curve_type) {
case CURVE_LINEAR:
c = new LinearCurve();
break;
case CURVE_AUTOSPLINE:
c = new AutoSplineCurve();
break;
case CURVE_LINE:
c = new LineCurve();
break;
}
this->curves->insert(curve, c);
} while ((curve = strtok(NULL, ":")) != NULL);
free(cn);
this->ml = ml;
}
Event::~Event()
{
free(this->title);
/* free the hash tables for the curves */
if (this->curves != NULL) {
this->curves->destroy_values();
delete this->curves;
this->curves = NULL;
}
}
void Event::add_curvepoint(Hashtable *markers, const char *element, const char **attr)
{
float timeval = -1.0f;
/* first we need to grab the time */
int i = 0;
while (attr[i]) {
if (strcmp(attr[i], "time") == 0) {
timeval = parse_time(markers, attr[i + 1]);
break;
}
i += 2;
}
if (timeval == -1.0f) {
throw new FatalException(element, "Missing time= attribute");
}
if (strcmp(element, "start") == 0) {
if (this->start != -1.0f) {
throw new FatalException(element, "Multiple <start> tags");
}
this->start = timeval;
}
if (strcmp(element, "end") == 0) {
if (this->stop != -1.0f) {
throw new FatalException(element, "Multiple <end> tags");
}
this->stop = timeval;
}
i = 0;
while (attr[i]) {
if (strcmp(attr[i], "time") == 0) {
i += 2;
continue;
}
if (this->curves == NULL) {
throw new FatalException(attr[i], "No such curve!");
}
Curve *c = (Curve *)this->curves->lookup(attr[i]);
if (c == NULL) {
throw new FatalException(attr[i], "No such curve!");
}
c->add_curvepoint(timeval, atof(attr[i + 1]));
i += 2;
}
}
void Event::end_curvedata()
{
if (this->start == -1.0f) {
throw new FatalException("Missing <start> tag!");
}
if (this->stop == -1.0f) {
throw new FatalException("Missing <end> tag!");
}
if (this->curves != NULL) {
this->curves->finish_curvedata(this->start, this->stop);
}
}
void Event::start_effect() {}
void Event::draw_scene(float progress) {}
void Event::end_effect() {}
float Event::get_val(char *attr_name, float progress)
{
if (this->curves == NULL) {
throw new FatalException(attr_name, "No curves defined in effect!");
}
Curve *c = (Curve *)this->curves->lookup(attr_name);
if (c == NULL) {
throw new FatalException(attr_name, "No such curve defined!");
}
return c->get_value(progress);
}
float Event::parse_time(Hashtable *markers, const char *timestr)
{
if (timestr[0] < '0' || timestr[0] > '9') {
/* a marker name -- does it contain a '-' or '+'? */
char buf[256];
float adj;
strcpy(buf, timestr);
unsigned int p = strcspn(buf, "-+");
if (p == strlen(buf)) {
adj = 0.0f;
} else {
if (buf[p] == '+') {
adj = atof(buf + p + 1);
} else {
adj = atof(buf + p);
}
buf[p] = '\0';
}
void *f = markers->lookup(buf);
if (f == NULL) {
throw new FatalException(buf, "No such marker.");
}
return *((float *)f) + adj;
} else {
return atof(timestr);
}
}
#endif
|