Skip to content

Instantly share code, notes, and snippets.

@asterite
Last active August 29, 2015 14:21
Show Gist options
  • Save asterite/d28e1bc4fe1a22f3dfd0 to your computer and use it in GitHub Desktop.
Save asterite/d28e1bc4fe1a22f3dfd0 to your computer and use it in GitHub Desktop.
require "crsfml"
require "./src/nes"
def color(x)
r = (x >> 16) & 0xFF
g = (x >> 8) & 0xFF
b = x & 0xFF
SF.color(r, g, b, 0xFF)
end
def handle_events(window, nes)
while event = window.poll_event()
case event.type
when SF::Event::Closed
window.close()
when SF::Event::KeyPressed
case event.key.code
when SF::Keyboard::Z
nes.control_pad.press(ControlPad::Button::A)
when SF::Keyboard::X
nes.control_pad.press(ControlPad::Button::B)
when SF::Keyboard::Up
nes.control_pad.press(ControlPad::Button::Up)
when SF::Keyboard::Down
nes.control_pad.press(ControlPad::Button::Down)
when SF::Keyboard::Left
nes.control_pad.press(ControlPad::Button::Left)
when SF::Keyboard::Right
nes.control_pad.press(ControlPad::Button::Right)
when SF::Keyboard::O
nes.control_pad.press(ControlPad::Button::Start)
when SF::Keyboard::P
nes.control_pad.press(ControlPad::Button::Select)
end
when SF::Event::KeyReleased
case event.key.code
when SF::Keyboard::Z
nes.control_pad.release(ControlPad::Button::A)
when SF::Keyboard::X
nes.control_pad.release(ControlPad::Button::B)
when SF::Keyboard::Up
nes.control_pad.release(ControlPad::Button::Up)
when SF::Keyboard::Down
nes.control_pad.release(ControlPad::Button::Down)
when SF::Keyboard::Left
nes.control_pad.release(ControlPad::Button::Left)
when SF::Keyboard::Right
nes.control_pad.release(ControlPad::Button::Right)
when SF::Keyboard::O
nes.control_pad.release(ControlPad::Button::Start)
when SF::Keyboard::P
nes.control_pad.release(ControlPad::Button::Select)
end
end
end
end
filename = ARGV.first? || abort("error: missing rom filename")
nes = Nes.new filename
p "PRG banks: #{nes.rom.prg_banks}"
p "CHR banks: #{nes.rom.chr_banks}"
p "MAPPER: #{nes.rom.mapper_number}"
window = SF::RenderWindow.new(
SF.video_mode(256, 240), "NES",
settings: SF.context_settings(depth: 32, antialiasing: 8)
)
window.vertical_sync_enabled = true
window.framerate_limit = 60
image = SF::Image.new 256, 240
texture = SF::Texture.new 256, 240
count = 0
while window.open?
count += 1
nes.step
if count % 100_000 == 0
count = 0
handle_events window, nes
256.times do |x|
240.times do |y|
image.set_pixel(x, y, color(nes.ppu.shown[x][y]))
end
end
texture.update image, 0, 0
sprite = SF::Sprite.new(texture)
window.draw sprite
window.display
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment