frusdl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,881 @@
1
+ # Copyright (c) 2008, Bjorn De Meyer
2
+ #
3
+ # This software is provided 'as-is', without any express or implied
4
+ # warranty. In no event will the authors be held liable for any damages
5
+ # arising from the use of this software.
6
+ #
7
+ # Permission is granted to anyone to use this software for any purpose,
8
+ # including commercial applications, and to alter it and redistribute it
9
+ # freely, subject to the following restrictions:
10
+ # 1. The origin of this software must not be misrepresented; you must not
11
+ # claim that you wrote the original software. If you use this software
12
+ # in a product, an acknowledgment in the product documentation would be
13
+ # appreciated but is not required.
14
+ #
15
+ # 2. Altered source versions must be plainly marked as such, and must not be
16
+ # misrepresented as being the original software.
17
+ #
18
+ # 3. This notice may not be removed or altered from any source
19
+ # distribution.
20
+
21
+ module Frusdl
22
+ module Low
23
+
24
+ module SDL
25
+ extend FFI::Library
26
+ ffi_lib('SDL') # load SDL library through ffi
27
+
28
+ # Define some constants for the init function
29
+ SDL_INIT_TIMER = 0x00000001
30
+ SDL_INIT_AUDIO = 0x00000010
31
+ SDL_INIT_VIDEO = 0x00000020
32
+ SDL_INIT_CDROM = 0x00000100
33
+ SDL_INIT_JOYSTICK = 0x00000200
34
+ SDL_INIT_NOPARACHUTE = 0x00100000
35
+ SDL_INIT_EVENTTHREAD = 0x01000000
36
+ SDL_INIT_EVERYTHING = 0x0000FFFF
37
+ # And some for video functions
38
+ SDL_SWSURFACE = 0x00000000
39
+ SDL_HWSURFACE = 0x00000001
40
+ SDL_ASYNCBLIT = 0x00000004
41
+ SDL_ANYFORMAT = 0x10000000
42
+ SDL_HWPALETTE = 0x20000000
43
+ SDL_DOUBLEBUF = 0x40000000
44
+ SDL_FULLSCREEN = 0x80000000
45
+ SDL_OPENGL = 0x00000002
46
+ SDL_OPENGLBLIT = 0x0000000A
47
+ SDL_RESIZABLE = 0x00000010
48
+ SDL_NOFRAME = 0x00000020
49
+ # And some more for mouse grabbing
50
+ SDL_GRAB_QUERY = -1
51
+ SDL_GRAB_OFF = 0
52
+ SDL_GRAB_ON = 1
53
+ SDL_GRAB_FULLSCREEN = 2
54
+
55
+
56
+
57
+ class SDL_Rect < FFI::Struct
58
+ layout :x => :short,
59
+ :y => :short,
60
+ :w => :ushort,
61
+ :h => :ushort
62
+ end
63
+
64
+ class SDL_Color < FFI::Struct
65
+ layout :r => :uchar,
66
+ :g => :uchar,
67
+ :b => :uchar,
68
+ :unused => :uchar
69
+ end
70
+
71
+
72
+ class SDL_Palette < FFI::Struct
73
+ layout :ncolors => :int,
74
+ :colors => :pointer
75
+ end
76
+
77
+ class SDL_VideoInfo < FFI::Struct
78
+ layout :bitfields => :ulong,
79
+ :video_mem => :ulong,
80
+ :vfmt => :pointer,
81
+ :current_w => :int,
82
+ :current_h => :int
83
+ end
84
+
85
+ =begin
86
+ # XXX: How to do these bitfields???
87
+ Uint32 hw_available:1;
88
+ Uint32 wm_available:1;
89
+ Uint32 blit_hw:1;
90
+ Uint32 blit_hw_CC:1;
91
+ Uint32 blit_hw_A:1;
92
+ Uint32 blit_sw:1;
93
+ Uint32 blit_sw_CC:1;
94
+ Uint32 blit_sw_A:1;
95
+ Uint32 blit_fill:1;
96
+ Uint32 video_mem;
97
+ SDL_PixelFormat *vfmt;
98
+ int current_w;
99
+ int current_h;
100
+ =end
101
+
102
+
103
+
104
+ class SDL_PixelFormat < FFI::Struct
105
+ layout :palette => :pointer,
106
+ :bitsperpixel => :uchar,
107
+ :bytesperpixel => :uchar,
108
+ :rloss => :uchar,
109
+ :gloss => :uchar,
110
+ :bloss => :uchar,
111
+ :aloss => :uchar,
112
+ :rshift => :uchar,
113
+ :gshift => :uchar,
114
+ :bshift => :uchar,
115
+ :ashift => :uchar,
116
+ :rmask => :uint,
117
+ :gmask => :uint,
118
+ :bmask => :uint,
119
+ :amask => :uint,
120
+ :colorkey => :uint,
121
+ :alpha => :uchar
122
+ end
123
+
124
+ class SDL_Surface < FFI::ManagedStruct
125
+ layout :flags => :ulong ,
126
+ :format => :pointer ,
127
+ :w => :int ,
128
+ :h => :int ,
129
+ :pitch => :short ,
130
+ :pixels => :pointer
131
+ # the rest of the C struct is mostly private
132
+ def self.release(ptr)
133
+ if ptr && (!ptr.null?)
134
+ SDL.SDL_FreeSurface(ptr)
135
+ end
136
+ end
137
+
138
+ end
139
+
140
+ # SDL_audio
141
+ class SDL_AudioSpec < FFI::Struct
142
+ layout :freq => :int, # DSP frequency -- samples per second
143
+ :format => :ushort, # Audio data format
144
+ :channels => :uchar, # Number of channels: 1 mono, 2 stereo
145
+ :silence => :uchar, # Audio buffer silence value (calculated)
146
+ :samples => :ushort, # Audio buffer size in samples (power of 2)
147
+ :padding => :ushort, # Necessary for some compile environments
148
+ :size => :ulong, # Audio buffer size in bytes (calculated)
149
+ :callback => :pointer,
150
+ # This function is called when the audio device needs more data.
151
+ # 'stream' is a pointer to the audio data buffer
152
+ # 'len' is the length of that buffer in bytes.
153
+ # Once the callback returns, the buffer will no longer be valid.
154
+ # Stereo samples are stored in a LRLRLR ordering.
155
+ :userdata => :pointer
156
+ end
157
+
158
+ SDL_LIL_ENDIAN = :little_endian
159
+
160
+ if [-1].pack('V') == [-1].pack('N')
161
+ SDL_BYTEORDER = SDL_LIL_ENDIAN
162
+ else
163
+ SDL_BYTEORDER = SDL_BIG_ENDIAN
164
+ end
165
+
166
+ AUDIO_U8 = 0x0008 # Unsigned 8-bit samples
167
+ AUDIO_S8 = 0x8008 # Signed 8-bit samples
168
+ AUDIO_U16LSB = 0x0010 # Unsigned 16-bit samples
169
+ AUDIO_S16LSB = 0x8010 # Signed 16-bit samples
170
+ AUDIO_U16MSB = 0x1010 # As above, but big-endian byte order
171
+ AUDIO_S16MSB = 0x9010 # As above, but big-endian byte order
172
+ AUDIO_U16 = AUDIO_U16LSB
173
+ AUDIO_S16 = AUDIO_S16LSB
174
+
175
+ # Native audio byte ordering
176
+ if SDL_BYTEORDER == SDL_LIL_ENDIAN
177
+ AUDIO_U16SYS = AUDIO_U16LSB
178
+ AUDIO_S16SYS = AUDIO_S16LSB
179
+ else
180
+ AUDIO_U16SYS = AUDIO_U16MSB
181
+ AUDIO_S16SYS = AUDIO_S16MSB
182
+ end
183
+
184
+ SDL_AUDIO_STOPPED = 0
185
+ SDL_AUDIO_PLAYING = 1
186
+ SDL_AUDIO_PAUSED = 2
187
+ SDL_MIX_MAXVOLUME = 128
188
+
189
+ # SDL_AudioCVT skipped for now
190
+ attach_function :SDL_AudioDriverName, [:pointer, :int], :pointer
191
+ attach_function :SDL_OpenAudio, [:pointer, :pointer ], :int
192
+ attach_function :SDL_GetAudioStatus, [], :int
193
+ attach_function :SDL_PauseAudio, [:int], :void
194
+ attach_function :SDL_LoadWAV_RW, [:pointer, :int, :pointer, :pointer, :pointer], :pointer
195
+
196
+ # Compatibility convenience function -- loads a WAV from a file
197
+ def self.SDL_LoadWAV(filename, spec, audio_buf, audio_len)
198
+ SDL_LoadWAV_RW(SDL_RWFromFile(filename, "rb"),1, spec, audio_buf, audio_len)
199
+ end
200
+
201
+ attach_function :SDL_FreeWAV, [:pointer], :void
202
+ # SDL_BuildAudioCVT skipped
203
+ # SDL_ConvertAudio(SDL_AudioCVT *cvt) skipped
204
+ attach_function :SDL_MixAudio, [:pointer, :pointer, :ulong, :int], :void
205
+ attach_function :SDL_LockAudio, [], :void
206
+ attach_function :SDL_UnlockAudio, [], :void
207
+ attach_function :SDL_CloseAudio, [], :void
208
+
209
+ # SDL_error
210
+ attach_function :SDL_SetError , [:string], :void
211
+ attach_function :SDL_GetError , [], :string
212
+ attach_function :SDL_ClearError , [], :void
213
+
214
+ # SDL_keyboard
215
+ class SDL_keysym < FFI::Struct
216
+ layout :scancode => :uchar, # hardware specific scancode
217
+ :sym => :int, # SDL virtual keysym
218
+ :mod => :int, # current key modifiers
219
+ :unicode => :ushort # translated character
220
+ end
221
+
222
+ attach_function :SDL_EnableUNICODE, [:int], :int
223
+ SDL_DEFAULT_REPEAT_DELAY = 500
224
+ SDL_DEFAULT_REPEAT_INTERVAL = 30
225
+ attach_function :SDL_EnableKeyRepeat, [:int, :int], :int
226
+ attach_function :SDL_GetKeyRepeat , [:pointer, :pointer], :void
227
+ attach_function :SDL_GetKeyState, [:int], :pointer
228
+ attach_function :SDL_GetModState, [], :int
229
+ attach_function :SDL_SetModState, [:int], :void
230
+ attach_function :SDL_GetKeyName, [:int], :string
231
+
232
+ # SDL_keysym (lots of boring constants ahead :p )
233
+ SDLK_UNKNOWN = 0
234
+ SDLK_FIRST = 0
235
+ SDLK_BACKSPACE = 8
236
+ SDLK_TAB = 9
237
+ SDLK_CLEAR = 12
238
+ SDLK_RETURN = 13
239
+ SDLK_PAUSE = 19
240
+ SDLK_ESCAPE = 27
241
+ SDLK_SPACE = 32
242
+ SDLK_EXCLAIM = 33
243
+ SDLK_QUOTEDBL = 34
244
+ SDLK_HASH = 35
245
+ SDLK_DOLLAR = 36
246
+ SDLK_AMPERSAND = 38
247
+ SDLK_QUOTE = 39
248
+ SDLK_LEFTPAREN = 40
249
+ SDLK_RIGHTPAREN = 41
250
+ SDLK_ASTERISK = 42
251
+ SDLK_PLUS = 43
252
+ SDLK_COMMA = 44
253
+ SDLK_MINUS = 45
254
+ SDLK_PERIOD = 46
255
+ SDLK_SLASH = 47
256
+ SDLK_0 = 48
257
+ SDLK_1 = 49
258
+ SDLK_2 = 50
259
+ SDLK_3 = 51
260
+ SDLK_4 = 52
261
+ SDLK_5 = 53
262
+ SDLK_6 = 54
263
+ SDLK_7 = 55
264
+ SDLK_8 = 56
265
+ SDLK_9 = 57
266
+ SDLK_COLON = 58
267
+ SDLK_SEMICOLON = 59
268
+ SDLK_LESS = 60
269
+ SDLK_EQUALS = 61
270
+ SDLK_GREATER = 62
271
+ SDLK_QUESTION = 63
272
+ SDLK_AT = 64
273
+ SDLK_LEFTBRACKET = 91
274
+ SDLK_BACKSLASH = 92
275
+ SDLK_RIGHTBRACKET = 93
276
+ SDLK_CARET = 94
277
+ SDLK_UNDERSCORE = 95
278
+ SDLK_BACKQUOTE = 96
279
+ SDLK_a = 97
280
+ SDLK_b = 98
281
+ SDLK_c = 99
282
+ SDLK_d = 100
283
+ SDLK_e = 101
284
+ SDLK_f = 102
285
+ SDLK_g = 103
286
+ SDLK_h = 104
287
+ SDLK_i = 105
288
+ SDLK_j = 106
289
+ SDLK_k = 107
290
+ SDLK_l = 108
291
+ SDLK_m = 109
292
+ SDLK_n = 110
293
+ SDLK_o = 111
294
+ SDLK_p = 112
295
+ SDLK_q = 113
296
+ SDLK_r = 114
297
+ SDLK_s = 115
298
+ SDLK_t = 116
299
+ SDLK_u = 117
300
+ SDLK_v = 118
301
+ SDLK_w = 119
302
+ SDLK_x = 120
303
+ SDLK_y = 121
304
+ SDLK_z = 122
305
+ SDLK_DELETE = 127
306
+ SDLK_WORLD_0 = 160
307
+ SDLK_WORLD_1 = 161
308
+ SDLK_WORLD_2 = 162
309
+ SDLK_WORLD_3 = 163
310
+ SDLK_WORLD_4 = 164
311
+ SDLK_WORLD_5 = 165
312
+ SDLK_WORLD_6 = 166
313
+ SDLK_WORLD_7 = 167
314
+ SDLK_WORLD_8 = 168
315
+ SDLK_WORLD_9 = 169
316
+ SDLK_WORLD_10 = 170
317
+ SDLK_WORLD_11 = 171
318
+ SDLK_WORLD_12 = 172
319
+ SDLK_WORLD_13 = 173
320
+ SDLK_WORLD_14 = 174
321
+ SDLK_WORLD_15 = 175
322
+ SDLK_WORLD_16 = 176
323
+ SDLK_WORLD_17 = 177
324
+ SDLK_WORLD_18 = 178
325
+ SDLK_WORLD_19 = 179
326
+ SDLK_WORLD_20 = 180
327
+ SDLK_WORLD_21 = 181
328
+ SDLK_WORLD_22 = 182
329
+ SDLK_WORLD_23 = 183
330
+ SDLK_WORLD_24 = 184
331
+ SDLK_WORLD_25 = 185
332
+ SDLK_WORLD_26 = 186
333
+ SDLK_WORLD_27 = 187
334
+ SDLK_WORLD_28 = 188
335
+ SDLK_WORLD_29 = 189
336
+ SDLK_WORLD_30 = 190
337
+ SDLK_WORLD_31 = 191
338
+ SDLK_WORLD_32 = 192
339
+ SDLK_WORLD_33 = 193
340
+ SDLK_WORLD_34 = 194
341
+ SDLK_WORLD_35 = 195
342
+ SDLK_WORLD_36 = 196
343
+ SDLK_WORLD_37 = 197
344
+ SDLK_WORLD_38 = 198
345
+ SDLK_WORLD_39 = 199
346
+ SDLK_WORLD_40 = 200
347
+ SDLK_WORLD_41 = 201
348
+ SDLK_WORLD_42 = 202
349
+ SDLK_WORLD_43 = 203
350
+ SDLK_WORLD_44 = 204
351
+ SDLK_WORLD_45 = 205
352
+ SDLK_WORLD_46 = 206
353
+ SDLK_WORLD_47 = 207
354
+ SDLK_WORLD_48 = 208
355
+ SDLK_WORLD_49 = 209
356
+ SDLK_WORLD_50 = 210
357
+ SDLK_WORLD_51 = 211
358
+ SDLK_WORLD_52 = 212
359
+ SDLK_WORLD_53 = 213
360
+ SDLK_WORLD_54 = 214
361
+ SDLK_WORLD_55 = 215
362
+ SDLK_WORLD_56 = 216
363
+ SDLK_WORLD_57 = 217
364
+ SDLK_WORLD_58 = 218
365
+ SDLK_WORLD_59 = 219
366
+ SDLK_WORLD_60 = 220
367
+ SDLK_WORLD_61 = 221
368
+ SDLK_WORLD_62 = 222
369
+ SDLK_WORLD_63 = 223
370
+ SDLK_WORLD_64 = 224
371
+ SDLK_WORLD_65 = 225
372
+ SDLK_WORLD_66 = 226
373
+ SDLK_WORLD_67 = 227
374
+ SDLK_WORLD_68 = 228
375
+ SDLK_WORLD_69 = 229
376
+ SDLK_WORLD_70 = 230
377
+ SDLK_WORLD_71 = 231
378
+ SDLK_WORLD_72 = 232
379
+ SDLK_WORLD_73 = 233
380
+ SDLK_WORLD_74 = 234
381
+ SDLK_WORLD_75 = 235
382
+ SDLK_WORLD_76 = 236
383
+ SDLK_WORLD_77 = 237
384
+ SDLK_WORLD_78 = 238
385
+ SDLK_WORLD_79 = 239
386
+ SDLK_WORLD_80 = 240
387
+ SDLK_WORLD_81 = 241
388
+ SDLK_WORLD_82 = 242
389
+ SDLK_WORLD_83 = 243
390
+ SDLK_WORLD_84 = 244
391
+ SDLK_WORLD_85 = 245
392
+ SDLK_WORLD_86 = 246
393
+ SDLK_WORLD_87 = 247
394
+ SDLK_WORLD_88 = 248
395
+ SDLK_WORLD_89 = 249
396
+ SDLK_WORLD_90 = 250
397
+ SDLK_WORLD_91 = 251
398
+ SDLK_WORLD_92 = 252
399
+ SDLK_WORLD_93 = 253
400
+ SDLK_WORLD_94 = 254
401
+ SDLK_WORLD_95 = 255
402
+ SDLK_KP0 = 256
403
+ SDLK_KP1 = 257
404
+ SDLK_KP2 = 258
405
+ SDLK_KP3 = 259
406
+ SDLK_KP4 = 260
407
+ SDLK_KP5 = 261
408
+ SDLK_KP6 = 262
409
+ SDLK_KP7 = 263
410
+ SDLK_KP8 = 264
411
+ SDLK_KP9 = 265
412
+ SDLK_KP_PERIOD = 266
413
+ SDLK_KP_DIVIDE = 267
414
+ SDLK_KP_MULTIPLY = 268
415
+ SDLK_KP_MINUS = 269
416
+ SDLK_KP_PLUS = 270
417
+ SDLK_KP_ENTER = 271
418
+ SDLK_KP_EQUALS = 272
419
+ SDLK_UP = 273
420
+ SDLK_DOWN = 274
421
+ SDLK_RIGHT = 275
422
+ SDLK_LEFT = 276
423
+ SDLK_INSERT = 277
424
+ SDLK_HOME = 278
425
+ SDLK_END = 279
426
+ SDLK_PAGEUP = 280
427
+ SDLK_PAGEDOWN = 281
428
+ SDLK_F1 = 282
429
+ SDLK_F2 = 283
430
+ SDLK_F3 = 284
431
+ SDLK_F4 = 285
432
+ SDLK_F5 = 286
433
+ SDLK_F6 = 287
434
+ SDLK_F7 = 288
435
+ SDLK_F8 = 289
436
+ SDLK_F9 = 290
437
+ SDLK_F10 = 291
438
+ SDLK_F11 = 292
439
+ SDLK_F12 = 293
440
+ SDLK_F13 = 294
441
+ SDLK_F14 = 295
442
+ SDLK_F15 = 296
443
+ SDLK_NUMLOCK = 300
444
+ SDLK_CAPSLOCK = 301
445
+ SDLK_SCROLLOCK = 302
446
+ SDLK_RSHIFT = 303
447
+ SDLK_LSHIFT = 304
448
+ SDLK_RCTRL = 305
449
+ SDLK_LCTRL = 306
450
+ SDLK_RALT = 307
451
+ SDLK_LALT = 308
452
+ SDLK_RMETA = 309
453
+ SDLK_LMETA = 310
454
+ SDLK_LSUPER = 311
455
+ SDLK_RSUPER = 312
456
+ SDLK_MODE = 313
457
+ SDLK_COMPOSE = 314
458
+ SDLK_HELP = 315
459
+ SDLK_PRINT = 316
460
+ SDLK_SYSREQ = 317
461
+ SDLK_BREAK = 318
462
+ SDLK_MENU = 319
463
+ SDLK_POWER = 320
464
+ SDLK_EURO = 321
465
+ SDLK_UNDO = 322
466
+ SDLK_LAST = 323
467
+
468
+ KMOD_NONE = 0x0000
469
+ KMOD_LSHIFT = 0x0001
470
+ KMOD_RSHIFT = 0x0002
471
+ KMOD_LCTRL = 0x0040
472
+ KMOD_RCTRL = 0x0080
473
+ KMOD_LALT = 0x0100
474
+ KMOD_RALT = 0x0200
475
+ KMOD_LMETA = 0x0400
476
+ KMOD_RMETA = 0x0800
477
+ KMOD_NUM = 0x1000
478
+ KMOD_CAPS = 0x2000
479
+ KMOD_MODE = 0x4000
480
+ KMOD_RESERVED = 0x8000
481
+ KMOD_CTRL = (KMOD_LCTRL|KMOD_RCTRL)
482
+ KMOD_SHIFT = (KMOD_LSHIFT|KMOD_RSHIFT)
483
+ KMOD_ALT = (KMOD_LALT|KMOD_RALT)
484
+ KMOD_META = (KMOD_LMETA|KMOD_RMETA)
485
+ # Phew, all done! ^_^
486
+
487
+ # SDL_mouse
488
+ class SDL_Cursor < FFI::Struct
489
+ # SDL_Rect area; # need typedef for this. How to do it?
490
+ # Sint16 hot_x, hot_y; /* The "tip" of the cursor */
491
+ # Uint8 *data; /* B/W cursor data */
492
+ # Uint8 *mask; /* B/W cursor mask */
493
+ # Uint8 *save[2]; /* Place to save cursor area */
494
+ # WMcursor *wm_cursor; /* Window-manager cursor */
495
+ end
496
+
497
+ attach_function :SDL_GetMouseState , [:pointer, :pointer], :uchar
498
+ attach_function :SDL_GetRelativeMouseState, [:pointer, :pointer], :uchar
499
+ attach_function :SDL_WarpMouse , [:ushort, :ushort], :void
500
+ attach_function :SDL_CreateCursor , [:pointer, :pointer, :int, :int, :int, :int, ], :pointer
501
+ attach_function :SDL_SetCursor , [:pointer], :void
502
+ attach_function :SDL_FreeCursor , [:pointer], :void
503
+ attach_function :SDL_ShowCursor , [:int], :int
504
+
505
+ SDL_BUTTON_LEFT = 1
506
+ SDL_BUTTON_MIDDLE = 2
507
+ SDL_BUTTON_RIGHT = 3
508
+ SDL_BUTTON_WHEELUP = 4
509
+ SDL_BUTTON_WHEELDOWN = 5
510
+
511
+ # SDL_rwops. We need this because a lot of SDL uses this inside macros
512
+ # which we obviously have to reimplement as Ruby methods.
513
+ class SDL_RWops < FFI::Struct
514
+ layout :seek => :pointer, # is a callback in reality
515
+ :read => :pointer, # is a callback in reality
516
+ :write => :pointer, # is a callback in reality
517
+ :close => :pointer, # is a callback in reality
518
+ :type => :ulong
519
+ # rest is private
520
+ end
521
+
522
+ attach_function :SDL_RWFromFile , [:string, :string], :pointer
523
+ attach_function :SDL_RWFromFP , [:pointer, :int] , :pointer
524
+ attach_function :SDL_RWFromMem , [:pointer, :int] , :pointer
525
+ attach_function :SDL_RWFromConstMem , [:pointer, :int] , :pointer
526
+ attach_function :SDL_AllocRW , [] , :pointer
527
+ attach_function :SDL_FreeRW , [:pointer] , :void
528
+
529
+ RW_SEEK_SET = 0
530
+ RW_SEEK_CUR = 1
531
+ RW_SEEK_END = 2
532
+ # SDL uses macros to "easily" read and write from an SDL_RWops structure
533
+ # unfortunately, we need to use callbacks for this
534
+ def SDL_RWseek(ctx, offset, whence)
535
+ ctx[:seek].call(ctx, offset, whence)
536
+ end
537
+
538
+ def SDL_RWtell(ctx)
539
+ ctx[:seek].call(ctx, 0, RW_SEEK_CUR)
540
+ end
541
+
542
+ def SDL_RWread(ctx, ptr, size, n)
543
+ ctx[:read].call(ctx, ptr, size, n)
544
+ end
545
+
546
+ def SDL_RWwrite(ctx, offset, whence)
547
+ ctx[:write].call(ctx, ptr, size, n)
548
+ end
549
+
550
+ def SDL_RWclose(ctx)
551
+ ctx[:close].call(ctx)
552
+ end
553
+
554
+ # We don't need more of SDL_rwops, because we have ruby's much
555
+ # easier to use File.
556
+
557
+ # SDL_timer
558
+ SDL_TIMESLICE = 10
559
+ TIMER_RESOLUTION = 10
560
+ attach_function :SDL_GetTicks, [:void], :ulong
561
+ # Wait a specified number of milliseconds before returning
562
+ attach_function :SDL_Delay, [:ulong], :void
563
+ attach_function :SDL_SetTimer, [ :ulong, :pointer], :int
564
+ attach_function :SDL_AddTimer, [:ulong, :pointer, :pointer], :pointer
565
+ attach_function :SDL_RemoveTimer, [:pointer], :int
566
+
567
+ # SDL_version
568
+ class SDL_version < FFI::Struct
569
+ layout :major => :uchar,
570
+ :minor => :uchar,
571
+ :patch => :uchar
572
+ end
573
+
574
+ attach_function :SDL_Linked_Version, [], :pointer
575
+
576
+
577
+
578
+ # SDL_event
579
+ SDL_RELEASED = 0
580
+ SDL_PRESSED = 1
581
+ SDL_NOEVENT = 0
582
+ SDL_ACTIVEEVENT = 1
583
+ SDL_KEYDOWN = 2
584
+ SDL_KEYUP = 3
585
+ SDL_MOUSEMOTION = 4
586
+ SDL_MOUSEBUTTONDOWN = 5
587
+ SDL_MOUSEBUTTONUP = 6
588
+ SDL_JOYAXISMOTION = 7
589
+ SDL_JOYBALLMOTION = 8
590
+ SDL_JOYHATMOTION = 9
591
+ SDL_JOYBUTTONDOWN = 10
592
+ SDL_JOYBUTTONUP = 11
593
+ SDL_QUIT = 12
594
+ SDL_SYSWMEVENT = 13
595
+ SDL_EVENT_RESERVEDA = 14
596
+ SDL_EVENT_RESERVEDB = 15
597
+ SDL_VIDEORESIZE = 16
598
+ SDL_VIDEOEXPOSE = 17
599
+ SDL_EVENT_RESERVED2 = 18
600
+ SDL_EVENT_RESERVED3 = 19
601
+ SDL_EVENT_RESERVED4 = 20
602
+ SDL_EVENT_RESERVED5 = 21
603
+ SDL_EVENT_RESERVED6 = 22
604
+ SDL_EVENT_RESERVED7 = 23
605
+ SDL_USEREVENT = 24
606
+ SDL_NUMEVENTS = 32
607
+
608
+ # Application visibility event structure
609
+ class SDL_ActiveEvent < FFI::Struct
610
+ layout :type => :uchar,
611
+ :gain => :uchar,
612
+ :state => :uchar
613
+ end
614
+
615
+
616
+ # Keyboard event structure
617
+ class SDL_KeyboardEvent < FFI::Struct
618
+ layout :type => :uchar,
619
+ :which => :uchar,
620
+ :state => :uchar,
621
+ :keysym => :pointer
622
+ end
623
+
624
+ # Keyboard event structure
625
+ class SDL_MouseMotionEvent < FFI::Struct
626
+ layout :type => :uchar,
627
+ :which => :uchar,
628
+ :state => :uchar,
629
+ :x => :ushort,
630
+ :y => :ushort,
631
+ :xrel => :short,
632
+ :yrel => :short
633
+ end
634
+
635
+ # Mouse button event structure
636
+ class SDL_MouseButtonEvent < FFI::Struct
637
+ layout :type => :uchar,
638
+ :which => :uchar,
639
+ :button => :uchar,
640
+ :state => :uchar,
641
+ :x => :ushort,
642
+ :y => :ushort
643
+ end
644
+
645
+ # Joystick axis motion event structure
646
+ class SDL_JoyAxisEvent < FFI::Struct
647
+ layout :type => :uchar,
648
+ :which => :uchar,
649
+ :axis => :uchar,
650
+ :value => :short
651
+ end
652
+
653
+ # Joystick trackball motion event structure
654
+ class SDL_JoyBallEvent < FFI::Struct
655
+ layout :type => :uchar,
656
+ :which => :uchar,
657
+ :ball => :uchar,
658
+ :xrel => :short,
659
+ :yrel => :short
660
+ end
661
+
662
+ # Joystick hat position change event structure
663
+ class SDL_JoyHatEvent < FFI::Struct
664
+ layout :type => :uchar,
665
+ :which => :uchar,
666
+ :hat => :uchar,
667
+ :value => :uchar
668
+ end
669
+
670
+ # Joystick button event structure
671
+ class SDL_JoyButtonEvent < FFI::Struct
672
+ layout :type => :uchar,
673
+ :which => :uchar,
674
+ :button => :uchar,
675
+ :state => :uchar
676
+ end
677
+
678
+ # The "window resized" event
679
+ class SDL_ResizeEvent < FFI::Struct
680
+ layout :type => :uchar,
681
+ :x => :int,
682
+ :y => :int
683
+ end
684
+
685
+ class SDL_ExposeEvent < FFI::Struct
686
+ layout :type => :uchar
687
+ end
688
+
689
+ class SDL_QuitEvent < FFI::Struct
690
+ layout :type => :uchar
691
+ end
692
+
693
+ # A user-defined event type
694
+ class SDL_UserEvent < FFI::Struct
695
+ layout :type => :uchar,
696
+ :code => :int,
697
+ :data1 => :pointer,
698
+ :data2 => :pointer
699
+ end
700
+
701
+ # General event structure. Cheat a bit because it's a union
702
+ class SDL_Event < FFI::Struct
703
+ layout :type => :uchar,
704
+ :fake1 => :ulong,
705
+ :fake2 => :ulong,
706
+ :fake3 => :ulong,
707
+ :fake4 => :ulong
708
+ end
709
+
710
+ attach_function :SDL_PumpEvents, [], :void
711
+
712
+ SDL_ADDEVENT = 0
713
+ SDL_PEEKEVENT = 1
714
+ SDL_GETEVENT = 2
715
+
716
+ attach_function :SDL_PeepEvents, [:pointer, :int, :int, :ulong], :int
717
+ attach_function :SDL_PollEvent, [:pointer], :int
718
+ attach_function :SDL_WaitEvent, [:pointer], :int
719
+ attach_function :SDL_PushEvent, [:pointer], :int
720
+ attach_function :SDL_SetEventFilter, [:pointer], :void
721
+ attach_function :SDL_GetEventFilter, [], :pointer
722
+ SDL_QUERY = -1
723
+ SDL_IGNORE = 0
724
+ SDL_DISABLE = 0
725
+ SDL_ENABLE = 1
726
+ attach_function :SDL_EventState, [:uchar, :int], :int
727
+
728
+
729
+ class SDL_Joystick < FFI::Struct
730
+ layout :fake => :pointer
731
+ # The struct is private
732
+ def self.release(ptr)
733
+ if ptr && (!ptr.null?)
734
+ SDL.SDL_JoystickClose(ptr)
735
+ end
736
+ end
737
+
738
+ end
739
+
740
+
741
+ # SDL_joystick
742
+ # Count the number of joysticks attached to the system
743
+ attach_function :SDL_NumJoysticks, [], :int
744
+
745
+ # Get the implementation dependent name of a joystick.
746
+ attach_function :SDL_JoystickName, [:int], :string
747
+
748
+ # Open a joystick for use - the index passed as an argument refers to
749
+ # the N'th joystick on the system. This index is the value which will
750
+ # identify this joystick in future joystick events.
751
+ attach_function :SDL_JoystickOpen, [:int], :pointer
752
+
753
+ # Returns 1 if the joystick has been opened, or 0 if it has not.
754
+ attach_function :SDL_JoystickOpened, [:int], :int
755
+
756
+ # Get the device index of an opened joystick.
757
+ attach_function :SDL_JoystickIndex, [:pointer], :int
758
+
759
+ # Get the number of general axis controls on a joystick
760
+ attach_function :SDL_JoystickNumAxes, [:pointer], :int
761
+
762
+ # Get the number of trackballs on a joystick
763
+ attach_function :SDL_JoystickNumBalls, [:pointer], :int
764
+
765
+ # Get the number of POV hats on a joystick
766
+ attach_function :SDL_JoystickNumHats, [:pointer], :int
767
+
768
+ # Get the number of buttons on a joystick
769
+ attach_function :SDL_JoystickNumButtons, [:pointer], :int
770
+
771
+ # Update joystick state
772
+ attach_function :SDL_JoystickUpdate, [], :void
773
+ attach_function :SDL_JoystickEventState, [:int], :int
774
+ attach_function :SDL_JoystickGetAxis, [:pointer, :int], :short
775
+ SDL_HAT_CENTERED = 0x00
776
+ SDL_HAT_UP = 0x01
777
+ SDL_HAT_RIGHT = 0x02
778
+ SDL_HAT_DOWN = 0x04
779
+ SDL_HAT_LEFT = 0x08
780
+ SDL_HAT_RIGHTUP = (SDL_HAT_RIGHT|SDL_HAT_UP)
781
+ SDL_HAT_RIGHTDOWN = (SDL_HAT_RIGHT|SDL_HAT_DOWN)
782
+ SDL_HAT_LEFTUP = (SDL_HAT_LEFT|SDL_HAT_UP)
783
+ SDL_HAT_LEFTDOWN = (SDL_HAT_LEFT|SDL_HAT_DOWN)
784
+ attach_function :SDL_JoystickGetHat, [:pointer, :int], :uchar
785
+ # Get the ball axis change since the last poll
786
+ attach_function :SDL_JoystickGetBall, [:pointer, :int, :pointer, :pointer], :int
787
+ # Get the current state of a button on a joystick The button indices start at 0.
788
+ attach_function :SDL_JoystickGetButton, [:pointer, :int], :uchar
789
+ attach_function :SDL_JoystickClose, [:pointer], :void
790
+
791
+
792
+ # SDL
793
+ attach_function :SDL_Init, [:ulong], :int
794
+ attach_function :SDL_Quit, [], :int
795
+ attach_function :SDL_WasInit, [:ulong], :ulong
796
+
797
+ # getenv/putenv, macro madness
798
+ begin
799
+ attach_function :SDL_getenv, [:string], :string
800
+ attach_function :SDL_putenv, [:string], :int
801
+ rescue FFI::NotFoundError
802
+ # possibly not in the dynamic library on some platforms
803
+ # So use normal getenv/setenv in stead
804
+ def self.SDL_getenv(s)
805
+ return ENV[s]
806
+ end
807
+
808
+ def self.SDL_putenv(s)
809
+ a = s.split('=')
810
+ ENV[a[0]] = a[1]
811
+ return 0
812
+ end
813
+ end
814
+
815
+ # SDL_video
816
+
817
+ attach_function :SDL_SetVideoMode , [:int, :int, :int, :ulong ], :pointer
818
+ attach_function :SDL_GetVideoSurface, [], :pointer
819
+ attach_function :SDL_GetVideoInfo, [], :pointer
820
+
821
+ attach_function :SDL_VideoDriverName, [:pointer, :int], :string
822
+ attach_function :SDL_ListModes , [:pointer, :ulong], :pointer
823
+ attach_function :SDL_VideoModeOK , [:int, :int, :int, :ulong ], :int
824
+
825
+
826
+ attach_function :SDL_Flip , [:pointer], :int
827
+ attach_function :SDL_MapRGB , [:pointer, :uchar, :uchar, :uchar], :ulong
828
+ attach_function :SDL_MapRGBA, [:pointer, :uchar, :uchar, :uchar, :uchar], :ulong
829
+ attach_function :SDL_CreateRGBSurface, [:ulong, :int, :int, :int, :ulong, :ulong, :ulong, :ulong], :pointer
830
+ attach_function :SDL_CreateRGBSurfaceFrom,
831
+ [:pointer, :int, :int, :int, :int, :ulong, :ulong, :ulong, :ulong], :pointer
832
+ attach_function :SDL_FreeSurface, [:pointer], :void
833
+ attach_function :SDL_LockSurface, [:pointer], :int
834
+ attach_function :SDL_UnlockSurface, [:pointer], :void
835
+ attach_function :SDL_LoadBMP_RW, [:pointer, :int], :pointer
836
+ attach_function :SDL_SaveBMP_RW, [:pointer, :pointer, :int], :int
837
+
838
+ attach_function :SDL_UpdateRect, [:pointer, :short, :short, :short, :short], :void
839
+ attach_function :SDL_UpdateRects, [:pointer, :int, :pointer], :void
840
+
841
+ attach_function :SDL_SetGamma , [:float, :float, :float], :int
842
+ attach_function :SDL_SetGammaRamp , [:pointer, :pointer, :pointer], :int
843
+ attach_function :SDL_GetGammaRamp , [:pointer, :pointer, :pointer], :int
844
+
845
+ def self.SDL_LoadBMP(filename)
846
+ SDL_LoadBMP_RW(SDL_RWFromFile(filename, "rb"), 1)
847
+ end
848
+
849
+ def self.SDL_SaveBMP(surface, filename)
850
+ SDL_SaveBMP_RW(surface, SDL_RWFromFile(filename, "wb"), 1)
851
+ end
852
+
853
+ attach_function :SDL_SetColorKey, [:pointer, :ulong, :ulong], :int
854
+ attach_function :SDL_SetAlpha , [:pointer, :ulong, :uchar], :int
855
+ attach_function :SDL_SetClipRect, [:pointer, :pointer], :int
856
+ attach_function :SDL_GetClipRect, [:pointer, :pointer], :void
857
+ attach_function :SDL_ConvertSurface, [:pointer, :pointer, :ulong], :pointer
858
+ attach_function :SDL_UpperBlit, [:pointer, :pointer, :pointer, :pointer], :int
859
+
860
+ def self.SDL_BlitSurface(src, srcrect, dst, dstrect)
861
+ SDL_UpperBlit(src, srcrect, dst, dstrect)
862
+ end
863
+
864
+ attach_function :SDL_FillRect , [:pointer, :pointer, :ulong], :int
865
+ attach_function :SDL_DisplayFormat , [:pointer, :pointer], :pointer
866
+ attach_function :SDL_DisplayFormatAlpha , [:pointer, :pointer], :pointer
867
+ attach_function :SDL_WM_SetCaption , [:string, :string] , :void
868
+ attach_function :SDL_WM_GetCaption , [:pointer, :pointer], :void
869
+ attach_function :SDL_WM_SetIcon , [:pointer, :pointer], :void
870
+ attach_function :SDL_WM_IconifyWindow , [], :int
871
+ attach_function :SDL_WM_ToggleFullScreen, [:pointer], :int
872
+ attach_function :SDL_WM_GrabInput , [:int], :int
873
+
874
+
875
+ end
876
+
877
+
878
+
879
+
880
+ end
881
+ end