World.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "HeapAllocator.h"
  25. #include "IdTable.h"
  26. #include "LinearAllocator.h"
  27. #include "Unit.h"
  28. #include "Camera.h"
  29. #include "Vector.h"
  30. #include "LinearAllocator.h"
  31. #include "RenderWorld.h"
  32. #include "SoundRenderer.h"
  33. namespace crown
  34. {
  35. #define MAX_UNITS 65000
  36. #define MAX_SOUNDS 64
  37. #define MAX_CAMERAS 16
  38. typedef Id UnitId;
  39. typedef Id CameraId;
  40. typedef Id MeshId;
  41. typedef Id SoundInstanceId;
  42. struct SoundInstance
  43. {
  44. SoundId m_sound;
  45. };
  46. class Mesh;
  47. class Vector3;
  48. class Quaternion;
  49. class World
  50. {
  51. public:
  52. World();
  53. void init();
  54. void shutdown();
  55. UnitId spawn_unit(const char* name, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion(Vector3(0, 1, 0), 0.0f));
  56. void kill_unit(UnitId unit);
  57. void link_unit(UnitId child, UnitId parent);
  58. void unlink_unit(UnitId child, UnitId parent);
  59. Unit* lookup_unit(UnitId unit);
  60. Camera* lookup_camera(CameraId camera);
  61. Mesh* lookup_mesh(MeshId mesh);
  62. RenderWorld& render_world();
  63. void update(Camera& camera, float dt);
  64. CameraId create_camera(UnitId unit, int32_t node, const Vector3& pos = Vector3::ZERO, const Quaternion& rot = Quaternion::IDENTITY);
  65. void destroy_camera(CameraId camera);
  66. SoundInstanceId play_sound(const char* name, const bool loop = false, const float volume = 1.0f, const Vector3& pos = Vector3::ZERO, const float range = 50.0f);
  67. void pause_sound(SoundInstanceId sound);
  68. void link_sound(SoundInstanceId sound, UnitId unit);
  69. void set_listener(const Vector3& pos, const Vector3& vel, const Vector3& or_up, const Vector3& or_at);
  70. void set_sound_position(SoundInstanceId sound, const Vector3& pos);
  71. void set_sound_range(SoundInstanceId sound, const float range);
  72. void set_sound_volume(SoundInstanceId sound, const float vol);
  73. private:
  74. LinearAllocator m_allocator;
  75. bool m_is_init : 1;
  76. SceneGraph m_scene_graph[MAX_UNITS];
  77. ComponentList m_component[MAX_UNITS];
  78. IdTable<MAX_UNITS> m_unit_table;
  79. List<Unit> m_units;
  80. IdTable<MAX_CAMERAS> m_camera_table;
  81. List<Camera> m_camera;
  82. IdTable<MAX_SOUNDS> m_sound_table;
  83. SoundInstance m_sound[MAX_SOUNDS];
  84. RenderWorld m_render_world;
  85. };
  86. } // namespace crown