Class: RbSDL2::Surface::PixelFormat

Inherits:
Object
  • Object
show all
Includes:
PixelFormatEnum
Defined in:
lib/rb_sdl2/surface/pixel_format.rb

Defined Under Namespace

Classes: PixelFormatPointer

Constant Summary

Constants included from PixelFormatEnum

PixelFormatEnum::FORMAT_MAP, PixelFormatEnum::INDEXED_TYPES, PixelFormatEnum::WITH_ALPHA

Instance Method Summary collapse

Methods included from PixelFormatEnum

#format_name, #fourcc, #fourcc?, #indexed_color?, #rgb?, #rgba?, to_name, to_num

Constructor Details

#initialize(ptr) ⇒ PixelFormat

Returns a new instance of PixelFormat.



19
20
21
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 19

def initialize(ptr)
  @st = ::SDL::PixelFormat.new(PixelFormatPointer.to_ptr(ptr))
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 23

def ==(other)
  other.respond_to?(:to_ptr) && other.to_ptr == to_ptr
end

#a_maskObject



27
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 27

def a_mask = @st[:Amask]

#a_mask?Boolean

Returns:

  • (Boolean)


35
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 35

def a_mask? = @st[:Amask] > 0

#b_maskObject



29
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 29

def b_mask = @st[:Bmask]

#bits_per_pixelObject Also known as: bpp



37
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 37

def bits_per_pixel = @st[:BitsPerPixel]

#bytes_per_pixelObject



40
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 40

def bytes_per_pixel = @st[:BytesPerPixel]

#formatObject



42
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 42

def format = @st[:format]

#g_maskObject



31
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 31

def g_mask = @st[:Gmask]

#pack_color(color) ⇒ Object

indexed format のときはパレット番号を戻す。



48
49
50
51
52
53
54
55
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 48

def pack_color(color)
  r, g, b, a = color
  if a_mask?
    ::SDL.MapRGBA(self, r, g, b, a || ::SDL::ALPHA_OPAQUE)
  else
    ::SDL.MapRGB(self, r, g, b)
  end
end

#paletteObject

パレットがある場合は Palette インスタンスを戻します。ない場合は nil を戻します。



60
61
62
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 60

def palette
  Palette.to_ptr(@st[:palette]) unless @st[:palette].null?
end

#palette=(pal) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 64

def palette=(pal)
  # SDL_SetPixelFormatPalette() はパレット・ポインターが NULL かどうかチェックしていない。
  if Palette === pal && !pal.to_ptr.null?
    err = ::SDL.SetPixelFormatPalette(self, pal)
    raise RbSDL2Error if err < 0
  else
    raise ArgumentError, "pointer is NULL"
  end
end

#palette?Boolean

Returns:

  • (Boolean)


74
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 74

def palette? = !@st[:palette].null?

#r_maskObject



33
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 33

def r_mask = @st[:Rmask]

#to_ptrObject



76
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 76

def to_ptr = @st.to_ptr

#unpack_pixel(num) ⇒ Object

indexed format のときはパレット番号を引数へ与える。



79
80
81
82
83
84
85
86
87
# File 'lib/rb_sdl2/surface/pixel_format.rb', line 79

def unpack_pixel(num)
  color = Array.new(a_mask? ? 4 : 3) { ::FFI::MemoryPointer.new(:uint8) }
  if a_mask?
    ::SDL.GetRGBA(num, self, *color)
  else
    ::SDL.GetRGB(num, self, *color)
  end
  color.map(&:read_uint8)
end