104 line
2.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #pragma once
  13. //
  14. // The Light class
  15. // ---------------
  16. //
  17. #include "engine/worldentity.h"
  18. #include <cfloat> /* for FLT_MAX */
  19. #include <string>
  20. namespace lol
  21. {
  22. struct LightType
  23. {
  24. enum Value
  25. {
  26. Directional = 0,
  27. Point,
  28. Max,
  29. }
  30. m_value;
  31. std::string tostring()
  32. {
  33. switch (m_value)
  34. {
  35. case Directional:
  36. return "<Directional>";
  37. case Point:
  38. return "<Point>";
  39. default:
  40. return "<UNDEFINED>";
  41. }
  42. }
  43. inline LightType(float v)
  44. {
  45. float top = FLT_MAX;
  46. int iv = Directional;
  47. for (int i = 0; i < Max; ++i)
  48. {
  49. LightType lv = LightType(i);
  50. float nv = lv.f();
  51. float ntop = lol::abs(nv - v);
  52. if (ntop < top)
  53. {
  54. top = ntop;
  55. iv = i;
  56. }
  57. }
  58. m_value = LightType(iv);
  59. }
  60. inline LightType(int v) : m_value((Value)v) {}
  61. inline LightType(Value v) : m_value(v) {}
  62. inline LightType() : m_value(Directional) {}
  63. inline operator Value() { return m_value; }
  64. inline float f() { return ((float)m_value / (float)Max); }
  65. };
  66. class Light : public WorldEntity
  67. {
  68. public:
  69. Light();
  70. ~Light();
  71. std::string GetName() const { return "<light>"; }
  72. void SetType(LightType const &type);
  73. LightType GetType();
  74. void SetColor(vec4 const &color);
  75. vec4 GetColor();
  76. void SetPosition(vec3 const &pos);
  77. vec3 GetPosition();
  78. protected:
  79. virtual void tick_game(float seconds);
  80. virtual void tick_draw(float seconds, Scene &scene);
  81. private:
  82. vec4 m_color;
  83. LightType m_type;
  84. };
  85. } /* namespace lol */