public
Created

ROFLBALT Screen

  • Download Gist
screen.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
class Screen
OFFSET = -20
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
attr_reader :width, :height, :world
def create_frame_buffer
@fb = Framebuffer.new @background
end
def draw renderable
renderable.each_pixel(world.ticks) do |x, y, pixel|
@fb.set x, y, pixel
end
end
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
def on_exit
print "\033[0m" # reset colours
print "\x1B[?25h" # re-enable cursor
print "\n"
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.