class Doomfire::SDL

Output to a separate SDL window

Constants

ARGB

Public Instance Methods

run() click to toggle source
# File lib/doomfire/sdl.rb, line 46
def run
  fire_loop
end
stop() click to toggle source
# File lib/doomfire/sdl.rb, line 50
def stop
  @exit_requested = true
end

Private Instance Methods

clear() click to toggle source
# File lib/doomfire/sdl.rb, line 113
def clear
  FFI_SDL.SDL_RenderClear(@renderer)
end
clear_screen() click to toggle source
# File lib/doomfire/sdl.rb, line 123
def clear_screen
  FFI_SDL.SDL_Quit()
end
fire_loop() click to toggle source
# File lib/doomfire/sdl.rb, line 56
def fire_loop
  loop do
    next if @event.nil? || @window.nil? || @renderer.nil? || @texture.nil?

    FFI_SDL.SDL_PollEvent(@event)
    @exit_requested = true if @event.type == FFI_SDL::SDL_QUIT

    if @exit_requested
      stop_fire if @counter.zero?
      break if @counter == 60

      @counter += 1
    end

    clear
    update_pixels
    print_pixels
  end

  clear_screen
end
prepare_output() click to toggle source
# File lib/doomfire/sdl.rb, line 89
def prepare_output
  @fire_width = 320
  @fire_height = 240

  FFI_SDL.SDL_Init(FFI_SDL::SDL_INIT_VIDEO)
  @window = FFI_SDL.SDL_CreateWindow(
    'Doomfire.rb',
    FFI_SDL::SDL_WINDOWPOS_CENTERED,
    FFI_SDL::SDL_WINDOWPOS_CENTERED,
    @fire_width,
    @fire_height,
    FFI_SDL::SDL_WINDOW_OPENGL
  )
  @renderer = FFI_SDL.SDL_CreateRenderer(@window, -1, 0)
  @texture = FFI_SDL.SDL_CreateTexture(
    @renderer,
    FFI_SDL::SDL_PIXELFORMAT_ARGB8888,
    FFI_SDL::SDL_TEXTUREACCESS_STREAMING,
    @fire_width,
    @fire_height
  )
  @event = FFI_SDL::SDL_Event.malloc
end
print_pixels() click to toggle source
update_pixels() click to toggle source
# File lib/doomfire/sdl.rb, line 78
def update_pixels
  (0..@fire_width).each do |x|
    (1...@fire_height).each do |y|
      spread_fire(y * @fire_width + x)
    end
  end

  @texdata = @pixels.map { |val| ARGB[val] }
  @texptr = Fiddle::Pointer[@texdata.pack('L*')]
end