| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | #pragma once |
| 38 | |
| 39 | #include <util/types.hh> |
| 40 | #include <util/structs.hh> |
| 41 | #include <util/vector.hh> |
| 42 | #include <shader/buffers.hh> |
| 43 | #define SOL_LUAJIT 1 |
| 44 | #define SOL_ALL_SAFETIES_ON 1 |
| 45 | #include <sol.hpp> |
| 46 | |
| 47 | struct Program; |
| 48 | |
| 49 | namespace gui { |
| 50 | class ConsoleLog; |
| 51 | } |
| 52 | |
| 53 | namespace lua { |
| 54 | struct LuaMesh { |
| 55 | vector<f32> vertices; |
| 56 | vector<u32> indices; |
| 57 | |
| 58 | struct Attribute { |
| 59 | int location; |
| 60 | int size; |
| 61 | int stride; |
| 62 | int offset; |
| 63 | }; |
| 64 | |
| 65 | std::vector<Attribute> attr; |
| 66 | |
| 67 | GLsizei GetIndexCount() const { |
| 68 | return static_cast<GLsizei>(indices.size()); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | struct MeshObject { |
| 73 | LuaMesh mesh; |
| 74 | Vector3 position; |
| 75 | Vector3 rotation; |
| 76 | Vector3 scale; |
| 77 | }; |
| 78 | |
| 79 | class LuaEngine { |
| 80 | public: |
| 81 | LuaEngine(const string &code, Program *prog, Info *info); |
| 82 | ~LuaEngine(); |
| 83 | |
| 84 | public: |
| 85 | vector<MeshObject> &GetObjects() { return m_Objects; } |
| 86 | const vector<MeshObject> &GetObjects() const { return m_Objects; } |
| 87 | |
| 88 | public: |
| 89 | void Reload(const string &code); |
| 90 | void Init(); |
| 91 | void Update(); |
| 92 | bool Validate(const string &code); |
| 93 | |
| 94 | public: |
| 95 | void SetProgram(Program *prog) { m_Program = prog; } |
| 96 | bool GetInitializationState() const { return m_IsInitialized; } |
| 97 | void SetInitializationState(bool state) { m_IsInitialized = state; } |
| 98 | string GetApiVersion() const { return m_ApiVersion; } |
| 99 | |
| 100 | private: |
| 101 | void LogError(sol::error err); |
| 102 | void LogError(const std::exception &err); |
| 103 | void LogError(const string &err); |
| 104 | sol::state MakeAPI(); |
| 105 | void BindEngine(sol::state &lua); |
| 106 | void BindSystem(sol::state &lua); |
| 107 | void BindIO(sol::state &lua); |
| 108 | void BindMath(sol::state &lua); |
| 109 | void BindGraphics(sol::state &lua); |
| 110 | void Execute(sol::state &lua); |
| 111 | i32 StringToKey(const string &key); |
| 112 | |
| 113 | private: |
| 114 | vector<MeshObject> m_Objects; |
| 115 | sol::state m_Lua; |
| 116 | string m_Code; |
| 117 | bool m_IsInitialized = false; |
| 118 | Program *m_Program = nullptr; |
| 119 | Info *m_Info = {}; |
| 120 | string m_ApiVersion = "1.0.0"; |
| 121 | f32 m_LastTime = 0.f; |
| 122 | }; |
| 123 | } |