class Display
Graphics Display
Constants
- BLACK
- DSTD_HEIGHT
- DSTD_WIDTH
- WHITE
Attributes
event_function[RW]
framerate[RW]
renderer[RW]
window[RW]
Public Class Methods
new(grid, framerate=60)
click to toggle source
# File lib/display.rb, line 17 def initialize(grid, framerate=60) SDL2.init(SDL2::INIT_VIDEO | SDL2::INIT_TIMER | SDL2::INIT_EVENTS) @window = create_window @renderer = window.create_renderer(-1, 0) @framerate = framerate @grid = grid @grid_height = Grid::ROWS @grid_width = Grid::COLS @cell_width = DSTD_WIDTH / Grid::COLS @cell_height = DSTD_HEIGHT / Grid::ROWS end
render_grid(grid)
click to toggle source
# File lib/display.rb, line 75 def self.render_grid(grid) new(grid).render end
Public Instance Methods
event_handler()
click to toggle source
# File lib/display.rb, line 44 def event_handler while ev = SDL2::Event.poll if SDL2::Event::KeyDown === ev && ev.scancode == SDL2::Key::Scan::ESCAPE exit end end end
plot_cell(cell)
click to toggle source
# File lib/display.rb, line 64 def plot_cell(cell) @renderer.draw_color = decode_color(cell.state == Cell::ALIVE ? WHITE : BLACK) @renderer.fill_rect( SDL2::Rect.new( cell.coordinates[0] * @cell_height, cell.coordinates[1] * @cell_width, @cell_height, @cell_width ) ) end
plot_grid()
click to toggle source
# File lib/display.rb, line 52 def plot_grid @grid.map do |row| row.map do |cell| plot_cell(cell) end end end
render()
click to toggle source
# File lib/display.rb, line 31 def render loop do event_handler clear_screen plot_grid update_grid @renderer.present sleep 1.0 / @framerate end end
update_grid()
click to toggle source
# File lib/display.rb, line 60 def update_grid @grid.inc_gen end
Private Instance Methods
clear_screen()
click to toggle source
# File lib/display.rb, line 81 def clear_screen @renderer.draw_color = decode_color(BLACK) @renderer.clear end
create_window()
click to toggle source
# File lib/display.rb, line 92 def create_window SDL2::Window.create( "Conway's Game of Life", SDL2::Window::POS_CENTERED, SDL2::Window::POS_CENTERED, DSTD_WIDTH, DSTD_HEIGHT, 0 ) end
decode_color(hex_string)
click to toggle source
# File lib/display.rb, line 86 def decode_color(hex_string) hex_string.chars.map do |color_code| (color_code.to_i(16) / 15) * 255 end end