sdl2_ffi 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,15 +1,73 @@
1
1
  require 'sdl2'
2
2
 
3
3
  module SDL2
4
+
4
5
  #SDL_pixels.h:261~267
5
- class Palette < FFI::Struct
6
+ class Palette < Struct
6
7
  layout :ncolors, :int,
7
- :colors, :pointer,
8
- :version, :uint32,
9
- :refcount, :int
10
-
8
+ :colors, :pointer,
9
+ :version, :uint32,
10
+ :refcount, :int
11
+
12
+ def self.create(num_colors)
13
+ SDL2.alloc_palette!(num_colors)
14
+ end
15
+
11
16
  def self.release(pointer)
12
17
  SDL2.free_palette(pointer)
13
- end
18
+ end
19
+
20
+ # \brief Set a range of colors in a palette
21
+ # \param colors: An array of colors to copy into the palette
22
+ # - Can be a FFI::Pointer, which is passed directly to the routine.
23
+ # - Can be a ruby array, an FFI::Pointer array will be created and values
24
+ # copied:
25
+ # - Elements that are arrays are converted to SDL_Color in the FFI::Pointer
26
+ # array.
27
+ # - Elements that are Color structs are copied into the array.
28
+ # \param firstcolor: The index of the first palette entry to modify.
29
+ # \param ncolors: The number of entries to modify
30
+ # - This defaults to the length of colors if colors is a Ruby Array
31
+ def set_colors(colors, firstcolor = 0, ncolors = nil)
32
+
33
+ if colors.kind_of? Array
34
+
35
+ ncolors = colors.count if ncolors.nil?
36
+
37
+ raise "(ncolors = #{ncolors} > colors.count #{colors.count}" if ncolors > colors.count
38
+
39
+ # We must build our on colors pointer:
40
+ colors_ptr = FFI::MemoryPointer.new(Color, ncolors)
41
+
42
+ colors.each_with_index() { |color, idx|
43
+
44
+ puts "color##{idx} = #{color.inspect}"
45
+
46
+ if idx < ncolors # Skip when the array count is more than ncolors
47
+
48
+ color_ptr = Color.new(colors_ptr[idx])
49
+
50
+ if color.kind_of? Array
51
+ color_ptr.set(*color)
52
+ elsif color.kind_of? Color
53
+ color_ptr.copy_from(color)
54
+ else
55
+ raise "Invalid Array Member"
56
+ end#if color.kind_of?
57
+
58
+ end
59
+
60
+ }#colors.each_with_index
61
+
62
+ colors = colors_ptr # Replace the passed in argument with our pointer
63
+
64
+ elsif colors.kind_of? FFI::Pointer
65
+ raise "ncolors must be specified" if ncolors.nil?
66
+ # Then we can just continue using it as is below.
67
+ end# if
68
+
69
+ SDL2.set_palette_colors(self, colors, firstcolor, ncolors)
70
+
71
+ end
14
72
  end
15
73
  end
@@ -2,27 +2,115 @@ require 'sdl2'
2
2
  require 'sdl2/palette'
3
3
 
4
4
  module SDL2
5
- #SDL_pixels.h:272~293
5
+
6
+ # SDL_pixels.h:272~293:typdef struct SDL_PixelFormat
7
+ # \note Everything in the pixel format structure is read-only.
6
8
  class PixelFormat < FFI::Struct
9
+
10
+ private :[] # see note
11
+
7
12
  layout :format, :pixel_format,
