sdl2_ffi 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +21 -59
  3. data/lib/sdl2.rb +52 -15
  4. data/lib/sdl2/application.rb +71 -0
  5. data/lib/sdl2/audio.rb +72 -24
  6. data/lib/sdl2/clipboard.rb +9 -3
  7. data/lib/sdl2/color.rb +8 -3
  8. data/lib/sdl2/cpuinfo.rb +30 -10
  9. data/lib/sdl2/engine.rb +52 -0
  10. data/lib/sdl2/engine/block_engine.rb +27 -0
  11. data/lib/sdl2/engine/engines.rb +46 -0
  12. data/lib/sdl2/error.rb +9 -3
  13. data/lib/sdl2/events.rb +68 -6
  14. data/lib/sdl2/gamecontroller.rb +60 -20
  15. data/lib/sdl2/gem_version.rb +1 -1
  16. data/lib/sdl2/gesture.rb +12 -4
  17. data/lib/sdl2/haptic.rb +90 -30
  18. data/lib/sdl2/hints.rb +12 -4
  19. data/lib/sdl2/image.rb +85 -3
  20. data/lib/sdl2/init.rb +15 -5
  21. data/lib/sdl2/joystick.rb +63 -21
  22. data/lib/sdl2/keyboard.rb +101 -17
  23. data/lib/sdl2/keycode.rb +72 -72
  24. data/lib/sdl2/library.rb +7 -0
  25. data/lib/sdl2/log.rb +78 -21
  26. data/lib/sdl2/mixer.rb +651 -0
  27. data/lib/sdl2/mixer/chunk.rb +47 -0
  28. data/lib/sdl2/mixer/lib_paths.rb +8 -0
  29. data/lib/sdl2/mouse.rb +39 -13
  30. data/lib/sdl2/pixels.rb +39 -13
  31. data/lib/sdl2/point.rb +29 -0
  32. data/lib/sdl2/power.rb +3 -1
  33. data/lib/sdl2/rect.rb +94 -19
  34. data/lib/sdl2/render.rb +156 -52
  35. data/lib/sdl2/rwops.rb +60 -20
  36. data/lib/sdl2/surface.rb +85 -28
  37. data/lib/sdl2/syswm.rb +3 -1
  38. data/lib/sdl2/timer.rb +18 -6
  39. data/lib/sdl2/touch.rb +12 -4
  40. data/lib/sdl2/ttf.rb +153 -59
  41. data/lib/sdl2/ttf/font.rb +21 -5
  42. data/lib/sdl2/version.rb +9 -3
  43. data/lib/sdl2/video.rb +210 -70
  44. data/lib/sdl2/window.rb +9 -3
  45. data/spec/fixtures/approvals/lazyfoonet_lesson_07_true_type_fonts/draws_the_message_to_the_screen.approved.png +0 -0
  46. data/spec/fixtures/approvals/lazyfoonet_lesson_07_true_type_fonts/writes_a_message_to_a_surface.approved.png +0 -0
  47. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/defaults_to_mouse_out.approved.png +0 -0
  48. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/has_a_button_sheet.approved.png +0 -0
  49. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/shows_mouse_down.approved.png +0 -0
  50. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/shows_mouse_in.approved.png +0 -0
  51. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/shows_mouse_out.approved.png +0 -0
  52. data/spec/fixtures/approvals/lazyfoonet_lesson_09_mouse_events/shows_mouse_up.approved.png +0 -0
  53. data/spec/fixtures/images/button_sheet.png +0 -0
  54. data/spec/functional/lazy_foo_tutorial/lazy_foo_01_hello_world_spec.rb +9 -2
  55. data/spec/functional/lazy_foo_tutorial/lazy_foo_03_extension_libraries_spec.rb +1 -0
  56. data/spec/functional/lazy_foo_tutorial/lazy_foo_04_event_driven_programming_spec.rb +4 -3
  57. data/spec/functional/lazy_foo_tutorial/lazy_foo_05_color_keying_spec.rb +1 -1
  58. data/spec/functional/lazy_foo_tutorial/lazy_foo_06_clip_blitting_and_sprite_sheets_spec.rb +4 -3
  59. data/spec/functional/lazy_foo_tutorial/lazy_foo_07_true_type_fonts_spec.rb +4 -2
  60. data/spec/functional/lazy_foo_tutorial/lazy_foo_08_key_presses_spec.rb +10 -22
  61. data/spec/functional/lazy_foo_tutorial/lazy_foo_09_mouse_events_spec.rb +121 -0
  62. data/spec/functional/lazy_foo_tutorial/lazy_foo_10_key_states_spec.rb +30 -0
  63. data/spec/functional/lazy_foo_tutorial/lazy_foo_11_playing_sounds_spec.rb +116 -0
  64. data/spec/functional/lazy_foo_tutorial/lazy_foo_12_timing_spec.rb +57 -0
  65. data/spec/spec_helper.rb +3 -1
  66. metadata +31 -2
