Designing a 2D scenegraph

Sage
Posts: 1,482
Joined: 2002.09
Post: #1
Has anyone had any experience making a 2D scenegraph-like structure? I've become more and more unsatisfied with the sprite classes in my Ruby game library. Individual sprites were rendered using C code, but there was a lot of Ruby glue code holding that together. I'm wondering if a scenegraph would make more sense. If I could call a single C function to do all the drawing defined for a scene without needing to set states haphazardly and copy information over from rigid body objects, life would be good. (not to mention faster)

My idea was to build a scene out of layer objects. The most basic layer would be a static mesh layer and have it's own drawing state (projection, blending, etc) and a collection of (possibly nested) static meshes. The meshes would have properties such as color, texture, and a transformation source (such as a rigid body or sprite controller).

Static meshes would be easy enough to implement by stuffing all the geometry in a big vertex array when the layer is defined. Instances of a mesh, would simply share the same indexes, and you'd only need to set the vertex pointer once for the entire layer. Defining a scene would be as simple as defining functions for setting the layers' state, and adding meshes to the layers. Drawing would simply plow through the data.

I'm not sure what to do about dynamic meshes though as each instance will need scratch space for recalculating vertexes. My thoughts were just to use a shared scratch buffer for all meshes in a dynamic layer, or to a stack allocated array for each individual mesh and move the vertex pointer each time. Using the stack would be easier, but would it be prohibitively expensive to keep moving the vertex pointer when most of the time you are probably drawing a single quad?

Any thoughts or advice?

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #2
Is it a dumb idea altogether or is this just not an interesting topic.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 37
Joined: 2006.08
Post: #3
I've pondered this very thing recently. I am using Lua, however, so performance is very nice. I'm a rabid Ruby fanboy, but you have to admit that it is slow. Lua is about 10 times faster in a direct comparison, and has a much faster/smaller C API.

With that out of the way:

I basically have layers implemented in both C and Lua. The "static" portion of a layer is stored in a C array. The static part is exposed to Lua via get and set functions. That arrangement allows fast culling and drawing of large areas of tiles that stay the same most of the time. Then the dynamic portion (any animated tiles, characters, whatever) is handled purely in Lua. The script has a "drawLayer()" function and a "currentLayer" variable that is available during any particular method. That way you can say "if currentLayer == 'foreground' draw player etc..."

Not quite a scene graph, but close enough.
Quote this message in a reply
Sage
Posts: 1,482
Joined: 2002.09
Post: #4
Well, I've found a fairly easy to implement, high performance sprite drawing system. The C code does pretty much everything and was only 200ish lines.

As far as Ruby's speed is concerned, I'm not hurting any. I've written a couple of games that have hundreds of sprites, that run at hundreds of frames a second. I'm more physics and fillrate limited than Ruby limited.

Scott Lembcke - Howling Moon Software
Author of Chipmunk Physics - A fast and simple rigid body physics library in C.
Quote this message in a reply
Member
Posts: 37
Joined: 2006.08
Post: #5
Skorche Wrote:Well, I've found a fairly easy to implement, high performance sprite drawing system. The C code does pretty much everything and was only 200ish lines.

As far as Ruby's speed is concerned, I'm not hurting any. I've written a couple of games that have hundreds of sprites, that run at hundreds of frames a second. I'm more physics and fillrate limited than Ruby limited.

So you're not doing the scenegraph for performance reasons? Just code it to make it conveneient and go from there.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  query on Level Designing mchowdhury1983 1 2,923 Nov 20, 2011 12:08 PM
Last Post: sealfin