Class: Hemi::Event::LoopMachine
- Inherits:
-
Object
- Object
- Hemi::Event::LoopMachine
- 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
-
.current ⇒ EventLoop
readonly
The currently active "loop-state", which logic and events are being handled in the main
#call
loop. -
.loops ⇒ Hash
readonly
A simple collection of "loop-states", with as their keys, and EventLoop instances as values.
Instance Attribute Summary collapse
-
#event ⇒ SDL2::Event
readonly
private
The last/current SDL event being handled.
Class Method Summary collapse
-
.[](name) ⇒ EventLoop
Fetch a registered EventLoop from
@loops
collection. -
.purge! ⇒ Object
private
Empties
@loops
collection and sets@current
state to nil. -
.register(name, logic = proc {}, events = {}) ⇒ Object
Registers an EventLoop in the
@loops
collection with thename
as its identifier. -
.switch(name) ⇒ Object
Transitions to another state, setting its EventLoop as
@current
.
Instance Method Summary collapse
-
#call ⇒ Object
Main operation method.
Class Attribute Details
.current ⇒ EventLoop (readonly)
Returns 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 |
.loops ⇒ Hash (readonly)
Returns 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
#event ⇒ SDL2::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.
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.
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
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
#call ⇒ Object
This method is being automatically called by Hemi::Engine's #run
.
Main operation method. Performs these steps in order:
- Wipes the current window (by drawing a black rectangle over it).
- Checks if SDL picked up any events and handles them.
- Processes the "frame" logic: calculating, moving and drawing to framebuffer.
- Copies the frame's content from framebuffer to display (inside the window).
- Checks if
Hemi::Engine.debug
has been set totrue
; opens apry
session if so. - Checks if
Hemi::Engine.stop
has been set totrue
; breaks from the loop if so. - 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 |