ruby-sdl-ffi 0.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,129 @@
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
+ # Aliases for integer-like types
34
+ ENUM = :int # :nodoc:
35
+ BOOL = :int # :nodoc:
36
+ GLATTR = :int # :nodoc:
37
+
38
+
39
+ LIL_ENDIAN = 1234
40
+ BIG_ENDIAN = 4321
41
+
42
+
43
+
44
+ # SDL.h
45
+
46
+ class Version < NiceFFI::Struct
47
+ layout( :major, :uint8,
48
+ :minor, :uint8,
49
+ :patch, :uint8 )
50
+ end
51
+
52
+ sdl_func :Linked_Version, [], Version.typed_pointer
53
+
54
+
55
+ INIT_TIMER = 0x00000001
56
+ INIT_AUDIO = 0x00000010
57
+ INIT_VIDEO = 0x00000020
58
+ INIT_CDROM = 0x00000100
59
+ INIT_JOYSTICK = 0x00000200
60
+ INIT_NOPARACHUTE = 0x00100000
61
+ INIT_EVENTTHREAD = 0x01000000
62
+ INIT_EVERYTHING = 0x0000FFFF
63
+
64
+ sdl_func :Init, [ :uint32 ], :int
65
+ sdl_func :InitSubSystem, [ :uint32 ], :int
66
+ sdl_func :QuitSubSystem, [ :uint32 ], :void
67
+ sdl_func :WasInit, [ :uint32 ], :uint32
68
+ sdl_func :Quit, [ ], :void
69
+
70
+
71
+
72
+ # SDL_active.h
73
+
74
+ APPMOUSEFOCUS = 0x01
75
+ APPINPUTFOCUS = 0x02
76
+ APPACTIVE = 0x04
77
+
78
+ sdl_func :GetAppState, [ ], :uint8
79
+
80
+
81
+
82
+ # SDL_cpuinfo.h
83
+
84
+ sdl_func :HasRDTSC, [ ], SDL::BOOL
85
+ sdl_func :HasMMX, [ ], SDL::BOOL
86
+ sdl_func :HasMMXExt, [ ], SDL::BOOL
87
+ sdl_func :Has3DNow, [ ], SDL::BOOL
88
+ sdl_func :Has3DNowExt, [ ], SDL::BOOL
89
+ sdl_func :HasSSE, [ ], SDL::BOOL
90
+ sdl_func :HasSSE2, [ ], SDL::BOOL
91
+ sdl_func :HasAltiVec, [ ], SDL::BOOL
92
+
93
+
94
+
95
+ # SDL_error.h
96
+
97
+ sdl_func :SetError, [ :string, :varargs ], :void
98
+ sdl_func :GetError, [ ], :string
99
+ sdl_func :ClearError, [ ], :void
100
+
101
+ ENOMEM = 0
102
+ EFREAD = 1
103
+ EFWRITE = 2
104
+ EFSEEK = 3
105
+ UNSUPPORTED = 4
106
+ LASTERROR = 5
107
+
108
+ sdl_func :Error, [ SDL::ENUM ], :void
109
+
110
+
111
+
112
+ # SDL_loadso.h
113
+
114
+ sdl_func :LoadObject, [ :string ], :pointer
115
+ sdl_func :LoadFunction, [ :pointer, :string ], :pointer
116
+ sdl_func :UnloadObject, [ :pointer ], :void
117
+
118
+
119
+
120
+ # SDL_thread.h
121
+
122
+ sdl_func :CreateThread, [ callback([:pointer], :int), :pointer ], :pointer
123
+
124
+ sdl_func :ThreadID, [ ], :uint32
125
+ sdl_func :GetThreadID, [ :pointer ], :uint32
126
+ sdl_func :WaitThread, [ :pointer, :pointer ], :void
127
+ sdl_func :KillThread, [ :pointer ], :void
128
+
129
+ end
@@ -0,0 +1,375 @@
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
+ RELEASED = 0
34
+ PRESSED = 1
35
+
36
+ NOEVENT = 0
37
+ ACTIVEEVENT = 1
38
+ KEYDOWN = 2
39
+ KEYUP = 3
40
+ MOUSEMOTION = 4
41
+ MOUSEBUTTONDOWN = 5
42
+ MOUSEBUTTONUP = 6
43
+ JOYAXISMOTION = 7
44
+ JOYBALLMOTION = 8
45
+ JOYHATMOTION = 9
46
+ JOYBUTTONDOWN = 10
47
+ JOYBUTTONUP = 11
48
+ QUIT = 12
49
+ SYSWMEVENT = 13
50
+ EVENT_RESERVEDA = 14
51
+ EVENT_RESERVEDB = 15
52
+ VIDEORESIZE = 16
53
+ VIDEOEXPOSE = 17
54
+ EVENT_RESERVED2 = 18
55
+ EVENT_RESERVED3 = 19
56
+ EVENT_RESERVED4 = 20
57
+ EVENT_RESERVED5 = 21
58
+ EVENT_RESERVED6 = 22
59
+ EVENT_RESERVED7 = 23
60
+ USEREVENT = 24
61
+ NUMEVENTS = 32
62
+
63
+
64
+ ACTIVEEVENTMASK = (1 << (ACTIVEEVENT))
65
+ JOYAXISMOTIONMASK = (1 << (JOYAXISMOTION))
66
+ JOYEVENTMASK = (1 << (JOYAXISMOTION)) | \
67
+ (1 << (JOYBALLMOTION)) | \
68
+ (1 << (JOYHATMOTION)) | \
69
+ (1 << (JOYBUTTONDOWN)) | \
70
+ (1 << (JOYBUTTONUP))
71
+ JOYBALLMOTIONMASK = (1 << (JOYBALLMOTION))
72
+ JOYBUTTONDOWNMASK = (1 << (JOYBUTTONDOWN))
73
+ JOYBUTTONUPMASK = (1 << (JOYBUTTONUP))
74
+ JOYHATMOTIONMASK = (1 << (JOYHATMOTION))
75
+ KEYDOWNMASK = (1 << (KEYDOWN))
76
+ KEYEVENTMASK = (1 << (KEYDOWN))|(1 << (KEYUP))
77
+ KEYUPMASK = (1 << (KEYUP))
78
+ MOUSEBUTTONDOWNMASK = (1 << (MOUSEBUTTONDOWN))
79
+ MOUSEBUTTONUPMASK = (1 << (MOUSEBUTTONUP))
80
+ MOUSEMOTIONMASK = (1 << (MOUSEMOTION))
81
+ MOUSEEVENTMASK = (1 << (MOUSEMOTION)) | \
82
+ (1 << (MOUSEBUTTONDOWN)) | \
83
+ (1 << (MOUSEBUTTONUP))
84
+ QUITMASK = (1 << (QUIT))
85
+ SYSWMEVENTMASK = (1 << (SYSWMEVENT))
86
+ VIDEOEXPOSEMASK = (1 << (VIDEOEXPOSE))
87
+ VIDEORESIZEMASK = (1 << (VIDEORESIZE))
88
+ ALLEVENTS = 0xFFFFFFFF
89
+
90
+
91
+
92
+ class ActiveEvent < NiceFFI::Struct
93
+ layout( :type, :uint8,
94
+ :gain, :uint8,
95
+ :state, :uint8 )
96
+ end
97
+
98
+
99
+ class KeyboardEvent < NiceFFI::Struct
100
+ layout( :type, :uint8,
101
+ :which, :uint8,
102
+ :state, :uint8,
103
+ :keysym, SDL::Keysym )
104
+ end
105
+
106
+
107
+ class MouseMotionEvent < NiceFFI::Struct
108
+ layout( :type, :uint8,
109
+ :which, :uint8,
110
+ :state, :uint8,
111
+ :x, :uint16,
112
+ :y, :uint16,
113
+ :xrel, :int16,
114
+ :yrel, :int16 )
115
+ end
116
+
117
+
118
+ class MouseButtonEvent < NiceFFI::Struct
119
+ layout( :type, :uint8,
120
+ :which, :uint8,
121
+ :button, :uint8,
122
+ :state, :uint8,
123
+ :x, :uint16,
124
+ :y, :uint16 )
125
+ end
126
+
127
+
128
+ class JoyAxisEvent < NiceFFI::Struct
129
+ layout( :type, :uint8,
130
+ :which, :uint8,
131
+ :axis, :uint8,
132
+ :value, :int16 )
133
+ end
134
+
135
+
136
+ class JoyBallEvent < NiceFFI::Struct
137
+ layout( :type, :uint8,
138
+ :which, :uint8,
139
+ :ball, :uint8,
140
+ :xrel, :int16,
141
+ :yrel, :int16 )
142
+ end
143
+
144
+
145
+ class JoyHatEvent < NiceFFI::Struct
146
+ layout( :type, :uint8,
147
+ :which, :uint8,
148
+ :hat, :uint8,
149
+ :value, :uint8 )
150
+ end
151
+
152
+
153
+ class JoyButtonEvent < NiceFFI::Struct
154
+ layout( :type, :uint8,
155
+ :which, :uint8,
156
+ :button, :uint8,
157
+ :state, :uint8 )
158
+ end
159
+
160
+
161
+ class ResizeEvent < NiceFFI::Struct
162
+ layout( :type, :uint8,
163
+ :w, :int,
164
+ :h, :int )
165
+ end
166
+
167
+
168
+ class ExposeEvent < NiceFFI::Struct
169
+ layout( :type, :uint8 )
170
+ end
171
+
172
+
173
+ class QuitEvent < NiceFFI::Struct
174
+ layout( :type, :uint8 )
175
+ end
176
+
177
+
178
+ class UserEvent < NiceFFI::Struct
179
+ layout( :type, :uint8,
180
+ :code, :int,
181
+ :data1, :pointer,
182
+ :data2, :pointer )
183
+ end
184
+
185
+
186
+ class SysWMEvent < NiceFFI::Struct
187
+ layout( :type, :uint8,
188
+ :msg, :pointer )
189
+ end
190
+
191
+
192
+ class Event < FFI::Union
193
+ layout( :type, :uint8,
194
+ :active, SDL::ActiveEvent,
195
+ :key, SDL::KeyboardEvent,
196
+ :motion, SDL::MouseMotionEvent,
197
+ :button, SDL::MouseButtonEvent,
198
+ :jaxis, SDL::JoyAxisEvent,
199
+ :jball, SDL::JoyBallEvent,
200
+ :jhat, SDL::JoyHatEvent,
201
+ :jbutton, SDL::JoyButtonEvent,
202
+ :resize, SDL::ResizeEvent,
203
+ :expose, SDL::ExposeEvent,
204
+ :quit, SDL::QuitEvent,
205
+ :user, SDL::UserEvent,
206
+ :syswm, SDL::SysWMEvent )
207
+
208
+
209
+ # Creates a generic Event containing a specific event.
210
+ # You usually don't need to do this, because you can pass
211
+ # specific events directly to SDL::SDL_PushEvent.
212
+ #
213
+ def self.wrap( event )
214
+ self.new( event.pointer )
215
+ end
216
+
217
+
218
+ # Extracts a specific event class from a generic Event.
219
+ def unwrap
220
+ case self[:type]
221
+ when ACTIVEEVENT; ActiveEvent.new(self.pointer)
222
+ when KEYDOWN, KEYUP; KeyboardEvent.new(self.pointer)
223
+ when MOUSEMOTION; MouseMotionEvent.new(self.pointer)
224
+
225
+ when MOUSEBUTTONDOWN, MOUSEBUTTONUP;
226
+ MouseButtonEvent.new(self.pointer)
227
+
228
+ when JOYAXISMOTION; JoyAxisEvent.new(self.pointer)
229
+ when JOYBALLMOTION; JoyBallEvent.new(self.pointer)
230
+ when JOYHATMOTION; JoyHatEvent.new(self.pointer)
231
+
232
+ when JOYBUTTONDOWN, JOYBUTTONUP;
233
+ JoyButtonEvent.new(self.pointer)
234
+
235
+ when QUIT; QuitEvent.new( self.pointer )
236
+ when SYSWMEVENT; SysWMEvent.new( self.pointer )
237
+ when VIDEORESIZE; ResizeEvent.new( self.pointer )
238
+ when VIDEOEXPOSE; ExposeEvent.new( self.pointer )
239
+ when USEREVENT; UserEvent.new( self.pointer )
240
+
241
+ else; raise TypeError, "Invalid event #{self.inspect}"
242
+ end
243
+ end
244
+
245
+ def inspect
246
+ super.gsub(">", " :type=#{self[:type]}>")
247
+ end
248
+
249
+ end
250
+
251
+
252
+ sdl_func :PumpEvents, [ ], :void
253
+
254
+
255
+ ADDEVENT = 0
256
+ PEEKEVENT = 1
257
+ GETEVENT = 2
258
+
259
+ func :__SDL_PeepEvents, "SDL_PeepEvents",
260
+ [ :pointer, :int, SDL::ENUM, :uint32 ], :int
261
+
262
+
263
+ # Behavior varies depending on action.
264
+ #
265
+ # PEEKEVENT or GETEVENT:
266
+ # events is the max number of events to retrieve.
267
+ # Returns an array of Events, or nil if there was an error.
268
+ # GETEVENT removes them from the queue, PEEKEVENT leaves them.
269
+ #
270
+ # ADDEVENT:
271
+ # events is an array of Events (or specific event instances)
272
+ # to append to the queue.
273
+ # Returns the number of events added, or -1 if there was an error.
274
+ #
275
+ def self.PeepEvents( events, action, mask )
276
+ # PeepEvents is very versatile, so we break it up into
277
+ # different actions...
278
+
279
+ case action
280
+
281
+ # Append the given events to the queue, return number added.
282
+ when ADDEVENT
283
+ numevents = events.size
284
+ mp = FFI::Buffer.new( SDL::Event, numevents )
285
+
286
+ # Dump the events into the Buffer as raw, hardcore bytes
287
+ events.each_with_index do |ev, i|
288
+ mp[i].put_bytes( 0, ev.pointer.get_bytes(0, ev.size) )
289
+ end
290
+
291
+ return __SDL_PeepEvents( mp, numevents, action, mask )
292
+
293
+ # Peek or Get the first N events and return them in an array.
294
+ # Peek does not remove them from the queue, but Get does.
295
+ when PEEKEVENT, GETEVENT
296
+ numevents = events.to_i
297
+ mp = FFI::Buffer.new( SDL::Event, numevents )
298
+ n = __SDL_PeepEvents( mp, numevents, action, mask )
299
+
300
+ # Something went wrong
301
+ return nil if( n == -1 )
302
+
303
+ events = []
304
+ n.times do |i|
305
+ events << Event.new( mp[i] ).unwrap
306
+ end
307
+
308
+ return events
309
+ end
310
+ end
311
+
312
+
313
+ func :__SDL_PollEvent, "SDL_PollEvent", [ :pointer ], :int
314
+
315
+ def self.PollEvent()
316
+ mp = FFI::MemoryPointer.new( SDL::Event, 1 )
317
+ n = __SDL_PollEvent( mp )
318
+ if n == 0
319
+ nil
320
+ else
321
+ Event.new(mp).unwrap
322
+ end
323
+ end
324
+
325
+
326
+ func :__SDL_WaitEvent, "SDL_WaitEvent", [ :pointer ], :int
327
+
328
+ def self.WaitEvent()
329
+ mp = FFI::MemoryPointer.new( SDL::Event, 1 )
330
+ n = __SDL_WaitEvent( mp )
331
+ if n == 0
332
+ nil
333
+ else
334
+ _extract_event( Event.new(mp) )
335
+ end
336
+ end
337
+
338
+
339
+ sdl_func :PushEvent, [ :pointer ], :int
340
+
341
+
342
+
343
+ callback(:eventfilter_cb, [ :pointer ], :int)
344
+
345
+ func :__SDL_SetEventFilter, "SDL_SetEventFilter",
346
+ [ :eventfilter_cb ], :void
347
+
348
+ def self.SetEventFilter( &block )
349
+ if( block_given? )
350
+ proc = Proc.new { |ev|
351
+ result = block.call( Event.new(ev).unwrap )
352
+ case result
353
+ when true; 1
354
+ when false, nil; 0
355
+ else; result
356
+ end
357
+ }
358
+ __SDL_SetEventFilter( proc )
359
+ else
360
+ __SDL_SetEventFilter( nil )
361
+ end
362
+ end
363
+
364
+
365
+ #sdl_func :GetEventFilter, [ ], :eventfilter_cb
366
+
367
+
368
+ QUERY = -1
369
+ IGNORE = 0
370
+ DISABLE = 0
371
+ ENABLE = 1
372
+
373
+ sdl_func :EventState, [ :uint8, :int ], :uint8
374
+
375
+ end