Module: Hemi::Engine

Defined in:
lib/hemi/engine.rb

Overview

Entry-level module, meant to be prepended in the main class of the game using the hemi gem.

Prepending this module allows developers to write their custom +initialize+ and +run+ methods without overriding any of the mandatory logic. Becasue of the prepend behavior, Hemi::Engine will end up first in ancestors' list.

So, technically, if Game implements its own initialize / run methods, those will be super-called from Hemi::Engine:

require "hemi"

class Game
  prepend Hemi::Engine
end

Game.ancestors
=> [Hemi::Engine, Game, Singleton, Object, PP::ObjectMixin, Kernel, BasicObject]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.debugBoolean (readonly)

Returns true if the engine is set to debugging mode.

Returns:

  • (Boolean)

    Returns true if the engine is set to debugging mode.



43
44
45
# File 'lib/hemi/engine.rb', line 43

def debug
  @debug
end

.stopBoolean (readonly)

Returns true if the engine is set to gracefully exit upon finishing this event loop.

Returns:

  • (Boolean)

    Returns true if the engine is set to gracefully exit upon finishing this event loop.



46
47
48
# File 'lib/hemi/engine.rb', line 46

def stop
  @stop
end

Instance Attribute Details

#windowSDL2::Window (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (SDL2::Window)


39
40
41
# File 'lib/hemi/engine.rb', line 39

def window
  @window
end

Class Method Details

.debug_off!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Ideally this method should NOT be called manually. It is called internally right after LoopMachine handles binding.pry in #debug? method.

Sets @debug instance variable to false. This makes sure that after opening a pry REPL session and ending it with calling exit from pry's REPL, the @debug flag is off and the program can continue operating normally.



85
86
87
# File 'lib/hemi/engine.rb', line 85

def debug_off!
  @debug = false
end

.debug_on!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Ideally this method should NOT be called manually, EventLoop calls it when it receives an event resulting in a :debug! action.

Sets @debug instance variable to true. Doing this will trigger the current LoopMachine's EventLoop to stop at the nearest binding.pry (thus, halting the game and allowing game's current state to be debugged).



73
74
75
# File 'lib/hemi/engine.rb', line 73

def debug_on!
  @debug = true
end

.prepended(klass) ⇒ Object

Ran when a class calls to be prepended by this module.

As mentioned - this module is meant to be prepended in the main game class. When prepended, this method is automatically ran, making the class a Singleton and makes sure all Hemi modules are loaded in required order.

Parameters:

  • klass (Class)


55
56
57
58
59
60
61
62
# File 'lib/hemi/engine.rb', line 55

def prepended(klass)
  klass.include Singleton

  Loader.load_tree "helpers"
  Loader.load_tree "render"
  Loader.load_tree "input"
  Loader.load_tree "event"
end

.stop!Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note:

Ideally this method should NOT be called manually, EventLoop calls it when it receives an event resulting in a :stop! action.

Sets @stop instance variable to true. This triggers a graceful exit of the entire LoopMachine's EventLoop - and, by extension, exits the program upon the next complete loop.



97
98
99
# File 'lib/hemi/engine.rb', line 97

def stop!
  @stop = true
end

Instance Method Details

#initializeObject

Initializes all required SDL modules and then proceeds to execute the constructor of the class it prepends.



32
33
34
35
# File 'lib/hemi/engine.rb', line 32

def initialize
  sdl_init
  super
end

#runObject

Starts the game engine. Takes care of all custom logic in prepended class' #run method, then initializes a program window and calls LoopMachine to start running its event loops.



105
106
107
108
109
# File 'lib/hemi/engine.rb', line 105

def run
  super if defined?(super)
  init_window
  start_loop
end