8
- :palette, Palette.by_ref,
9
- :bits_per_pixel, :uint8,
10
- :bytes_per_pixel, :uint8,
11
- :padding, [:uint8, 2],
12
- :r_mask, :uint32,
13
- :g_mask, :uint32,
14
- :b_mask, :uint32,
15
- :a_mask, :uint32,
16
- :r_loss, :uint8,
17
- :g_loss, :uint8,
18
- :b_loss, :uint8,
19
- :a_loss, :uint8,
20
- :refcount, :int,
21
- :next, PixelFormat.by_ref
22
-
13
+ :palette, Palette.by_ref,
14
+ :bits_per_pixel, :uint8,
15
+ :bytes_per_pixel, :uint8,
16
+ :padding, [:uint8, 2],
17
+ :r_mask, :uint32,
18
+ :g_mask, :uint32,
19
+ :b_mask, :uint32,
20
+ :a_mask, :uint32,
21
+ :r_loss, :uint8,
22
+ :g_loss, :uint8,
23
+ :b_loss, :uint8,
24
+ :a_loss, :uint8,
25
+ :refcount, :int,
26
+ :next, PixelFormat.by_ref
27
+
28
+ # Because the struct is read-only
29
+ [:format, :palette, :bytes_per_pixel, :bits_per_pixel,
30
+ :padding, :r_mask, :g_mask, :b_mask, :a_mask,
31
+ :r_loss, :g_loss, :b_loss, :refcount, :next].each do |field|
32
+ define_method field do
33
+ self[field]
34
+ end
35
+ end
36
+
37
+ # Get the human readable name of a pixel format
38
+ def self.get_name(pixel_format)
39
+ SDL2.get_pixel_format_name(pixel_format)
40
+ end
41
+
42
+ # Get the human readable name of this pixel format
43
+ def get_name
44
+ self.class.get_name self.format
45
+ end
46
+
47
+ # Convert enumerated pixel value format to bpp & RGBA mask values
48
+ # TODO: Review why this is needed? Why not just read the Struct fields?
49
+ def self.to_masks(format)
50
+ p = Hash.new(){UInt32Struct.new}
51
+ p[:bpp] = IntStruct.new
52
+ SDL2.pixel_format_enum_to_masks(format, p[:bpp], p[:Rmask], p[:Bmask],p[:Amask])
53
+ result = []
54
+ p.each_value{|s|result << s[:value]}
55
+ p.each(&:free)
56
+ return result
57
+ end
58
+
59
+ # TODO Review Redundancy: Basically return the masks you could read directly anyway?
60
+ def to_masks()
61
+ self.class.to_masks(self.format)
62
+ end
63
+
64
+ # SDL_AllocFormat, renamed 'create' to follow ruby paradigms.
65
+ # Create an PixelFormat from the Enumerated format value.
66
+ def self.create(pixel_format)
67
+ SDL2.alloc_format!(pixel_format)
68
+ end
69
+
70
+ # SDL_FreeFormat, renamed 'release' to follow FFI paradigms.
23
71
  def self.release(pointer)
24
72
  SDL2.free_format(pointer)
25
73
  end
74
+
75
+ # Set the palette
76
+ def set_palette(palette)
77
+ SDL2.set_pixel_format_palette(self, palette)
78
+ end
79
+
80
+ alias_method :palette=, :set_palette
81
+
82
+ # Maps an RGB triple (array) to an opaque pixel value
83
+ def map_rgb(rgb)
84
+ r, g, b = *rgb
85
+ SDL2.map_rgb(self, r, g, b)
86
+ end
87
+
88
+ # Maps an RGBA quadruple (array) to a pixel value
89
+ def map_rgba(rgba)
90
+ r, g, b, a = *rgba
91
+ SDL2.map_rgba(self, r, g, b, a)
92
+ end
93
+
94
+ # Get the RGB components (array) from a pixel value
95
+ def get_rgb(pixel)
96
+ ptr = Hash.new(){UInt8Struct.new}
97
+ SDL2.get_rgb(pixel, self, ptr[:r], ptr[:g], ptr[:b])
98
+ result = []
99
+ ptr.each_value{|s|result << s[:value]}
100
+ ptr.each(&:free)
101
+ return result
102
+ end
103
+
104
+ # Get the RGBA components (array) from a pixel value
105
+ def get_rgba(pixel)
106
+ ptr = Hash.new(){UInt8Struct.new}
107
+ SDL2.get_rgba(pixel, self, ptr[:r], ptr[:g], ptr[:b], ptr[:a])
108
+ result = []
109
+ ptr.each_value{|s|result << s[:value]}
110
+ ptr.each(&:free)
111
+ return result
112
+ end
113
+
26
114
  end
