Ejemplo n.º 1
0
	void parse_frame(JSONElement e, SpriteFrame& frame)
	{
		frame.name   = e.key("name"  ).to_string_id();
		frame.region = e.key("region").to_vector4();
		frame.offset = e.key("offset").to_vector2();
		frame.scale  = e.key("scale" ).to_vector2();
	}
Ejemplo n.º 2
0
	void compile(const char* path, CompileOptions& opts)
	{
		static const uint32_t VERSION = 1;

		Buffer buf = opts.read(path);
		JSONParser json(array::begin(buf));
		JSONElement root = json.root();

		Array<LevelUnit> units(default_allocator());
		Array<LevelSound> sounds(default_allocator());

		{
			JSONElement sounds_arr = root.key("sounds");
			const uint32_t size = sounds_arr.size();
			for (uint32_t i = 0; i < size; i++)
			{
				JSONElement e = sounds_arr[i];
				LevelSound ls;
				ls.name = e.key("name").to_resource_id("sound").name;
				ls.position = e.key("position").to_vector3();
				ls.volume = e.key("volume").to_float();
				ls.range = e.key("range").to_float();
				ls.loop = e.key("loop").to_bool();
				array::push_back(sounds, ls);
			}
		}

		{
			JSONElement units_arr = root.key("units");
			const uint32_t size = units_arr.size();
			for (uint32_t i = 0; i < size; i++)
			{
				JSONElement e = units_arr[i];
				LevelUnit lu;
				lu.name = e.key("name").to_resource_id("unit").name;
				lu.position = e.key("position").to_vector3();
				lu.rotation = e.key("rotation").to_quaternion();
				array::push_back(units, lu);
			}
		}

		LevelResource lr;
		lr.version = VERSION;
		lr.num_units = array::size(units);
		lr.num_sounds = array::size(sounds);

		uint32_t offt = sizeof(LevelResource);
		lr.units_offset = offt; offt += sizeof(LevelUnit) * lr.num_units;
		lr.sounds_offset = offt;

		opts._bw & lr
			& units
			& sounds;
	}
Ejemplo n.º 3
0
//-----------------------------------------------------------------------------
void ConsoleServer::process_command(TCPSocket /*client*/, const char* msg)
{
	JSONParser parser(msg);
	JSONElement root = parser.root();
	JSONElement command = root.key("command");

	DynamicString cmd;
	command.to_string(cmd);

	if (cmd == "reload")
	{
		JSONElement type = root.key_or_nil("resource_type");
		JSONElement name = root.key_or_nil("resource_name");

		DynamicString resource_type;
		DynamicString resource_name;
		type.to_string(resource_type);
		name.to_string(resource_name);

		char t[256];
		char n[256];
		string::strncpy(t, resource_type.c_str(), 256);
		string::strncpy(n, resource_name.c_str(), 256);
		device()->reload(t, n);
	}
	else if (cmd == "pause")
	{
		device()->pause();
	}
	else if (cmd == "unpause")
	{
		device()->unpause();
	}
}
Ejemplo n.º 4
0
//-----------------------------------------------------------------------------
void ConsoleServer::process_script(TCPSocket /*client*/, const char* msg)
{
	JSONParser parser(msg);
	JSONElement root = parser.root();

	DynamicString script;
	root.key("script").to_string(script);
	device()->lua_environment()->execute_string(script.c_str());
}
Ejemplo n.º 5
0
	void compile(const char* path, CompileOptions& opts)
	{
		Buffer buf = opts.read(path);
		JSONParser json(buf);
		JSONElement root = json.root();

		Array<TextureData> texdata(default_allocator());
		Array<UniformData> unidata(default_allocator());
		Array<char> names(default_allocator());
		Array<char> dynblob(default_allocator());

		ResourceId shader = root.key("shader").to_resource_id();
		parse_textures(root, texdata, names, dynblob);
		parse_uniforms(root, unidata, names, dynblob);

		MaterialResource mr;
		mr.version = MATERIAL_VERSION;
		mr.shader = shader;
		mr.num_textures = array::size(texdata);
		mr.texture_data_offset = sizeof(mr);
		mr.num_uniforms = array::size(unidata);
		mr.uniform_data_offset = sizeof(mr) + sizeof(TextureData) * array::size(texdata);
		mr.dynamic_data_size = array::size(dynblob);
		mr.dynamic_data_offset = sizeof(mr) + sizeof(TextureData) * array::size(texdata) + sizeof(UniformData) * array::size(unidata);

		// Write
		opts.write(mr.version);
		opts.write(mr._pad);
		opts.write(mr.shader);
		opts.write(mr.num_textures);
		opts.write(mr.texture_data_offset);
		opts.write(mr.num_uniforms);
		opts.write(mr.uniform_data_offset);
		opts.write(mr.dynamic_data_size);
		opts.write(mr.dynamic_data_offset);

		for (uint32_t i = 0; i < array::size(texdata); i++)
		{
			opts.write(texdata[i].sampler_name_offset);
			opts.write(texdata[i]._pad);
			opts.write(texdata[i].id);
			opts.write(texdata[i].data_offset);
			opts.write(texdata[i]._pad1);
		}

		for (uint32_t i = 0; i < array::size(unidata); i++)
		{
			opts.write(unidata[i].name_offset);
			opts.write(unidata[i].type);
			opts.write(unidata[i].data_offset);
		}

		opts.write(dynblob);
		opts.write(names);
	}
Ejemplo n.º 6
0
	static void parse_textures(JSONElement root, Array<TextureData>& textures, Array<char>& names, Array<char>& dynamic)
	{
		using namespace vector;

		Vector<DynamicString> keys(default_allocator());
		root.key("textures").to_keys(keys);

		for (uint32_t i = 0; i < size(keys); i++)
		{
			TextureHandle th;
			th.sampler_handle = 0;
			th.texture_handle = 0;

			ResourceId texid = root.key("textures").key(keys[i].c_str()).to_resource_id();

			TextureData td;
			td.sampler_name_offset = array::size(names); array::push(names, keys[i].c_str(), keys[i].length()); array::push_back(names, '\0');
			td.id = texid;
			td.data_offset = reserve_dynamic_data(th, dynamic);

			array::push_back(textures, td);
		}
	}
Ejemplo n.º 7
0
	void parse_animations(JSONElement e, Array<SpriteAnimationName>& names, Array<SpriteAnimationData>& anim_data, Array<uint32_t>& frames)
	{
		const uint32_t num = e.key("animations").size();
		for (uint32_t i = 0; i < num; i++)
		{
			JSONElement anim(e.key("animations")[i]);

			SpriteAnimationName san;
			san.id = anim.key("name").to_string_id();

			const uint32_t num_frames = anim.key("frames").size();
			SpriteAnimationData sad;
			sad.num_frames = num_frames;
			sad.first_frame = array::size(frames);
			sad.time = anim.key("time").to_float();

			// Read frames
			for (uint32_t ff = 0; ff < num_frames; ff++)
				array::push_back(frames, (uint32_t) anim.key("frames")[ff].to_int());

			array::push_back(names, san);
			array::push_back(anim_data, sad);
		}
	}
Ejemplo n.º 8
0
	static void parse_uniforms(JSONElement root, Array<UniformData>& uniforms, Array<char>& names, Array<char>& dynamic)
	{
		using namespace vector;

		Vector<DynamicString> keys(default_allocator());
		root.key("uniforms").to_keys(keys);

		for (uint32_t i = 0; i < size(keys); i++)
		{
			UniformHandle uh;
			uh.uniform_handle = 0;

			DynamicString type = root.key("uniforms").key(keys[i].c_str()).key("type").to_string();

			UniformData ud;
			ud.name_offset = array::size(names); array::push(names, keys[i].c_str(), keys[i].length()); array::push_back(names, '\0');
			ud.type = string_to_uniform_type(type.c_str());
			ud.data_offset = reserve_dynamic_data(uh, dynamic);

			switch (ud.type)
			{
				case UniformType::FLOAT:
				{
					float data = root.key("uniforms").key(keys[i].c_str()).key("value").to_float();
					reserve_dynamic_data(data, dynamic);
					break;
				}
				case UniformType::VECTOR2:
				{
					Vector2 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector2();
					reserve_dynamic_data(data, dynamic);
					break;
				}
				case UniformType::VECTOR3:
				{
					Vector3 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector3();
					reserve_dynamic_data(data, dynamic);
					break;
				}
				case UniformType::VECTOR4:
				{
					Vector4 data = root.key("uniforms").key(keys[i].c_str()).key("value").to_vector4();
					reserve_dynamic_data(data, dynamic);
					break;
				}
				default: CE_FATAL("Oops"); break;
			}

			array::push_back(uniforms, ud);
		}
	}
