hybridgroup-ruby-sdl-ffi 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,513 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009, 2010 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #
29
+ # Contributions:
30
+ #
31
+ # - Bart Leusink, 2011-01-31:
32
+ # Implemented SDL::SaveBMP and SDL::LoadBMP
33
+ #
34
+ #++
35
+
36
+
37
+ module SDL
38
+
39
+ ALPHA_OPAQUE = 255
40
+ ALPHA_TRANSPARENT = 0
41
+
42
+ class Rect < NiceFFI::Struct
43
+ layout( :x, :int16,
44
+ :y, :int16,
45
+ :w, :uint16,
46
+ :h, :uint16 )
47
+ end
48
+
49
+ class Color < NiceFFI::Struct
50
+ layout( :r, :uint8,
51
+ :g, :uint8,
52
+ :b, :uint8,
53
+ :unused, :uint8 )
54
+
55
+ hidden( :unused )
56
+
57
+ end
58
+
59
+
60
+ class Palette < NiceFFI::Struct
61
+ layout( :ncolors, :int,
62
+ :colors, :pointer )
63
+
64
+ include Enumerable
65
+
66
+ # Returns the color at the given index in the palette, as an
67
+ # SDL::Color instance.
68
+ def at( index )
69
+ index = (0...ncolors).to_a.at(index)
70
+ if index
71
+ SDL::Color.new( self[:colors] + index * SDL::Color.size )
72
+ end
73
+ end
74
+
75
+ # Yields an SDL::Color for each color in the palette.
76
+ def each
77
+ ncolors.times{ |i| yield at(i) }
78
+ end
79
+
80
+ end
81
+
82
+
83
+ class PixelFormat < NiceFFI::Struct
84
+ layout( :palette, Palette.typed_pointer,
85
+ :BitsPerPixel, :uint8,
86
+ :BytesPerPixel, :uint8,
87
+ :Rloss, :uint8,
88
+ :Gloss, :uint8,
89
+ :Bloss, :uint8,
90
+ :Aloss, :uint8,
91
+ :Rshift, :uint8,
92
+ :Gshift, :uint8,
93
+ :Bshift, :uint8,
94
+ :Ashift, :uint8,
95
+ :Rmask, :uint32,
96
+ :Gmask, :uint32,
97
+ :Bmask, :uint32,
98
+ :Amask, :uint32,
99
+ :colorkey, :uint32,
100
+ :alpha, :uint8 )
101
+ end
102
+
103
+ class Surface < NiceFFI::Struct
104
+ layout( :flags, :uint32,
105
+ :format, PixelFormat.typed_pointer,
106
+ :w, :int,
107
+ :h, :int,
108
+ :pitch, :uint16,
109
+ :pixels, :pointer,
110
+ :offset, :int,
111
+ :hwdata, :pointer,
112
+ :clip_rect, SDL::Rect,
113
+ :unused1, :uint32,
114
+ :locked, :uint32,
115
+ :map, :pointer,
116
+ :format_version, :uint,
117
+ :refcount, :int )
118
+
119
+ read_only( :flags, :format, :w, :h,
120
+ :pitch, :clip_rect, :refcount )
121
+
122
+ hidden( :offset, :hwdata, :unused1,
123
+ :locked, :map, :format_version )
124
+
125
+ def self.release( pointer )
126
+ SDL.FreeSurface( pointer )
127
+ end
128
+ end
129
+
130
+
131
+ SWSURFACE = 0x00000000
132
+ HWSURFACE = 0x00000001
133
+ ASYNCBLIT = 0x00000004
134
+ ANYFORMAT = 0x10000000
135
+ HWPALETTE = 0x20000000
136
+ DOUBLEBUF = 0x40000000
137
+ FULLSCREEN = 0x80000000
138
+ OPENGL = 0x00000002
139
+ OPENGLBLIT = 0x0000000A
140
+ RESIZABLE = 0x00000010
141
+ NOFRAME = 0x00000020
142
+ HWACCEL = 0x00000100
143
+ SRCCOLORKEY = 0x00001000
144
+ RLEACCELOK = 0x00002000
145
+ RLEACCEL = 0x00004000
146
+ SRCALPHA = 0x00010000
147
+ PREALLOC = 0x01000000
148
+
149
+ callback(:blit_cb, [ :pointer, :pointer, :pointer, :pointer ], :int)
150
+
151
+
152
+ class VideoInfo < NiceFFI::Struct
153
+ layout( :flags, :uint32,
154
+
155
+ ## flags contains:
156
+ # :hw_available, :uint32, #bitfield: 1
157
+ # :wm_available, :uint32, #bitfield: 1
158
+ # :UnusedBits1, :uint32, #bitfield: 6
159
+ # :UnusedBits2, :uint32, #bitfield: 1
160
+ # :blit_hw, :uint32, #bitfield: 1
161
+ # :blit_hw_CC, :uint32, #bitfield: 1
162
+ # :blit_hw_A, :uint32, #bitfield: 1
163
+ # :blit_sw, :uint32, #bitfield: 1
164
+ # :blit_sw_CC, :uint32, #bitfield: 1
165
+ # :blit_sw_A, :uint32, #bitfield: 1
166
+ # :blit_fill, :uint32, #bitfield: 1
167
+ # :UnusedBits3, :uint32, #bitfield: 16
168
+
169
+ :video_mem, :uint32,
170
+ :vfmt, PixelFormat.typed_pointer,
171
+ :current_w, :int,
172
+ :current_h, :int )
173
+
174
+ hidden( :flags )
175
+
176
+ def hw_available
177
+ self[:flags][1]
178
+ end
179
+
180
+ def wm_available
181
+ self[:flags][2]
182
+ end
183
+
184
+ def blit_hw
185
+ self[:flags][9]
186
+ end
187
+
188
+ def blit_hw_CC
189
+ self[:flags][10]
190
+ end
191
+
192
+ def blit_hw_A
193
+ self[:flags][11]
194
+ end
195
+
196
+ def blit_sw
197
+ self[:flags][12]
198
+ end
199
+
200
+ def blit_sw_CC
201
+ self[:flags][13]
202
+ end
203
+
204
+ def blit_sw_A
205
+ self[:flags][14]
206
+ end
207
+
208
+ def blit_fill
209
+ self[:flags][15]
210
+ end
211
+ end
212
+
213
+
214
+ YV12_OVERLAY = 0x32315659
215
+ IYUV_OVERLAY = 0x56555949
216
+ YUY2_OVERLAY = 0x32595559
217
+ UYVY_OVERLAY = 0x59565955
218
+ YVYU_OVERLAY = 0x55595659
219
+
220
+ class Overlay < NiceFFI::Struct
221
+ layout( :format, :uint32,
222
+ :w, :int,
223
+ :h, :int,
224
+ :planes, :int,
225
+ :pitches, :pointer,
226
+ :pixels, :pointer,
227
+ :hwfuncs, :pointer,
228
+ :hwdata, :pointer,
229
+ :hw_overlay, :uint32,
230
+ :UnusedBits, :uint32 )
231
+
232
+ read_only( :format, :w, :h, :planes,
233
+ :pitches, :hw_overlay )
234
+
235
+ hidden( :hwfuncs, :hwdata, :UnusedBits )
236
+
237
+ def self.release( pointer )
238
+ SDL.FreeYUVOverlay( pointer )
239
+ end
240
+ end
241
+
242
+
243
+ GL_RED_SIZE = 0
244
+ GL_GREEN_SIZE = 1
245
+ GL_BLUE_SIZE = 2
246
+ GL_ALPHA_SIZE = 3
247
+ GL_BUFFER_SIZE = 4
248
+ GL_DOUBLEBUFFER = 5
249
+ GL_DEPTH_SIZE = 6
250
+ GL_STENCIL_SIZE = 7
251
+ GL_ACCUM_RED_SIZE = 8
252
+ GL_ACCUM_GREEN_SIZE = 9
253
+ GL_ACCUM_BLUE_SIZE = 10
254
+ GL_ACCUM_ALPHA_SIZE = 11
255
+ GL_STEREO = 12
256
+ GL_MULTISAMPLEBUFFERS = 13
257
+ GL_MULTISAMPLESAMPLES = 14
258
+ GL_ACCELERATED_VISUAL = 15
259
+ GL_SWAP_CONTROL = 16
260
+
261
+ LOGPAL = 0x01
262
+ PHYSPAL = 0x02
263
+
264
+
265
+ sdl_func :VideoInit, [ :string, :uint32 ], :int
266
+ sdl_func :VideoQuit, [ ], :void
267
+
268
+ sdl_func :VideoDriverName, [ :string, :int ], :string
269
+
270
+ sdl_func :GetVideoSurface, [],
271
+ SDL::Surface.typed_pointer( :autorelease => false )
272
+
273
+ sdl_func :GetVideoInfo, [ ], SDL::VideoInfo.typed_pointer
274
+
275
+ sdl_func :VideoModeOK, [ :int, :int, :int, :uint32 ], :int
276
+
277
+ ## Don't know how to implement this one. :-\
278
+ # sdl_func :ListModes, [ :pointer, :uint32 ], :pointer
279
+
280
+
281
+ func :__SetVideoMode, "SDL_SetVideoMode", [ :int, :int, :int, :uint32 ],
282
+ SDL::Surface.typed_pointer( :autorelease => false )
283
+
284
+ def self.SetVideoMode( *args )
285
+ result = __SetVideoMode(*args)
286
+ if defined? SDL::Mac
287
+ SDL::Mac::HIServices.make_current_front
288
+ SDL::Mac.make_menus("ruby")
289
+ end
290
+ return result
291
+ end
292
+
293
+
294
+ # Sets the application name, if the platform supports it. This
295
+ # method is safe to call even on platforms which do not support it
296
+ # (but does nothing). Currently this method only has an effect on
297
+ # Mac OS X.
298
+ #
299
+ # The effect of this method depends on the platform. On Mac OS X, it
300
+ # sets the text used in the main application menu.
301
+ #
302
+ # (Note: this method does not correspond to any part of the SDL API.
303
+ # It communicates with the platform directly.)
304
+ #
305
+ # Example:
306
+ # SDL.set_app_name("SpaceQuest 4000")
307
+ #
308
+ def self.set_app_name( app_name )
309
+ if defined? SDL::Mac
310
+ SDL::Mac.set_app_name( app_name )
311
+ end
312
+ nil
313
+ end
314
+
315
+
316
+ func :__UpdateRects, "SDL_UpdateRects", [ :pointer, :int, :pointer ], :void
317
+
318
+ def self.UpdateRects( surf, rects )
319
+ rects_mp = FFI::Buffer.new( Rect, rects.length )
320
+
321
+ rects.each_with_index do |rect, i|
322
+ rects_mp[i].put_bytes( 0, rect.to_bytes )
323
+ end
324
+
325
+ __UpdateRects( surf, rects.length, rects_mp )
326
+ end
327
+
328
+
329
+ sdl_func :UpdateRect, [ :pointer, :int32, :int32, :uint32, :uint32 ], :void
330
+ sdl_func :Flip, [ :pointer ], :int
331
+
332
+
333
+
334
+ sdl_func :SetGamma, [ :float, :float, :float ], :int
335
+ sdl_func :SetGammaRamp, [ :pointer, :pointer, :pointer ], :int
336
+
337
+
338
+ func :__SDL_GetGammaRamp, "SDL_GetGammaRamp",
339
+ [ :buffer_out, :buffer_out, :buffer_out ], :int
340
+
341
+ def self.GetGammaRamp()
342
+ rtable = FFI::Buffer.new( :uint16, 256 )
343
+ gtable = FFI::Buffer.new( :uint16, 256 )
344
+ btable = FFI::Buffer.new( :uint16, 256 )
345
+
346
+ n = __SDL_GetGammaRamp( rtable, gtable, btable )
347
+
348
+ if( n == -1 )
349
+ return nil
350
+ else
351
+ return [ rtable.get_array_of_uint16(0, 256),
352
+ gtable.get_array_of_uint16(0, 256),
353
+ btable.get_array_of_uint16(0, 256) ]
354
+ end
355
+ end
356
+
357
+
358
+ sdl_func :SetColors, [ :pointer, :pointer, :int, :int ], :int
359
+ sdl_func :SetPalette, [ :pointer, :int, :pointer, :int, :int ], :int
360
+ sdl_func :MapRGB, [ :pointer, :uint8, :uint8, :uint8 ], :uint32
361
+ sdl_func :MapRGBA, [ :pointer, :uint8, :uint8, :uint8, :uint8 ], :uint32
362
+
363
+
364
+ func :__SDL_GetRGB, "SDL_GetRGB",
365
+ [ :uint32, :pointer, :buffer_out, :buffer_out, :buffer_out ], :void
366
+
367
+ def self.GetRGB( uint32, format )
368
+ r = FFI::Buffer.new( :uint8 )
369
+ g = FFI::Buffer.new( :uint8 )
370
+ b = FFI::Buffer.new( :uint8 )
371
+ __SDL_GetRGB( uint32, format, r, g, b )
372
+ return [r.get_uint8(0), g.get_uint8(0), b.get_uint8(0)]
373
+ end
374
+
375
+
376
+
377
+ func :__SDL_GetRGBA, "SDL_GetRGBA",
378
+ [ :uint32, :pointer, :buffer_out,
379
+ :buffer_out, :buffer_out, :buffer_out ], :void
380
+
381
+ def self.GetRGBA( uint32, format )
382
+ r = FFI::Buffer.new( :uint8 )
383
+ g = FFI::Buffer.new( :uint8 )
384
+ b = FFI::Buffer.new( :uint8 )
385
+ a = FFI::Buffer.new( :uint8 )
386
+ __SDL_GetRGBA( uint32, format, r, g, b, a )
387
+ return [r.get_uint8(0), g.get_uint8(0), b.get_uint8(0), a.get_uint8(0)]
388
+ end
389
+
390
+
391
+
392
+ sdl_func :CreateRGBSurface,
393
+ [ :uint32, :int, :int, :int, :uint32, :uint32, :uint32, :uint32 ],
394
+ SDL::Surface.typed_pointer
395
+
396
+ sdl_func :CreateRGBSurfaceFrom,
397
+ [ :pointer, :int, :int, :int, :int,
398
+ :uint32, :uint32, :uint32, :uint32 ],
399
+ SDL::Surface.typed_pointer
400
+
401
+
402
+ sdl_func :FreeSurface, [ :pointer ], :void
403
+ sdl_func :LockSurface, [ :pointer ], :int
404
+ sdl_func :UnlockSurface, [ :pointer ], :void
405
+
406
+
407
+ sdl_func :LoadBMP_RW, [ :pointer, :int ], SDL::Surface.typed_pointer
408
+ sdl_func :SaveBMP_RW, [ :pointer, :pointer, :int ], :int
409
+
410
+
411
+ def self.LoadBMP( file )
412
+ return LoadBMP_RW( RWFromFile( file, "rb" ), 1 )
413
+ end
414
+
415
+ def self.SaveBMP( surface, file )
416
+ return SaveBMP_RW( surface, RWFromFile( file, "wb" ), 1 )
417
+ end
418
+
419
+
420
+ sdl_func :SetColorKey, [ :pointer, :uint32, :uint32 ], :int
421
+ sdl_func :SetAlpha, [ :pointer, :uint32, :uint8 ], :int
422
+
423
+
424
+
425
+ sdl_func :SetClipRect, [ :pointer, :pointer ], SDL::BOOL
426
+
427
+ func :__SDL_GetClipRect, "SDL_GetClipRect", [ :pointer, :buffer_out ], :void
428
+
429
+ def self.GetClipRect( surface )
430
+ mp = FFI::Buffer.new( Rect )
431
+ __SDL_GetClipRect( surface, mp )
432
+ return Rect.new( mp )
433
+ end
434
+
435
+
436
+ sdl_func :ConvertSurface,
437
+ [ :pointer, :pointer, :uint32 ], SDL::Surface.typed_pointer
438
+
439
+
440
+ func :BlitSurface, "SDL_UpperBlit",
441
+ [ :pointer, :pointer, :pointer, :pointer ], :int
442
+
443
+
444
+ sdl_func :FillRect, [ :pointer, :pointer, :uint32 ], :int
445
+
446
+
447
+ sdl_func :DisplayFormat, [ :pointer ], SDL::Surface.typed_pointer
448
+ sdl_func :DisplayFormatAlpha, [ :pointer ], SDL::Surface.typed_pointer
449
+
450
+
451
+ sdl_func :CreateYUVOverlay, [ :int, :int, :uint32, :pointer ],
452
+ SDL::Overlay.typed_pointer
453
+
454
+ sdl_func :LockYUVOverlay, [ :pointer ], :int
455
+ sdl_func :UnlockYUVOverlay, [ :pointer ], :void
456
+ sdl_func :DisplayYUVOverlay, [ :pointer, :pointer ], :int
457
+ sdl_func :FreeYUVOverlay, [ :pointer ], :void
458
+
459
+
460
+ sdl_func :GL_LoadLibrary, [ :string ], :int
461
+ sdl_func :GL_GetProcAddress, [ :string ], :pointer
462
+ sdl_func :GL_SetAttribute, [ SDL::GLATTR, :int ], :int
463
+
464
+
465
+ func :__GL_GetAttribute, "SDL_GL_GetAttribute",
466
+ [ SDL::GLATTR, :buffer_out ], :int
467
+
468
+ def self.GL_GetAttribute( attrib )
469
+ buffer = FFI::Buffer.new( :int )
470
+ result = __GL_GetAttribute( attrib, buffer )
471
+ if( result == -1 )
472
+ return nil
473
+ else
474
+ return buffer.get_int(0)
475
+ end
476
+ end
477
+
478
+
479
+ sdl_func :GL_SwapBuffers, [ ], :void
480
+ sdl_func :GL_UpdateRects, [ :int, :pointer ], :void
481
+ sdl_func :GL_Lock, [ ], :void
482
+ sdl_func :GL_Unlock, [ ], :void
483
+
484
+
485
+
486
+ sdl_func :WM_SetCaption, [ :string, :string ], :void
487
+
488
+ func :__SDL_WM_GetCaption, "SDL_WM_GetCaption",
489
+ [ :buffer_out, :buffer_out ], :void
490
+
491
+ def self.WM_GetCaption()
492
+ title = FFI::Buffer.new( :pointer )
493
+ icont = FFI::Buffer.new( :pointer )
494
+ __SDL_WM_GetCaption( title, icont )
495
+ return [ title.get_pointer(0).get_string(0),
496
+ icont.get_pointer(0).get_string(0) ]
497
+ end
498
+
499
+
500
+
501
+ sdl_func :WM_SetIcon, [ :pointer, :pointer ], :void
502
+ sdl_func :WM_IconifyWindow, [ ], :int
503
+ sdl_func :WM_ToggleFullScreen, [ :pointer ], :int
504
+
505
+
506
+ GRAB_QUERY = -1
507
+ GRAB_OFF = 0
508
+ GRAB_ON = 1
509
+ GRAB_FULLSCREEN = 2
510
+
511
+ sdl_func :WM_GrabInput, [ SDL::ENUM ], SDL::ENUM
512
+
513
+ end
@@ -0,0 +1,81 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009, 2010 John Croisant
8
+ #
9
+ # Permission is hereby granted, free of charge, to any person obtaining
10
+ # a copy of this software and associated documentation files (the
11
+ # "Software"), to deal in the Software without restriction, including
12
+ # without limitation the rights to use, copy, modify, merge, publish,
13
+ # distribute, sublicense, and/or sell copies of the Software, and to
14
+ # permit persons to whom the Software is furnished to do so, subject to
15
+ # the following conditions:
16
+ #
17
+ # The above copyright notice and this permission notice shall be
18
+ # included in all copies or substantial portions of the Software.
19
+ #
20
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21
+ # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22
+ # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23
+ # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24
+ # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25
+ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26
+ # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
+ #
28
+ #++
29
+
30
+
31
+ require 'nice-ffi'
32
+
33
+
34
+ module SDL
35
+ extend NiceFFI::Library
36
+
37
+ unless defined? SDL::LOAD_PATHS
38
+ SDL::LOAD_PATHS = NiceFFI::PathSet::DEFAULT.dup
39
+
40
+ rubysdlffi_path = ENV["RUBYSDLFFI_PATH"]
41
+ if rubysdlffi_path and not rubysdlffi_path.empty?
42
+ paths = rubysdlffi_path.split( File::PATH_SEPARATOR ).compact
43
+ SDL::LOAD_PATHS.prepend!(paths)
44
+ end
45
+
46
+ if defined? ::SDL_PATHS
47
+ SDL::LOAD_PATHS.prepend!(::SDL_PATHS)
48
+ end
49
+ end
50
+
51
+ load_library "SDL", SDL::LOAD_PATHS
52
+
53
+ def self.sdl_func( name, args, ret )
54
+ func name, "SDL_#{name}", args, ret
55
+ end
56
+
57
+ end
58
+
59
+
60
+ this_dir = File.expand_path( File.dirname(__FILE__) )
61
+
62
+ # NOTE: keyboard and video are deliberately loaded early,
63
+ # because event and mouse depend on them, respectively.
64
+
65
+ %w{
66
+ mac
67
+ core
68
+ keyboard
69
+ video
70
+ audio
71
+ cdrom
72
+ event
73
+ joystick
74
+ keysyms
75
+ mouse
76
+ mutex
77
+ rwops
78
+ timer
79
+ }.each do |f|
80
+ require File.join( this_dir, "sdl", f )
81
+ end