You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

91 lines
3.3 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
  5. // (c) 2010-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. //
  12. // The CommandStack struct
  13. // ------------------
  14. //
  15. #if !defined __COMMANDSTACK_COMMANDSTACK_H__
  16. #define __COMMANDSTACK_COMMANDSTACK_H__
  17. namespace lol
  18. {
  19. //Utility struct to convert command code to pseudo-bytecode
  20. struct CommandStack
  21. {
  22. private:
  23. Array<int, int, int> m_commands;
  24. Array<float> m_floats;
  25. Array<int> m_ints;
  26. int m_f_cur;
  27. int m_i_cur;
  28. public:
  29. //GET/SET exec
  30. int GetCmdNb() { return m_commands.Count(); }
  31. int GetCmd(int i)
  32. {
  33. ASSERT(0 <= i && i < m_commands.Count());
  34. m_f_cur = m_commands[i].m2;
  35. m_i_cur = m_commands[i].m3;
  36. return m_commands[i].m1;
  37. }
  38. //cmd storage
  39. void AddCmd(int cmd) { m_commands.Push(cmd, m_floats.Count(), m_ints.Count()); }
  40. //GETTER
  41. inline float F() { return m_floats[m_f_cur++]; }
  42. inline int I() { return m_ints[m_i_cur++]; }
  43. inline int E() { return I(); }
  44. inline bool B() { return !!I(); }
  45. inline vec2 V2() { vec2 v(F()); v.y = F(); return v; }
  46. inline vec3 V3() { vec3 v(V2(), 0.f); v.z = F(); return v; }
  47. inline vec4 V4() { vec4 v(V3(), 0.f); v.w = F(); return v; }
  48. inline ivec2 IV2() { ivec2 v(I()); v.y = I(); return v; }
  49. inline ivec3 IV3() { ivec3 v(IV2(), 0); v.z = I(); return v; }
  50. inline ivec4 IV4() { ivec4 v(IV3(), 0); v.w = I(); return v; }
  51. // Alternate getters
  52. inline void GetValue(float &f) { f = F(); }
  53. inline void GetValue(int &i) { i = I(); }
  54. inline void GetValue(bool &b) { b = B(); }
  55. inline void GetValue(vec2 &v2) { v2 = V2(); }
  56. inline void GetValue(vec3 &v3) { v3 = V3(); }
  57. inline void GetValue(vec4 &v4) { v4 = V4(); }
  58. inline void GetValue(ivec2 &iv2) { iv2 = IV2(); }
  59. inline void GetValue(ivec3 &iv3) { iv3 = IV3(); }
  60. inline void GetValue(ivec4 &iv4) { iv4 = IV4(); }
  61. // For Safe Enum
  62. template<class DEF> inline
  63. void GetValue(SafeEnum<DEF> &i) { i = typename DEF::Type(I()); }
  64. //SETTER
  65. CommandStack &operator<<(int i) { m_ints << i; return *this; }
  66. CommandStack &operator<<(float f) { m_floats << f; return *this; }
  67. CommandStack &operator<<(bool b) { return (*this << (int)b); }
  68. CommandStack &operator<<(vec2 v) { return (*this << v.x << v.y); }
  69. CommandStack &operator<<(vec3 v) { return (*this << v.xy << v.z); }
  70. CommandStack &operator<<(vec4 v) { return (*this << v.xyz << v.w); }
  71. CommandStack &operator<<(ivec2 iv) { return (*this << iv.x << iv.y); }
  72. CommandStack &operator<<(ivec3 iv) { return (*this << iv.xy << iv.z); }
  73. CommandStack &operator<<(ivec4 iv) { return (*this << iv.xyz << iv.w); }
  74. // For Safe Enum
  75. template<class DEF> inline
  76. CommandStack &operator<<(SafeEnum<DEF> &i) { return *this << i.ToScalar(); }
  77. };
  78. } /* namespace lol */
  79. #endif /* __COMMANDSTACK_COMMANDSTACK_H__ */