Ejemplo n.º 1
0
	void compile(const char* path, CompileOptions& opts)
	{
		Buffer buf = opts.read(path);
		TempAllocator4096 ta;
		JsonObject object(ta);
		sjson::parse(buf, object);

		Array<PhysicsConfigMaterial> materials(default_allocator());
		Array<PhysicsConfigShape> shapes(default_allocator());
		Array<PhysicsConfigActor> actors(default_allocator());
		CollisionFilterCompiler cfc(opts);

		// Parse materials
		if (map::has(object, FixedString("collision_filters")))
			cfc.parse(object["collision_filters"]);
		if (map::has(object, FixedString("materials")))
			parse_materials(object["materials"], materials);
		if (map::has(object, FixedString("shapes")))
			parse_shapes(object["shapes"], shapes);
		if (map::has(object, FixedString("actors")))
			parse_actors(object["actors"], actors);

		// Setup struct for writing
		PhysicsConfigResource pcr;
		pcr.version       = RESOURCE_VERSION_PHYSICS_CONFIG;
		pcr.num_materials = array::size(materials);
		pcr.num_shapes    = array::size(shapes);
		pcr.num_actors    = array::size(actors);
		pcr.num_filters   = array::size(cfc._filters);

		u32 offt = sizeof(PhysicsConfigResource);
		pcr.materials_offset = offt;
		offt += sizeof(PhysicsConfigMaterial) * pcr.num_materials;

		pcr.shapes_offset = offt;
		offt += sizeof(PhysicsConfigShape) * pcr.num_shapes;

		pcr.actors_offset = offt;
		offt += sizeof(PhysicsConfigActor) * pcr.num_actors;

		pcr.filters_offset = offt;
		offt += sizeof(PhysicsCollisionFilter) * pcr.num_filters;

		// Write all
		opts.write(pcr.version);
		opts.write(pcr.num_materials);
		opts.write(pcr.materials_offset);
		opts.write(pcr.num_shapes);
		opts.write(pcr.shapes_offset);
		opts.write(pcr.num_actors);
		opts.write(pcr.actors_offset);
		opts.write(pcr.num_filters);
		opts.write(pcr.filters_offset);

		// Write material objects
		for (u32 i = 0; i < pcr.num_materials; ++i)
		{
			opts.write(materials[i].name._id);
			opts.write(materials[i].static_friction);
			opts.write(materials[i].dynamic_friction);
			opts.write(materials[i].restitution);
		}

		// Write material objects
		for (u32 i = 0; i < pcr.num_shapes; ++i)
		{
			opts.write(shapes[i].name._id);
			opts.write(shapes[i].trigger);
			opts.write(shapes[i]._pad[0]);
			opts.write(shapes[i]._pad[1]);
			opts.write(shapes[i]._pad[2]);
		}

		// Write actor objects
		for (u32 i = 0; i < pcr.num_actors; ++i)
		{
			opts.write(actors[i].name._id);
			opts.write(actors[i].linear_damping);
			opts.write(actors[i].angular_damping);
			opts.write(actors[i].flags);
		}

		for (u32 i = 0; i < array::size(cfc._filters); ++i)
		{
			opts.write(cfc._filters[i].name._id);
			opts.write(cfc._filters[i].me);
			opts.write(cfc._filters[i].mask);
		}
	}
Ejemplo n.º 2
0
int read_level(int lev)
{
    char filename[MAXFILENAME];
    char readline[MAXLINE];
    char *data;
    int  gotPaths = 0, gotShapes = 0, gotDelays = 0;
    int elem;
    FILE* lf;
    static int maxLevel = 1;

    snprintf(filename, MAXFILENAME, "%s/levels/level%d.xgl", DATADIR, lev);
	filename[MAXFILENAME-1] = '\0';
    lf = fopen(filename, "r");
    if(!lf) {
		/* Dont use recursion, to avoid stack overflow if start level is
		 * very high (or the game is played for a _very_ long time. :-)
		 * -- JEH */
		for (lev = lev - maxLevel; lev > 0 && ! lf ;lev = lev - maxLevel) {
			metaLevel++;
			/* real_level_path is already set, so.. */
			snprintf(filename, MAXFILENAME, "%s/levels/level%d.xgl", DATADIR, lev);
			filename[MAXFILENAME-1] = '\0';
			lf = fopen(filename, "r");
		}

        //fprintf(stderr, "Can't open level file %s\n", filename);
		//return read_level(lev - maxLevel);
    }

    if(lev > maxLevel)
		maxLevel = lev;

    while(!feof(lf)) {
		if(get_line(lf, readline) <= 0) {
			if(gotShapes && gotPaths && gotDelays) {
				return 1;
			}
			fprintf(stderr, "Error reading level file %s:\n", filename);
			if(!gotShapes)
				fprintf(stderr, "  Missing shapes\n");
			if(!gotPaths)
				fprintf(stderr, "  Missing paths\n");
			if(!gotDelays)
				fprintf(stderr, "  Missing delays\n");

			fclose(lf);
			return 0;
		}

		switch((get_token(readline, &elem, &data))) {
		case PF_PATHDEF:
			parse_path(elem, data);
			break;
		case PF_SHAPES:
			gotShapes = parse_shapes(data);
			break;
		case PF_PATHS:
			gotPaths = parse_paths(data);
			break;
		case PF_DELAYS:
			gotDelays = parse_delays(data);
			break;
		default:
			break;
		}
    }

    fclose(lf);

    if(gotShapes && gotPaths && gotDelays) {
		return 1;
    }

    return 0;
}