PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/embed3/embed3.cpp

#
C++ | 146 lines | 93 code | 15 blank | 38 comment | 11 complexity | 109208134cdb78200714fedc14e9902e MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* embed3.cpp A C++ embeded interpreter
  2. This will register a C++ class with Lua, and then call a Lua function
  3. passing C++ objects to this function.
  4. */
  5. /* Deal with Microsoft's attempt at deprecating C standard runtime functions */
  6. #if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
  7. # define _CRT_SECURE_NO_DEPRECATE
  8. #endif
  9. /* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
  10. #if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
  11. # define _SCL_SECURE_NO_DEPRECATE
  12. #endif
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. extern "C" {
  18. #include <lua.h>
  19. #include <lauxlib.h>
  20. #include <lualib.h>
  21. }
  22. /* The SWIG external runtime is generated by using.
  23. swig -lua -externalruntime swigluarun.h
  24. It contains useful function used by SWIG in its wrappering
  25. SWIG_TypeQuery() SWIG_NewPointerObj()
  26. */
  27. #include "swigluarun.h" // the SWIG external runtime
  28. /* the SWIG wrappered library */
  29. extern "C" int luaopen_example(lua_State*L);
  30. // the code itself
  31. #include "example.h"
  32. // this code pushes a C++ pointer as well as the SWIG type onto the Lua stack
  33. bool push_pointer(lua_State*L, void* ptr, const char* type_name, int owned = 0) {
  34. // task 1: get the object 'type' which is registered with SWIG
  35. // you need to call SWIG_TypeQuery() with the class name
  36. // (normally, just look in the wrapper file to get this)
  37. swig_type_info * pTypeInfo = SWIG_TypeQuery(L, type_name);
  38. if (pTypeInfo == 0)
  39. return false; // error
  40. // task 2: push the pointer to the Lua stack
  41. // this requires a pointer & the type
  42. // the last param specifies if Lua is responsible for deleting the object
  43. SWIG_NewPointerObj(L, ptr, pTypeInfo, owned);
  44. return true;
  45. }
  46. /* This is an example of how to call the Lua function
  47. void onEvent(Event e)
  48. its very tedious, but gives you an idea of the issues involed.
  49. */
  50. int call_onEvent(lua_State *L, Event e) {
  51. int top;
  52. /* ok, here we go:
  53. push a, push b, call 'add' check & return res
  54. */
  55. top = lua_gettop(L); /* for later */
  56. lua_pushstring(L, "onEvent"); /* function name */
  57. lua_gettable(L, LUA_GLOBALSINDEX); /* function to be called */
  58. if (!lua_isfunction(L, -1)) {
  59. printf("[C++] error: cannot find function 'OnEvent'\n");
  60. lua_settop(L, top); // reset
  61. return 0;
  62. }
  63. // push the event object
  64. push_pointer(L, &e, "Event *", 0);
  65. if (lua_pcall(L, 1, 0, 0) != 0) /* call function with 1 arguments and no result */
  66. {
  67. printf("[C++] error running function `OnEvent': %s\n", lua_tostring(L, -1));
  68. lua_settop(L, top); // reset
  69. return 0;
  70. }
  71. lua_settop(L, top); /* reset stack */
  72. return 1; // ok
  73. }
  74. int main(int argc, char* argv[]) {
  75. printf("[C++] Welcome to the simple embedded Lua example v3\n");
  76. printf("[C++] We are in C++\n");
  77. printf("[C++] opening a Lua state & loading the libraries\n");
  78. lua_State *L = lua_open();
  79. luaopen_base(L);
  80. luaopen_string(L);
  81. luaopen_math(L);
  82. printf("[C++] now loading the SWIG wrappered library\n");
  83. luaopen_example(L);
  84. printf("[C++] all looks ok\n");
  85. printf("\n");
  86. printf("[C++] lets create an Engine and pass a pointer to Lua\n");
  87. Engine engine;
  88. /* this code will pass a pointer into lua, but C++ still owns the object
  89. this is a little tedious, to do, but lets do it
  90. we need to pass the pointer (obviously), the type name
  91. and a flag which states if Lua should delete the pointer once its finished with it
  92. The type name is a class name string which is registered with SWIG
  93. (normally, just look in the wrapper file to get this)
  94. in this case we don't want Lua to delete the pointer so the ownership flag is 0
  95. */
  96. push_pointer(L,&engine,"Engine *",0);
  97. lua_setglobal(L, "pEngine"); // set as global variable
  98. printf("[C++] now lets load the file 'runme.lua'\n");
  99. printf("[C++] any lua code in this file will be executed\n");
  100. if (luaL_loadfile(L, "runme.lua") || lua_pcall(L, 0, 0, 0)) {
  101. printf("[C++] ERROR: cannot run lua file: %s", lua_tostring(L, -1));
  102. exit(3);
  103. }
  104. printf("[C++] We are now back in C++, all looks ok\n");
  105. printf("\n");
  106. printf("[C++] Lets call the Lua function onEvent(e)\n");
  107. printf("[C++] We will give it different events, as we wish\n");
  108. printf("[C++] Starting with STARTUP\n");
  109. Event ev;
  110. ev.mType = Event::STARTUP;
  111. call_onEvent(L, ev);
  112. printf("[C++] ok\n");
  113. printf("[C++] now we will try MOUSEPRESS,KEYPRESS,MOUSEPRESS\n");
  114. ev.mType = Event::MOUSEPRESS;
  115. call_onEvent(L, ev);
  116. ev.mType = Event::KEYPRESS;
  117. call_onEvent(L, ev);
  118. ev.mType = Event::MOUSEPRESS;
  119. call_onEvent(L, ev);
  120. printf("[C++] ok\n");
  121. printf("[C++] Finally we will SHUTDOWN\n");
  122. ev.mType = Event::SHUTDOWN;
  123. call_onEvent(L, ev);
  124. printf("[C++] ok\n");
  125. printf("\n");
  126. printf("[C++] all finished, closing the lua state\n");
  127. lua_close(L);
  128. return 0;
  129. }