Class: Hemi::Event::LoopMachine

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/hemi/event/loop_machine.rb

Overview

The brain of the operation. A state machine which keeps performing assigned logic, while listening for assigned events.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.currentEventLoop (readonly)

Returns The currently active "loop-state", which logic and events are being handled in the main #call loop.

Returns:

  • (EventLoop)

    The currently active "loop-state", which logic and events are being handled in the main #call loop.



47
48
49
# File 'lib/hemi/event/loop_machine.rb', line 47

def current
  @current
end

.loopsHash (readonly)

Returns A simple collection of "loop-states", with as their keys, and EventLoop instances as values.

Returns:

  • (Hash)

    A simple collection of "loop-states", with as their keys, and EventLoop instances as values.



44
45
46
# File 'lib/hemi/event/loop_machine.rb', line 44

def loops
  @loops
end

Instance Attribute Details

#eventSDL2::Event (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 the last/current SDL event being handled.

Returns:

  • (SDL2::Event)

    the last/current SDL event being handled.



12
13
14
# File 'lib/hemi/event/loop_machine.rb', line 12

def event
  @event
end

Class Method Details

.[](name) ⇒ EventLoop

Fetch a registered EventLoop from @loops collection.

Parameters:

  • name (Symbol)

Returns:



79
80
81
# File 'lib/hemi/event/loop_machine.rb', line 79

def [](name)
  @loops[name]
end

.purge!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.

Empties @loops collection and sets @current state to nil. Currently only used to ensure spec scenario isolation.



93
94
95
96
# File 'lib/hemi/event/loop_machine.rb', line 93

def purge!
  @loops   = {}
  @current = nil
end

.register(name, logic = proc {}, events = {}) ⇒ Object

Registers an EventLoop in the @loops collection with the name as its identifier. If the collection was empty before registration, the first EventLoop is automatically set as current.

Examples:

excerpt from demo/01-loop_machine_demo.rb

class LoopMachineDemo
  prepend Hemi::Engine
  LM = Hemi::Event::LoopMachine

  def run
   LM.register(:text, text_block, text_events)
   LM.register(:image, sprite_block, sprite_events)
  end
end

Parameters:

  • name (Symbol)

    The EventLoop's identifier, i.e. - a key in @loops collection.

  • logic (Proc) (defaults to: proc {})

    Proc-wrapped logic to be ran at every iteration of the loop (frame).

  • events (Hash) (defaults to: {})

    A simple collection of events and their corresponding actions.



67
68
69
70
71
72
73
# File 'lib/hemi/event/loop_machine.rb', line 67

def register(name, logic = proc {}, events = {})
  event_loop   = EventLoop.new(logic, events)
  @current     = event_loop if loops.empty?
  @loops[name] = event_loop

  event_loop
end

.switch(name) ⇒ Object

Transitions to another state, setting its EventLoop as @current. Starts performing its logic and listening to its events.



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

def switch(name)
  @current = LoopMachine[name]
end

Instance Method Details

#callObject

Note:

This method is being automatically called by Hemi::Engine's #run.

Main operation method. Performs these steps in order:

  1. Wipes the current window (by drawing a black rectangle over it).
  2. Checks if SDL picked up any events and handles them.
  3. Processes the "frame" logic: calculating, moving and drawing to framebuffer.
  4. Copies the frame's content from framebuffer to display (inside the window).
  5. Checks if Hemi::Engine.debug has been set to true; opens a pry session if so.
  6. Checks if Hemi::Engine.stop has been set to true; breaks from the loop if so.
  7. Sleeps for a hardcoded amount of time.


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/hemi/event/loop_machine.rb', line 25

def call
  loop do
    Hemi::Render::Window.wipe_screen

    handle_events while poll_event
    process_logic

    Hemi::Render::Window.renderer.present

    debug?
    break if Hemi::Engine.stop

    sleep 0.1
  end
end