2
0

level_resource.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "allocator.h"
  25. #include "assert.h"
  26. #include "bundle.h"
  27. #include "file.h"
  28. #include "resource.h"
  29. #include "types.h"
  30. #include "vector3.h"
  31. #include "quaternion.h"
  32. namespace crown
  33. {
  34. struct LevelHeader
  35. {
  36. uint32_t num_units;
  37. uint32_t units_offset;
  38. uint32_t num_sounds;
  39. uint32_t sounds_offset;
  40. };
  41. struct LevelUnit
  42. {
  43. ResourceId name;
  44. Vector3 position;
  45. Quaternion rotation;
  46. };
  47. struct LevelSound
  48. {
  49. ResourceId name;
  50. Vector3 position;
  51. float volume;
  52. float range;
  53. bool loop;
  54. };
  55. struct LevelResource
  56. {
  57. //-----------------------------------------------------------------------------
  58. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  59. {
  60. File* file = bundle.open(id);
  61. const size_t file_size = file->size();
  62. void* res = allocator.allocate(file_size);
  63. file->read(res, file_size);
  64. bundle.close(file);
  65. return res;
  66. }
  67. //-----------------------------------------------------------------------------
  68. static void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  69. {
  70. }
  71. static void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  72. {
  73. }
  74. //-----------------------------------------------------------------------------
  75. static void unload(Allocator& allocator, void* resource)
  76. {
  77. allocator.deallocate(resource);
  78. }
  79. //-----------------------------------------------------------------------------
  80. uint32_t num_units() const
  81. {
  82. return ((LevelHeader*) this)->num_units;
  83. }
  84. //-----------------------------------------------------------------------------
  85. const LevelUnit* get_unit(uint32_t i) const
  86. {
  87. CE_ASSERT(i < num_units(), "Index out of bounds");
  88. const LevelHeader* h = (LevelHeader*) this;
  89. const LevelUnit* begin = (LevelUnit*) (((char*) this) + h->units_offset);
  90. return &begin[i];
  91. }
  92. //-----------------------------------------------------------------------------
  93. uint32_t num_sounds() const
  94. {
  95. return ((LevelHeader*) this)->num_sounds;
  96. }
  97. //-----------------------------------------------------------------------------
  98. const LevelSound* get_sound(uint32_t i) const
  99. {
  100. CE_ASSERT(i < num_sounds(), "Index out of bounds");
  101. const LevelHeader* h = (LevelHeader*) this;
  102. const LevelSound* begin = (LevelSound*) (((char*) this) + h->sounds_offset);
  103. return &begin[i];
  104. }
  105. };
  106. } // namespace crown