| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- /*
- * Copyright (c) 2012-2017 Daniele Bartolini and individual contributors.
- * License: https://github.com/taylor001/crown/blob/master/LICENSE
- */
- #include "array.h"
- #include "compile_options.h"
- #include "dynamic_string.h"
- #include "file.h"
- #include "filesystem.h"
- #include "json_object.h"
- #include "level_resource.h"
- #include "map.h"
- #include "memory.h"
- #include "sjson.h"
- #include "temp_allocator.h"
- #include "unit_compiler.h"
- namespace crown
- {
- namespace level_resource_internal
- {
- void compile(const char* path, CompileOptions& opts)
- {
- Buffer buf = opts.read(path);
- TempAllocator4096 ta;
- JsonObject object(ta);
- sjson::parse(buf, object);
- Array<LevelSound> sounds(default_allocator());
- {
- JsonObject sounds_json(ta);
- sjson::parse_object(object["sounds"], sounds_json);
- auto cur = json_object::begin(sounds_json);
- auto end = json_object::end(sounds_json);
- for (; cur != end; ++cur)
- {
- JsonObject sound(ta);
- sjson::parse_object(cur->pair.second, sound);
- DynamicString sound_name(ta);
- sjson::parse_string(sound["name"], sound_name);
- DATA_COMPILER_ASSERT_RESOURCE_EXISTS("sound"
- , sound_name.c_str()
- , opts
- );
- LevelSound ls;
- ls.name = sjson::parse_resource_id(sound["name"]);
- ls.position = sjson::parse_vector3 (sound["position"]);
- ls.volume = sjson::parse_float (sound["volume"]);
- ls.range = sjson::parse_float (sound["range"]);
- ls.loop = sjson::parse_bool (sound["loop"]);
- array::push_back(sounds, ls);
- }
- }
- UnitCompiler uc(opts);
- uc.compile_multiple_units(object["units"]);
- Buffer unit_blob = uc.blob();
- // Write
- LevelResource lr;
- lr.version = RESOURCE_VERSION_LEVEL;
- lr.num_sounds = array::size(sounds);
- lr.units_offset = sizeof(LevelResource);
- lr.sounds_offset = lr.units_offset + array::size(unit_blob);
- opts.write(lr.version);
- opts.write(lr.units_offset);
- opts.write(lr.num_sounds);
- opts.write(lr.sounds_offset);
- opts.write(unit_blob);
- for (u32 i = 0; i < array::size(sounds); ++i)
- {
- opts.write(sounds[i].name);
- opts.write(sounds[i].position);
- opts.write(sounds[i].volume);
- opts.write(sounds[i].range);
- opts.write(sounds[i].loop);
- opts.write(sounds[i]._pad[0]);
- opts.write(sounds[i]._pad[1]);
- opts.write(sounds[i]._pad[2]);
- }
- }
- } // namespace level_resource_internal
- namespace level_resource
- {
- const UnitResource* unit_resource(const LevelResource* lr)
- {
- return (const UnitResource*)((char*)lr + lr->units_offset);
- }
- u32 num_sounds(const LevelResource* lr)
- {
- return lr->num_sounds;
- }
- const LevelSound* get_sound(const LevelResource* lr, u32 i)
- {
- CE_ASSERT(i < num_sounds(lr), "Index out of bounds");
- const LevelSound* begin = (LevelSound*)((char*)lr + lr->sounds_offset);
- return &begin[i];
- }
- } // namespace level_resource
- } // namespace crown
|