25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

225 satır
6.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2013 Sam Hocevar <sam@hocevar.net>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #pragma once
  12. //
  13. // The Scene setup class
  14. // ----------------
  15. //
  16. namespace lol
  17. {
  18. //-----------------------------------------------------------------------------
  19. class SceneSetup
  20. {
  21. friend class SceneSetupLuaLoader;
  22. friend class SceneSetupLuaObject;
  23. public:
  24. //CTor/DTor
  25. SceneSetup() : SceneSetup("default") { }
  26. SceneSetup(String const& name);
  27. ~SceneSetup();
  28. static char const *GetName() { return "<scenesetup>"; }
  29. //--
  30. bool Startup();
  31. bool Shutdown(bool destroy=false);
  32. //-- Setup command
  33. void AddLight(LightType type);
  34. void SetupScene();
  35. //-- main funcs
  36. void SetPosition(vec3 const& v);
  37. void SetLookAt(vec3 const& v);
  38. void SetColor(vec4 const& c);
  39. //-------------------------------------------------------------------------
  40. struct DisplayBase : public StructSafeEnum
  41. {
  42. enum Type
  43. {
  44. Gizmo,
  45. Light,
  46. Max
  47. };
  48. protected:
  49. virtual bool BuildEnumMap(map<int64_t, String>& enum_map)
  50. {
  51. enum_map[Gizmo] = "Gizmo";
  52. enum_map[Light] = "Light";
  53. return true;
  54. }
  55. };
  56. typedef SafeEnum<DisplayBase> Display;
  57. void Show(Display const& d);
  58. void Hide(Display const& d);
  59. void Toggle(Display const& d);
  60. protected:
  61. void Set(Display const& d, DisplayFlag const& f);
  62. protected:
  63. //-------------------------------------------------------------------------
  64. struct CommandBase : public StructSafeEnum
  65. {
  66. enum Type
  67. {
  68. AddLight,
  69. SetupScene,
  70. Max
  71. };
  72. protected:
  73. virtual bool BuildEnumMap(map<int64_t, String>& enum_map)
  74. {
  75. enum_map[AddLight] = "AddLight";
  76. enum_map[SetupScene] = "SetupScene";
  77. return true;
  78. }
  79. };
  80. typedef SafeEnum<CommandBase> Command;
  81. public:
  82. //--
  83. String m_name;
  84. Command m_last_cmd;
  85. vec4 m_clear_color;
  86. array<Light *> m_lights;
  87. array<String, String> m_custom_cmd;
  88. bool m_show_gizmo;
  89. bool m_show_lights;
  90. };
  91. typedef Lolua::VarEnum<SceneSetup::DisplayBase> LuaDisplay;
  92. //-----------------------------------------------------------------------------
  93. class SceneSetupLuaObject : public LuaObject
  94. {
  95. typedef Lolua::VarPtr<SceneSetupLuaObject> LuaSSetupPtr;
  96. public:
  97. //-------------------------------------------------------------------------
  98. SceneSetupLuaObject(String& name);
  99. virtual ~SceneSetupLuaObject();
  100. //-------------------------------------------------------------------------
  101. static SceneSetupLuaObject* New(LuaState* l, int arg_nb);
  102. static const LuaObjectLib* GetLib();
  103. //-------------------------------------------------------------------------
  104. public:
  105. //-- Setup command
  106. static int AddLight(LuaState* l);
  107. static int SetupScene(LuaState* l);
  108. //-- main funcs
  109. static int SetPosition(LuaState* l);
  110. static int SetLookAt(LuaState* l);
  111. static int SetColor(LuaState* l);
  112. static int Show(LuaState* l);
  113. static int Hide(LuaState* l);
  114. static int Toggle(LuaState* l);
  115. //-- Setup command
  116. void AddLight(LightType type);
  117. void SetupScene();
  118. //-- main funcs
  119. void SetPosition(vec3& v);
  120. void SetLookAt(vec3& v);
  121. void SetColor(vec4& c);
  122. void Show(SceneSetup::Display const& d);
  123. void Hide(SceneSetup::Display const& d);
  124. void Toggle(SceneSetup::Display const& d);
  125. private:
  126. SceneSetup* m_setup = nullptr;
  127. };
  128. //-----------------------------------------------------------------------------
  129. class SceneSetupLuaLoader : public LuaLoader
  130. {
  131. friend class SceneSetupLuaObject;
  132. public:
  133. SceneSetupLuaLoader();
  134. virtual ~SceneSetupLuaLoader();
  135. //Virtual Store lua object ------------------------------------------------
  136. virtual void Store(LuaObject* obj);
  137. array<SceneSetupLuaObject*>& GetInstances();
  138. //-------------------------------------------------------------------------
  139. protected:
  140. static void RegisterSetup(SceneSetup* setup);
  141. static bool GetRegisteredSetups(map<String, SceneSetup*>& setups);
  142. public:
  143. bool GetLoadedSetups(map<String, SceneSetup*>& setups);
  144. private:
  145. static map<String, SceneSetup*> m_setups;
  146. };
  147. /*
  148. addlight { return token::T_ADDLIGHT; }
  149. position { return token::T_OBJPOSITION; }
  150. lookat { return token::T_OBJLOOKAT; }
  151. color { return token::T_OBJCOLOR; }
  152. clearcolor { return token::T_CLEARCOLOR; }
  153. showgizmo { return token::T_SHOWGIZMO; }
  154. showlight { return token::T_SHOWLIGHT; }
  155. custom { return token::T_CUSTOMCMD; }
  156. custom { return token::T_CUSTOMCMD; }
  157. light_command:
  158. T_ADDLIGHT { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT"; }
  159. | T_ADDLIGHT fv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  160. uc.m_sstp.m_lights.last()->SetType(LightType($2)); }
  161. | T_ADDLIGHT svv { uc.m_sstp.m_lights << new Light(); uc.m_last_cmd = "ADDLIGHT";
  162. uc.m_sstp.m_lights.last()->SetType(FindValue<LightType>($2)); }
  163. ;
  164. setup_command:
  165. T_OBJPOSITION v3 { if (uc.m_last_cmd == "ADDLIGHT")
  166. uc.m_sstp.m_lights.last()->SetPosition(vec3($2[0], $2[1], $2[2])); }
  167. | T_OBJLOOKAT v3 { if (uc.m_last_cmd == "ADDLIGHT")
  168. {
  169. } }
  170. | T_OBJCOLOR v4{ if (uc.m_last_cmd == "ADDLIGHT")
  171. uc.m_sstp.m_lights.last()->SetColor(vec4($2[0], $2[1], $2[2], $2[3])); }
  172. | T_OBJCOLOR COLOR{ uint32_t x = $2;
  173. ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  174. vec4 vv = vec4(v) * (1.f / 255.f);
  175. if (uc.m_last_cmd == "ADDLIGHT")
  176. uc.m_sstp.m_lights.last()->SetColor(vv); }
  177. ;
  178. scene_command:
  179. T_CLEARCOLOR v4{ uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], $2[3]); }
  180. T_CLEARCOLOR v3{ uc.m_sstp.m_clear_color = vec4($2[0], $2[1], $2[2], 1.f); }
  181. | T_CLEARCOLOR COLOR{ uint32_t x = $2; ivec4 v(x >> 24, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff);
  182. uc.m_sstp.m_clear_color = vec4(v) * (1.f / 255.f); }
  183. | T_SHOWGIZMO bv{ uc.m_sstp.m_show_gizmo = $2; }
  184. | T_SHOWLIGHT bv{ uc.m_sstp.m_show_lights = $2; }
  185. ;
  186. custom_command:
  187. T_CUSTOMCMD svv sv{ uc.m_sstp.m_custom_cmd.push($2, $3); }
  188. ;
  189. */
  190. } /* namespace lol */