@@ -12,6 +12,7 @@ describe "LazyFoo.net: Lesson 04: Event Driven Programming" do
12
12
  @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
13
13
 
14
14
  @screen = @window.surface
15
+ @screen.fill_rect(@screen.rect, [0,0,0,SDL2::ALPHA_OPAQUE])
15
16
 
16
17
  @image = @screen.convert(Image.load!(img_path('x.png')))
17
18
 
@@ -22,14 +23,14 @@ describe "LazyFoo.net: Lesson 04: Event Driven Programming" do
22
23
  until @quit do
23
24
 
24
25
  while event = Event.poll()
25
- puts "GOT EVENT TYPE: #{event.type_symbol}"
26
+ #puts "GOT EVENT TYPE: #{event.type}" if SDL2::PrintDebug
26
27
  if (event.type == :QUIT)
27
28
  @quit = true
28
29
  end
29
30
  end
30
31
 
31
- my_quit_event = SDL2::Event.new
32
- my_quit_event.type = SDL2::EVENTTYPE::QUIT
32
+ my_quit_event = Event.cast(QuitEvent.cast(type: :QUIT))
33
+
33
34
 
34
35
  SDL2.push_event!(my_quit_event)
35
36
 
@@ -15,7 +15,7 @@ describe "LazyFoo.net: Lesson 05: Color Keying" do
15
15
 
16
16
  @foo = @screen.convert(Image.load!(img_path('foo.jpg')))
17
17
 
18
- @foo.color_key = @foo.format.map_rgb([0, 0xff, 0xff])
18
+ @foo.color_key = [0, 0xff, 0xff]
19
19
 
20
20
  @screen.blit_in(@background)
21
21
  @screen.blit_in(@foo)
@@ -7,6 +7,7 @@ describe "LazyFoo.net: Lesson 06: Clip Blitting and Sprite Sheets" do
7
7
  SDL2.init!(:EVERYTHING)
8
8
  @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
9
9
  @screen = @window.surface
10
+ @screen.fill_rect(@screen.rect, [0,0,0,ALPHA_OPAQUE])
10
11
 
11
12
  @sprites = Array.new(4) do |idx|