Ejemplo n.º 9
0
size_t NetworkFile::size()
{
	using namespace string_stream;

	TempAllocator1024 alloc;
	StringStream command(alloc);

	// Request the file
	command << "{\"type\":\"filesystem\",\"filesystem\":\"size\",";
	command << "\"file\":\"" << _filename << "\"}";

	network_filesystem::send(_socket, c_str(command));

	// Wait for response
	Array<char> response(default_allocator());
	network_filesystem::read_response(_socket, response);

	JSONParser parser(array::begin(response));
	JSONElement root = parser.root();

	return (size_t) root.key("size").to_int();
}
Ejemplo n.º 10
0
	void compile(const char* path, CompileOptions& opts)
	{
		Buffer buf = opts.read(path);
		JSONParser json(buf);
		JSONElement root = json.root();

		// Read width/height
		const float width  = root.key("width" ).to_float();
		const float height = root.key("height").to_float();
		const uint32_t num_frames = root.key("frames").size();

		Array<float> vertices(default_allocator());
		Array<uint16_t> indices(default_allocator());
		uint32_t num_idx = 0;
		for (uint32_t i = 0; i < num_frames; i++)
		{
			JSONElement e(root.key("frames")[i]);

			SpriteFrame frame;
			parse_frame(e, frame);

			const SpriteFrame& fd = frame;

			// Compute uv coords
			const float u0 = fd.region.x / width;
			const float v0 = fd.region.y / height;
			const float u1 = (fd.region.x + fd.region.z) / width;
			const float v1 = (fd.region.y + fd.region.w) / height;

			// Compute positions
			const float w = fd.region.z / CROWN_DEFAULT_PIXELS_PER_METER;
			const float h = fd.region.w / CROWN_DEFAULT_PIXELS_PER_METER;

			const float x0 = fd.scale.x * (-w * 0.5f) + fd.offset.x;
			const float y0 = fd.scale.y * (-h * 0.5f) + fd.offset.y;
			const float x1 = fd.scale.x * ( w * 0.5f) + fd.offset.x;
			const float y1 = fd.scale.y * ( h * 0.5f) + fd.offset.y;

			array::push_back(vertices, x0); array::push_back(vertices, y0); // position
			array::push_back(vertices, u0); array::push_back(vertices, v0); // uv

			array::push_back(vertices, x1); array::push_back(vertices, y0); // position
			array::push_back(vertices, u1); array::push_back(vertices, v0); // uv

			array::push_back(vertices, x1); array::push_back(vertices, y1); // position
			array::push_back(vertices, u1); array::push_back(vertices, v1); // uv

			array::push_back(vertices, x0); array::push_back(vertices, y1); // position
			array::push_back(vertices, u0); array::push_back(vertices, v1); // uv

			array::push_back(indices, uint16_t(num_idx)); array::push_back(indices, uint16_t(num_idx + 1)); array::push_back(indices, uint16_t(num_idx + 2));
			array::push_back(indices, uint16_t(num_idx)); array::push_back(indices, uint16_t(num_idx + 2)); array::push_back(indices, uint16_t(num_idx + 3));
			num_idx += 4;
		}

		const uint32_t num_vertices = array::size(vertices) / 4; // 4 components per vertex
		const uint32_t num_indices = array::size(indices);

		// Write header
		opts.write(SPRITE_VERSION);

		opts.write(num_vertices);
		for (uint32_t i = 0; i < array::size(vertices); i++)
		{
			opts.write(vertices[i]);
		}

		opts.write(num_indices);
		for (uint32_t i = 0; i < array::size(indices); i++)
		{
			opts.write(indices[i]);
		}
	}
Ejemplo n.º 11
0
	//-----------------------------------------------------------------------------
	void compile(const char* path, CompileOptions& opts)
	{
		Buffer buf = opts.read(path);
		JSONParser json(array::begin(buf));
		JSONElement root = json.root();

		DynamicString vs_code;
		DynamicString fs_code;
		DynamicString varying_def;

		root.key("vs_code").to_string(vs_code);
		root.key("fs_code").to_string(fs_code);
		root.key("varying_def").to_string(varying_def);

		DynamicString vs_code_path;
		DynamicString fs_code_path;
		DynamicString varying_def_path;
		DynamicString tmpvs_path;
		DynamicString tmpfs_path;

		opts.get_absolute_path(vs_code.c_str(), vs_code_path);
		opts.get_absolute_path(fs_code.c_str(), fs_code_path);
		opts.get_absolute_path(varying_def.c_str(), varying_def_path);
		opts.get_absolute_path("tmpvs", tmpvs_path);
		opts.get_absolute_path("tmpfs", tmpfs_path);

		const char* compile_vs[] =
		{
			"shaderc",
			"-f", vs_code_path.c_str(),
			"-o", tmpvs_path.c_str(),
			"--varyingdef", varying_def_path.c_str(),
			"--type", "vertex",
			"--platform", opts.platform(),
#if CROWN_PLATFORM_WINDOWS
			"--profile", "vs_3_0",
#endif
			NULL
		};
		os::execute_process(compile_vs);

		const char* compile_fs[] =
		{
			"shaderc",
			"-f", fs_code_path.c_str(),
			"-o", tmpfs_path.c_str(),
			"--varyingdef", varying_def_path.c_str(),
			"--type", "fragment",
			"--platform", opts.platform(),
#if CROWN_PLATFORM_WINDOWS
			"--profile", "ps_3_0",
#endif
			NULL
		};
		os::execute_process(compile_fs);

		Buffer tmpvs = opts.read(tmpvs_path.c_str());
		Buffer tmpfs = opts.read(tmpfs_path.c_str());

		opts.write(uint32_t(1)); // version
		opts.write(uint32_t(array::size(tmpvs)));
		opts.write(array::begin(tmpvs), array::size(tmpvs));
		opts.write(uint32_t(array::size(tmpfs)));
		opts.write(array::begin(tmpfs), array::size(tmpfs));

		opts.delete_file(tmpvs_path.c_str());
		opts.delete_file(tmpfs_path.c_str());
	}