今天玩了一把腳本,玩完的感覺是真的好稀飯腳本啊。
1、編譯lua注意的總結
lua的項目默認是用在linux上用bjam編譯,這點很爽。如果要在windows上編譯,有兩種方式:
a)用vs的命令行進入lua根目錄,執行“etc\luavs.bat”即可,執行成功后會在src目錄下生成以下三個文件:
lua.exe
lua.lib
lua.dll
b)手動建立項目文件
自己新建一個靜態庫或動態庫,將根目錄下的src文件夾下所有文件拉進來,刪除lua.c\luac.c這2個目錄即可。注意需要設置編譯選項中的字符集為“使用多字節字符集”,不然lua庫運行時初始化會崩潰。
今天先用的第二個方式編譯,一切正常,但是運行的時候發現初始化lua庫的時候程序崩潰,跟蹤發現問題出在lua_lib這個函數中,不是很得要領。后來嘗試用第一種方式,才沒有崩潰。比較發現需要設置編譯選項中的字符集為“使用多字節字符集”。
luavs.bat內容如下:
cd src
cl /O2 /W3 /c /DLUA_BUILD_AS_DLL l*.c
del lua.obj luac.obj
link /DLL /out:lua51.dll l*.obj
cl /O2 /W3 /c /DLUA_BUILD_AS_DLL lua.c
link /out:lua.exe lua.obj lua51.lib
cd ..
當然手動編譯為dll的時候需要設置編譯選項“LUA_BUILD_AS_DLL ”
2.編譯luabind
編譯luabind的時候,沒耐心看官方說明文檔,直接拉到vs中編譯。當然還是看了下源文件的bjam的相關文件,沒有發現有什么特殊的地方需要注意。所以直接編譯,直接通過。
總結下,碰到這些開源項目沒有vs工程文件,編譯說明不像ogre那么全,第一感覺就是怕麻煩。其實那些開源社區的貢獻者是非常聰明的人,并不會給我添多少麻煩,如果我非常周折還是有編譯問題,說明我走彎路了,得靜下來考慮。
3.luabind、luaplus、tolua++。。。
沒有找到比較文檔,中文有說luabind需要boost所以如何如何的,被我無視掉。所以優劣再說了。luabind是官方一個wiki中介紹的。luplus是武俠使用的,tolua是cegui使用的。
4.腳本賞析
這是腳步:


-- Lua script for test program.

print( 'Hello from LUA!' )

app = getApplication()

app:setBackgroundColour( ColourValue( 0, 0, .25 ) )

camera = app:getCamera()

camera:setPosition( Vector3(0,100,500) )

camera:lookAt( Vector3(0,0,0) )

camera:setNearClipDistance( 5 )

e = app:createEntity( 'Ninja', 'ninja.mesh' )

e:setDisplaySkeleton( true )

rootNode = app:getRootNode()

child = rootNode:createChildSceneNode( 'NinjaNode' )

child:attachObject( e )

child:yaw( 180 )

child:setPosition( Vector3.ZERO )

e = app:createEntity( 'floor', 'floor' )

e:setMaterialName( 'Examples/GrassFloor' , 'Autodetect' )

child = rootNode:createChildSceneNode( 'FloorNode' )

child:attachObject( e )
這是綁定ogre函數的代碼:


void bindVector3( lua_State* L )


{
module(L)
[
class_<Vector3>( "Vector3" )
.def(tostring(self))
.def_readwrite( "x", &Vector3::x )
.def_readwrite( "y", &Vector3::y )
.def_readwrite( "z", &Vector3::z )
.def(constructor<>())
.def(constructor<Vector3&>())
.def(constructor<Real, Real, Real>())
.def("absDotProduct", &Vector3::absDotProduct)
.def("crossProduct", &Vector3::crossProduct )
.def("directionEquals", &Vector3::directionEquals )
.def("distance", &Vector3::distance )
.def("dotProduct", &Vector3::dotProduct )
.def("getRotationTo", &Vector3::getRotationTo )
.def("isZeroLength", &Vector3::isZeroLength )
.def("length", &Vector3::length )
.def("makeCeil", &Vector3::makeCeil )
.def("makeFloor", &Vector3::makeFloor )
.def("midPoint", &Vector3::midPoint )
.def("normalise", &Vector3::normalise )
.def("nornaliseCopy", &Vector3::normalisedCopy )
.def("perpendicular", &Vector3::perpendicular )
.def("positionCloses", &Vector3::positionCloses )
.def("positionEquals", &Vector3::positionEquals )
//.def("ptr", &Vector3::ptr )
.def("randomDeviant", &Vector3::randomDeviant )
.def("reflect", &Vector3::reflect )
.def("squaredDistance", &Vector3::squaredDistance )
.def("squaredLength", &Vector3::squaredLength )

// Operators

.def( self + other<Vector3>() )
.def( self - other<Vector3>() )
.def( self * other<Vector3>() )
.def( self * Real() )
];

LUA_CONST_START( Vector3 )
LUA_CONST( Vector3, ZERO);
LUA_CONST( Vector3, UNIT_X );
LUA_CONST( Vector3, UNIT_X);
LUA_CONST( Vector3, UNIT_Y);
LUA_CONST( Vector3, UNIT_Z);
LUA_CONST( Vector3, NEGATIVE_UNIT_X);
LUA_CONST( Vector3, NEGATIVE_UNIT_Y);
LUA_CONST( Vector3, NEGATIVE_UNIT_Z);
LUA_CONST( Vector3, UNIT_SCALE);
LUA_CONST_END;
}
編譯運行后的結果: