|
|
@@ -1,491 +0,0 @@
|
|
|
-/*
|
|
|
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
|
|
|
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
|
|
|
-
|
|
|
-Permission is hereby granted, free of charge, to any person
|
|
|
-obtaining a copy of this software and associated documentation
|
|
|
-files (the "Software"), to deal in the Software without
|
|
|
-restriction, including without limitation the rights to use,
|
|
|
-copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
-copies of the Software, and to permit persons to whom the
|
|
|
-Software is furnished to do so, subject to the following
|
|
|
-conditions:
|
|
|
-
|
|
|
-The above copyright notice and this permission notice shall be
|
|
|
-included in all copies or substantial portions of the Software.
|
|
|
-
|
|
|
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
-OTHER DEALINGS IN THE SOFTWARE.
|
|
|
-*/
|
|
|
-
|
|
|
-#include "level_resource.h"
|
|
|
-#include "array.h"
|
|
|
-#include "memory.h"
|
|
|
-#include "json_parser.h"
|
|
|
-#include "filesystem.h"
|
|
|
-#include "reader_writer.h"
|
|
|
-
|
|
|
-namespace crown
|
|
|
-{
|
|
|
-namespace level_resource
|
|
|
-{
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void parse_units(JSONElement root, Array<LevelUnit>& units)
|
|
|
- {
|
|
|
- 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");
|
|
|
- lu.position = e.key("position").to_vector3();
|
|
|
- lu.rotation = e.key("rotation").to_quaternion();
|
|
|
-
|
|
|
- array::push_back(units, lu);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void parse_sounds(JSONElement root, Array<LevelSound>& sounds)
|
|
|
- {
|
|
|
- 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");
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void compile(Filesystem& fs, const char* resource_path, File* out_file)
|
|
|
- {
|
|
|
-/* File* file = fs.open(resource_path, FOM_READ);
|
|
|
- JSONParser json(*file);
|
|
|
- fs.close(file);
|
|
|
-
|
|
|
- JSONElement root = json.root();
|
|
|
-
|
|
|
- Array<LevelUnit> units(default_allocator());
|
|
|
- Array<LevelSound> sounds(default_allocator());
|
|
|
-
|
|
|
- parse_units(root, units);
|
|
|
- parse_sounds(root, sounds);
|
|
|
-
|
|
|
- LevelHeader lh;
|
|
|
- lh.num_units = array::size(units);
|
|
|
- lh.num_sounds = array::size(sounds);
|
|
|
-
|
|
|
- uint32_t offt = sizeof(LevelHeader);
|
|
|
- lh.units_offset = offt; offt += sizeof(LevelUnit) * lh.num_units;
|
|
|
- lh.sounds_offset = offt;
|
|
|
-
|
|
|
- out_file->write((char*) &lh, sizeof(LevelHeader));
|
|
|
-
|
|
|
- if (lh.num_units)
|
|
|
- {
|
|
|
- out_file->write((char*) array::begin(units), sizeof(LevelUnit) * lh.num_units);
|
|
|
- }
|
|
|
- if (lh.num_sounds)
|
|
|
- {
|
|
|
- out_file->write((char*) array::begin(sounds), sizeof(LevelSound) * lh.num_sounds);
|
|
|
- }*/
|
|
|
- }
|
|
|
-
|
|
|
- void compile(const char* path, CompileOptions& opts)
|
|
|
- {
|
|
|
- Buffer buf = opts.read(path);
|
|
|
- JSONParser json(array::begin(buf));
|
|
|
- JSONElement root = json.root();
|
|
|
-
|
|
|
- Array<LevelUnit> units(default_allocator());
|
|
|
- Array<LevelSound> sounds(default_allocator());
|
|
|
- parse_units(root, units);
|
|
|
- parse_sounds(root, sounds);
|
|
|
-
|
|
|
- LevelHeader lh;
|
|
|
- lh.num_units = array::size(units);
|
|
|
- lh.num_sounds = array::size(sounds);
|
|
|
-
|
|
|
- uint32_t offt = sizeof(LevelHeader);
|
|
|
- lh.units_offset = offt; offt += sizeof(LevelUnit) * lh.num_units;
|
|
|
- lh.sounds_offset = offt;
|
|
|
-
|
|
|
- // Version 1
|
|
|
- // opts.write(lh);
|
|
|
- // if (lh.num_units)
|
|
|
- // opts.write(array::begin(units), sizeof(LevelUnit) * lh.num_units);
|
|
|
- // if (lh.num_sounds)
|
|
|
- // opts.write(array::begin(sounds), sizeof(LevelSound) * lh.num_sounds);
|
|
|
-
|
|
|
- // Version 2
|
|
|
- // opts.write(lh.num_units);
|
|
|
- // opts.write(lh.units_offset);
|
|
|
- // opts.write(lh.num_sounds);
|
|
|
- // opts.write(lh.sounds_offset);
|
|
|
-
|
|
|
- // if (lh.num_units) {
|
|
|
- // for (uint32_t i = 0; i < array::size(units); i++)
|
|
|
- // {
|
|
|
- // LevelUnit lu = units[i];
|
|
|
- // opts.write(lu.name);
|
|
|
- // opts.write(lu.position);
|
|
|
- // opts.write(lu.rotation);
|
|
|
- // opts.write(uint32_t(0xABABABAB));
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- // if (lh.num_sounds) {
|
|
|
- // for (uint32_t i = 0; i < array::size(sounds); i++)
|
|
|
- // {
|
|
|
- // LevelSound ls = sounds[i];
|
|
|
- // opts.write(ls.name);
|
|
|
- // opts.write(ls.position);
|
|
|
- // opts.write(ls.volume);
|
|
|
- // opts.write(ls.range);
|
|
|
- // opts.write(ls.loop);
|
|
|
- // opts.write(uint8_t(0xff));
|
|
|
- // opts.write(uint8_t(0xff));
|
|
|
- // opts.write(uint8_t(0xff));
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- // Version 3
|
|
|
- BinaryWriter& bw = opts._bw;
|
|
|
- bw & lh;
|
|
|
- bw & units;
|
|
|
- bw & sounds;
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
|
|
|
- {
|
|
|
- File* file = bundle.open(id);
|
|
|
- const size_t file_size = file->size();
|
|
|
-
|
|
|
- void* res = allocator.allocate(file_size);
|
|
|
- file->read(res, file_size);
|
|
|
-
|
|
|
- bundle.close(file);
|
|
|
-
|
|
|
- return res;
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void online(StringId64 /*id*/, ResourceManager& /*rm*/)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
|
|
|
- {
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- void unload(Allocator& allocator, void* resource)
|
|
|
- {
|
|
|
- allocator.deallocate(resource);
|
|
|
- }
|
|
|
-} // namespace level_resource
|
|
|
-} // namespace crown
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-/*
|
|
|
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
|
|
|
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
|
|
|
-
|
|
|
-Permission is hereby granted, free of charge, to any person
|
|
|
-obtaining a copy of this software and associated documentation
|
|
|
-files (the "Software"), to deal in the Software without
|
|
|
-restriction, including without limitation the rights to use,
|
|
|
-copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
-copies of the Software, and to permit persons to whom the
|
|
|
-Software is furnished to do so, subject to the following
|
|
|
-conditions:
|
|
|
-
|
|
|
-The above copyright notice and this permission notice shall be
|
|
|
-included in all copies or substantial portions of the Software.
|
|
|
-
|
|
|
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
|
|
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
|
|
-OTHER DEALINGS IN THE SOFTWARE.
|
|
|
-*/
|
|
|
-
|
|
|
-#pragma once
|
|
|
-
|
|
|
-#include "allocator.h"
|
|
|
-#include "assert.h"
|
|
|
-#include "bundle.h"
|
|
|
-#include "file.h"
|
|
|
-#include "resource.h"
|
|
|
-#include "types.h"
|
|
|
-#include "vector3.h"
|
|
|
-#include "quaternion.h"
|
|
|
-
|
|
|
-namespace crown
|
|
|
-{
|
|
|
-
|
|
|
-template <typename STREAM, typename T> inline STREAM& operator&(STREAM& stream, T& t)
|
|
|
-{
|
|
|
- return t.serialize(stream);
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, Vector3>(BinaryWriter& bw, Vector3& v)
|
|
|
-{
|
|
|
- bw.write(v.x);
|
|
|
- bw.write(v.y);
|
|
|
- bw.write(v.z);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, Vector3> (BinaryReader& br, Vector3& v)
|
|
|
-{
|
|
|
- br.read(v.x);
|
|
|
- br.read(v.y);
|
|
|
- br.read(v.z);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, Quaternion>(BinaryWriter& bw, Quaternion& q)
|
|
|
-{
|
|
|
- bw.write(q.x);
|
|
|
- bw.write(q.y);
|
|
|
- bw.write(q.z);
|
|
|
- bw.write(q.w);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, Quaternion> (BinaryReader& br, Quaternion& q)
|
|
|
-{
|
|
|
- br.read(q.x);
|
|
|
- br.read(q.y);
|
|
|
- br.read(q.z);
|
|
|
- br.read(q.w);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, bool>(BinaryWriter& bw, bool& v)
|
|
|
-{
|
|
|
- bw.write(v);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, bool> (BinaryReader& br, bool& v)
|
|
|
-{
|
|
|
- br.read(v);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, float>(BinaryWriter& bw, float& v)
|
|
|
-{
|
|
|
- bw.write(v);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, float> (BinaryReader& br, float& v)
|
|
|
-{
|
|
|
- br.read(v);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, uint8_t>(BinaryWriter& bw, uint8_t& v)
|
|
|
-{
|
|
|
- bw.write(v);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, uint8_t> (BinaryReader& br, uint8_t& v)
|
|
|
-{
|
|
|
- br.read(v);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, uint32_t>(BinaryWriter& bw, uint32_t& v)
|
|
|
-{
|
|
|
- bw.write(v);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, uint32_t> (BinaryReader& br, uint32_t& v)
|
|
|
-{
|
|
|
- br.read(v);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, ResourceId>(BinaryWriter& bw, ResourceId& id)
|
|
|
-{
|
|
|
- bw.write(id.type);
|
|
|
- bw.write(id.name);
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, ResourceId> (BinaryReader& br, ResourceId& id)
|
|
|
-{
|
|
|
- br.read(id.type);
|
|
|
- br.read(id.name);
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-struct LevelHeader
|
|
|
-{
|
|
|
- template <typename STREAM>
|
|
|
- inline STREAM& serialize(STREAM& stream)
|
|
|
- {
|
|
|
- return stream
|
|
|
- & num_units
|
|
|
- & units_offset
|
|
|
- & num_sounds
|
|
|
- & sounds_offset;
|
|
|
- }
|
|
|
-
|
|
|
- uint32_t num_units;
|
|
|
- uint32_t units_offset;
|
|
|
- uint32_t num_sounds;
|
|
|
- uint32_t sounds_offset;
|
|
|
-};
|
|
|
-
|
|
|
-struct LevelUnit
|
|
|
-{
|
|
|
- template <typename STREAM>
|
|
|
- inline STREAM& serialize(STREAM& stream)
|
|
|
- {
|
|
|
- return stream
|
|
|
- & name
|
|
|
- & position
|
|
|
- & rotation
|
|
|
- & _pad0
|
|
|
- & _pad1
|
|
|
- & _pad2
|
|
|
- & _pad3;
|
|
|
- }
|
|
|
-
|
|
|
- ResourceId name;
|
|
|
- Vector3 position;
|
|
|
- Quaternion rotation;
|
|
|
- uint8_t _pad0;
|
|
|
- uint8_t _pad1;
|
|
|
- uint8_t _pad2;
|
|
|
- uint8_t _pad3;
|
|
|
-};
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, Array<LevelUnit> >(BinaryWriter& bw, Array<LevelUnit>& arr)
|
|
|
-{
|
|
|
- for (uint32_t i = 0; i < array::size(arr); i++)
|
|
|
- bw & arr[i];
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, Array<LevelUnit> > (BinaryReader& br, Array<LevelUnit>& arr)
|
|
|
-{
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-struct LevelSound
|
|
|
-{
|
|
|
- template <typename STREAM>
|
|
|
- inline STREAM& serialize(STREAM& stream)
|
|
|
- {
|
|
|
- return stream & name
|
|
|
- & position
|
|
|
- & volume
|
|
|
- & range
|
|
|
- & loop
|
|
|
- & _pad0
|
|
|
- & _pad1
|
|
|
- & _pad2;
|
|
|
- }
|
|
|
-
|
|
|
- ResourceId name;
|
|
|
- Vector3 position;
|
|
|
- float volume;
|
|
|
- float range;
|
|
|
- bool loop;
|
|
|
- uint8_t _pad0;
|
|
|
- uint8_t _pad1;
|
|
|
- uint8_t _pad2;
|
|
|
-};
|
|
|
-
|
|
|
-template <> inline BinaryWriter& operator&<BinaryWriter, Array<LevelSound> >(BinaryWriter& bw, Array<LevelSound>& arr)
|
|
|
-{
|
|
|
- for (uint32_t i = 0; i < array::size(arr); i++)
|
|
|
- bw & arr[i];
|
|
|
- return bw;
|
|
|
-}
|
|
|
-
|
|
|
-template <> inline BinaryReader& operator& <BinaryReader, Array<LevelSound> > (BinaryReader& br, Array<LevelSound>& arr)
|
|
|
-{
|
|
|
- return br;
|
|
|
-}
|
|
|
-
|
|
|
-struct LevelResource
|
|
|
-{
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- uint32_t num_units() const
|
|
|
- {
|
|
|
- return ((LevelHeader*) this)->num_units;
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- const LevelUnit* get_unit(uint32_t i) const
|
|
|
- {
|
|
|
- CE_ASSERT(i < num_units(), "Index out of bounds");
|
|
|
-
|
|
|
- const LevelHeader* h = (LevelHeader*) this;
|
|
|
- const LevelUnit* begin = (LevelUnit*) (((char*) this) + h->units_offset);
|
|
|
- return &begin[i];
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- uint32_t num_sounds() const
|
|
|
- {
|
|
|
- return ((LevelHeader*) this)->num_sounds;
|
|
|
- }
|
|
|
-
|
|
|
- //-----------------------------------------------------------------------------
|
|
|
- const LevelSound* get_sound(uint32_t i) const
|
|
|
- {
|
|
|
- CE_ASSERT(i < num_sounds(), "Index out of bounds");
|
|
|
-
|
|
|
- const LevelHeader* h = (LevelHeader*) this;
|
|
|
- const LevelSound* begin = (LevelSound*) (((char*) this) + h->sounds_offset);
|
|
|
- return &begin[i];
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-namespace level_resource
|
|
|
-{
|
|
|
- void compile(const char* path, CompileOptions& opts);
|
|
|
- void* load(Allocator& allocator, Bundle& bundle, ResourceId id);
|
|
|
- void online(StringId64 /*id*/, ResourceManager& /*rm*/);
|
|
|
- void offline(StringId64 /*id*/, ResourceManager& /*rm*/);
|
|
|
- void unload(Allocator& allocator, void* resource);
|
|
|
-} // namespace level_resource
|
|
|
-} // namespace crown
|