ruby-sdl-ffi 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,215 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009 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
+ this_dir = File.expand_path( File.dirname(__FILE__) )
32
+
33
+ require File.join( this_dir, "sdl" )
34
+
35
+
36
+ module SDL
37
+ module Mixer
38
+ extend NiceFFI::Library
39
+ load_library "SDL_mixer", SDL::LOAD_PATHS
40
+
41
+
42
+ def self.mix_func( name, args, ret )
43
+ func name, "Mix_#{name}", args, ret
44
+ end
45
+
46
+
47
+ mix_func :Linked_Version, [], SDL::Version.typed_pointer
48
+
49
+
50
+ CHANNELS = 8
51
+ DEFAULT_FREQUENCY = 22050
52
+ DEFAULT_CHANNELS = 2
53
+
54
+ DEFAULT_FORMAT = if( FFI::Platform::BYTE_ORDER ==
55
+ FFI::Platform::LITTLE_ENDIAN)
56
+ AUDIO_S16LSB
57
+ else
58
+ AUDIO_S16MSB
59
+ end
60
+
61
+ MAX_VOLUME = 128
62
+
63
+
64
+ class Chunk < NiceFFI::Struct
65
+ layout( :allocated, :int,
66
+ :abuf, :pointer,
67
+ :alen, :uint32,
68
+ :volume, :uint8 )
69
+
70
+ def self.release( pointer )
71
+ SDL::Mixer.FreeChunk( pointer )
72
+ end
73
+ end
74
+
75
+
76
+
77
+ class Music < NiceFFI::OpaqueStruct
78
+ #--
79
+ # Mix_Music struct (in C) has a hidden layout, which changes
80
+ # depending on which sound format libraries were available
81
+ # at compile time.
82
+ #++
83
+
84
+ def self.release( pointer )
85
+ SDL::Mixer.FreeMusic( pointer )
86
+ end
87
+ end
88
+
89
+
90
+
91
+ NO_FADING = 0
92
+ FADING_OUT = 1
93
+ FADING_IN = 2
94
+
95
+ MUS_NONE = 0
96
+ MUS_CMD = 1
97
+ MUS_WAV = 2
98
+ MUS_MOD = 3
99
+ MUS_MID = 4
100
+ MUS_OGG = 5
101
+ MUS_MP3 = 6
102
+ MUS_MP3_MAD = 7
103
+
104
+
105
+ mix_func :OpenAudio, [ :int, :uint16, :int, :int ], :int
106
+ mix_func :AllocateChannels, [ :int ], :int
107
+ mix_func :QuerySpec, [ :pointer, :pointer, :pointer ], :int
108
+
109
+
110
+ def self.LoadWAV( file )
111
+ LoadWAV_RW( SDL.RWFromFile(file, "rb"), 1 )
112
+ end
113
+
114
+ mix_func :LoadWAV_RW, [ :pointer, :int ], Chunk.typed_pointer
115
+ mix_func :LoadMUS, [ :string ], Music.typed_pointer
116
+ mix_func :LoadMUS_RW, [ :pointer ], Music.typed_pointer
117
+ mix_func :QuickLoad_WAV, [ :pointer ], Chunk.typed_pointer
118
+ mix_func :QuickLoad_RAW, [ :pointer, :uint32 ], Chunk.typed_pointer
119
+
120
+
121
+ mix_func :FreeChunk, [ :pointer ], :void
122
+ mix_func :FreeMusic, [ :pointer ], :void
123
+
124
+
125
+ mix_func :GetMusicType, [ :pointer ], :int
126
+
127
+
128
+ callback( :mix_hook_cb, [:pointer, :pointer, :int], :void)
129
+
130
+ mix_func :SetPostMix, [ :mix_hook_cb, :pointer ], :void
131
+ mix_func :HookMusic, [ :mix_hook_cb, :pointer ], :void
132
+ mix_func :HookMusicFinished, [ callback([], :void) ], :void
133
+ mix_func :GetMusicHookData, [ ], :pointer
134
+ mix_func :ChannelFinished, [ callback([:int], :void) ], :void
135
+
136
+
137
+ CHANNEL_POST = -2
138
+
139
+
140
+ callback(:mix_effectfunc_cb, [ :int, :pointer, :int, :pointer ], :void)
141
+ callback(:mix_effectdone_cb, [ :int, :pointer ], :void)
142
+
143
+ mix_func :RegisterEffect,
144
+ [ :int, :mix_effectfunc_cb, :mix_effectdone_cb, :pointer ], :int
145
+
146
+ mix_func :UnregisterEffect, [ :int, :mix_effectfunc_cb ], :int
147
+ mix_func :UnregisterAllEffects, [ :int ], :int
148
+
149
+
150
+ EFFECTSMAXSPEED = "MIX_EFFECTSMAXSPEED"
151
+
152
+
153
+ mix_func :SetPanning, [ :int, :uint8, :uint8 ], :int
154
+ mix_func :SetPosition, [ :int, :int16, :uint8 ], :int
155
+ mix_func :SetDistance, [ :int, :uint8 ], :int
156
+ mix_func :SetReverseStereo, [ :int, :int ], :int
157
+
158
+
159
+ mix_func :ReserveChannels, [ :int ], :int
160
+ mix_func :GroupChannel, [ :int, :int ], :int
161
+ mix_func :GroupChannels, [ :int, :int, :int ], :int
162
+ mix_func :GroupAvailable, [ :int ], :int
163
+ mix_func :GroupCount, [ :int ], :int
164
+ mix_func :GroupOldest, [ :int ], :int
165
+ mix_func :GroupNewer, [ :int ], :int
166
+
167
+
168
+ mix_func :PlayChannelTimed, [ :int, :pointer, :int, :int ], :int
169
+ mix_func :PlayMusic, [ :pointer, :int ], :int
170
+ mix_func :FadeInMusic, [ :pointer, :int, :int ], :int
171
+ mix_func :FadeInMusicPos, [ :pointer, :int, :int, :double ], :int
172
+ mix_func :FadeInChannelTimed, [ :int, :pointer, :int, :int, :int ], :int
173
+
174
+
175
+ mix_func :Volume, [ :int, :int ], :int
176
+ mix_func :VolumeChunk, [ :pointer, :int ], :int
177
+ mix_func :VolumeMusic, [ :int ], :int
178
+
179
+
180
+ mix_func :HaltChannel, [ :int ], :int
181
+ mix_func :HaltGroup, [ :int ], :int
182
+ mix_func :HaltMusic, [ ], :int
183
+ mix_func :ExpireChannel, [ :int, :int ], :int
184
+
185
+
186
+ mix_func :FadeOutChannel, [ :int, :int ], :int
187
+ mix_func :FadeOutGroup, [ :int, :int ], :int
188
+ mix_func :FadeOutMusic, [ :int ], :int
189
+ mix_func :FadingMusic, [ ], :int
190
+ mix_func :FadingChannel, [ :int ], :int
191
+
192
+
193
+ mix_func :Pause, [ :int ], :void
194
+ mix_func :Resume, [ :int ], :void
195
+ mix_func :Paused, [ :int ], :int
196
+ mix_func :PauseMusic, [ ], :void
197
+ mix_func :ResumeMusic, [ ], :void
198
+ mix_func :RewindMusic, [ ], :void
199
+ mix_func :PausedMusic, [ ], :int
200
+
201
+
202
+ mix_func :SetMusicPosition, [ :double ], :int
203
+ mix_func :Playing, [ :int ], :int
204
+ mix_func :PlayingMusic, [ ], :int
205
+ mix_func :SetMusicCMD, [ :string ], :int
206
+
207
+ mix_func :SetSynchroValue, [ :int ], :int
208
+ mix_func :GetSynchroValue, [ ], :int
209
+
210
+ mix_func :GetChunk, [ :int ], Chunk.typed_pointer
211
+
212
+ mix_func :CloseAudio, [ ], :void
213
+
214
+ end
215
+ end
@@ -0,0 +1,76 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009 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
+ # Check if the application has defined SDL_PATHS with some
39
+ # paths to check first for SDL libraries.
40
+ SDL::LOAD_PATHS = if defined? ::SDL_PATHS
41
+ NiceFFI::PathSet::DEFAULT.prepend( ::SDL_PATHS )
42
+ else
43
+ NiceFFI::PathSet::DEFAULT
44
+ end
45
+ end
46
+
47
+ load_library "SDL", SDL::LOAD_PATHS
48
+
49
+ def self.sdl_func( name, args, ret )
50
+ func name, "SDL_#{name}", args, ret
51
+ end
52
+
53
+ end
54
+
55
+
56
+ this_dir = File.expand_path( File.dirname(__FILE__) )
57
+
58
+ # NOTE: keyboard and video are deliberately loaded early,
59
+ # because event and mouse depend on them, respectively.
60
+
61
+ %w{
62
+ core
63
+ keyboard
64
+ video
65
+ audio
66
+ cdrom
67
+ event
68
+ joystick
69
+ keysyms
70
+ mouse
71
+ mutex
72
+ rwops
73
+ timer
74
+ }.each do |f|
75
+ require File.join( this_dir, "sdl", f )
76
+ end
@@ -0,0 +1,132 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009 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
+ module SDL
32
+
33
+ class AudioSpec < NiceFFI::Struct
34
+
35
+ SDL::callback(:audiospec_cb, [ :pointer, :pointer, :int ], :void)
36
+
37
+ layout( :freq, :int,
38
+ :format, :uint16,
39
+ :channels, :uint8,
40
+ :silence, :uint8,
41
+ :samples, :uint16,
42
+ :padding, :uint16,
43
+ :size, :uint32,
44
+ :callback, :audiospec_cb,
45
+ :userdata, :pointer )
46
+
47
+ def callback=(cb)
48
+ @callback = cb
49
+ self[:callback] = @callback
50
+ end
51
+
52
+ def callback
53
+ @callback
54
+ end
55
+
56
+ end
57
+
58
+
59
+ AUDIO_U8 = 0x0008
60
+ AUDIO_S8 = 0x8008
61
+ AUDIO_U16LSB = 0x0010
62
+ AUDIO_S16LSB = 0x8010
63
+ AUDIO_U16MSB = 0x1010
64
+ AUDIO_S16MSB = 0x9010
65
+ AUDIO_U16 = 0x0010
66
+ AUDIO_S16 = 0x8010
67
+ AUDIO_U16SYS = 0x0010
68
+ AUDIO_S16SYS = 0x8010
69
+
70
+
71
+ # callback( :filters_cb, [ :pointer, :uint16 ], :void)
72
+
73
+ # class AudioCVT < NiceFFI::Struct
74
+ # layout( :needed, :int,
75
+ # :src_format, :uint16,
76
+ # :dst_format, :uint16,
77
+ # :rate_incr, :double,
78
+ # :buf, :pointer,
79
+ # :len, :int,
80
+ # :len_cvt, :int,
81
+ # :len_mult, :int,
82
+ # :len_ratio, :double,
83
+ # :filters, [:filters_cb, 10],
84
+ # :filter_index, :int )
85
+ # end
86
+
87
+
88
+ sdl_func :AudioInit, [ :string ], :int
89
+ sdl_func :AudioQuit, [ ], :void
90
+ sdl_func :OpenAudio, [ :pointer, :pointer ], :int
91
+
92
+
93
+ func :__AudioDriverName, "SDL_AudioDriverName", [:pointer, :int], :pointer
94
+
95
+ def self.AudioDriverName
96
+ b = FFI::Buffer.new(:char, 1024)
97
+ result = __AudioDriverName( b, 1024 )
98
+ if result.null?
99
+ nil
100
+ else
101
+ b.get_string(0,1024)
102
+ end
103
+ end
104
+
105
+
106
+ AUDIO_STOPPED = 0
107
+ AUDIO_PLAYING = 1
108
+ AUDIO_PAUSED = 2
109
+
110
+ sdl_func :GetAudioStatus, [ ], SDL::ENUM
111
+ sdl_func :PauseAudio, [ :int ], :void
112
+
113
+ sdl_func :LoadWAV_RW,
114
+ [ :pointer, :int, :pointer, :pointer, :pointer ], :pointer
115
+
116
+ sdl_func :FreeWAV, [ :pointer ], :void
117
+
118
+ sdl_func :BuildAudioCVT,
119
+ [ :pointer, :uint16, :uint8, :int, :uint16, :uint8, :int ], :int
120
+
121
+ sdl_func :ConvertAudio, [ :pointer ], :int
122
+
123
+
124
+ MIX_MAXVOLUME = 128
125
+
126
+ sdl_func :MixAudio, [ :pointer, :pointer, :uint32, :int ], :void
127
+
128
+ sdl_func :LockAudio, [ ], :void
129
+ sdl_func :UnlockAudio, [ ], :void
130
+ sdl_func :CloseAudio, [ ], :void
131
+
132
+ end
@@ -0,0 +1,87 @@
1
+ #--
2
+ #
3
+ # This file is one part of:
4
+ #
5
+ # Ruby-SDL-FFI - Ruby-FFI bindings to SDL
6
+ #
7
+ # Copyright (c) 2009 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
+ module SDL
32
+
33
+ CD_ERROR = -1
34
+ CD_TRAYEMPTY = 0
35
+ CD_STOPPED = 1
36
+ CD_PLAYING = 2
37
+ CD_PAUSED = 3
38
+
39
+ AUDIO_TRACK = 0x00
40
+ DATA_TRACK = 0x04
41
+
42
+ MAX_TRACKS = 99
43
+
44
+ class CDtrack < NiceFFI::Struct
45
+ layout( :id, :uint8,
46
+ :type, :uint8,
47
+ :unused, :uint16,
48
+ :length, :uint32,
49
+ :offset, :uint32 )
50
+
51
+ hidden( :unused )
52
+
53
+ end
54
+
55
+
56
+ if RUBY_PLATFORM =~ /java/
57
+ # 2009-10-21: JRuby FFI does not support arrays of structs in structs.
58
+ # Attempting it can raise an un-rescuable NotImplementedError! :(
59
+ puts "Warning: Skipping class SDL::CD due to JRuby limitations."
60
+ else
61
+
62
+ class CD < NiceFFI::Struct
63
+ layout( :id, :int,
64
+ :status, SDL::ENUM,
65
+ :numtracks, :int,
66
+ :cur_track, :int,
67
+ :cur_frame, :int,
68
+ :track, [CDtrack.by_value, SDL::MAX_TRACKS+1] )
69
+ end
70
+
71
+ end
72
+
73
+ CD_FPS = 75
74
+
75
+ sdl_func :CDNumDrives, [ ], :int
76
+ sdl_func :CDName, [ :int ], :string
77
+ sdl_func :CDOpen, [ :int ], :pointer
78
+ sdl_func :CDStatus, [ :pointer ], SDL::ENUM
79
+ sdl_func :CDPlayTracks, [ :pointer, :int, :int, :int, :int ], :int
80
+ sdl_func :CDPlay, [ :pointer, :int, :int ], :int
81
+ sdl_func :CDPause, [ :pointer ], :int
82
+ sdl_func :CDResume, [ :pointer ], :int
83
+ sdl_func :CDStop, [ :pointer ], :int
84
+ sdl_func :CDEject, [ :pointer ], :int
85
+ sdl_func :CDClose, [ :pointer ], :void
86
+
87
+ end