Class: RbSDL2::KeyboardState

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/rb_sdl2/keyboard/keyboard_state.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeKeyboardState

Returns a new instance of KeyboardState.

Raises:



6
7
8
9
10
11
12
13
14
15
# File 'lib/rb_sdl2/keyboard/keyboard_state.rb', line 6

def initialize
  num_keys = ::FFI::MemoryPointer.new(:int)
  # SDL_GetKeyboardState が戻すポインターは SDL がロードされた時点でメモリー確保している。

  # 戻されたポインターは不変と考えてよい。

  # SDL_GetKeyboardState は SDL_init より前に呼ぶことができる。

  # SDL_GetKeyboardState は引数に NULL ポインターを与えた場合にエラーを戻す。

  @ptr = ::SDL2.SDL_GetKeyboardState(num_keys)
  raise RbSDL2Error if @ptr.null?
  @size = num_keys.read_int
end

Instance Attribute Details

#sizeObject (readonly) Also known as: length

Returns the value of attribute size.



29
30
31
# File 'lib/rb_sdl2/keyboard/keyboard_state.rb', line 29

def size
  @size
end

Instance Method Details

#[](nth) ⇒ Object

nth のキーが押されている場合、nth を戻す。nth のキーが押されていない、nth が範囲外の場合は nil を戻す。真偽値を戻すようにしなかったのは、このメソッドを応用したコードを書く際に index 情報を不要にするためである。戻り値が nil | obj なのは Numeric#nonzero? を参考にした。(この戻り値は Ruby において真偽値と同等である)



21
22
23
24
25
# File 'lib/rb_sdl2/keyboard/keyboard_state.rb', line 21

def [](nth)
  if 0 <= nth && nth < size && @ptr[nth].read_uint8 == ::SDL2::SDL_PRESSED
    nth
  end
end

#eachObject



27
# File 'lib/rb_sdl2/keyboard/keyboard_state.rb', line 27

def each = block_given? ? size.times { |i| yield(self[i]) } : to_enum

#to_strObject



32
# File 'lib/rb_sdl2/keyboard/keyboard_state.rb', line 32

def to_str = @ptr.read_bytes(size)