Saturday, July 24, 2010

Instancing

I've mostly been working on performance and internal tweaks to Sgine lately, but one of the things I've recently added that is both a performance benefit and a really "nice-to-have" for game development particularly is what I call "instancing". In a game you often have many instances of an object on the screen at any given time whether it's a wall, a character, or something else it can be really performance intensive and a waste of memory to have each instance completely represented independently of the others, particularly for complex objects. To help resolve this I've created ComponentInstance to allow creation of a Component and then re-use without all the baggage.

For example, see the following code snippet:

package org.sgine.ui

import org.sgine.core.Color
import org.sgine.core.Resource

import org.sgine.render.Debug
import org.sgine.render.StandardDisplay

object TestInstancing extends StandardDisplay with Debug {
 def setup() = {
  val image = new Image(Resource("puppies.jpg"))
  scene += image
  
  val i1 = ComponentInstance(image)
  i1.location.set(-300.0, 200.0, 5.0)
  i1.scale.set(0.4)
  i1.alpha := 0.5
  i1.color := Color.Red
  scene += i1
  
  val i2 = ComponentInstance(image)
  i2.location.set(300.0, 200.0, 5.0)
  i2.scale.set(0.4)
  i2.alpha := 0.5
  i2.color := Color.Green
  scene += i2
  
  val i3 = ComponentInstance(image)
  i3.location.set(-300.0, -200.0, 5.0)
  i3.scale.set(0.4)
  i3.alpha := 0.5
  i3.color := Color.Blue
  scene += i3
  
  val i4 = ComponentInstance(image)
  i4.location.set(300.0, -200.0, 5.0)
  i4.scale.set(0.4)
  i4.alpha := 0.5
  scene += i4
 }
}

The result of the above code is this:



This also makes it easy to modify one component and reflect it in all instances. There's a lot more functionality in the pipeline that will help increase the performance of Sgine, but unfortunately most of it is internal and not as cool to look at in a blog post. :)

No comments:

Post a Comment

Scala Engine for high-performance interactive applications.