n2liquid's sandbox

Direct3D 9 Bindings for NodeJS — SonettoJS’s Baby Steps

Posted on: May 17, 2011

The Sonetto Project is slowly being reborn for the production of a game prototype. This time, instead of writing everything in C++ and leaving scripting for game events, the engine will be, as much as possible, scripted.

Sonetto will be the purely C++ portion, and SonettoJS will be the real juice of the engine. libsonetto is going to be wrapped by SonettoJS in a way that is easy to use from JavaScript using NodeJS: heavy calculations will be left in Sonetto; juice will be JS.

As of now, not much can be done:

SonettoJS's first polygon

var Graphics = require('./SonettoJS/Graphics').LoadBackend('Direct3D9'); 
// creates a game window var gcore = new Graphics.Core
(
  {
    window:
    {
      title: 'SonettoJS: Hi-Technical Excellent ' +
             'Taste and Flavor'
    },

    display: { resolution: [1280, 720], vsync: true }
  }
);

// clears the screen
gcore.clear([40, 140, 240]);

// creates a dynamic vertex buffer containing
// 6 vertices with diffuse coloring on gcore
var vb = new Graphics.VertexBuffer
(
    gcore, 6, { is: 'dynamic', has: 'diffuse' }
);

// writes the vertices
vb.write
(
  {
    // top triangle
    0: { position: [+0.75, +0.75], diffuse: [ 40, 140, 240] }, // T-R
    1: { position: [+0.75, -0.75], diffuse: [  0,  90, 190] }, // B-R
    2: { position: [-0.75, +0.75], diffuse: [190,   0,  90] }, // T-L

    // bottom triangle
    3: { position: [-0.75, +0.75], diffuse: [190,   0,  90] }, // T-L
    4: { position: [+0.75, -0.75], diffuse: [  0,  90, 190] }, // B-R
    5: { position: [-0.75, -0.75], diffuse: [ 40, 140, 240] }  // B-L
  }
);

// starts the rendering loop
var updateInterval = setInterval
(
  function ()
  {
    gcore.render
    (
      function (renderer)
      {
        // applies the vertex buffer to gcore
        vb.apply(gcore);

        // tells the renderer to draw the vertex buffer
        // as a list of triangles
        renderer.drawTriangleList();
      }
    );

    // updates the graphics core (window event loop, etc.)
    gcore.update();
  },

  10
);
Advertisements

3 Responses to "Direct3D 9 Bindings for NodeJS — SonettoJS’s Baby Steps"

could you explain o make a tutorial how did you make the NodeJS library to show your DirectX code inside NodeJS ?

Joyal, sorry for the late reply. For some reason I wasn’t notified via email of your comment.

Unfortunately I don’t have that source code anymore, but I didn’t do anything special to make that happen. Look up for Node / V8 C++ binding tutorials and reference material, and then just create JavaScript bindings to the DirectX functionality you want.

If I were to do this again today, however, I’d probably use a different strategy: ZeroMQ message passing. I’d just spin up a Direct3D thread or child process and communicate with it using ZeroMQ. There are good ZeroMQ libraries for both C and NodeJS, and I think they’re much easier to use than the crappy V8 / C++ bindings API.

With the communication channel established, I’d probably just use BSON for encoding message objects.

But of course, if you go that way, your messages better be few and useful. If possible, I’d use an array of commands for drawing the whole frame, and thus send only 1 message per frame. I think that would be optimal, unless your JavaScript stalls. Then you’d have a problem… Maybe you could accumulate drawing commands in an array and only flush it before the next frame or before doing some heavier computing with JavaScript.

Anyway, just some random thoughts.

Cheers!

By the way, if you’re going to run your Direct3D code in the same process as your JavaScript code, keep in mind that calling Direct3D functions from outside the main thread is known to be problematic. I’ve heard of people who tried and eventually gave up, and instead put the rendering logic on the main thread and the controlling logic on a secondary thread. It sounds weird, but that’s the approach when a certain library has problems running on secondary threads: You put them on the main thread and everything else on others.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cute bouncing pixels

un.ma.i!

Twitter (technical)

Error: Please make sure the Twitter account is public.

Twitter (personal)

Error: Twitter did not respond. Please wait a few minutes and refresh this page.

Get messaged when I post something new!
Just enter your e-mail and hit Follow:

Join 171 other followers

%d bloggers like this: