Jump to content
  • Advertisement
Sign in to follow this  
quarnster

Binder code generation using libclang + a preprocessor for c++ syntactic sugar

This topic is 2236 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Writing binder code is a chore and I figured it wouldn't be too hard to use libclang + a python script to automatically generate them. In the end I spent far, far, faaaar more time on the python script than what it would have taken to write the binding code manually, but now it's at a state where it is actually useful in my code base. Configuration is done via a json script with lots of options for regular expression matching.

The very simple preprocessor, unrelated to the above allows you to use -> object dereferencing and * pointers, allowing more code to be copy and pasted between your c++ code base and AngelScript scripts and set typedefs for types that aren't AngelScript builtins. This also lets you tell your editor that your AngelScript files are c++ files so you get code completion, error checking etc.

As an example the following is all AngelScript code for a simple test in my engine before it goes through the preprocessor:
#include "System/Engine.h"
#include "Effect/MarchingCubes.h"
#include "Effect/MetaSphere.h"
#include "Physics/PhysicsWorld.h"
#include "Renderer/Renderer.h"
#include "Renderer/GfxDevice.h"
#include "Physics/RigidBody.h"
#include "Physics/RigidBodyNode.h"
#include "3D/Entity.h"
void mainscript(Engine* engine, RenderNode* n)
{
PhysicsWorld* w = engine->GetPhysicsWorld();
Shape* sphere = w->CreateSphereShape(1.0);
Shape* plane = w->CreateStaticPlaneShape(Vector3(0,1,0), 1);
RigidBody* sphereBody = w->CreateRigidBody(1.0, sphere);
sphereBody->SetTransform(Transform(Quaternion(0,0,0,1), Vector3(0, 50, 0)));
RigidBody* planeBody = w->CreateRigidBody(0.0, plane);
planeBody->SetTransform(Transform(Quaternion(0,0,0,1), Vector3(0, -1, 0)));
n->SetTransformIndex(sphereBody->GetRigidBodyNode()->GetTransformIndex());
w->AddRigidBody(sphereBody);
w->AddRigidBody(planeBody);
MetaSphere ms;
MarchingCubes mc(&ms, 0.7f);
Camera camera;
camera.SetLocalTransform(Transform(Quaternion(0, 0, 0, 1), Vector3(0, 0, 40)));
GfxDevice* gfx = engine->GetGfxDevice();
Renderer* rend = gfx->GetRenderer();
float add = 3.0/60.0f;
float time = 0;
while (gfx->IsOpen())
{
time += add;
//LogDebug("Render");
ms.Update(time);
mc.Lock();
for (int i = 0; i < ms.GetCount(); i++)
{
// TODO: this starts walking from inside the center of the sphere.
// As we only care about the surface, it'd be better if we could
// start at a surface cube rather than having to scan all cubes
// from the center of the sphere out to the surface.
Vector3 pos = ms.GetCenter(i);
mc.WalkVoxel(pos.x(),pos.y(),pos.z());
}
mc.Unlock();
mc.Update();
n->SetMesh(mc.GetMesh());
engine->Update();
engine->PrepareRendering(&camera);
rend->Render(n);
engine->FinishRendering();
gfx->SwapBuffers();
}
sphere->DelRef();
plane->DelRef();
sphereBody->DelRef();
planeBody->DelRef();
}


Use if you want, don't if you don't cool.png

Share this post


Link to post
Share on other sites
Advertisement
Nice! Just what I need: Angelscript code looking extremely similar to C++! Just gave a quick look at the preprocessor... you´re basically performing a "search and replace" so "->" goes "." and " * " turns to "@", right? In other words, pointers end up being (safe) references? Haven´t tried it yet, but just came to my mind: What happens if I want to print some text that includes "->" and/or " * "? Will the preprocessor change those to "." and "@"?

I´ve also noticed I´ll need Boost to use this, right? Edited by Tzarls

Share this post


Link to post
Share on other sites
It's a regular expression search and replace pretty much, and I don't intend to do anything other than that with it. I consider it to be public domain so do with it what you want. "->" is directly replaced as is, * is doing a little bit more clever stuff to avoid replacing legitimate uses of it (such as a multiply operation). Boost is used for the regex capabilities and the foreach statement.

Share this post


Link to post
Share on other sites
It would be nice to have "new" and "delete" now that we have "->" and " * "... ;)

I´ll see if I can add those myself, as soon as i can get rid of one nasty bug that´s been crashing my code for the last few days.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!