diff options
author | Jakob Odersky <jodersky@gmail.com> | 2009-11-05 21:02:40 +0000 |
---|---|---|
committer | Jakob Odersky <jodersky@gmail.com> | 2009-11-05 21:02:40 +0000 |
commit | 9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70 (patch) | |
tree | 4e97fadc391b310ee1cc7156fda590dff414b2c3 /src/graphyx/actors | |
parent | 034bc5930ea6f01745f64a6070711d899d2c27ae (diff) | |
download | sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.tar.gz sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.tar.bz2 sims-9d20024aa35cd7f923ebfc1ed9a2ffbf2731da70.zip |
Initial import.
Diffstat (limited to 'src/graphyx/actors')
-rw-r--r-- | src/graphyx/actors/GUIActor.scala | 32 | ||||
-rw-r--r-- | src/graphyx/actors/PhysicsActor.scala | 72 |
2 files changed, 104 insertions, 0 deletions
diff --git a/src/graphyx/actors/GUIActor.scala b/src/graphyx/actors/GUIActor.scala new file mode 100644 index 0000000..a704f6a --- /dev/null +++ b/src/graphyx/actors/GUIActor.scala @@ -0,0 +1,32 @@ +/* + * Graphyx + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package graphyx.actors + +import graphyx.graphics._ +import graphyx.gui._ +import scala.actors._ + +class GUIActor extends Actor{ + val container = new Container + + var continue = true + + def act() = { + container.show() + println("GUI actor started.") + while (continue) { + receive { + case Exit => { + continue = false + } + case s @ Scene(_) => container.update(s) + case other => println("Engine received unknown command: " + other) + } + } + println("GUI actor exited.") + } +} diff --git a/src/graphyx/actors/PhysicsActor.scala b/src/graphyx/actors/PhysicsActor.scala new file mode 100644 index 0000000..b93541f --- /dev/null +++ b/src/graphyx/actors/PhysicsActor.scala @@ -0,0 +1,72 @@ +/* + * Graphyx + * copyright (c) 2009 Jakob Odersky + * made available under the MIT License +*/ + +package graphyx.actors + +import graphyx.graphics._ +import sims.dynamics._ +import scala.actors._ +class PhysicsActor extends Actor{ + var world = new World + + var continue = true + var simulate = false + + private var _fps = 0 + def fps = _fps + + def act{ + println("Physics actor started.") + while (continue) { + + var t0 = System.nanoTime + + if (simulate) { + world.step() + } + + Graphyx.guiActor ! new Scene(world) {override val fps = _fps} + + while (mailboxSize > 0) { + receive { + case Stop => { + simulate = false + println("Simulation stopped.") + } + case Start => { + simulate = true + println("Simulation started.") + } + case Exit => { + continue = false + } + case sw @ SetWorld(w: World) => world = w + + case FireEvent => Graphyx.test.fireEvent() + + case other => println("Engine received unknown command: '" + other + "'") + } + } + + val h = (System.nanoTime - t0) / 1000000 + val f = 60 + val T = (1.0/f) * 1000 + if (T-h > 0) + Thread.sleep((T-h).toLong) + _fps = (1.0/((System.nanoTime - t0) / 1000000000.0)).toInt + //println((1.0/((System.nanoTime - t0) / 1000000000.0)).toInt) + } + println("Physics actor exited.") + } +} + +case object Start +case object Stop +case object Exit +case class SetWorld(world: World) +case object FireEvent + + |