Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

262 рядки
7.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. // © 2014—2015 Benjamin “Touky” Huet <huet.benjamin@gmail.com>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #pragma once
  14. //
  15. // The Scene class
  16. // ---------------
  17. //
  18. #include <stdint.h>
  19. #include "tileset.h"
  20. #include "light.h"
  21. #include "camera.h"
  22. #include "mesh/mesh.h"
  23. #define LOL_MAX_LIGHT_COUNT 8
  24. namespace lol
  25. {
  26. //-----------------------------------------------------------------------------
  27. class PrimitiveSource
  28. {
  29. friend class Scene;
  30. public:
  31. PrimitiveSource() { }
  32. virtual ~PrimitiveSource() { }
  33. virtual void Render(Scene& scene);
  34. private:
  35. };
  36. class PrimitiveRenderer
  37. {
  38. friend class Scene;
  39. public:
  40. PrimitiveRenderer() { }
  41. virtual ~PrimitiveRenderer() { }
  42. virtual void Render(Scene& scene, PrimitiveSource* primitive);
  43. private:
  44. bool m_fire_and_forget = false;
  45. };
  46. //-----------------------------------------------------------------------------
  47. class SceneDisplayData;
  48. class SceneDisplay
  49. {
  50. friend class Scene;
  51. public:
  52. SceneDisplay() { }
  53. virtual ~SceneDisplay() { }
  54. /* pos/size/... methods */
  55. virtual void SetResolution(ivec2 resolution) { UNUSED(resolution); }
  56. virtual void SetPosition(ivec2 position) { UNUSED(position); }
  57. /* TODO: Should that be there or in Video ? */
  58. static void Add(SceneDisplay* display);
  59. static int GetCount();
  60. static SceneDisplay* GetDisplay(int index = 0);
  61. static void DestroyAll();
  62. /* Implement these in the platform section */
  63. static int GetPhysicalCount();
  64. static const char* GetPhysicalName(int index = 0);
  65. //protected:
  66. virtual void Enable();
  67. virtual void Disable();
  68. private:
  69. SceneDisplayData *data;
  70. };
  71. //-----------------------------------------------------------------------------
  72. class SceneData;
  73. class Scene
  74. {
  75. friend class Video;
  76. private:
  77. static array<Scene*> g_scenes;
  78. Scene(ivec2 size);
  79. ~Scene();
  80. public:
  81. static void AddNew(ivec2 size);
  82. private: //Private because I don't know if we should have it
  83. static void DestroyScene(Scene* scene);
  84. private:
  85. static void DestroyAll();
  86. public:
  87. static int GetCount();
  88. static bool IsReady(int index = 0);
  89. static Scene& GetScene(int index = 0);
  90. public:
  91. //TODO: don't like the name
  92. void Link(Entity* entity);
  93. bool IsRelevant(Entity* entity);
  94. public:
  95. Camera* GetCamera(int cam_idx = -1);
  96. int PushCamera(Camera *cam);
  97. void PopCamera(Camera *cam);
  98. void SetTileCam(int cam_idx);
  99. void Reset();
  100. /* ============================== */
  101. # define _KEY_IDX (uintptr_t)key /* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */
  102. /* ============================== */
  103. private:
  104. int HasPrimitiveSource(uintptr_t key);
  105. int AddPrimitiveSource(uintptr_t key, class PrimitiveSource* source);
  106. void SetPrimitiveSource(int index, uintptr_t key, class PrimitiveSource* source);
  107. void ReleasePrimitiveSource(int index, uintptr_t key);
  108. void ReleaseAllPrimitiveSources(uintptr_t key);
  109. public:
  110. /* === Primitive source stuff === */
  111. /* Returns the number of primitive source set to the given entity */
  112. template <typename T>
  113. int HasPrimitiveSource(T* key)
  114. {
  115. ASSERT(key);
  116. return HasPrimitiveSource(_KEY_IDX);
  117. }
  118. /* Add a primitive sources linked to the given entity
  119. * Returns the slot number */
  120. template <typename T>
  121. int AddPrimitiveSource(T* key, class PrimitiveSource* source)
  122. {
  123. ASSERT(key);
  124. return AddPrimitiveSource(_KEY_IDX, source);
  125. }
  126. /* Update the primitive source at index linked to the given entity
  127. * Deletes the old one
  128. * The slot is kept even if source == nullptr */
  129. template <typename T>
  130. void SetPrimitiveSource(int index, T* key, class PrimitiveSource* source)
  131. {
  132. ASSERT(key);
  133. SetPrimitiveSource(index, _KEY_IDX, source);
  134. }
  135. /* Remove primitive source at index set to the given entity */
  136. template <typename T>
  137. void ReleasePrimitiveSource(int index, T* key)
  138. {
  139. ASSERT(key);
  140. ReleasePrimitiveSource(index, _KEY_IDX);
  141. }
  142. /* Remove all primitive source set to the given entity */
  143. template <typename T>
  144. void ReleaseAllPrimitiveSources(T* key)
  145. {
  146. ASSERT(key);
  147. ReleaseAllPrimitiveSources(_KEY_IDX);
  148. }
  149. private:
  150. int HasPrimitiveRenderer(uintptr_t key);
  151. void AddPrimitiveRenderer(uintptr_t key, class PrimitiveRenderer* renderer);
  152. void SetPrimitiveRenderer(int index, uintptr_t key, class PrimitiveRenderer* renderer);
  153. void ReleasePrimitiveRenderer(int index, uintptr_t key);
  154. void ReleaseAllPrimitiveRenderers(uintptr_t key);
  155. public:
  156. /* === Primitive renderer stuff === */
  157. /* Returns the number of primitive renderer set to the given entity */
  158. template <typename T>
  159. int HasPrimitiveRenderer(T* key)
  160. {
  161. ASSERT(key);
  162. return HasPrimitiveRenderer(_KEY_IDX);
  163. }
  164. /* Add a primitive renderer linked to the given entity
  165. * The primitive is considered as Fire&Forget and
  166. * will be destroyed at the end of the frame */
  167. template <typename T>
  168. void AddPrimitiveRenderer(T* key, class PrimitiveRenderer* renderer)
  169. {
  170. ASSERT(key);
  171. AddPrimitiveRenderer(_KEY_IDX, renderer);
  172. }
  173. /* Update the primitive renderer linked to the given entity
  174. * Deletes the old one
  175. * Will assert if renderer == nullptr */
  176. template <typename T>
  177. void SetPrimitiveRenderer(int index, T* key, class PrimitiveRenderer* renderer)
  178. {
  179. ASSERT(key && renderer);
  180. SetPrimitiveRenderer(index, _KEY_IDX, renderer);
  181. }
  182. /* Remove primitive renderer at index set to the given entity */
  183. template <typename T>
  184. void ReleasePrimitiveRenderer(int index, T* key)
  185. {
  186. ASSERT(key);
  187. ReleasePrimitiveRenderer(index, _KEY_IDX);
  188. }
  189. /* Remove all primitive renderer set to the given entity */
  190. template <typename T>
  191. void ReleaseAllPrimitiveRenderers(T* key)
  192. {
  193. ASSERT(key);
  194. ReleaseAllPrimitiveRenderers(_KEY_IDX);
  195. }
  196. /* ============================== */
  197. # undef _KEY_IDX /* (uintptr_t)key *//* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */
  198. /* ============================== */
  199. /* FIXME: this should be deprecated -- it doesn't really match
  200. * the architecture we want to build */
  201. void AddTile(TileSet *tileset, int id, vec3 pos, vec2 scale, float radians);
  202. void AddTile(TileSet *tileset, int id, mat4 model);
  203. public:
  204. void AddLine(vec3 a, vec3 b, vec4 color);
  205. void AddLine(vec3 a, vec3 b, vec4 color, float duration, int mask);
  206. void AddLight(Light *light);
  207. array<Light *> const &GetLights();
  208. /* === Render stuff === */
  209. void SetDisplay(SceneDisplay* display);
  210. void EnableDisplay();
  211. void DisableDisplay();
  212. void pre_render(float seconds);
  213. void render(float seconds);
  214. void post_render(float seconds);
  215. private:
  216. void render_primitives();
  217. void render_tiles();
  218. void render_lines(float seconds);
  219. SceneData *data;
  220. };
  221. } /* namespace lol */