27
-
115
+
28
116
  end
@@ -1,35 +1,258 @@
1
- require 'sdl2'
1
+ require 'sdl2/error'
2
2
  require 'sdl2/color'
3
3
  require 'sdl2/pixel_format'
4
+ require 'yinum'
4
5
 
6
+ # SDL_pixels.h API
5
7
  module SDL2
6
-
7
8
  ALPHA_OPAQUE = 255
8
9
  ALPHA_TRANSPARENT = 0
9
-
10
- enum :pixeltype, [:unknown, :index1, :index4, :index8,
11
- :packed8, :packed16, :packed32,
12
- :arrayU8, :arrayU16, :arrayU32,
13
- :arrayF16, :arrayF32
14
- ]
15
- enum :bitmaporder, [:none, :_4321, :_1234]
16
- enum :packedorder, [:none, :xrgb, :rgbx, :argb, :rgba, :xbgr, :bgrx, :abgr, :bgra]
17
- enum :arrayorder, [:none, :rgb, :rgba, :argb, :bgr, :bgra, :abgr]
18
- enum :packedlayout, [:none, :_332, :_4444, :_1555, :_5551, :_565, :_8888, :_2101010, :_1010102]
19
-
10
+
11
+ PIXELTYPE = Enum.new :PIXELTYPE, {
12
+ UNKNOWN: 0,
13
+ INDEX1: 1,
14
+ INDEX4: 2,
15
+ INDEX8: 3,
16
+ PACKED8: 4,
17
+ PACKED16: 5,
18
+ PACKED32: 6,
19
+ ARRAYU8: 7,
20
+ ARRAYU16: 8,
21
+ ARRAYU32: 9,
22
+ ARRAYF16: 10,
23
+ ARRAYF32: 11
24
+ }
25
+ enum :pixeltype, PIXELTYPE.by_name
26
+
27
+ BITMAPORDER = Enum.new :BITMAPORDER, {
28
+ NONE: 0,
29
+ _4321: 1,
30
+ _1234: 2
31
+ }
32
+ enum :bitmaporder, BITMAPORDER.by_name
33
+
34
+ PACKEDORDER = Enum.new :PACKEDORDER, {
35
+ NONE: 0,
36
+ XRGB: 1,
37
+ RGBX: 2,
38
+ ARGB: 3,
39
+ RGBA: 4,
40
+ XBGR: 5,
41
+ BGRX: 6,
42
+ ABGR: 7,
43
+ BGRA: 8
44
+ }
45
+ enum :packedorder, PACKEDORDER.by_name
46
+
47
+ ARRAYORDER = Enum.new :ARRAYORDER, {
48
+ NONE: 0,
49
+ RGB: 1,
50
+ RGBA: 2,
51
+ ARGB: 3,
52
+ BGR: 4,
53
+ BGRA: 5,
54
+ ABGR: 6
55
+ }
56
+ enum :arrayorder, ARRAYORDER.by_name
57
+
58
+ PACKEDLAYOUT = Enum.new :PACKEDLAYOUT, {
59
+ NONE: 0,
60
+ _332: 1,
61
+ _4444: 2,
62
+ _1555: 3,
63
+ _5551: 4,
64
+ _565: 5,
65
+ _8888: 6,
66
+ _2101010: 7,
67
+ _1010102: 8
68
+ }
69
+ enum :packedlayout, PACKEDLAYOUT.by_name
70
+
71
+ #MACRO: SDL_DEFINE_PIXELFOURCC(A, B, C, D) SDL_FOURCC(A, B, C, D)
72
+ def self.define_pixelfourcc(a,b,c,d); SDL2.fourcc(a,b,c,d); end
73
+
74
+ #MACRO: SDL_DEFINE_PIXELFORMAT(type, order, layout, bits, bytes) \
75
+ # ((1 << 28) | ((type) << 24) | ((order) << 20) | ((layout) << 16) | \
76
+ # ((bits) << 8) | ((bytes) << 0))
77
+ def self.define_pixelformat(type, order, layout, bits, bytes)
78
+ ((1 << 28) | ((type) << 24) | ((order) << 20) | ((layout) << 16) | ((bits) << 8) | ((bytes) << 0))
79
+ end
80
+
81
+ #MACRO: SDL_PIXELFLAG(X) (((X) >> 28) & 0x0F)
82
+ def self.pixelflag(x); (((x) >> 28) & 0x0F); end
83
+
84
+ #MACRO: SDL_PIXELTYPE(X) (((X) >> 24) & 0x0F)
85
+ def self.pixeltype(x); (((x) >> 24) & 0x0F); end
86
+
87
+ #MACRO: SDL_PIXELORDER(X) (((X) >> 20) & 0x0F)
88
+ def self.pixelorder(x); (((x) >> 20) & 0x0F); end
89
+
90
+ #MACRO: SDL_PIXELLAYOUT(X) (((X) >> 16) & 0x0F)
91
+ def self.pixellayout(x); (((x) >> 16) & 0x0F); end
92
+
93
+ #MACRO: SDL_BITSPERPIXEL(X) (((X) >> 8) & 0xFF)
94
+ def self.bitsperpixel(x); (((x) >> 8) & 0xFF); end
95
+
96
+ #MACRO: SDL_BYTESPERPIXEL(X) \
97
+ # (SDL_ISPIXELFORMAT_FOURCC(X) ? \
98
+ # ((((X) == SDL_PIXELFORMAT_YUY2) || \
99
+ # ((X) == SDL_PIXELFORMAT_UYVY) || \
100
+ # ((X) == SDL_PIXELFORMAT_YVYU)) ? 2 : 1) : (((X) >> 0) & 0xFF))
101
+ def self.bytesperpixel(x)
102
+ (ispixelformat_fourcc(x) ?
103
+ ((( x == PIXELFORMAT.YUV2 ) ||
104
+ ( x == PIXELFORMAT.UYVY ) ||
105
+ ( x == PIXELFORMAT.YVYU )) ? 2 : 1) : ((x >> 0) & 0xFF))
106
+ end
107
+
108
+ #MACRO: SDL_ISPIXELFORMAT_INDEXED(format) \
109
+ #(!SDL_ISPIXELFORMAT_FOURCC(format) && \
110
+ #((SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX1) || \
111
+ #(SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX4) || \
112
+ #(SDL_PIXELTYPE(format) == SDL_PIXELTYPE_INDEX8)))
113
+ def self.ispixelformat_indexed(format)
114
+ (!ispixelformat_fourcc(format) &&
115
+ ((pixeltype(format) == PIXELTYPE.INDEX1) ||
116
+ (pixeltype(format) == PIXELTYPE.INDEX4) ||
117
+ (pixeltype(format) == PIXELTYPE.INDEX8)))
118
+ end
119
+
120
+ #MACRO: SDL_ISPIXELFORMAT_ALPHA(format) \
121
+ #(!SDL_ISPIXELFORMAT_FOURCC(format) && \
122
+ #((SDL_PIXELORDER(format) == SDL_PACKEDORDER_ARGB) || \
123
+ #(SDL_PIXELORDER(format) == SDL_PACKEDORDER_RGBA) || \
124
+ #(SDL_PIXELORDER(format) == SDL_PACKEDORDER_ABGR) || \
125
+ #(SDL_PIXELORDER(format) == SDL_PACKEDORDER_BGRA)))
126
+ def self.ispixelformat_alpha(format)
127
+ (!pixelformat_fourcc(format) &&
128
+ ((pixelorder(format) == PACKEDORDER.ARGB) ||
129
+ (pixelorder(format) == PACKEDORDER.RGBA) ||
130
+ (pixelorder(format) == PACKEDORDER.ABGR) ||
131
+ (pixelorder(format) == PACKEDORDER.BGRA)))
132
+ end
133
+
134
+ #MACRO: SDL_ISPIXELFORMAT_FOURCC(format) \
135
+ #((format) && (SDL_PIXELFLAG(format) != 1))
136
+ def self.ispixelformat_fourcc(format)
137
+ ((format) && (pixelflag(format) != 1))
138
+ end
139
+
140
+ PIXELFORMAT = Enum.new(:PIXELFORMAT, {
141
+ UNKNOWN: 0,
142
+ INDEX1LSB:
143
+ define_pixelformat(PIXELTYPE.INDEX1, BITMAPORDER._4321, 0,
144
+ 1, 0),
145
+ INDEX1MSB:
146
+ define_pixelformat(PIXELTYPE.INDEX1, BITMAPORDER._1234, 0,
147
+ 1, 0),
148
+ INDEX4LSB:
149
+ define_pixelformat(PIXELTYPE.INDEX4, BITMAPORDER._4321, 0,
150
+ 4, 0),
151
+ INDEX4MSB:
152
+ define_pixelformat(PIXELTYPE.INDEX4, BITMAPORDER._1234, 0,
153
+ 4, 0),
154
+ INDEX8:
155
+ define_pixelformat(PIXELTYPE.INDEX8, 0, 0, 8, 1),
156
+ RGB332:
157
+ define_pixelformat(PIXELTYPE.PACKED8, PACKEDORDER.XRGB,
158
+ PACKEDLAYOUT._332, 8, 1),
159
+ RGB444:
160
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.XRGB,
161
+ PACKEDLAYOUT._4444, 12, 2),
162
+ RGB555:
163
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.XRGB,
164
+ PACKEDLAYOUT._1555, 15, 2),
165
+ BGR555:
166
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.XBGR,
167
+ PACKEDLAYOUT._1555, 15, 2),
168
+ ARGB4444:
169
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.ARGB,
170
+ PACKEDLAYOUT._4444, 16, 2),
171
+ RGBA4444:
172
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.RGBA,
173
+ PACKEDLAYOUT._4444, 16, 2),
174
+ ABGR4444:
175
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.ABGR,
176
+ PACKEDLAYOUT._4444, 16, 2),
177
+ BGRA4444:
178
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.BGRA,
179
+ PACKEDLAYOUT._4444, 16, 2),
180
+ ARGB1555:
181
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.ARGB,
182
+ PACKEDLAYOUT._1555, 16, 2),
183
+ RGBA5551:
184
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.RGBA,
185
+ PACKEDLAYOUT._5551, 16, 2),
186
+ ABGR1555:
187
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.ABGR,
188
+ PACKEDLAYOUT._1555, 16, 2),
189
+ BGRA5551:
190
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.BGRA,
191
+ PACKEDLAYOUT._5551, 16, 2),
192
+ RGB565:
193
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.XRGB,
194
+ PACKEDLAYOUT._565, 16, 2),
195
+ BGR565:
196
+ define_pixelformat(PIXELTYPE.PACKED16, PACKEDORDER.XBGR,
197
+ PACKEDLAYOUT._565, 16, 2),
198
+ RGB24:
199
+ define_pixelformat(PIXELTYPE.ARRAYU8, ARRAYORDER.RGB, 0,
200
+ 24, 3),
201
+ BGR24:
202
+ define_pixelformat(PIXELTYPE.ARRAYU8, ARRAYORDER.BGR, 0,
203
+ 24, 3),
204
+ RGB888:
205
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.XRGB,
206
+ PACKEDLAYOUT._8888, 24, 4),
207
+ RGBX8888:
208
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.RGBX,
209
+ PACKEDLAYOUT._8888, 24, 4),
210
+ BGR888:
211
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.XBGR,
212
+ PACKEDLAYOUT._8888, 24, 4),
213
+ BGRX8888:
214
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.BGRX,
215
+ PACKEDLAYOUT._8888, 24, 4),
216
+ ARGB8888:
217
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.ARGB,
218
+ PACKEDLAYOUT._8888, 32, 4),
219
+ RGBA8888:
220
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.RGBA,
221
+ PACKEDLAYOUT._8888, 32, 4),
222
+ ABGR8888:
223
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.ABGR,
224
+ PACKEDLAYOUT._8888, 32, 4),
225
+ BGRA8888:
226
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.BGRA,
227
+ PACKEDLAYOUT._8888, 32, 4),
228
+ ARGB2101010:
229
+ define_pixelformat(PIXELTYPE.PACKED32, PACKEDORDER.ARGB,
230
+ PACKEDLAYOUT._2101010, 32, 4),
231
+
232
+ YV12:
233
+ define_pixelfourcc('Y', 'V', '1', '2'),
234
+ IYUV:
235
+ define_pixelfourcc('I', 'Y', 'U', 'V'),
236
+ YUY2:
237
+ define_pixelfourcc('Y', 'U', 'Y', '2'),
238
+ UYVY:
239
+ define_pixelfourcc('U', 'Y', 'V', 'Y'),
240
+ YVYU:
241
+ define_pixelfourcc('Y', 'V', 'Y', 'U')
242
+ })
243
+
20
244
  api :SDL_GetPixelFormatName, [:pixel_format], :string
