SoundComponent.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef EXAMPLE_COMPONENT_H
  23. #define EXAMPLE_COMPONENT_H
  24. #pragma once
  25. #ifndef COMPONENT_H
  26. #include "T3D/components/component.h"
  27. #endif
  28. #ifndef RENDER_COMPONENT_INTERFACE_H
  29. #include "T3D/components/render/renderComponentInterface.h"
  30. #endif
  31. class SFXSource;
  32. //SoundComponent
  33. //A basic example of the various functions you can utilize to make your own component!
  34. //This example doesn't really DO anything, persay, but you can readily copy it as a base
  35. //and use it as a starting point for your own.
  36. class SoundComponent : public Component, public RenderComponentInterface, public EditorInspectInterface
  37. {
  38. typedef Component Parent;
  39. public:
  40. enum PublicConstants
  41. {
  42. MaxSoundThreads = 4, ///< Should be a power of 2
  43. };
  44. /// @name Network state masks
  45. /// @{
  46. ///
  47. enum SoundComponentMasks
  48. {
  49. SoundMaskN = Parent::NextFreeMask << 6, ///< Extends + MaxSoundThreads bits
  50. };
  51. enum BaseMaskConstants
  52. {
  53. SoundMask = (SoundMaskN << MaxSoundThreads) - SoundMaskN,
  54. };
  55. /// @name Scripted Sound
  56. /// @{
  57. struct Sound {
  58. bool play; ///< Are we playing this sound?
  59. SimTime timeout; ///< Time until we stop playing this sound.
  60. SFXTrack* profile; ///< Profile on server
  61. SFXSource* sound; ///< Sound on client
  62. Sound()
  63. {
  64. play = false;
  65. timeout = 0;
  66. profile = NULL;
  67. sound = NULL;
  68. }
  69. };
  70. Sound mSoundThread[MaxSoundThreads];
  71. SFXTrack* mSoundFile[MaxSoundThreads];
  72. bool mPreviewSound[MaxSoundThreads];
  73. bool mPlay[MaxSoundThreads];
  74. /// @}
  75. SoundComponent();
  76. virtual ~SoundComponent();
  77. DECLARE_CONOBJECT(SoundComponent);
  78. virtual bool onAdd();
  79. virtual void onRemove();
  80. static void initPersistFields();
  81. static bool _previewSound(void *object, const char *index, const char *data);
  82. static bool _autoplay(void *object, const char *index, const char *data);
  83. virtual U32 packUpdate(NetConnection *con, U32 mask, BitStream *stream);
  84. virtual void unpackUpdate(NetConnection *con, BitStream *stream);
  85. virtual void onComponentRemove();
  86. virtual void onComponentAdd();
  87. virtual void componentAddedToOwner(Component *comp);
  88. virtual void componentRemovedFromOwner(Component *comp);
  89. virtual void onInspect();
  90. virtual void onEndInspect();
  91. virtual void processTick();
  92. virtual void advanceTime(F32 dt);
  93. virtual void interpolateTick(F32 delta);
  94. void prepRenderImage(SceneRenderState* state);
  95. void _renderObject(ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat);
  96. virtual void playAudio(U32 slotNum, SFXTrack* profile = NULL);
  97. virtual void stopAudio(U32 slot);
  98. virtual void updateServerAudio();
  99. virtual void updateAudioState(Sound& st);
  100. virtual void updateAudioPos();
  101. //why god why
  102. virtual TSShape* getShape() { return NULL; };
  103. Signal< void(RenderComponentInterface*) > onShapeChanged;
  104. virtual TSShapeInstance* getShapeInstance() { return NULL; };
  105. Signal< void(RenderComponentInterface*) > onShapeInstanceChanged;
  106. virtual MatrixF getNodeTransform(S32 nodeIdx) { return MatrixF::Identity; };
  107. virtual Vector<MatrixF> getNodeTransforms() { return NULL; };
  108. virtual void setNodeTransforms(Vector<MatrixF> transforms) {};
  109. };
  110. #endif