System.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #pragma once
  23. #include "Vec3.h"
  24. #include "Mat3.h"
  25. #include "Mat4.h"
  26. #include "Sprite.h"
  27. #include "Device.h"
  28. #include "Renderer.h"
  29. #include "VertexBuffer.h"
  30. #include "IndexBuffer.h"
  31. #include "Data.h"
  32. #include "Random.h"
  33. #include <ctime>
  34. namespace crown
  35. {
  36. struct EntityId
  37. {
  38. uint32_t innerId;
  39. uint32_t index;
  40. };
  41. struct Entity
  42. {
  43. Entity() : mSprite(NULL), mLocalTransform(Mat4::IDENTITY)
  44. {
  45. static Random random(time(0));
  46. mVelocity = Vec3(random.GetUnitFloat(), random.GetUnitFloat(), random.GetUnitFloat());
  47. }
  48. EntityId mId;
  49. Vec3 mVelocity;
  50. Mat4 mLocalTransform;
  51. Sprite* mSprite;
  52. };
  53. #define MAX_ENTITIES 64 * 1024
  54. class System
  55. {
  56. public:
  57. System();
  58. ~System();
  59. EntityId CreateEntity();
  60. void DestroyEntity(EntityId id);
  61. Entity& GetEntity(EntityId id);
  62. void Update(uint32_t dt);
  63. private:
  64. uint32_t mNextId; // Next available Id
  65. uint32_t mLastIndex; // Count of entities
  66. uint32_t mFreelist; // Start index of the freelist
  67. Entity mEntities[MAX_ENTITIES]; // Entities
  68. VertexData mVertices[4];
  69. FaceData mFaces[2];
  70. VertexBuffer* mVertexBuffer;
  71. IndexBuffer* mIndexBuffer;
  72. };
  73. //-----------------------------------------------------------------------------
  74. inline System::System() : mNextId(0), mLastIndex(0), mFreelist(MAX_ENTITIES)
  75. {
  76. mVertices[0] = VertexData(Vec3(-1.0f, -1.0f, 0.0f), Vec3(0, 0, 1.0f), Vec2(0.0f, 0.0f), Color4::RED);
  77. mVertices[1] = VertexData(Vec3(+1.0f, -1.0f, 0.0f), Vec3(0, 0, 1.0f), Vec2(1.0f, 0.0f), Color4::RED);
  78. mVertices[2] = VertexData(Vec3(+1.0f, +1.0f, 0.0f), Vec3(0, 0, 1.0f), Vec2(1.0f, 1.0f), Color4::RED);
  79. mVertices[3] = VertexData(Vec3(-1.0f, +1.0f, 0.0f), Vec3(0, 0, 1.0f), Vec2(0.0f, 1.0f), Color4::RED);
  80. mFaces[0] = FaceData(0, 1, 2);
  81. mFaces[1] = FaceData(0, 2, 3);
  82. mVertexBuffer = GetDevice()->GetRenderer()->CreateVertexBuffer();
  83. mIndexBuffer = GetDevice()->GetRenderer()->CreateIndexBuffer();
  84. mVertexBuffer->SetVertexData((VertexBufferMode) (VBM_TEXTURE_COORDS | VBM_NORMAL_COORDS | VBM_COLOR_COORDS), (float*) &mVertices[0], 4);
  85. mIndexBuffer->SetIndexData(&mFaces[0].vertex[0], 6);
  86. }
  87. //-----------------------------------------------------------------------------
  88. inline System::~System()
  89. {
  90. }
  91. //-----------------------------------------------------------------------------
  92. inline EntityId System::CreateEntity()
  93. {
  94. EntityId id;
  95. id.innerId = mNextId++;
  96. if (mFreelist == MAX_ENTITIES)
  97. {
  98. id.index = mLastIndex++;
  99. mEntities[id.index].mId = id;
  100. }
  101. else
  102. {
  103. id.index = mFreelist;
  104. mFreelist = mEntities[mFreelist].mId.index;
  105. }
  106. return id;
  107. }
  108. //-----------------------------------------------------------------------------
  109. inline void System::DestroyEntity(EntityId id)
  110. {
  111. mEntities[id.index].mId.innerId = MAX_ENTITIES;
  112. mEntities[id.index].mId.index = mFreelist;
  113. mFreelist = id.index;
  114. }
  115. //-----------------------------------------------------------------------------
  116. inline Entity& System::GetEntity(EntityId id)
  117. {
  118. return mEntities[id.index];
  119. }
  120. void System::Update(uint32_t dt)
  121. {
  122. for (uint32_t i = 0; i < mLastIndex; i++)
  123. {
  124. if (mEntities[i].mId.innerId == MAX_ENTITIES)
  125. {
  126. //continue;
  127. }
  128. Renderer* renderer = GetDevice()->GetRenderer();
  129. mEntities[i].mLocalTransform.SetTranslation(mEntities[i].mLocalTransform.GetTranslation() + mEntities[i].mVelocity * (float)dt/1000.0f);
  130. renderer->SetMatrix(MT_MODEL, mEntities[i].mLocalTransform);
  131. renderer->RenderVertexIndexBuffer(mVertexBuffer, mIndexBuffer);
  132. }
  133. }
  134. } // namespace crown