Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

scenesetup.cpp 8.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // 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. #if HAVE_CONFIG_H
  12. # include "config.h"
  13. #endif
  14. #include <lol/engine.h>
  15. #include "scenesetup.h"
  16. namespace lol
  17. {
  18. //-----------------------------------------------------------------------------
  19. //CTor/DTor
  20. SceneSetup::SceneSetup(String const& name)
  21. {
  22. m_name = name;
  23. m_clear_color = vec4(vec3::zero, 1.f);
  24. m_show_gizmo = true;
  25. m_show_lights = true;
  26. }
  27. //----
  28. SceneSetup::~SceneSetup()
  29. {
  30. Shutdown(true);
  31. }
  32. //-----------------------------------------------------------------------------
  33. bool SceneSetup::Startup()
  34. {
  35. for (int i = 0; i < m_lights.count(); i++)
  36. Ticker::Ref(m_lights[i]);
  37. return true;
  38. }
  39. //-----------------------------------------------------------------------------
  40. bool SceneSetup::Shutdown(bool destroy)
  41. {
  42. for (int i = 0; i < m_lights.count(); i++)
  43. if (m_lights[i]->IsTicked())
  44. Ticker::Unref(m_lights[i]);
  45. if (destroy)
  46. m_lights.empty();
  47. return true;
  48. }
  49. //-----------------------------------------------------------------------------
  50. void SceneSetup::AddLight(LightType type)
  51. {
  52. m_last_cmd = SceneSetup::Command::AddLight;
  53. m_lights << new Light();
  54. m_lights.last()->SetType(type);
  55. }
  56. //-----------------------------------------------------------------------------
  57. void SceneSetup::SetupScene()
  58. {
  59. m_last_cmd = SceneSetup::Command::SetupScene;
  60. }
  61. //-----------------------------------------------------------------------------
  62. void SceneSetup::SetPosition(vec3 const& v)
  63. {
  64. switch (m_last_cmd.ToScalar())
  65. {
  66. case SceneSetup::Command::AddLight:
  67. m_lights.last()->SetPosition(v);
  68. break;
  69. }
  70. }
  71. //-----------------------------------------------------------------------------
  72. void SceneSetup::SetLookAt(vec3 const& v)
  73. {
  74. switch (m_last_cmd.ToScalar())
  75. {
  76. case SceneSetup::Command::AddLight:
  77. //TODO: Do better
  78. m_lights.last()->SetPosition(v - m_lights.last()->GetPosition());
  79. break;
  80. }
  81. }
  82. //-----------------------------------------------------------------------------
  83. void SceneSetup::SetColor(vec4 const& c)
  84. {
  85. switch (m_last_cmd.ToScalar())
  86. {
  87. case SceneSetup::Command::AddLight:
  88. m_lights.last()->SetColor(c);
  89. break;
  90. case SceneSetup::Command::SetupScene:
  91. m_clear_color = c;
  92. break;
  93. }
  94. }
  95. //-----------------------------------------------------------------------------
  96. void SceneSetup::Show(SceneSetup::Display const& d)
  97. {
  98. Set(d, DisplayFlag::On);
  99. }
  100. //-----------------------------------------------------------------------------
  101. void SceneSetup::Hide(SceneSetup::Display const& d)
  102. {
  103. Set(d, DisplayFlag::Off);
  104. }
  105. //-----------------------------------------------------------------------------
  106. void SceneSetup::Toggle(SceneSetup::Display const& d)
  107. {
  108. Set(d, DisplayFlag::Toggle);
  109. }
  110. //-----------------------------------------------------------------------------
  111. void SceneSetup::Set(SceneSetup::Display const& d, DisplayFlag const& f)
  112. {
  113. bool value = f == DisplayFlag::On ? true : false;
  114. switch (m_last_cmd.ToScalar())
  115. {
  116. case SceneSetup::Command::SetupScene:
  117. switch (d.ToScalar())
  118. {
  119. case SceneSetup::Display::Gizmo:
  120. m_show_gizmo = f == DisplayFlag::Toggle ? !m_show_gizmo : value;
  121. break;
  122. case SceneSetup::Display::Light:
  123. m_show_lights = f == DisplayFlag::Toggle ? !m_show_lights : value;
  124. break;
  125. }
  126. break;
  127. }
  128. }
  129. //-----------------------------------------------------------------------------
  130. SceneSetupLuaObject::SceneSetupLuaObject(String& name) : LuaObject()
  131. {
  132. m_setup = new SceneSetup(name);
  133. SceneSetupLuaLoader::RegisterSetup(m_setup);
  134. }
  135. //-----------------------------------------------------------------------------
  136. SceneSetupLuaObject::~SceneSetupLuaObject()
  137. {
  138. }
  139. //-----------------------------------------------------------------------------
  140. SceneSetupLuaObject* SceneSetupLuaObject::New(lua_State* l, int arg_nb)
  141. {
  142. UNUSED(arg_nb);
  143. auto s = LuaStack::Begin(l);
  144. auto n = s.Get<String>();
  145. return new SceneSetupLuaObject(n);
  146. }
  147. //-- Setup command ------------------------------------------------------------
  148. int SceneSetupLuaObject::AddLight(lua_State* l)
  149. {
  150. auto s = LuaStack::Begin(l);
  151. auto o = s.GetPtr<SceneSetupLuaObject>();
  152. auto t = s.Get<String>();
  153. o->m_setup->AddLight(FindValue<LightType>(t.C()));
  154. return s.End();
  155. }
  156. int SceneSetupLuaObject::SetupScene(lua_State* l)
  157. {
  158. auto s = LuaStack::Begin(l);
  159. auto o = s.GetPtr<SceneSetupLuaObject>();
  160. o->m_setup->SetupScene();
  161. return s.End();
  162. }
  163. //-- main funcs ---------------------------------------------------------------
  164. int SceneSetupLuaObject::SetPosition(lua_State* l)
  165. {
  166. auto s = LuaStack::Begin(l);
  167. auto o = s.GetPtr<SceneSetupLuaObject>();
  168. auto c = s.Get<vec3>();
  169. o->m_setup->SetPosition(c);
  170. return s.End();
  171. }
  172. int SceneSetupLuaObject::SetLookAt(lua_State* l)
  173. {
  174. auto s = LuaStack::Begin(l);
  175. auto o = s.GetPtr<SceneSetupLuaObject>();
  176. auto c = s.Get<vec3>();
  177. o->m_setup->SetLookAt(c);
  178. return s.End();
  179. }
  180. int SceneSetupLuaObject::SetColor(lua_State* l)
  181. {
  182. auto s = LuaStack::Begin(l);
  183. auto o = s.GetPtr<SceneSetupLuaObject>();
  184. auto c = s.Get<vec4>();
  185. o->m_setup->SetColor(c);
  186. return s.End();
  187. }
  188. int SceneSetupLuaObject::Show(lua_State* l)
  189. {
  190. auto s = LuaStack::Begin(l);
  191. auto o = s.GetPtr<SceneSetupLuaObject>();
  192. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  193. o->m_setup->Show(e);
  194. return s.End();
  195. }
  196. int SceneSetupLuaObject::Hide(lua_State* l)
  197. {
  198. auto s = LuaStack::Begin(l);
  199. auto o = s.GetPtr<SceneSetupLuaObject>();
  200. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  201. o->m_setup->Hide(e);
  202. return s.End();
  203. }
  204. int SceneSetupLuaObject::Toggle(lua_State* l)
  205. {
  206. auto s = LuaStack::Begin(l);
  207. auto o = s.GetPtr<SceneSetupLuaObject>();
  208. auto e = s.GetEnum<SceneSetup::DisplayBase>();
  209. o->m_setup->Toggle(e);
  210. return s.End();
  211. }
  212. //-----------------------------------------------------------------------------
  213. const LuaObjectLibrary* SceneSetupLuaObject::GetLib()
  214. {
  215. typedef SceneSetupLuaObject SSLO;
  216. static const LuaObjectLibrary lib = LuaObjectLibrary(
  217. "SceneSetup",
  218. //Statics
  219. { { nullptr, nullptr } },
  220. //Methods
  221. {
  222. //-----------------------------------------------------------------
  223. { "AddLight", AddLight },
  224. { "Setup", SetupScene },
  225. //-- main funcs
  226. { "Position", SetPosition },
  227. { "LookAt", SetLookAt },
  228. { "Color", SetColor },
  229. { "Show", Show },
  230. { "Hide", Hide },
  231. { "Toggle", Toggle },
  232. },
  233. //Variables
  234. { { nullptr, nullptr, nullptr } });
  235. return &lib;
  236. }
  237. //-----------------------------------------------------------------------------
  238. map<String, SceneSetup*> SceneSetupLuaLoader::m_setups;
  239. SceneSetupLuaLoader::SceneSetupLuaLoader() : LuaLoader()
  240. {
  241. lua_State* l = GetLuaState();
  242. LuaObjectHelper::Register<SceneSetupLuaObject>(l);
  243. }
  244. //-----------------------------------------------------------------------------
  245. SceneSetupLuaLoader::~SceneSetupLuaLoader() { }
  246. //-----------------------------------------------------------------------------
  247. static array<SceneSetupLuaObject*> g_instances;
  248. void SceneSetupLuaLoader::Store(LuaObject* obj)
  249. {
  250. SceneSetupLuaObject* ezm = static_cast<SceneSetupLuaObject*>(obj);
  251. g_instances << ezm;
  252. }
  253. //-----------------------------------------------------------------------------
  254. array<SceneSetupLuaObject*>& SceneSetupLuaLoader::GetInstances()
  255. {
  256. return g_instances;
  257. }
  258. //-----------------------------------------------------------------------------
  259. void SceneSetupLuaLoader::RegisterSetup(SceneSetup* setup)
  260. {
  261. m_setups[setup->m_name] = setup;
  262. }
  263. //-----------------------------------------------------------------------------
  264. bool SceneSetupLuaLoader::GetRegisteredSetups(map<String, SceneSetup*>& setups)
  265. {
  266. setups = m_setups;
  267. return !!m_setups.count();
  268. }
  269. //-----------------------------------------------------------------------------
  270. bool SceneSetupLuaLoader::GetLoadedSetups(map<String, SceneSetup*>& setups)
  271. {
  272. return GetRegisteredSetups(setups);
  273. }
  274. } /* namespace lol */