World.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 "Types.h"
  25. #include "HeapAllocator.h"
  26. #include "IdArray.h"
  27. #include "LinearAllocator.h"
  28. #include "Unit.h"
  29. #include "Camera.h"
  30. #include "Vector.h"
  31. #include "RenderWorld.h"
  32. #include "SoundRenderer.h"
  33. #include "PoolAllocator.h"
  34. #include "SceneGraphManager.h"
  35. namespace crown
  36. {
  37. #define MAX_UNITS 65000
  38. #define MAX_SOUNDS 64
  39. #define MAX_CAMERAS 16
  40. typedef Id UnitId;
  41. typedef Id CameraId;
  42. typedef Id MeshId;
  43. typedef Id SoundId;
  44. typedef Id SpriteId;
  45. struct Sound
  46. {
  47. SoundBufferId buffer;
  48. SoundSourceId source;
  49. Matrix4x4 world;
  50. float volume;
  51. float range;
  52. bool loop : 1;
  53. bool playing : 1;
  54. };
  55. struct UnitToSound
  56. {
  57. UnitId unit;
  58. SoundId sound;
  59. int32_t node;
  60. };
  61. class Mesh;
  62. class Sprite;
  63. class Vector3;
  64. class Quaternion;
  65. class World
  66. {
  67. public:
  68. World();
  69. UnitId spawn_unit(const char* name, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion(Vector3(0, 1, 0), 0.0f));
  70. void destroy_unit(UnitId id);
  71. uint32_t num_units() const;
  72. void link_unit(UnitId child, UnitId parent, int32_t node);
  73. void unlink_unit(UnitId unit);
  74. Unit* lookup_unit(UnitId unit);
  75. Camera* lookup_camera(CameraId camera);
  76. Mesh* lookup_mesh(MeshId mesh);
  77. Sprite* lookup_sprite(SpriteId sprite);
  78. RenderWorld& render_world();
  79. void update(float dt);
  80. void render(Camera* camera);
  81. CameraId create_camera(SceneGraph& sg, int32_t node);
  82. void destroy_camera(CameraId id);
  83. MeshId create_mesh(ResourceId id, SceneGraph& sg, int32_t node);
  84. void destroy_mesh(MeshId id);
  85. SpriteId create_sprite(ResourceId id, SceneGraph& sg, int32_t node);
  86. void destroy_sprite(SpriteId id);
  87. SoundId play_sound(const char* name, const bool loop = false, const float volume = 1.0f, const Vector3& pos = Vector3::ZERO, const float range = 50.0f);
  88. void stop_sound(SoundId sound);
  89. void link_sound(SoundId sound, Unit* unit, int32_t node);
  90. void set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at);
  91. void set_sound_position(SoundId sound, const Vector3& pos);
  92. void set_sound_range(SoundId sound, const float range);
  93. void set_sound_volume(SoundId sound, const float vol);
  94. private:
  95. PoolAllocator m_unit_pool;
  96. PoolAllocator m_camera_pool;
  97. IdArray<MAX_UNITS, Unit*> m_units;
  98. IdArray<MAX_CAMERAS, Camera*> m_cameras;
  99. IdArray<MAX_SOUNDS, Sound> m_sounds;
  100. SceneGraphManager m_graph_manager;
  101. // Connections
  102. List<UnitToSound> m_unit_to_sound;
  103. RenderWorld m_render_world;
  104. };
  105. } // namespace crown