Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

273 строки
7.7 KiB

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