Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #pragma once
  2. extern "C"{
  3. #include <lua.h>
  4. #include <lualib.h>
  5. #include <lauxlib.h>
  6. }
  7. #include <luabind/luabind.hpp>
  8. #include "alpha/LogManager.h"
  9. #include "alpha/_luaImplementation.h"
  10.  
  11. ///Wrap lua using R.A.I.I.
  12. class Lua_State
  13. {
  14. public:
  15. Lua_State() : L(luaL_newstate()){
  16. luabind::open(L);///Connect automatically luabind to all states
  17. }
  18. ~Lua_State() { lua_close(L); }
  19. operator lua_State*() { return L; }///implicite conversion Lua_State ------> lua_state*
  20. private:
  21. lua_State *L;
  22. };
  23.  
  24. inline static void LuaOpenLibs(lua_State *L){ ///convenience functions;
  25. luaopen_io(L);
  26. luaopen_base(L);
  27. luaopen_table(L);
  28. luaopen_string(L);
  29. luaopen_math(L);
  30. luaL_openlibs(L);
  31. }
  32.  
  33.  
  34. inline static void ReportLuaError(lua_State *L, int status){
  35. if(status != 0){
  36. LOG_ERROR_INFO("LuaLoader", "ERROR : LUA ERROR", lua_tostring(L, -1));
  37. lua_pop(L, 1);
  38. }
  39. }
  40.  
  41. inline static int LuaExecuteScript(lua_State *L){
  42. return lua_pcall(L, 0, LUA_MULTRET, 0);
  43. }
  44.  
  45.  
  46. class LuaScript
  47. {
  48. public:
  49. LuaScript(){ LoadAlphaLuaBinding(m_luaState); }
  50. ~LuaScript(){}
  51.  
  52. bool Load(const char* scriptName);
  53. bool ExecuteScript();
  54. Lua_State GetState() { return m_luaState; }
  55.  
  56. template<class F>
  57. void RegisterFunction(const char* name, F f){
  58. luabind::module(m_luaState)
  59. [
  60. luabind::def(name, f)
  61. ];
  62. }
  63.  
  64. private:
  65. Lua_State m_luaState = Lua_State();
  66. int m_status;
  67. };
  68.  
  69. #pragma once
  70. #include <iostream>
  71. #include "alpha/Shader.h"
  72. #include "alpha/Window.h"
  73. #include "alpha/Vertex.h"
  74. #include "alpha/Mesh.h"
  75. #include "alpha/Texture.h"
  76. #include "alpha/Material.h"
  77. #include "alpha/Transform.h"
  78. #include "alpha/Camera.h"
  79. #include "alpha/ModelLoader.h"
  80. #include "alpha/InputHandler.h"
  81. #include "alpha/wrapper/SDL_Handle.h"
  82. extern "C"{
  83. #include <lua.h>
  84. #include <lualib.h>
  85. #include <lauxlib.h>
  86. }
  87. #include <luabind/luabind.hpp>
  88.  
  89. #define DEFINE_LUA_IMPLEMENTAION
  90.  
  91.  
  92. inline void LoadAlphaLuaBinding(lua_State *L){
  93. using namespace luabind;
  94.  
  95. module(L)
  96. [
  97. class_<Window>("Window")
  98. .def(constructor<unsigned, unsigned, unsigned, unsigned,
  99. const char*, bool, bool>())
  100. .def("Clear", &Window::Clear)
  101. .def("SwapBuffers", &Window::SwapBuffers)
  102. .def("Update", &Window::Update)
  103. .def("Resized", &Window::Resized)
  104. .def("GetWidth", &Window::GetWidth)
  105. .def("GetHeight", &Window::GetHeight)
  106. .def("SetWidth", &Window::SetWidth)
  107. .def("SetHeight", &Window::SetHeight)
  108. .def("HasMoved", &Window::HasMoved)
  109. .def("SetTitle", &Window::SetTitle)
  110. .def("SetMousePosition", &Window::SetMousePosition)
  111. .def("CaptureMouse", &Window::CaptureMouse)
  112. .def("BlockPosition", &Window::BlockPosition)
  113. .def("GetXPosition", &Window::GetXPosition)
  114. .def("GetYPosition", &Window::GetYPosition)
  115. .def("SetPosition", &Window::SetPosition)
  116. .def("SetMouseVisible", &Window::SetMouseVisible)
  117.  
  118. , ///END
  119.  
  120. class_<InputHandler>("InputHandler")
  121. .def(constructor<>())
  122. .def("Update", &InputHandler::Update)
  123. .def("GetMousePosition", &InputHandler::GetMousePosition)
  124. .def("KeyDown", &InputHandler::KeyDown)
  125. .def("CloseRequested", &InputHandler::CloseRequested)
  126. ///And so on for a ~100 lines...
  127.  
  128. ];
  129.  
  130.  
  131.  
  132. }
  133.  
  134. input = InputHandler()
  135. window = Window(100, 100, 1000, 900, "Hello Gangnam Style !", false, false)
  136.  
  137. while input:CloseRequested() == false do
  138. window:Clear(0.5, 0.5, 0.5, 1)
  139. window:SwapBuffers()
  140. end
  141.  
  142. io.read()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement