| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278 |
- /*
- 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 "types.h"
- #include "resource.h"
- #include "bundle.h"
- #include "allocator.h"
- #include "file.h"
- #include "physics_types.h"
- #include "matrix4x4.h"
- #include "camera.h"
- namespace crown
- {
- // All offsets are absolute
- struct UnitHeader
- {
- ResourceId physics_resource;
- StringId64 sprite_animation;
- uint32_t num_renderables;
- uint32_t renderables_offset;
- uint32_t num_materials;
- uint32_t materials_offset;
- uint32_t num_cameras;
- uint32_t cameras_offset;
- uint32_t num_scene_graph_nodes;
- uint32_t scene_graph_nodes_offset;
- uint32_t num_keys;
- uint32_t keys_offset;
- uint32_t values_size;
- uint32_t values_offset;
- };
- struct UnitRenderable
- {
- enum { MESH, SPRITE } type;
- ResourceId resource;
- StringId32 name;
- int32_t node;
- bool visible;
- };
- struct UnitMaterial
- {
- StringId64 id;
- };
- struct UnitCamera
- {
- uint32_t name;
- int32_t node;
- ProjectionType::Enum type;
- float fov;
- float near;
- float far;
- };
- struct UnitNode
- {
- StringId32 name;
- Matrix4x4 pose;
- int32_t parent;
- };
- struct ValueType
- {
- enum Enum
- {
- BOOL,
- FLOAT,
- STRING,
- VECTOR3
- };
- };
- struct Key
- {
- StringId32 name;
- uint32_t type;
- uint32_t offset;
- };
- struct UnitResource
- {
- //-----------------------------------------------------------------------------
- static 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;
- }
- //-----------------------------------------------------------------------------
- static void online(StringId64 /*id*/, ResourceManager& /*rm*/)
- {
- }
- static void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
- {
- }
- //-----------------------------------------------------------------------------
- static void unload(Allocator& allocator, void* resource)
- {
- allocator.deallocate(resource);
- }
- ResourceId sprite_animation() const
- {
- ResourceId id;
- id.type = SPRITE_ANIMATION_TYPE;
- id.name = ((UnitHeader*) this)->sprite_animation;
- return id;
- }
- //-----------------------------------------------------------------------------
- ResourceId physics_resource() const
- {
- return ((UnitHeader*) this)->physics_resource;
- }
- //-----------------------------------------------------------------------------
- uint32_t num_renderables() const
- {
- return ((UnitHeader*) this)->num_renderables;
- }
- //-----------------------------------------------------------------------------
- UnitRenderable get_renderable(uint32_t i) const
- {
- CE_ASSERT(i < num_renderables(), "Index out of bounds");
- UnitHeader* h = (UnitHeader*) this;
- UnitRenderable* begin = (UnitRenderable*) (((char*) this) + h->renderables_offset);
- return begin[i];
- }
- //-----------------------------------------------------------------------------
- uint32_t num_materials() const
- {
- return ((UnitHeader*) this)->num_materials;
- }
- //-----------------------------------------------------------------------------
- UnitMaterial get_material(uint32_t i) const
- {
- CE_ASSERT(i < num_materials(), "Index out of bounds");
- UnitHeader* h = (UnitHeader*) this;
- UnitMaterial* begin = (UnitMaterial*) (((char*) this) + h->materials_offset);
- return begin[i];
- }
- //-----------------------------------------------------------------------------
- uint32_t num_cameras() const
- {
- return ((UnitHeader*) this)->num_cameras;
- }
- //-----------------------------------------------------------------------------
- UnitCamera get_camera(uint32_t i) const
- {
- CE_ASSERT(i < num_cameras(), "Index out of bounds");
- UnitHeader* h = (UnitHeader*) this;
- UnitCamera* begin = (UnitCamera*) (((char*) this) + h->cameras_offset);
- return begin[i];
- }
- //-----------------------------------------------------------------------------
- uint32_t num_scene_graph_nodes() const
- {
- return ((UnitHeader*) this)->num_scene_graph_nodes;
- }
- //-----------------------------------------------------------------------------
- UnitNode* scene_graph_nodes() const
- {
- UnitHeader* h = (UnitHeader*) this;
- return (UnitNode*) (((char*) this) + h->scene_graph_nodes_offset);
- }
- //-----------------------------------------------------------------------------
- uint32_t num_keys() const
- {
- return ((UnitHeader*) this)->num_keys;
- }
- //-----------------------------------------------------------------------------
- bool has_key(const char* k) const
- {
- UnitHeader* h = (UnitHeader*) this;
- const uint32_t nk = num_keys();
- Key* begin = (Key*) (((char*) this) + h->keys_offset);
- for (uint32_t i = 0; i < nk; i++)
- {
- if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
- {
- return true;
- }
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- bool get_key(const char* k, Key& out_k) const
- {
- UnitHeader* h = (UnitHeader*) this;
- const uint32_t nk = num_keys();
- Key* begin = (Key*) (((char*) this) + h->keys_offset);
- for (uint32_t i = 0; i < nk; i++)
- {
- if (begin[i].name == string::murmur2_32(k, string::strlen(k)))
- {
- out_k = begin[i];
- return true;
- }
- }
- return false;
- }
- //-----------------------------------------------------------------------------
- uint32_t values_size() const
- {
- return ((UnitHeader*) this)->values_size;
- }
- //-----------------------------------------------------------------------------
- const char* values() const
- {
- UnitHeader* h = (UnitHeader*) this;
- return ((char*) this) + h->values_offset;
- }
- private:
- // Disable construction
- UnitResource();
- };
- } // namespace crown
|