Created
September 10, 2012 06:55
-
-
Save khayrov/3689307 to your computer and use it in GitHub Desktop.
luabind example: export Bullet vector algebra and inheriting C++ classes in Lua
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <lua.hpp> | |
#include <luabind/luabind.hpp> | |
#include <luabind/operator.hpp> | |
#include <luabind/adopt_policy.hpp> | |
#include <LinearMath/btVector3.h> | |
#include <string> | |
#include <iostream> | |
using namespace luabind; | |
namespace | |
{ | |
std::ostream &operator <<(std::ostream &os, const btVector3 &vec) | |
{ | |
os << "Vec3(" << vec.x() << ", " << vec.y() << ", " << vec.z() << ")"; | |
return os; | |
} | |
} // anonymous namespace | |
class Player | |
{ | |
btVector3 position; | |
std::string name; | |
public: | |
Player(const std::string &name) : position(0, 0, 0), name(name) | |
{ } | |
virtual ~Player() { } | |
const btVector3 &getPosition() const | |
{ | |
return position; | |
} | |
void setPosition(const btVector3 &newPos) | |
{ | |
position = newPos; | |
onPositionChange(); | |
} | |
const std::string &getName() const | |
{ | |
return name; | |
} | |
void say(const std::string &message) | |
{ | |
std::cout << name << " says: " << message << std::endl; | |
} | |
virtual void onPositionChange() | |
{ | |
} | |
}; | |
class PlayerWrapper : public Player, public wrap_base | |
{ | |
public: | |
PlayerWrapper(const std::string &name) : Player(name) | |
{ } | |
void onPositionChange() | |
{ | |
std::cout << "PlayerWrapper::onPositionChange\n"; | |
call<void>("onPositionChange"); | |
} | |
static void default_onPositionChange(Player *p) | |
{ | |
p->Player::onPositionChange(); | |
} | |
}; | |
int main() | |
{ | |
lua_State *L = luaL_newstate(); | |
luaL_openlibs(L); | |
open(L); | |
module(L) [ | |
class_<btVector3>("vec3") | |
.def(constructor<btScalar, btScalar, btScalar>()) | |
.property("x", &btVector3::getX, &btVector3::setX) | |
.property("y", &btVector3::getY, &btVector3::setY) | |
.property("z", &btVector3::getZ, &btVector3::setZ) | |
.def("length", &btVector3::length) | |
.def("length2", &btVector3::length2) | |
.def("distance", &btVector3::distance) | |
.def("distance2", &btVector3::distance2) | |
.def("normalize", &btVector3::normalize) | |
.def("normalized", &btVector3::normalized) | |
.def("dot", &btVector3::dot) | |
.def("angle", &btVector3::angle) | |
.def("rotate", &btVector3::rotate) | |
.def(-const_self) | |
.def(const_self + const_self) | |
.def(const_self - const_self) | |
.def(const_self * btScalar()) | |
.def(const_self / btScalar()) | |
.def(const_self == const_self) | |
.def(tostring(const_self)), | |
class_<Player, PlayerWrapper>("Player") | |
.def(constructor<const std::string&>()) | |
.property("name", &Player::getName) | |
.def("getPosition", &Player::getPosition) | |
.def("setPosition", &Player::setPosition) | |
.def("say", &Player::say) | |
.def("onPositionChange", &Player::onPositionChange, &PlayerWrapper::default_onPositionChange) | |
]; | |
luaL_dostring(L, | |
"function foobar()\n" | |
" local v1 = vec3(1, 2, 3)\n" | |
" v1 = v1 * 3\n" | |
" v1 = v1 + vec3(4, 5, 6)\n" | |
" v1 = -v1\n" | |
" v1 = v1:rotate(vec3(0, 0, 1), 1)\n" | |
" local x = v1:angle(vec3(1, 0, 0))\n" | |
" v1:normalize()\n" | |
" v1.x = v1.y * 2\n" | |
" local s = tostring(v1)\n" | |
" return s\n" | |
"end\n" | |
"\n" | |
"class 'MyPlayer' (Player)\n" | |
"function MyPlayer:__init(name)\n" | |
" Player.__init(self, name)\n" | |
"end\n" | |
"function MyPlayer:onPositionChange()\n" | |
" self:say('I am moving')\n" | |
"end\n" | |
"\n" | |
"function quux()\n" | |
" local p = MyPlayer('Bob')\n" | |
" p:setPosition(vec3(1, 2, 3))\n" | |
" return p\n" | |
"end\n"); | |
try | |
{ | |
Player *p = call_function<Player*>(L, "quux")[adopt(result)]; | |
std::cout << "Player is at: " << p->getPosition() << std::endl; | |
p->setPosition(btVector3(1, 0, 0)); | |
delete p; | |
} | |
catch (const luabind::error &e) | |
{ | |
object error_msg(from_stack(e.state(), -1)); | |
std::cout << "Lua error:\n" << error_msg << std::endl; | |
} | |
catch (const std::exception &e) | |
{ | |
std::cout << "Error:\n" << e.what() << std::endl; | |
} | |
lua_close(L); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CXXFLAGS=-O2 -Wall -g -fPIC $(shell pkg-config --cflags lua5.1 luabind) -I/usr/local/include/bullet | |
LDFLAGS=-lstdc++ $(shell pkg-config --libs lua5.1 luabind) -lLinearMath | |
all: luavector | |
luavector: luavector.o | |
luavector.o: luavector.cpp | |
.PHONY: clean | |
clean: | |
rm -f luavector *.o |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment