forked from romeroadrian/nes.cr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_sfml.cr
98 lines (82 loc) · 2.69 KB
/
main_sfml.cr
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
require "crsfml"
require "./src/nes"
filename = ARGV.first? || abort("error: missing rom filename")
nes = Nes.new filename
window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "nes.cr")
window.vertical_sync_enabled = true
clock = SF::Clock.new
class NesDrawable
include SF::Drawable
def initialize(@nes : Nes)
end
def draw(target : SF::RenderTarget, states : SF::RenderStates)
texture = SF::Texture.new(256, 240)
sprite = SF::Sprite.new(texture)
pixels = Array(UInt8).new(256 * 240 * 4)
240.times do |y|
256.times do |x|
color = @nes.ppu.shown[x][y]
pixels << (color >> 16).to_u8
pixels << (color >> 8).to_u8
pixels << color.to_u8
pixels << 0xFF_u8
end
end
texture.update(pixels.to_unsafe)
target.draw sprite
end
end
nes_drawable = NesDrawable.new(nes)
view = SF::View.new(SF.float_rect(0, 0, 256, 240))
window.view = view
while window.open?
while event = window.poll_event
case event
when SF::Event::Closed
window.close
when SF::Event::KeyPressed
case event.code
when SF::Keyboard::Key::Z
nes.control_pad.press(ControlPad::Button::A)
when SF::Keyboard::Key::X
nes.control_pad.press(ControlPad::Button::B)
when SF::Keyboard::Key::Up
nes.control_pad.press(ControlPad::Button::Up)
when SF::Keyboard::Key::Down
nes.control_pad.press(ControlPad::Button::Down)
when SF::Keyboard::Key::Left
nes.control_pad.press(ControlPad::Button::Left)
when SF::Keyboard::Key::Right
nes.control_pad.press(ControlPad::Button::Right)
when SF::Keyboard::Key::O
nes.control_pad.press(ControlPad::Button::Start)
when SF::Keyboard::Key::P
nes.control_pad.press(ControlPad::Button::Select)
end
when SF::Event::KeyReleased
case event.code
when SF::Keyboard::Key::Z
nes.control_pad.release(ControlPad::Button::A)
when SF::Keyboard::Key::X
nes.control_pad.release(ControlPad::Button::B)
when SF::Keyboard::Key::Up
nes.control_pad.release(ControlPad::Button::Up)
when SF::Keyboard::Key::Down
nes.control_pad.release(ControlPad::Button::Down)
when SF::Keyboard::Key::Left
nes.control_pad.release(ControlPad::Button::Left)
when SF::Keyboard::Key::Right
nes.control_pad.release(ControlPad::Button::Right)
when SF::Keyboard::Key::O
nes.control_pad.release(ControlPad::Button::Start)
when SF::Keyboard::Key::P
nes.control_pad.release(ControlPad::Button::Select)
end
end
end
elapsed = clock.restart.as_milliseconds
nes.step(elapsed)
window.clear
window.draw(nes_drawable)
window.display
end