Class: Amaterasu::HAL::SDL2

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/hal/sdl2.rb,
lib/amaterasu/hal/sdl2/bindings.rb

Defined Under Namespace

Modules: Bindings

Constant Summary collapse

LCD =
Bindings
LCD_WIDTH =
160
LCD_HEIGHT =
144
SCALE =
3
PALETTE =
[0xFFFFFFFF, 0xFFAAAAAA, 0xFF555555, 0xFF000000].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSDL2

Returns a new instance of SDL2.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/amaterasu/hal/sdl2.rb', line 17

def initialize
  LCD.init(LCD::INIT_VIDEO)
  @window = LCD.create_window(
    'Amaterasu',
    LCD::WINDOWPOS_CENTERED,
    LCD::WINDOWPOS_CENTERED,
    LCD_WIDTH * SCALE,
    LCD_HEIGHT * SCALE,
    LCD::WINDOW_SHOWN
  )
  @renderer = LCD.create_renderer(@window, -1, LCD::RENDERER_ACCELERATED)
  @texture = LCD.create_texture(
    @renderer,
    LCD::PIXELFORMAT_ARGB8888,
    LCD::TEXTUREACCESS_STREAMING,
    LCD_WIDTH,
    LCD_HEIGHT
  )

  @pixel_buffer = FFI::MemoryPointer.new(:uint32, LCD_WIDTH * LCD_HEIGHT)
  @event = FFI::MemoryPointer.new(:uint8, LCD::EVENT_SIZE)
  @frame_count = 0
  @fps_timer = Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

Instance Attribute Details

#joypadObject

Returns the value of attribute joypad.



15
16
17
# File 'lib/amaterasu/hal/sdl2.rb', line 15

def joypad
  @joypad
end

Instance Method Details

#draw(framebuffer) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/amaterasu/hal/sdl2.rb', line 42

def draw(framebuffer)
  @pixel_buffer.write_array_of_uint32(framebuffer.map { |shade| PALETTE[shade] })
  LCD.update_texture(@texture, FFI::Pointer::NULL, @pixel_buffer, LCD_WIDTH * 4)
  LCD.render_copy(@renderer, @texture, FFI::Pointer::NULL, FFI::Pointer::NULL)
  LCD.render_present(@renderer)

  @frame_count += 1
  @elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - @fps_timer

  if @elapsed >= 1.0
    fps = @frame_count / @elapsed
    # LCD.set_window_title(@window, "Amaterasu | FPS: #{fps.round(2)}")
    $stdout.print "\rFPS: #{fps.round(2)}  "
    $stdout.flush
  end

  while LCD.poll_event(@event) == 1
    # first 4 bytes of the event struct are the event type (uint32)
    type = @event.read_uint32
    case type
    when LCD::QUIT
      shutdown
      exit
    end
  end

  keyboard_state = LCD.get_keyboard_state(nil)

  if keyboard_state.get_uint8(LCD::SCANCODE_UP) == 0
    joypad.release_dpad(:up)
  else
    joypad.press_dpad(:up)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_DOWN) == 0
    joypad.release_dpad(:down)
  else
    joypad.press_dpad(:down)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_RIGHT) == 0
    joypad.release_dpad(:right)
  else
    joypad.press_dpad(:right)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_LEFT) == 0
    joypad.release_dpad(:left)
  else
    joypad.press_dpad(:left)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_Z) == 0
    joypad.release_face(:a)
  else
    joypad.press_face(:a)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_X) == 0
    joypad.release_face(:b)
  else
    joypad.press_face(:b)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_RETURN) == 0
    joypad.release_face(:start)
  else
    joypad.press_face(:start)
  end

  if keyboard_state.get_uint8(LCD::SCANCODE_RSHIFT) == 0
    joypad.release_face(:select)
  else
    joypad.press_face(:select)
  end
end

#shutdownObject



119
120
121
122
123
124
# File 'lib/amaterasu/hal/sdl2.rb', line 119

def shutdown
  LCD.destroy_texture(@texture)
  LCD.destroy_renderer(@renderer)
  LCD.destroy_window(@window)
  LCD.quit
end