Skip to content

Instantly share code, notes, and snippets.

@ruprict
Created February 3, 2012 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruprict/1726860 to your computer and use it in GitHub Desktop.
Save ruprict/1726860 to your computer and use it in GitHub Desktop.
ROFLBALT Screen
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment