sdl2_ffi 0.0.3 → 0.0.4

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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/README.md +31 -17
  4. data/bin/yard +16 -0
  5. data/bin/yardoc +16 -0
  6. data/bin/yri +16 -0
  7. data/graph +566 -0
  8. data/lib/enumerable_constants.rb +82 -0
  9. data/lib/sdl2.rb +74 -117
  10. data/lib/sdl2/audio.rb +10 -7
  11. data/lib/sdl2/clipboard.rb +3 -3
  12. data/lib/sdl2/events.rb +228 -168
  13. data/lib/sdl2/gem_version.rb +1 -1
  14. data/lib/sdl2/haptic.rb +366 -55
  15. data/lib/sdl2/hints.rb +36 -24
  16. data/lib/sdl2/image.rb +4 -10
  17. data/lib/sdl2/init.rb +21 -31
  18. data/lib/sdl2/joystick.rb +15 -12
  19. data/lib/sdl2/keycode.rb +261 -261
  20. data/lib/sdl2/library.rb +96 -0
  21. data/lib/sdl2/mouse.rb +22 -17
  22. data/lib/sdl2/pixel_format.rb +2 -1
  23. data/lib/sdl2/pixels.rb +114 -161
  24. data/lib/sdl2/rect.rb +14 -10
  25. data/lib/sdl2/render.rb +29 -13
  26. data/lib/sdl2/renderer.rb +9 -2
  27. data/lib/sdl2/rwops.rb +11 -7
  28. data/lib/sdl2/scancode.rb +246 -245
  29. data/lib/sdl2/stdinc.rb +213 -0
  30. data/lib/sdl2/surface.rb +23 -7
  31. data/lib/sdl2/ttf.rb +23 -19
  32. data/lib/sdl2/version.rb +8 -2
  33. data/lib/sdl2/video.rb +64 -73
  34. data/lib/sdl2/window.rb +143 -36
  35. data/lib/sdl2_ffi.rb +2 -1
  36. data/sdl2_ffi.gemspec +3 -1
  37. data/test/fixtures/an_example.png +0 -0
  38. data/test/fixtures/background.bmp +0 -0
  39. data/test/fixtures/hello.bmp +0 -0
  40. data/test/functional/examples/test_lazy_foo_examples.rb +123 -0
  41. data/test/functional/examples/test_readme_examples.rb +15 -0
  42. data/test/unit/sdl2/test_haptic.rb +17 -0
  43. data/test/unit/sdl2/test_hints.rb +9 -9
  44. data/test/unit/sdl2/test_init.rb +8 -8
  45. data/test/unit/sdl2/test_log.rb +1 -1
  46. data/test/unit/sdl2/test_pixel_format.rb +3 -1
  47. data/test/unit/sdl2/test_video.rb +3 -3
  48. data/test/unit/sdl2/test_window.rb +4 -4
  49. data/test/unit/test_scratch.rb +2 -2
  50. metadata +37 -16
@@ -7,9 +7,9 @@ require 'sdl2/display/mode'
7
7
 
8
8
  module SDL2
9
9
 
10
- # System Window
11
- # A rectangular area you can blit into.
12
- class Window < Struct
10
+ # Window Flags Constants
11
+ module WINDOW
12
+ include EnumerableConstants
13
13
  FULLSCREEN = 0x00000001
14
14
  OPENGL = 0x00000002
15
15
  SHOWN = 0x00000004
@@ -23,6 +23,66 @@ module SDL2
23
23
  MOUSE_FOCUS = 0x00000400
24
24
  FULLSCREEN_DESKTOP = ( FULLSCREEN | 0x00001000 )
25
25
  FOREIGN = 0x00000800
26
+ end
27
+
28
+ # Used to indicate that you don't care what the window position is.
29
+ module WINDOWPOS
30
+ include EnumerableConstants
31
+ UNEDFINED_MASK = 0x1FFF0000
32
+
33
+ # Used to generate the UNDEFINED constant,
34
+ # which if I understand correctly is just UNDEFINED_MASK anyways.
35
+ # I think this is to encourage divers/applications to write specific
36
+ # undefined codes? `</ramble>`
37
+ def self.undefined_display(x)
38
+ self::UNEDFINED_MASK|x
39
+ end
40
+
41
+ UNDEFINED = undefined_display(0)
42
+
43
+ # SDL_video.h doesn't have much documentation on this,
44
+ # I think it is used internally, but may be useful for applications.
45
+ def self.is_undefined(x)
46
+ (((x)&0xFFFFF00000) == self::UNDEFINED_MASK)
47
+ end
48
+
49
+ CENTERED_MASK = 0x2FFF0000
50
+
51
+ def self.centered_display(x)
52
+ self::CENTERED_MASK | x
53
+ end
54
+
55
+ CENTERED = centered_display(0)
56
+
57
+ def self.is_centered(x)
58
+ (((x)&0xFFFF0000) == self::CENTERED_MASK)
59
+ end
60
+ end
61
+
62
+ # Event subtype for window events
63
+ module WINDOWEVENT
64
+ include EnumerableConstants
65
+ NONE = next_const_value
66
+ SHOWN = next_const_value
67
+ HIDDEN = next_const_value
68
+ EXPOSED = next_const_value
69
+ MOVED = next_const_value
70
+ RESIZED = next_const_value
71
+ SIZE_CHANGED = next_const_value
72
+ MINIMIZED = next_const_value
73
+ MAXIMIZED = next_const_value
74
+ RESTORED = next_const_value
75
+ ENTER = next_const_value
76
+ LEAVE = next_const_value
77
+ FOCUS_GAINED = next_const_value
78
+ FOCUS_LOST = next_const_value
79
+ CLOSE = next_const_value
80
+ end
81
+
82
+ # System Window
83
+ # A rectangular area you can blit into.
84
+ class Window < Struct
85
+ include WINDOW
26
86
 
27
87
  layout :magic, :pointer,
28
88
  :id, :uint32,
@@ -47,53 +107,69 @@ module SDL2
47
107
  :shaper, :pointer, # TODO: WindowShaper.by_ref,
48
108
  :data, :pointer,
49
109
  :driverdata, :pointer,
50
- :prev, Window.by_ref,
51
- :next, Window.by_ref
110
+ :prev, :pointer,
111
+ :next, :pointer
52
112
 
113
+ # A simple wrapper for data objects associated with a window.
53
114
  class Data
54
115
 
116
+ # Create a data object manager for a given window
55
117
  def initialize(for_window)
56
118
  @for_window = for_window
57
119
  end
58
120
 
59
- def [](name)
60
- named(name)
61
- end
62
-
121
+ # Return the data named
63
122
  def named(name)
64
123
  SDL2.get_window_data(@for_window, name.to_s)
65
124
  end
66
125
 
126
+ alias_method :[], :named
127
+
128
+ # Set the data named to value specified.
67
129
  def named=(name, value)
68
130
  SDL2.set_window_data(@for_window, name.to_s, value.to_s)
69
131
  end
132
+
133
+ alias_method :[]=, :named=
70
134
  end
71
135
 
136
+ # The Window's data manager.
72
137
  attr_reader :data
73
138
 
139
+ # Construct a new window.
74
140
  def initialize(*args, &block)
75
141
  super(*args, &block)
76
142
  @data = Data.new(self)
77
143
  end
78
144
 
145
+ # Construct a new window with given:
146
+ # @param title: The caption to use for the window
147
+ # @param x: The x-position of the window
148
+ # @param y: The y-position of the window
149
+ # @param w: The width of the window
150
+ # @param h: The height of the window
151
+ # @param flags: Window Flags to use in construction
79
152
  def self.create(title ='', x = 0, y = 0, w = 100, h = 100, flags = 0)
80
- SDL2.create_window(title, x, y, w, h, flags)
153
+ SDL2.create_window!(title, x, y, w, h, flags)
81
154
  end
82
155
 
156
+ # Constructs a new window from arbitrary system-specific structure
157
+ # @param data: Some system-specific pointer
158
+ # See SDL Documentation
83
159
  def self.create_from(data)
84
- create_window_from(data)
160
+ create_window_from!(data)
85
161
  end
86
162
 
163
+ # Returns the identified window already created
164
+ # @param id: The window identifier to retrieve
87
165
  def self.from_id(id)
88
- get_window_from_id(id)
89
- end
90
-
91
- def self.create!(*args)
92
- creation = create(*args)
93
- get_error() if creation.null?
94
- return creation
166
+ get_window_from_id!(id)
95
167
  end
96
168
 
169
+ # Constructs both a window and a renderer
170
+ # @param w: The Width of the pair to create
171
+ # @param h: The Height of the pair to create
172
+ # @param flags: Window flags to utilize in creation
97
173
  def self.create_with_renderer(w, h, flags)
98
174
  window = Window.new
99
175
  renderer = Renderer.new
@@ -104,18 +180,22 @@ module SDL2
104
180
  end
105
181
  end
106
182
 
183
+ # Release memory utilized by structure
107
184
  def self.release(pointer)
108
185
  destroy_window(pointer)
109
186
  end
110
187
 
188
+ # Return the brightness level
111
189
  def brightness
112
190
  SDL2.get_window_brightness(self)
113
191
  end
114
192
 
193
+ # Set the brightness level
115
194
  def brightness=(level)
116
195
  SDL2.set_window_brightness(self, level.to_f)
117
196
  end
118
197
 
198
+ # Get a copy of the DisplayMode structure
119
199
  def display_mode
120
200
  dm = SDL2::Display::Mode.new
121
201
  if SDL2.get_window_display_mode(self, dm) == 0
@@ -126,82 +206,93 @@ module SDL2
126
206
  end
127
207
  end
128
208
 
209
+ # Get the display index associated with this window
129
210
  def display_index
130
211
  SDL2.get_window_display_index(self)
131
212
  end
132
213
 
214
+ # Get the display associated with this window
133
215
  def display
134
216
  Display[display_index]
135
217
  end
136
218
 
219
+ # Get the window flags
137
220
  def flags
138
221
  SDL2.get_window_flags(self)
139
222
  end
140
223
 
224
+ # The window's input grab mode
141
225
  def grab?
142
- get_window_grab(self) == :true
226
+ SDL2.get_window_grab?(self)
143
227
  end
144
228
 
229
+ # Set the input grab mode
145
230
  def grab=(value)
146
- unless value == :true or value == :false
147
- value = value ? :true : :false
148
- end
149
- set_window_grab(self, value)
231
+ SDL2.set_window_grab(self, value)
150
232
  end
151
233
 
234
+ # Get the window identifier
152
235
  def id
153
236
  SDL2.get_window_id(self)
154
237
  end
155
238
 
239
+ # Get the window pixel format
156
240
  def pixel_format
157
241
  SDL2.get_window_pixel_format(self)
158
242
  end
159
243
 
244
+ # Get the window title caption
160
245
  def title
161
246
  SDL2.get_window_title(self)
162
247
  end
163
248
 
249
+ # Set the window title caption
164
250
  def title=(value)
165
251
  SDL2.set_window_title(self, value)
166
252
  end
167
253
 
254
+ # Hide the window
168
255
  def hide
169
256
  SDL2.hide_window(self)
170
257
  end
171
258
 
259
+ # Maximize the window
172
260
  def maximize
173
261
  SDL2.maximize_window(self)
174
262
  end
175
263
 
264
+ # Minimize the window
176
265
  def minimize
177
266
  SDL2.minimize_window(self)
178
267
  end
179
268
 
269
+ # Raise the window
180
270
  def raise_above
181
271
  SDL2.raise_window(self)
182
272
  end
183
273
 
274
+ # Restore the window
184
275
  def restore
185
276
  SDL2.restore_window(self)
186
277
  end
187
-
278
+
279
+ # Show the window
188
280
  def show
189
281
  SDL2.show_window(self)
190
282
  end
191
283
 
284
+ # Set the window's icon from a surface
192
285
  def icon=(surface)
193
286
  set_window_icon(self, surface)
194
287
  end
195
288
 
289
+ # Update the window's surface
196
290
  def update_surface()
197
- SDL2.update_window_surface(self)
198
- end
199
-
200
- def update_surface!()
201
- SDL2.raise_error_unless update_surface == 0
202
- return 0
291
+ SDL2.update_window_surface!(self)
203
292
  end
204
293
 
294
+ # Get the window's current size
295
+ # @return Array => [<width>, <height>]
205
296
  def current_size()
206
297
  w_struct, h_struct = IntStruct.new, IntStruct.new
207
298
  SDL2::get_window_size(self, w_struct, h_struct)
@@ -209,10 +300,14 @@ module SDL2
209
300
  [w, h]
210
301
  end
211
302
 
303
+ # Set the window's current size
304
+ # @param size: A array containing the [width,height]
212
305
  def current_size=(size)
213
306
  SDL2.set_window_size(self, size[0], size[1])
214
307
  end
215
308
 
309
+ # Get the window's maximum_size
310
+ # @return Array => [<width>, <height>]
216
311
  def maximum_size
217
312
  w_struct, h_struct = IntStruct.new, IntStruct.new
218
313
  SDL2::get_window_maximum_size(self, w_struct, h_struct)
@@ -220,21 +315,29 @@ module SDL2
220
315
  [w, h]
221
316
  end
222
317
 
318
+ # Set the window's maximum size
319
+ # @param size: A array containing the [width,height]
223
320
  def maximum_size=(size)
224
321
  SDL2.set_window_maximum_size(self, size[0], size[1])
225
322
  end
226
323
 
324
+ # Get the window's minimum size
325
+ # @return Array => [<width>, <height>]
227
326
  def minimum_size
228
327
  w_struct, h_struct = IntStruct.new, IntStruct.new
229
328
  SDL2::get_window_minimum_size(self, w_struct, h_struct)
230
- w, h = w_struct[:value], h_struct[:value]
329
+ w, h = w_struct[:value], h_struct[:value]
231
330
  [w, h]
232
331
  end
233
332
 
333
+ # Set the window's minimum size
334
+ # @param size: A array containing the [width,height]
234
335
  def minimum_size=(size)
235
336
  SDL2.set_window_minimum_size(self, size[0], size[1])
236
- end
237
-
337
+ end
338
+
339
+ # Get the window's position
340
+ # @return Array => [<x>, <y>]
238
341
  def position
239
342
  position = [IntStruct.new, IntStruct.new]
240
343
  SDL2::get_window_position(self, position[0], position[1])
@@ -242,15 +345,19 @@ module SDL2
242
345
  position.each{|struct|struct.pointer.free}
243
346
  [x, y]
244
347
  end
245
-
348
+
349
+ # Set the window's position
350
+ # @param size: A array containing the [x,y]
246
351
  def position=(location)
247
352
  SDL2::set_window_position(self, location[0],location[1])
248
353
  end
249
-
354
+
355
+ # Return the surface associated with the window
250
356
  def surface
251
357
  SDL2.get_window_surface(self)
252
358
  end
253
-
359
+
360
+ # Set the window's FULLSCREEN mode flags.
254
361
  def fullscreen=(flags)
255
362
  SDL2.set_window_fullscreen(self, flags)
256
363
  end
@@ -1,6 +1,7 @@
1
1
  require 'sdl2'
2
2
  require 'ffi'
3
3
 
4
- # Alias the module, in case of confusion?
4
+ # Simple ALIAS of SDL2
5
5
  SDL2_FFI = SDL2
6
+ # Simple ALIAS of SDL2
6
7
  Sdl2_Ffi = SDL2
@@ -19,7 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'ffi'
22
- spec.add_dependency 'yinum'
23
22
  spec.add_dependency 'activesupport'
24
23
 
25
24
  spec.add_development_dependency 'bundler', '~> 1.3'
@@ -28,4 +27,7 @@ Gem::Specification.new do |spec|
28
27
  spec.add_development_dependency 'pry'
29
28
  spec.add_development_dependency 'guard'
30
29
  spec.add_development_dependency 'guard-minitest'
30
+ spec.add_development_dependency 'yard'
31
+
32
+
31
33
  end
Binary file
@@ -0,0 +1,123 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe "Hello World example translated from: http://lazyfoo.net/SDL_tutorials/lesson01/index2.php" do
4
+
5
+ BMP = Hash.new(){ | bmps, key | bmps[key] = File.expand_path("#{key}.bmp", FIXTURE_DIR) }
6
+ PNG = Hash.new(){ | bmps, key | bmps[key] = File.expand_path("#{key}.png", FIXTURE_DIR) }
7
+
8
+
9
+ # Functional Translated from:
10
+ # http://lazyfoo.net/SDL_tutorials/lesson01/index2.php
11
+ it "runs LazyFoo.net's Hello World example" do
12
+
13
+ require 'sdl2'
14
+
15
+ hello = nil
16
+ window = nil
17
+ screen = nil
18
+
19
+ # BANG(!) versions of methods Raise RuntimeErrors if needed.
20
+ SDL2.init!(:EVERYTHING)
21
+
22
+ window = SDL2::Window.create('Hello World', :UNDEFINED, :UNDEFINED, 640, 480, :SHOWN)
23
+
24
+
25
+ screen = window.surface
26
+
27
+ #binding.pry
28
+ hello = SDL2::Surface.load_bmp(BMP[:hello])
29
+
30
+ screen.blit_in(hello)
31
+
32
+ window.update_surface
33
+
34
+ #SDL2::delay(2000)
35
+
36
+ SDL2::quit() # Since the Quit function can't fail, there is no BANG(!) version.
37
+
38
+ end
39
+
40
+ # Functional Example Translated from:
41
+ # http://lazyfoo.net/SDL_tutorials/lesson02/index.php
42
+ it "runs LazyFoo.net's Optimized Surface Loading and Blitting " do
43
+
44
+ width = 640
45
+ height = 480
46
+
47
+ message = nil
48
+ background = nil
49
+ SDL2.init!(:EVERYTHING)
50
+
51
+ window = SDL2::Window.create("Optimized Images!", :CENTERED, :CENTERED, width, height)
52
+ @screen = window.surface
53
+
54
+
55
+ def load_image(filename)
56
+ loadedImage = SDL2.load_bmp!(filename)
57
+ optimizedImage = @screen.convert(loadedImage)
58
+ loadedImage.free
59
+ return optimizedImage
60
+ end
61
+
62
+ def apply_surface(x, y, source, dest)
63
+ offset = SDL2::Rect.new
64
+ offset.x, offset.y = x,y
65
+ offset.w, offset.h = source.w, source.h
66
+ source.blit_out(dest, offset)
67
+ end
68
+
69
+
70
+
71
+ message = load_image(BMP[:hello])
72
+ background = load_image(BMP[:background])
73
+
74
+ apply_surface(0,0, background, @screen)
75
+ apply_surface(320,0, background, @screen)
76
+ apply_surface(0,240, background, @screen)
77
+ apply_surface(320,240, background, @screen)
78
+
79
+ apply_surface(180, 140, message, @screen)
80
+
81
+
82
+ window.update_surface
83
+
84
+ # SDL2.delay(2000) # Watch it for a sec.
85
+
86
+ message.free
87
+ background.free
88
+
89
+ SDL2.quit()
90
+
91
+ end
92
+
93
+ it "runs LazyFoo.net's SDL Extension Libraries" do
94
+ require 'sdl2/image'
95
+
96
+ SDL2.init! :EVERYTHING
97
+
98
+ @window = SDL2::Window.create("Extension Libraries", :CENTERED, :CENTERED, 640, 480)
99
+ @screen = @window.surface
100
+
101
+ # We can load more now, not just BMPs!
102
+ def load_image(filename)
103
+ loadedImage = SDL2::Image.load!(filename)
104
+ optimizedImage = @screen.convert(loadedImage)
105
+ loadedImage.free
106
+ return optimizedImage
107
+ end
108
+
109
+ a_png = load_image(PNG[:an_example])
110
+
111
+ a_png.blit_out(@screen)
112
+
113
+ @window.update_surface
114
+
115
+ SDL2.delay(2000) # sleep 1
116
+
117
+ a_png.free
118
+
119
+ SDL2.quit()
120
+
121
+ end
122
+
123
+ end