PageRenderTime 16ms CodeModel.GetById 7ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

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

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