Well, I don't know what's the best solution, but, what I would want to have is a library that read all the level from one svg or whatever file. bodies, shapes, custom properties, sprites...
Matt, I missed your code, is it published?
I sepnt the weekend looking for a solution to my needs of level builder, and decided to evolve Mik's idea.
As I was reading your code, Mik, I quickly thought: "damn! I need rotating the rects". I have never imanginated how dificult it is for me.
You have to remember the transformation matrix that you studied a long time ago! And belive me, it's not trival ...
But I finally got it working and imported in my game the rectangles with the definde rotation. Here is the code:
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
// Parsing the SVG
NSString *transform;
//************************ globals **********************
if ([elementName isEqualToString:@"g"]) {// Get the document transformation values
transform = [attributeDict valueForKey:@"transform"];
NSString *transformString = [transform substringToIndex:[transform length] - 1];
transformString = [transformString substringFromIndex:10];
NSArray *transformCoords = [transformString componentsSeparatedByString:@","];
transformX = [[transformCoords objectAtIndex:0] floatValue];
transformY = [[transformCoords objectAtIndex:1] floatValue];
//************************ rectangulo **********************
}else if ([elementName isEqualToString:@"rect"]) {// Get the Blocks
NSString *type = [attributeDict valueForKey:@"inkscape:label"];
if ([type isEqualToString:@"#Block"]) {
float x = [[attributeDict valueForKey:@"x"] floatValue];
float y = [[attributeDict valueForKey:@"y"] floatValue];
float width = [[attributeDict valueForKey:@"width"] floatValue];
float height = [[attributeDict valueForKey:@"height"] floatValue];
NSString *tmatrix = [attributeDict valueForKey:@"transform"];
double angulo = 0.0f;
if (tmatrix) {
NSString *transformString = [tmatrix substringToIndex:[tmatrix length] - 1];
transformString = [transformString substringFromIndex:7];
NSArray *tCoords = [transformString componentsSeparatedByString:@","];
double a = [[tCoords objectAtIndex:0] floatValue];
double b = [[tCoords objectAtIndex:1] floatValue];
double c = [[tCoords objectAtIndex:2] floatValue];
double d = [[tCoords objectAtIndex:3] floatValue];
double e = [[tCoords objectAtIndex:4] floatValue];
double f = [[tCoords objectAtIndex:5] floatValue];
//obtengo el angulo
angulo = atan2(b,a);
//Calculo el entro del rectangulo sin transformar
x = x + width/2;
y = y + height/2;
//aplico la transformada
float xTr = a*x + c*y + e;
float yTr = b*x + d*y + f;
x = xTr;
y = yTr;
}
else {
// Calculo el centro del rect.
x = x + width/2;
y = y + height/2;
}
//aplico la traslacion por defecto del documento
x = x + transformX;
y = y + transformY;
//creo el objeto
[self createBlock:cpv(x,y) width:width height:height angle:angulo];
}
//************************ paths **********************
}else if ([elementName isEqualToString:@"path"]) {// Get the Ground parts
NSString *data = [attributeDict valueForKey:@"d"];
[self createLandscapePart:(NSString *)data];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
//
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
//
}
- (void) parserDidEndDocument:(NSXMLParser *)parser{
NSLog(@"SVG Level Builder Terminated.");
}
- (void)createBlock:(cpVect)position width:(float)width height:(float)height angle:(float)angulo {
//CMBody *squareBody = [mSpace addBodyWithMass:5 moment:0];
CMBody *squareBody = [mSpace addStaticBody];
[squareBody setPositionUsingVect:position];
[squareBody addToSpace];
CMShape *squareShape = [squareBody addRectangleWithWidth:width height:height];
[squareShape setElasticity:0.5];
[squareShape setFriction:0.5];
//set angle
[squareBody setAngle:angulo];
//add it
[squareShape addToSpace];
}
Of course you have to call it using Mik's way:
// Parse the SVG level
NSLog(@"SVG Level Builder Initiated.");
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"level3" ofType:@"svg"];
NSXMLParser *parser;
parser = [[NSXMLParser alloc] initWithData:[NSData dataWithContentsOfFile:filePath]];
[parser setDelegate:self];
[parser parse];
[parser release];
I hope I find the time to try to work on a more complete parser for svg files, and post it in this thread...