lua-users home
lua-l archive

HELP NEEDED - tolua++ question

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all.
 I'm really new to lua/tolua and I need some clarification about how
exactly the integration must be made.
 In fact I want to see how usefull is tolua to integrate scripting
functionality to given classes. For that, I've build a really basic
classe in file Object.h/Object.cpp and from the header file I've create
a clean version name Object.pkg.
 
 After this I know that I must build the binded file via :
	tolua++ -o Object_Bind.cpp Object.pkg
 or
	tolua++ -o Object_Bind.cpp -H Object_Bind.h Object.pkg
 The goal of this is to allow me to build the following script :
	obj1 = Object:new()
	obj1:SetName("Name1")
	obj2 = Object:new("Name2")
	
	obj1:Display()
	obj2:Display()
	
	obj1:delete()
	obj2:delete()
 The big problem now is that I don't know what I need to include in my
main.cpp file. What I've made build correctly but as soon as I run it it
crash.
 
 I'll really appreciate all the possible help. Any pointer to tolua
exemple are really welcome.
 I've found Doris application but it's a little bit to complex to begin
for study tolua.
Really thanks.
Files are the following one :
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++
//Object.h
class Object
{
 char* szObjName;
public:
 Object();
 Object(char* szName);
 ~Object();
 char* GetName();
 void SetName(char* szName);
 void Display();
};
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++
//Object.cpp
#include "Object.h"
#include <iostream.h>
#include <string>
////////////////////////////////////////////////////////////////////////
////////////////////////////
//Constructors and destructor
Object::Object()
{
 szObjName=0x00;
 cout << "Object default constructor" << endl;
}
Object::Object(char *szName)
{
 szObjName=0x00;
 if(szName)
 {
 //Allocate the necessary space for the new object's name
 int size=strlen(szName);
 szObjName = new char[size+1];
 memset(szObjName,0x00,size+1);
 //Copy the new name in the buffer
 strncpy(szObjName, szName, size);
 }
 cout << "Object's constructor with parameter" << endl;
}
Object::~Object()
{
 if (szObjName)
 delete [] szObjName;
 cout << "Object desconstructor" << endl;
}
////////////////////////////////////////////////////////////////////////
////////////////////////////
//Acessors
char* Object::GetName()
{
 return szObjName;
}
void Object::SetName(char* sz)
{
 if(sz)
 {
 //Test if we must unallocate the current name's buffer
 if (szObjName)
	delete [] szObjName;
 
 //Allocate the necessary space for the new object's name
 int size=strlen(sz);
 szObjName = new char[size+1];
 memset(szObjName,0x00,size+1);
 //Copy the new name in the buffer
 strncpy(szObjName, sz, size);
 }
}
////////////////////////////////////////////////////////////////////////
////////////////////////////
//Method to display data
void Object::Display()
{
 cout << "Object : " << endl;
 cout << "\tname : " << szObjName << endl;
 
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++
//Main.cpp
#include <stdio.h>
#include <math.h>
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include "tolua.h"
}
#include "Object.h"
#include "Object_Bind.h"
int main(int argc, char **argv)
{
 
printf("================================================================
=========\n");
 printf("Scripting Engine using Lua - v0.02\n");
 printf("J. Buyck - 29/01/04\n");
 
printf("================================================================
=========\n");
 //Initialize Lua VM
 lua_State* lua_VM = lua_open();
 if (lua_VM == NULL)
 {
 printf("Error Initializing Lua\n");
 return -1;
 }
 //Load Lua standart modules
 lua_baselibopen(lua_VM);
 lua_iolibopen(lua_VM);
 lua_strlibopen(lua_VM);
 lua_mathlibopen(lua_VM);
 tolua_Object_open(0x00);
 //Load the Lua script
 lua_dofile(lua_VM,"./startup.lua");
 //Close the load VM
 lua_close(lua_VM);
 return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
++++++++++++++
//Makefile
LUA=/root/Travail/Scripting/Lua/lua-5.0
LUALIB=$(LUA)/lib
LUAINC=$(LUA)/include
LUABINDER=/root/Travail/Scripting/Lua/LuaBinder
LUABINDERLIB=$(LUABINDER)/lib 
LUABINDERINC=$(LUABINDER)/include
LIB=-L$(LUALIB) -L$(LUABINDERLIB)
INC=-I$(LUAINC) -I$(LUABINDERINC)
 
main: Object.o Object_Bind.o
	g++ -o main main.cpp Object.o Object_Bind.o $(LIB) $(INC) -llua
-llualib -ltolua++ -lm -Wno-deprecated
clean :
	rm -rf *~ *.o
Object.o:
	g++ -c Object.cpp
Object_Bind.o:
	g++ -c Object_Bind.cpp

AltStyle によって変換されたページ (->オリジナル) /