Class: Screen
- Inherits:
-
Object
- Object
- Screen
- Defined in:
- lib/roflbalt.rb
Constant Summary collapse
- OFFSET =
-20
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
-
#world ⇒ Object
readonly
Returns the value of attribute world.
Instance Method Summary collapse
- #create_frame_buffer ⇒ Object
- #draw(renderable) ⇒ Object
-
#initialize(width, height, world) ⇒ Screen
constructor
A new instance of Screen.
- #on_exit ⇒ Object
- #render(start_time) ⇒ Object
Constructor Details
#initialize(width, height, world) ⇒ Screen
Returns a new instance of Screen.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/roflbalt.rb', line 45 def initialize width, height, world @width = width @height = height @world = world @background = world.background create_frame_buffer %x{stty -icanon -echo} print "\033[0m" # reset print "\033[2J" # clear screen print "\x1B[?25l" # disable cursor end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
56 57 58 |
# File 'lib/roflbalt.rb', line 56 def height @height end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
56 57 58 |
# File 'lib/roflbalt.rb', line 56 def width @width end |
#world ⇒ Object (readonly)
Returns the value of attribute world.
56 57 58 |
# File 'lib/roflbalt.rb', line 56 def world @world end |
Instance Method Details
#create_frame_buffer ⇒ Object
57 58 59 |
# File 'lib/roflbalt.rb', line 57 def create_frame_buffer @fb = Framebuffer.new @background end |
#draw(renderable) ⇒ Object
60 61 62 63 64 |
# File 'lib/roflbalt.rb', line 60 def draw renderable renderable.each_pixel(world.ticks) do |x, y, pixel| @fb.set x, y, pixel end end |
#on_exit ⇒ Object
90 91 92 93 94 |
# File 'lib/roflbalt.rb', line 90 def on_exit print "\033[0m" # reset colours print "\x1B[?25h" # re-enable cursor print "\n" end |
#render(start_time) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/roflbalt.rb', line 65 def render start_time print "\e[H" buffer = '' previous_pixel = nil (0...height).each do |y| (OFFSET...(width + OFFSET)).each do |x| pixel = @fb.get(x, y) if Pixel === previous_pixel && Pixel === pixel && pixel.color_equal?(previous_pixel) buffer << pixel.char else buffer << pixel.to_s end previous_pixel = pixel end buffer << "\n" end print "\033[0m" dt = Time.new.to_f - start_time; target_time = 0.04 sleep target_time - dt if dt < target_time print buffer create_frame_buffer end |