12
13
  SDL2::Rect.cast(
@@ -19,8 +20,8 @@ describe "LazyFoo.net: Lesson 06: Clip Blitting and Sprite Sheets" do
19
20
  @dots = @screen.convert(Image.load(img_path('sprites.jpg')))
20
21
  @dots.color_key = {r: 0, g: 0xFF, b: 0xFF}
21
22
 
22
- @screen.fill_rect(@screen.rect, {r: 0xFF, g: 0xFF, b: 0xFF})
23
-
23
+ @screen.fill_rect(@screen.rect, {r: 0xFF, g: 0xFF, b: 0xFF, a: ALPHA_OPAQUE})
24
+
24
25
  @screen.blit_in(@dots, @sprites[0], {x: 0, y: 0})
25
26
  @screen.blit_in(@dots, @sprites[1], {x: 540, y: 0})
26
27
  @screen.blit_in(@dots, @sprites[2], {x: 0, y: 380})
@@ -31,7 +32,7 @@ describe "LazyFoo.net: Lesson 06: Clip Blitting and Sprite Sheets" do
31
32
 
32
33
  after do
33
34
  @dots.free
34
-
35
+
35
36
  quit()
36
37
  end
37
38
 
@@ -23,10 +23,12 @@ describe "LazyFoo.net: Lesson 07: True Type Fonts" do
23
23
  #@textColor = Color.cast({r: 255,g: 255,b: 255,a: 255})
24
24
  @textColor = Color.cast(r: 255, g: 0, b: 0, a: 255)
25
25
 
26
- @message = @font.render_text_blended_wrapped("The quick brown fox jumps over the lazy dog", 480, @textColor)
26
+
27
+ @message = @font.render_text_blended_wrapped("The quick brown fox jumps over the lazy dog", @screen.w, @textColor)
28
+ # Optimize message:
27
29
  #@message = @screen.convert(@message)
28
30
 
29
- @background.blit_out(@screen, @screen.rect)
31
+ @background.blit_out(@screen)
30
32
  @message.blit_out(@screen, x: 0, y: 150)
31
33
 
32
34
  @window.update_surface
@@ -21,26 +21,14 @@ describe "LazyFoo.net: Lesson 08: Key Presses" do
21
21
  @rightMessage = @font.render_text_solid("Right was pressed")
22
22
  @quit = false
23
23
 
24
+ # Simulated user events. If you don't want to simulate, comment line:77
24
25
  @events = [
25
- SDL2::KeyboardEvent.cast(
26
- type: SDL2::EVENTTYPE::KEYDOWN,
27
- keysym: Keysym.cast(sym: SDL2::KEYCODE::UP)
28
- ),
29
- SDL2::KeyboardEvent.cast(
30
- type: SDL2::EVENTTYPE::KEYDOWN,
31
- keysym: Keysym.cast(sym: SDL2::KEYCODE::DOWN)
32
- ),
33
-
34
- SDL2::KeyboardEvent.cast(
35
- type: SDL2::EVENTTYPE::KEYDOWN,
36
- keysym: Keysym.cast(sym: SDL2::KEYCODE::LEFT)
37
- ),
38
-
39
- SDL2::KeyboardEvent.cast(
40
- type: SDL2::EVENTTYPE::KEYDOWN,
41
- keysym: Keysym.cast(sym: SDL2::KEYCODE::RIGHT)
42
- ),
43
- SDL2::QuitEvent.cast(type: SDL2::EVENTTYPE::QUIT)
26
+ SDL2::KeyboardEvent.cast(type: :KEYDOWN, keysym: Keysym.cast(sym: :UP)),
27
+ SDL2::KeyboardEvent.cast(type: :KEYDOWN, keysym: Keysym.cast(sym: :DOWN)),
28
+ SDL2::KeyboardEvent.cast(type: :KEYDOWN, keysym: Keysym.cast(sym: :LEFT)),
29
+ SDL2::KeyboardEvent.cast(type: :KEYDOWN, keysym: Keysym.cast(sym: :RIGHT)),
30
+ SDL2::QuitEvent.cast(type: :QUIT)
31
+
44
32
  ]
45
33
 
46
34
  @background.blit_out(@screen) # Clear screen.
@@ -50,7 +38,7 @@ describe "LazyFoo.net: Lesson 08: Key Presses" do
50
38
  @message = nil
51
39
 
52
40
  while (event = Event.poll())
53
- puts event.type
41
+ #puts event.type
54
42
 
55
43
  if event.type == :KEYDOWN
56
44
  case event.key.keysym.sym
@@ -86,9 +74,9 @@ describe "LazyFoo.net: Lesson 08: Key Presses" do
86
74
 
87
75
  #binding.pry
88
76
  begin
89
- SDL2.push_event(SDL2::Event.cast(enum.next))
77
+ SDL2::Event.push(enum.next)
90
78
  rescue StopIteration
91
- puts "NO MORE EVENTS"
79
+ #puts "NO MORE EVENTS"
92
80
 
93
81
  end
94
82
 
@@ -0,0 +1,121 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/application'
4
+
5
+ class ButtonEngine < Engine
6
+ OVER = 0
7
+ OUT = 1
8
+ DOWN = 2
9
+ UP = 3
10
+
11
+ def initialize(opts)
12
+ super(opts)
13
+ @box = Rect.cast(opts)
14
+ @button_sheet = Image.load!(img_path('button_sheet.png'))
15
+ @clips = Array.new(4) do |idx|
16
+ Rect.cast(x: (idx % 2) * 320,y: (idx / 2) * 240,w: 320, h: 240)
17
+ end#@clip
18
+
19
+ #initial clip state:
20
+ @clip = @clips[OUT]
21
+
22
+ on({type: :MOUSEMOTION}) do |event|
23
+
24
+ offset = Point.cast([event.motion.x, event.motion.y])
25
+ if @box.enclose_points(offset)
26
+ puts "Mouse OVER"if SDL2::PrintDebug
27
+ @clip = @clips[OVER]if SDL2::PrintDebug
28
+ else
29
+ puts "Mouse OUT" if SDL2::PrintDebug
30
+ @clip = @clips[OUT]
31
+ end
32
+ end#on :MOUSEMOTION
33
+
34
+ on(
35
+ {type: :MOUSEBUTTONDOWN, button: {button: Mouse::BUTTON::LEFT}},
36
+ {type: :MOUSEBUTTONUP, button: {button: Mouse::BUTTON::LEFT}}
37
+ ) do |event|
38
+ offset = Point.cast([event.button.x, event.button.y])
39
+ if @box.enclose_points(offset)
40
+ if event.type == :MOUSEBUTTONDOWN
41
+ @clip = @clips[DOWN]
42
+ elsif event.type == :MOUSEBUTTONUP
43
+ @clip = @clips[UP]
44
+ else
45
+ raise "UNKOWN EVENT TYPE: #{event.type}"
46
+ end
47
+ end
48
+ end#on :MOUSEBUTTON*
49
+
50
+ end# initialize
51
+
52
+ def paint_to(surface)
53
+ if @clip
54
+ puts "Painting"if SDL2::PrintDebug
55
+
56
+ @button_sheet.blit_out(surface, @box, @clip) if @clip
57
+ @clip = nil
58
+ true # signal painting
59
+ else
60
+ false # signal no painting
61
+ end
62
+ end
63
+
64
+ attr_reader :button_sheet
65
+ end#ButtonEngine
66
+
67
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson09/index.php
68
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
69
+ describe "LazyFoo.net: Lesson 09: Mouse Events" do
70
+
71
+ before do
72
+ @application = Application.new()
73
+ @application.window.title = subject
74
+ @button = ButtonEngine.new(x: 170, y: 120, w: 320, h: 240)
75
+ @application.engines << @button
76
+ @application.loop(1) #Prime it
77
+ end
78
+
79
+ after do
80
+ @application.quit()
81
+ end
82
+
83
+ it 'has a button sheet' do
84
+ verify(){@button.button_sheet}
85
+ end
86
+
87
+ it 'shows mouse out' do
88
+ Event.push({type: :MOUSEMOTION, motion: {x: 10, y: 10}})
89
+ #delay(1000)
90
+ @application.loop(1)
91
+ verify(){@application.window.surface}
92
+
93
+ end
94
+
95
+ it 'shows mouse in' do
96
+ Event.push({type: :MOUSEMOTION, motion: {x: 177, y: 122}})
97
+ @application.loop(1)
98
+ verify(){@application.window.surface}
99
+ end
100
+
101
+ it 'shows mouse down' do
102
+ Event.push({type: :MOUSEBUTTONDOWN, button: {button: Mouse::BUTTON::LEFT, x: 177, y: 122}})
103
+ @application.loop(1)
104
+ verify(){@application.window.surface}
105
+ end
106
+
107
+ it 'shows mouse up' do
108
+ Event.push({type: :MOUSEBUTTONUP, button: {button: Mouse::BUTTON::LEFT, x: 177, y: 122}})
109
+ @application.loop(1)
110
+ verify(){@application.window.surface}
111
+ end
112
+
113
+ it 'defaults to mouse out' do
114
+ verify(){@application.window.surface}
115
+ end
116
+
117
+ # If you want to play with it, uncomment this:
118
+ #it 'works' do
119
+ # @application.loop()
120
+ #end
121
+ end
@@ -0,0 +1,30 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/application'
4
+
5
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson10/index.php
6
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
7
+
8
+ describe "LazyFoo.net: Lesson 10: Key States" do
9
+
10
+ before do
11
+
12
+ @application = Application.new
13
+
14
+
15
+ #binding.pry
16
+ end
17
+
18
+ after do
19
+ @application.quit
20
+
21
+ end
22
+
23
+ it "can get the key states" do
24
+ @state = Keyboard.get_state
25
+ expect(@state.count).to eq(512)
26
+ end
27
+
28
+
29
+
30
+ end
@@ -0,0 +1,116 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/mixer'
4
+ require 'sdl2/application'
5
+ require 'sdl2/ttf'
6
+
7
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson11/index.php
8
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
9
+
10
+ describe "LazyFoo.net: Lesson 11: Playing sounds" do
11
+
12
+ class PlayingSoundsEngine < SDL2::Engine
13
+
14
+ def initialize(opts = {})
15
+ # Initialize our engine
16
+ super(opts)
17
+
18
+ # Music & Sound
19
+ Mixer::init!(:FLAC)
20
+ Mixer::open_audio!(MIX::DEFAULT_FREQUENCY, MIX::DEFAULT_FORMAT, 2, 4096)
21
+ # Text/Fonts
22
+ SDL2::TTF::init!()
23
+
24
+ @background = Image.load!(img_path('background.png'))
25
+
26
+ @font = TTF::Font.open(fixture('fonts/GaroaHackerClubeBold.otf'), 30)
27
+
28
+ @music = Mixer::Music.load(fixture('music/beat.wav'))
29
+
30
+ @scratch = Mixer::Chunk.load_wav(fixture('sounds/scratch.wav'))
31
+ @high = Mixer::Chunk.load_wav(fixture('sounds/high.wav'))
32
+ @med = Mixer::Chunk.load_wav(fixture('sounds/medium.wav'))
33
+ @low = Mixer::Chunk.load_wav(fixture('sounds/low.wav'))
34
+
35
+ on({type: :KEYDOWN, key: {keysym: {sym: :N1}}}) do |event|
36
+ puts "Playing Scratch!"
37
+ puts "Played on Channel: #{@scratch.play}"
38
+ true
39
+ end
40
+
41
+ on({type: :KEYDOWN, key: {keysym: {sym: :N2}}}) do |event|
42
+ puts "Playing High!"
43
+ puts "Played on Channel: #{@high.play}"
44
+ true
45
+ end
46
+
47
+ on({type: :KEYDOWN, key: {keysym: {sym: :N3}}}) do |event|
48
+ puts "Playing Medium!"
49
+ puts "Played on Channel: #{@med.play}"
50
+
51
+ true
52
+ end
53
+
54
+ on({type: :KEYDOWN, key: {keysym: {sym: :N4}}}) do |event|
55
+ puts "Playing Low!"
56
+ puts "Played on Channel: #{@low.play}"
57
+ true
58
+ end
59
+
60
+ # Play/Pause/Resume music
61
+ on({type: :KEYDOWN, key: {keysym: {sym: :N9}}}) do |event|
62
+ puts "Play/Pause/Resume Music!"
63
+ unless Mix::Music.playing?
64
+ Mix::play_music!(@music, -1)
65
+ else
66
+ if Mix::Music.paused?
67
+ Mix::Music.resume()
68
+ else
69
+ Mix::Music.pause()
70
+ end
71
+ end
72
+ true
73
+ end
74
+ # Stop the music.
75
+ on({type: :KEYDOWN, key: {keysym: {sym: :N0}}}) do |event|
76
+ puts "Stop Music!"
77
+ Mix::halt_music!()
78
+ true
79
+ end
80
+ end#initialize
81
+
82
+ def paint_to(surface)
83
+ unless @painted
84
+ @background.blit_out(surface)
85
+ @painted = true
86
+ end
87
+ end
88
+ end
89
+
90
+ before do
91
+
92
+ @app = Application.new(title: subject)
93
+ @app.engines << PlayingSoundsEngine.new()
94
+ @screen = @app.window.surface
95
+ @app.window.title = "Monitor Music"
96
+
97
+ end
98
+
99
+ after do
100
+
101
+ @app.quit()
102
+
103
+ Mix::close_audio()
104
+
105
+ #TTF::quit()
106
+
107
+ #SDL2::quit()
108
+
109
+ end
110
+
111
+ it 'loads' do
112
+ #@app.loop(nil, delay: 100)
113
+ pending "Don't know how to test this."
114
+ end
115
+
116
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/ttf'
4
+ require 'sdl2/application'
5
+ #binding.pry
6
+ require 'sdl2/engine/block_engine'
7
+
8
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson12/index.php
9
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
10
+
11
+ describe "LazyFoo.net: Lesson 11: Playing sounds" do
12
+
13
+ before do
14
+ @app = SDL2::Application.new()
15
+
16
+ @app.engines << @engine = SDL2::Engine::BlockEngine.new
17
+
18
+ @running = true
19
+ @start = SDL2::get_ticks()
20
+
21
+ TTF::init!
22
+ @font = SDL2::TTF::Font.open(fixture('fonts/GaroaHackerClubeBold.otf'),64)
23
+
24
+ @app.on({type: :KEYDOWN, key: {keysym: {sym: :S}}}) do
25
+ if @running == true
26
+ @running = false
27
+ @start = 0
28
+ else
29
+ @running = true
30
+ @start = SDL2::get_ticks()
31
+ end
32
+ end
33
+
34
+ @engine.painter = Proc.new do |surface|
35
+ puts "Painting"
36
+ if(@running)
37
+ puts ticks = SDL2::get_ticks() - @start
38
+ time = @font.render_text_blended((ticks/1000).to_s, {r: 255,g: 255,b: 255,a: 255})
39
+ surface.fill_rect(surface.rect, [0,0,0,255])
40
+ time.blit_out(surface)
41
+
42
+ end
43
+ true
44
+ end
45
+
46
+ end
47
+
48
+ after do
49
+
50
+ end
51
+
52
+ it "works" do
53
+ pending "don't know how I should test this..."
54
+ @app.loop(nil, delay: 1000)
55
+ end
56
+
57
+ end