Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

342 linhas
9.6 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. /*
  39. * A quick and dirty Tile structure for 2D blits
  40. */
  41. struct Tile
  42. {
  43. mat4 m_model;
  44. TileSet *m_tileset;
  45. int m_id;
  46. };
  47. class PrimitiveRenderer
  48. {
  49. friend class Scene;
  50. public:
  51. PrimitiveRenderer() { }
  52. virtual ~PrimitiveRenderer() { }
  53. virtual void Render(Scene& scene, std::shared_ptr<PrimitiveSource> primitive);
  54. private:
  55. bool m_fire_and_forget = false;
  56. };
  57. class SceneDisplay
  58. {
  59. friend class Scene;
  60. public:
  61. SceneDisplay() { }
  62. virtual ~SceneDisplay() { }
  63. /* pos/size/... methods */
  64. virtual void set_resolution(ivec2 resolution) { UNUSED(resolution); }
  65. virtual ivec2 resolution() const { return ivec2(0); }
  66. virtual void SetPosition(ivec2 position) { UNUSED(position); }
  67. /* TODO: Should that be there or in Video ? */
  68. static void Add(SceneDisplay* display);
  69. static int GetCount();
  70. static SceneDisplay* GetDisplay(int index = 0);
  71. static void DestroyAll();
  72. /* Implement these in the platform section */
  73. static int GetPhysicalCount();
  74. static const char* GetPhysicalName(int index = 0);
  75. //protected:
  76. virtual void Enable();
  77. virtual void Disable();
  78. };
  79. class Scene
  80. {
  81. friend class Video;
  82. private:
  83. static array<Scene*> g_scenes;
  84. Scene(ivec2 size);
  85. ~Scene();
  86. public:
  87. static void AddNew(ivec2 size);
  88. private: //Private because I don't know if we should have it
  89. static void DestroyScene(Scene* scene);
  90. private:
  91. static void DestroyAll();
  92. public:
  93. static int GetCount();
  94. static bool IsReady(int index = 0);
  95. static Scene& GetScene(int index = 0);
  96. public:
  97. //TODO: don't like the name
  98. void Link(entity* entity);
  99. bool IsRelevant(entity* entity);
  100. public:
  101. Camera* GetCamera(int cam_idx = -1);
  102. int PushCamera(Camera *cam);
  103. void PopCamera(Camera *cam);
  104. void SetTileCam(int cam_idx);
  105. void Reset();
  106. std::shared_ptr<Renderer> get_renderer() { return m_renderer; }
  107. /* ============================== */
  108. # define _KEY_IDX (uintptr_t)key /* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */
  109. /* ============================== */
  110. private:
  111. int HasPrimitiveSource(uintptr_t key);
  112. int AddPrimitiveSource(uintptr_t key, std::shared_ptr<class PrimitiveSource> source);
  113. void SetPrimitiveSource(int index, uintptr_t key, std::shared_ptr<class PrimitiveSource> source);
  114. void ReleasePrimitiveSource(int index, uintptr_t key);
  115. void ReleaseAllPrimitiveSources(uintptr_t key);
  116. public:
  117. /* === Primitive source stuff === */
  118. /* Returns the number of primitive source set to the given entity */
  119. template <typename T>
  120. int HasPrimitiveSource(T* key)
  121. {
  122. ASSERT(key);
  123. return HasPrimitiveSource(_KEY_IDX);
  124. }
  125. /* Add a primitive sources linked to the given entity
  126. * Returns the slot number */
  127. template <typename T>
  128. int AddPrimitiveSource(T* key, std::shared_ptr<class PrimitiveSource> source)
  129. {
  130. ASSERT(key);
  131. return AddPrimitiveSource(_KEY_IDX, source);
  132. }
  133. /* Update the primitive source at index linked to the given entity
  134. * Deletes the old one
  135. * The slot is kept even if source == nullptr */
  136. template <typename T>
  137. void SetPrimitiveSource(int index, T* key, std::shared_ptr<class PrimitiveSource> source)
  138. {
  139. ASSERT(key);
  140. SetPrimitiveSource(index, _KEY_IDX, source);
  141. }
  142. /* Remove primitive source at index set to the given entity */
  143. template <typename T>
  144. void ReleasePrimitiveSource(int index, T* key)
  145. {
  146. ASSERT(key);
  147. ReleasePrimitiveSource(index, _KEY_IDX);
  148. }
  149. /* Remove all primitive source set to the given entity */
  150. template <typename T>
  151. void ReleaseAllPrimitiveSources(T* key)
  152. {
  153. ASSERT(key);
  154. ReleaseAllPrimitiveSources(_KEY_IDX);
  155. }
  156. private:
  157. int HasPrimitiveRenderer(uintptr_t key);
  158. void AddPrimitiveRenderer(uintptr_t key, std::shared_ptr<class PrimitiveRenderer> renderer);
  159. void SetPrimitiveRenderer(int index, uintptr_t key, std::shared_ptr<class PrimitiveRenderer> renderer);
  160. void ReleasePrimitiveRenderer(int index, uintptr_t key);
  161. void ReleaseAllPrimitiveRenderers(uintptr_t key);
  162. public:
  163. /* === Primitive renderer stuff === */
  164. /* Returns the number of primitive renderer set to the given entity */
  165. template <typename T>
  166. int HasPrimitiveRenderer(T* key)
  167. {
  168. ASSERT(key);
  169. return HasPrimitiveRenderer(_KEY_IDX);
  170. }
  171. /* Add a primitive renderer linked to the given entity
  172. * The primitive is considered as Fire&Forget and
  173. * will be destroyed at the end of the frame */
  174. template <typename T>
  175. void AddPrimitiveRenderer(T* key, std::shared_ptr<class PrimitiveRenderer> renderer)
  176. {
  177. ASSERT(key);
  178. AddPrimitiveRenderer(_KEY_IDX, renderer);
  179. }
  180. /* Update the primitive renderer linked to the given entity
  181. * Deletes the old one
  182. * Will assert if renderer == nullptr */
  183. template <typename T>
  184. void SetPrimitiveRenderer(int index, T* key, std::shared_ptr<class PrimitiveRenderer> renderer)
  185. {
  186. ASSERT(key && renderer);
  187. SetPrimitiveRenderer(index, _KEY_IDX, renderer);
  188. }
  189. /* Remove primitive renderer at index set to the given entity */
  190. template <typename T>
  191. void ReleasePrimitiveRenderer(int index, T* key)
  192. {
  193. ASSERT(key);
  194. ReleasePrimitiveRenderer(index, _KEY_IDX);
  195. }
  196. /* Remove all primitive renderer set to the given entity */
  197. template <typename T>
  198. void ReleaseAllPrimitiveRenderers(T* key)
  199. {
  200. ASSERT(key);
  201. ReleaseAllPrimitiveRenderers(_KEY_IDX);
  202. }
  203. /* ============================== */
  204. # undef _KEY_IDX /* (uintptr_t)key *//* TOUKY: I don't like that. hash should be fixed to handle these custom stuff */
  205. /* ============================== */
  206. /* FIXME: this should be deprecated -- it doesn't really match
  207. * the architecture we want to build */
  208. void AddTile(TileSet *tileset, int id, vec3 pos, vec2 scale, float radians);
  209. void AddTile(TileSet *tileset, int id, mat4 model);
  210. public:
  211. void AddLine(vec3 a, vec3 b, vec4 color);
  212. void AddLine(vec3 a, vec3 b, vec4 color, float duration, int mask);
  213. void AddLight(Light *light);
  214. array<Light *> const &GetLights();
  215. /* === Render stuff === */
  216. void SetDisplay(SceneDisplay* display);
  217. void EnableDisplay();
  218. void DisableDisplay();
  219. void resize(ivec2 size);
  220. void pre_render(float seconds);
  221. void render(float seconds);
  222. void post_render(float seconds);
  223. private:
  224. void render_primitives();
  225. void render_tiles();
  226. void render_lines(float seconds);
  227. ivec2 m_size, m_wanted_size;
  228. std::shared_ptr<Renderer> m_renderer;
  229. //
  230. // The old SceneData stuff
  231. //
  232. /* Mask ID */
  233. /* TODO: Do a mask class that handles more than 64 slots */
  234. static uint64_t g_used_id;
  235. uint64_t m_mask_id = 0;
  236. /* Scene display: if none has been set to the scene,
  237. * the default one created by the app will be used */
  238. SceneDisplay* m_display = nullptr;
  239. /** Render buffers: where to render to. */
  240. std::shared_ptr<Framebuffer> m_renderbuffer[4];
  241. struct postprocess
  242. {
  243. std::shared_ptr<Shader> m_shader[2];
  244. std::shared_ptr<VertexBuffer> m_vbo;
  245. std::shared_ptr<VertexDeclaration> m_vdecl;
  246. ShaderUniform m_buffer_uni[2][3];
  247. ShaderAttrib m_coord[2];
  248. }
  249. m_pp;
  250. /* Sources are shared by all scenes.
  251. * Renderers are scene-dependent. They get the primitive in the identical
  252. * slot to render with the given scene.
  253. * Primitives and renderers will be kept until:
  254. * - Updated by entity
  255. * - Marked Fire&Forget
  256. * - Scene is destroyed */
  257. std::map<uintptr_t, array<std::shared_ptr<PrimitiveRenderer>>> m_prim_renderers;
  258. static std::map<uintptr_t, array<std::shared_ptr<PrimitiveSource>>> g_prim_sources;
  259. static mutex g_prim_mutex;
  260. Camera *m_default_cam;
  261. array<Camera *> m_camera_stack;
  262. /* Old line API <P0, P1, COLOR, TIME, MASK> */
  263. struct line_api
  264. {
  265. //float m_duration, m_segment_size;
  266. //vec4 m_color;
  267. array<vec3, vec3, vec4, float, int, bool, bool> m_lines;
  268. int /*m_mask,*/ m_debug_mask;
  269. std::shared_ptr<Shader> m_shader;
  270. std::shared_ptr<VertexDeclaration> m_vdecl;
  271. }
  272. m_line_api;
  273. /* The old tiles API */
  274. struct tile_api
  275. {
  276. int m_cam;
  277. array<Tile> m_tiles;
  278. array<Tile> m_palettes;
  279. array<Light *> m_lights;
  280. std::shared_ptr<Shader> m_shader;
  281. std::shared_ptr<Shader> m_palette_shader;
  282. std::shared_ptr<VertexDeclaration> m_vdecl;
  283. array<std::shared_ptr<VertexBuffer>> m_bufs;
  284. }
  285. m_tile_api;
  286. };
  287. } /* namespace lol */