Skip to content
Corey Powell edited this page May 28, 2015 · 3 revisions

Bootstrapping

If you want just a bit more control over moon, or you added some fancy extensions that need to be loaded, before, during or after Moon's regular bootstrapping, you may just want to write one yourself.

If you've read the Getting Started, you'll know that moon is a glorified mruby interpreter. And the repository comes with a scripts/bootstrap.rb and bin/moon-player (and its variants). moon-player is just a shell script which calls moon-mruby which loads the scripts/bootstrap.rb and it does its magic.

Well enough prep work.

Moon provides an Engine class which will initialize most of the boring and painful things.

One of the odd things about Engine is that its initialize takes a block, this block will be used as the step callback for your apps main loop (notice I used app, not game, because.. ok..).

engine = Moon::Engine.new do |e, delta|
  # e - the instance of the engine, in case you misplaced the one you created ;3
  # delta - time elapsed since the last frame
end

At this point you're saying "Well, that wasn't so hard", and this is where I say "Sure, except creating an Engine != Initialized Engine".

At this point, the engine has only been created and its step callback set, OpenGL, GLFW and all the likes haven't been setup as yet.

You'll need to manually initialize GLFW, as for the reason, if you wanted to create multiple engines (FOR SOME REASON), it would re-initialize GLFW each time, while I do believe GLFW.init does nothing on multiple calls, it leaves a bad taste in your mouth. So for the sake keeping your mouth minty fresh, YOU WILL initialize GLFW yourself, after that anything goes wrong, is all your fault™.

GLFW.init # in good favour, we call GLFW.init before doing anything

engine = Moon::Engine.new do |e, delta|
  # e - the instance of the engine, in case you misplaced the one you created ;3
  # delta - time elapsed since the last frame
end

In order to get the engine setup and its main window created, you'll need to call Engine#setup

engine.setup

This will smash a few heads, roll a few dies, and magically obtain the black arts known as an OpenGL context for you, while somehow speaking to your somewhat broken Display Server (X11 I'm looking at you).

You can now do other things, such as chasing gophers around your yard, or blowing bubbles while sitting on a mountain, before finally calling the Engine#main, at this point:

YOU HAVE FORFEITED ALL CONTROL TO MOON Given control to Moon

tl;dr You are now at the mercy of Moon.

engine.main

Don't panic, the engine will return control once its main loop exits (either by an error or by Engine#quit), note Engine#shutdown is used to destroy the window NOT stop the main loop (I'm saying this because I wrote it and went ahead and used #shutdown instead of #quit, so TAKE THIS FROM THE DEVELOPER.)

Once the engine returns your yBox controller and your somewhat broken copy of Lambda of Duty 6, you can now shutdown and drink some beer.

begin
  engine.main
ensure # ENSURE SHUTDOWN, yes, I said it
  engine.shutdown
end

After that just let mruby reclaim the Engine for crushing into fine gems for future use by other objects.

All together the basic bootstrap looks like this: (ignoring my comments)

GLFW.init

# load up your copy of Lambda of Duty 6
engine = Moon::Engine.new do |e, delta|
end

# Plug in your controller
engine.setup

# Let a rip
begin
  engine.main
ensure
  # possibly pull the controller and throw the yBox against the wall, or you got tired of all the bugs.
  engine.shutdown
end

# call it a day and go drink beer.

Now if you don't mind, I have gophers to chase...