21
245
  api :SDL_PixelFormatEnumToMasks, [:pixel_format, IntStruct.by_ref, UInt32Struct.by_ref, UInt32Struct.by_ref,UInt32Struct.by_ref,UInt32Struct.by_ref,], :bool
22
246
  api :SDL_MasksToPixelFormatEnum, [:int, :uint32, :uint32, :uint32, :uint32], :pixel_format
23
- api :SDL_AllocFormat, [:pixel_format], PixelFormat.auto_ptr
247
+ api :SDL_AllocFormat, [:pixel_format], PixelFormat.auto_ptr, {error: true, filter: TRUE_WHEN_NOT_NULL}
24
248
  api :SDL_FreeFormat, [PixelFormat.by_ref], :void
25
- api :SDL_AllocPalette, [:count], Palette.auto_ptr
249
+ api :SDL_AllocPalette, [:count], Palette.auto_ptr, {error: true, filter: TRUE_WHEN_NOT_NULL}
26
250
  api :SDL_SetPixelFormatPalette, [PixelFormat.by_ref, Palette.by_ref], :int
27
- api :SDL_SetPaletteColors, [Palette.by_ref, Color.by_ref, :int, :count], :int
251
+ api :SDL_SetPaletteColors, [Palette.by_ref, :pointer, :int, :count], :int
28
252
  api :SDL_FreePalette, [Palette.by_ref], :void
29
253
  api :SDL_MapRGB, [PixelFormat.by_ref, :uint8, :uint8, :uint8], :uint32
30
254
  api :SDL_MapRGBA, [PixelFormat.by_ref, :uint8, :uint8, :uint8, :uint8], :uint32
31
255
  api :SDL_GetRGB, [:uint32, PixelFormat.by_ref, UInt8Struct.by_ref,UInt8Struct.by_ref,UInt8Struct.by_ref], :void
32
256
  api :SDL_GetRGBA, [:uint32, PixelFormat.by_ref, UInt8Struct.by_ref,UInt8Struct.by_ref,UInt8Struct.by_ref,UInt8Struct.by_ref], :void
33
-
34
-
257
+
35
258
  end