| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*
- 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 "Allocator.h"
- #include "Filesystem.h"
- #include "StringUtils.h"
- #include "PhysicsCompiler.h"
- #include "Hash.h"
- #include "JSONParser.h"
- namespace crown
- {
- //-----------------------------------------------------------------------------
- PhysicsCompiler::PhysicsCompiler()
- {
- }
- //-----------------------------------------------------------------------------
- PhysicsCompiler::~PhysicsCompiler()
- {
- }
- //-----------------------------------------------------------------------------
- size_t PhysicsCompiler::compile_impl(Filesystem& fs, const char* resource_path)
- {
- File* file = fs.open(resource_path, FOM_READ);
- char* buf = (char*)default_allocator().allocate(file->size());
- file->read(buf, file->size());
- JSONParser json(buf);
- JSONElement root = json.root();
- // Read controller
- JSONElement controller = root.key_or_nil("controller");
- if (controller.is_nil())
- {
- m_has_controller = false;
- }
- else
- {
- parse_controller(controller);
- m_has_controller = true;
- }
- fs.close(file);
- default_allocator().deallocate(buf);
- return 1;
- }
- //-----------------------------------------------------------------------------
- void PhysicsCompiler::parse_controller(JSONElement controller)
- {
- JSONElement name = controller.key("name");
- JSONElement height = controller.key("height");
- JSONElement radius = controller.key("radius");
- JSONElement slope_limit = controller.key("slope_limit");
- JSONElement step_offset = controller.key("step_offset");
- JSONElement contact_offset = controller.key("contact_offset");
- m_controller.name = hash::murmur2_32(name.string_value(), name.size(), 0);
- m_controller.height = height.float_value();
- m_controller.radius = radius.float_value();
- m_controller.slope_limit = slope_limit.float_value();
- m_controller.step_offset = step_offset.float_value();
- m_controller.contact_offset = contact_offset.float_value();
- }
- //-----------------------------------------------------------------------------
- void PhysicsCompiler::write_impl(File* out_file)
- {
- PhysicsHeader h;
- h.version = 1;
- h.num_controllers = m_has_controller ? 1 : 0;
- uint32_t offt = sizeof(PhysicsHeader);
- h.controller_offset = offt;
- out_file->write((char*) &h, sizeof(PhysicsHeader));
- if (m_has_controller)
- {
- out_file->write((char*) &m_controller, sizeof(PhysicsController));
- }
- }
- } // namespace crown
|