sdl2_ffi 0.0.4 → 0.0.5

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 (92) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -0
  3. data/Gemfile +6 -0
  4. data/Guardfile +47 -25
  5. data/Rakefile +6 -0
  6. data/bin/approvals +16 -0
  7. data/bin/autospec +16 -0
  8. data/bin/htmldiff +16 -0
  9. data/bin/ldiff +16 -0
  10. data/bin/nokogiri +16 -0
  11. data/bin/rspec +16 -0
  12. data/lib/enumerable_constants.rb +23 -8
  13. data/lib/sdl2.rb +116 -39
  14. data/lib/sdl2/color.rb +19 -7
  15. data/lib/sdl2/events.rb +184 -85
  16. data/lib/sdl2/gamecontroller.rb +1 -1
  17. data/lib/sdl2/gem_version.rb +1 -1
  18. data/lib/sdl2/joystick.rb +1 -1
  19. data/lib/sdl2/keyboard.rb +3 -0
  20. data/lib/sdl2/mouse.rb +5 -5
  21. data/lib/sdl2/pixel_format.rb +11 -3
  22. data/lib/sdl2/pixels.rb +40 -40
  23. data/lib/sdl2/point.rb +17 -1
  24. data/lib/sdl2/power.rb +12 -3
  25. data/lib/sdl2/rect.rb +19 -0
  26. data/lib/sdl2/render.rb +3 -3
  27. data/lib/sdl2/scancode.rb +7 -0
  28. data/lib/sdl2/stdinc.rb +2 -0
  29. data/lib/sdl2/surface.rb +64 -12
  30. data/lib/sdl2/ttf.rb +52 -66
  31. data/lib/sdl2/ttf/font.rb +115 -0
  32. data/lib/sdl2/video.rb +23 -23
  33. data/lib/sdl2/window.rb +15 -15
  34. data/sdl2_ffi.gemspec +5 -2
  35. data/spec/fixtures/approvals/lazyfoonet_lesson_01_hello_world/draws_hello_to_the_window_surface.approved.png +0 -0
  36. data/spec/fixtures/approvals/lazyfoonet_lesson_01_hello_world/loaded_and_optimizes_hello_bitmap.approved.png +0 -0
  37. data/spec/fixtures/approvals/lazyfoonet_lesson_02_optimized_images/draws_the_message_and_background.approved.png +0 -0
  38. data/spec/fixtures/approvals/lazyfoonet_lesson_03_extension_libraries/should_blit_a_png_to_screen.approved.png +0 -0
  39. data/spec/fixtures/approvals/lazyfoonet_lesson_04_event_driven_programming/draws_something_to_the_screen.approved.png +0 -0
  40. data/spec/fixtures/approvals/lazyfoonet_lesson_05_color_keying/draws_the_sprite_using_a_color_key.approved.png +0 -0
  41. data/spec/fixtures/approvals/lazyfoonet_lesson_06_clip_blitting_and_sprite_sheets/draws_the_clipped_sprites_to_the_screen.approved.png +0 -0
  42. data/spec/fixtures/approvals/lazyfoonet_lesson_07_true_type_fonts/draws_the_message_to_the_screen.approved.png +0 -0
  43. data/spec/fixtures/approvals/lazyfoonet_lesson_07_true_type_fonts/writes_a_message_to_a_surface.approved.png +0 -0
  44. data/spec/fixtures/approvals/lazyfoonet_lesson_08_key_presses/rendered_the_down_message.approved.png +0 -0
  45. data/spec/fixtures/approvals/lazyfoonet_lesson_08_key_presses/rendered_the_left_message.approved.png +0 -0
  46. data/spec/fixtures/approvals/lazyfoonet_lesson_08_key_presses/rendered_the_right_message.approved.png +0 -0
  47. data/spec/fixtures/approvals/lazyfoonet_lesson_08_key_presses/rendered_the_up_message.approved.png +0 -0
  48. data/spec/fixtures/approvals/sdl2_color/looks_blue_when_it_should.approved.png +0 -0
  49. data/spec/fixtures/approvals/sdl2_color/looks_green_when_it_should.approved.png +0 -0
  50. data/spec/fixtures/approvals/sdl2_color/looks_red_when_it_should.approved.png +0 -0
  51. data/spec/fixtures/approvals/sdl2_color/should_cast_arrays_properly.approved.png +0 -0
  52. data/spec/fixtures/approvals/sdl2_color/should_cast_hashes_properly.approved.png +0 -0
  53. data/spec/fixtures/fonts/FreeMono.ttf +0 -0
  54. data/spec/fixtures/fonts/GaroaHackerClubeBold.otf +0 -0
  55. data/spec/fixtures/fonts/GaroaHackerClubeBold.otf.credits.yaml +4 -0
  56. data/spec/fixtures/images/an_example.png +0 -0
  57. data/spec/fixtures/images/background.bmp +0 -0
  58. data/spec/fixtures/images/background.jpg +0 -0
  59. data/spec/fixtures/images/background.png +0 -0
  60. data/spec/fixtures/images/foo.jpg +0 -0
  61. data/spec/fixtures/images/hello.bmp +0 -0
  62. data/spec/fixtures/images/sprites.jpg +0 -0
  63. data/spec/fixtures/images/tile.png +0 -0
  64. data/spec/fixtures/images/x.png +0 -0
  65. data/spec/functional/lazy_foo_tutorial/lazy_foo_01_hello_world_spec.rb +39 -0
  66. data/spec/functional/lazy_foo_tutorial/lazy_foo_02_optimized_images_spec.rb +51 -0
  67. data/spec/functional/lazy_foo_tutorial/lazy_foo_03_extension_libraries_spec.rb +31 -0
  68. data/spec/functional/lazy_foo_tutorial/lazy_foo_04_event_driven_programming_spec.rb +55 -0
  69. data/spec/functional/lazy_foo_tutorial/lazy_foo_05_color_keying_spec.rb +41 -0
  70. data/spec/functional/lazy_foo_tutorial/lazy_foo_06_clip_blitting_and_sprite_sheets_spec.rb +41 -0
  71. data/spec/functional/lazy_foo_tutorial/lazy_foo_07_true_type_fonts_spec.rb +56 -0
  72. data/spec/functional/lazy_foo_tutorial/lazy_foo_08_key_presses_spec.rb +120 -0
  73. data/spec/functional/lazy_foo_tutorial/lazy_foo_helper.rb +6 -0
  74. data/spec/png_writer.rb +21 -0
  75. data/spec/spec_helper.rb +23 -0
  76. data/spec/unit/sdl2/color_spec.rb +46 -0
  77. data/spec/unit/unit_helper.rb +3 -0
  78. data/test/approvals/clip_blitting_and_sprite_sheets.received.bmp +0 -0
  79. data/test/approvals/color_keying_example.approved.png +0 -0
  80. data/test/approvals/hello_world_example_hello.approved.png +0 -0
  81. data/test/approvals/hello_world_example_screen.approved.png +0 -0
  82. data/test/approvals/optimized_surface_loading.approved.png +0 -0
  83. data/test/fixtures/background.jpg +0 -0
  84. data/test/fixtures/foo.jpg +0 -0
  85. data/test/fixtures/sprites.jpg +0 -0
  86. data/test/fixtures/x.png +0 -0
  87. data/test/functional/examples/lazyfoo.net_s/test_clip_blitting_and_sprite_sheets.rb +50 -0
  88. data/test/functional/examples/lazyfoo.net_s/test_color_keying.rb +42 -0
  89. data/test/functional/examples/test_lazy_foo_examples.rb +120 -48
  90. data/test/test_helper.rb +9 -0
  91. metadata +138 -4
  92. data/graph +0 -566
@@ -0,0 +1,41 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/image'
4
+
5
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson05/index.php
6
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
7
+ describe "LazyFoo.net: Lesson 05: Color Keying" do
8
+
9
+ before do
10
+ SDL2::init!(:EVERYTHING)
11
+ @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
12
+ @screen = @window.surface
13
+
14
+ @background = @screen.convert(Image.load!(img_path('background.jpg')))
15
+
16
+ @foo = @screen.convert(Image.load!(img_path('foo.jpg')))
17
+
18
+ @foo.color_key = @foo.format.map_rgb([0, 0xff, 0xff])
19
+
20
+ @screen.blit_in(@background)
21
+ @screen.blit_in(@foo)
22
+
23
+ @window.update_surface
24
+
25
+
26
+ end
27
+
28
+ after do
29
+ @background.free
30
+ @foo.free
31
+ quit()
32
+
33
+ end
34
+
35
+ it "draws the sprite using a color key" do
36
+ verify(format: :png) do
37
+ @screen
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,41 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson04/index.php
4
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
5
+ describe "LazyFoo.net: Lesson 06: Clip Blitting and Sprite Sheets" do
6
+ before do
7
+ SDL2.init!(:EVERYTHING)
8
+ @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
9
+ @screen = @window.surface
10
+
11
+ @sprites = Array.new(4) do |idx|
12
+ SDL2::Rect.cast(
13
+ x: (idx % 2) * 100,
14
+ y: (idx / 2) * 100,
15
+ w: 100, h: 100
16
+ )
17
+ end
18
+
19
+ @dots = @screen.convert(Image.load(img_path('sprites.jpg')))
20
+ @dots.color_key = {r: 0, g: 0xFF, b: 0xFF}
21
+
22
+ @screen.fill_rect(@screen.rect, {r: 0xFF, g: 0xFF, b: 0xFF})
23
+
24
+ @screen.blit_in(@dots, @sprites[0], {x: 0, y: 0})
25
+ @screen.blit_in(@dots, @sprites[1], {x: 540, y: 0})
26
+ @screen.blit_in(@dots, @sprites[2], {x: 0, y: 380})
27
+ @screen.blit_in(@dots, @sprites[3], {x: 540, y: 380})
28
+
29
+ @window.update_surface
30
+ end
31
+
32
+ after do
33
+ @dots.free
34
+
35
+ quit()
36
+ end
37
+
38
+ it "draws the clipped sprites to the screen" do
39
+ verify(){@screen}
40
+ end
41
+ end
@@ -0,0 +1,56 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/ttf'
4
+
5
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson07/index.php
6
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
7
+ describe "LazyFoo.net: Lesson 07: True Type Fonts" do
8
+
9
+ before do
10
+
11
+ #binding.pry
12
+ SDL2.init!(:EVERYTHING)
13
+ @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
14
+ @screen = @window.surface
15
+ TTF.init!
16
+
17
+ @background = @screen.convert(Image.load(img_path('background.png')))
18
+
19
+ font_path = fixture('fonts/GaroaHackerClubeBold.otf')
20
+
21
+ @font = TTF::Font.open(font_path, 64)
22
+
23
+ #@textColor = Color.cast({r: 255,g: 255,b: 255,a: 255})
24
+ @textColor = Color.cast(r: 255, g: 0, b: 0, a: 255)
25
+
26
+ @message = @font.render_text_blended_wrapped("The quick brown fox jumps over the lazy dog", 480, @textColor)
27
+ #@message = @screen.convert(@message)
28
+
29
+ @background.blit_out(@screen, @screen.rect)
30
+ @message.blit_out(@screen, x: 0, y: 150)
31
+
32
+ @window.update_surface
33
+
34
+ #delay(1000)
35
+ end
36
+
37
+ after do
38
+ @background.free
39
+ @message.free
40
+ @font.free
41
+ quit()
42
+ end
43
+
44
+ it "loads a true type font" do
45
+ expect(@font).to be_a SDL2::TTF::Font
46
+ end
47
+
48
+ it "writes a message to a surface" do
49
+ verify(){@message}
50
+ end
51
+
52
+ it "draws the message to the screen" do
53
+ verify(){@screen}
54
+ end
55
+
56
+ end
@@ -0,0 +1,120 @@
1
+ require_relative 'lazy_foo_helper'
2
+
3
+ require 'sdl2/ttf'
4
+
5
+ #ORIGINAL: http://lazyfoo.net/SDL_tutorials/lesson08/index.php
6
+ # Adapted for Ruby & SDL 2.0 as functional test by BadQuanta
7
+ describe "LazyFoo.net: Lesson 08: Key Presses" do
8
+
9
+ before do
10
+ SDL2.init!(:EVERYTHING)
11
+ @window = Window.create(subject, :CENTERED, :CENTERED, 640, 480)
12
+ @screen = @window.surface
13
+
14
+ @background = @screen.convert(Image.load!(img_path('background.png')))
15
+
16
+ @font = TTF::Font.open(fixture('fonts/GaroaHackerClubeBold.otf'),28)
17
+
18
+ @upMessage = @font.render_text_solid("Up was pressed")
19
+ @downMessage = @font.render_text_solid("Down was pressed")
20
+ @leftMessage = @font.render_text_solid("Left was pressed")
21
+ @rightMessage = @font.render_text_solid("Right was pressed")
22
+ @quit = false
23
+
24
+ @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)
44
+ ]
45
+
46
+ @background.blit_out(@screen) # Clear screen.
47
+
48
+ enum = @events.each
49
+ until @quit do
50
+ @message = nil
51
+
52
+ while (event = Event.poll())
53
+ puts event.type
54
+
55
+ if event.type == :KEYDOWN
56
+ case event.key.keysym.sym
57
+ when :UP then
58
+ @message = @upMessage
59
+ when :DOWN then
60
+ @message = @downMessage
61
+ when :LEFT then
62
+ @message = @leftMessage
63
+ when :RIGHT then
64
+ @message = @rightMessage
65
+ end
66
+ elsif event.type == :QUIT
67
+ @quit = true
68
+ end
69
+ end
70
+
71
+ unless @message.nil? || @message.null?
72
+ @background.blit_out(@screen) # Draw over old message
73
+
74
+ @message.blit_out(@screen, {
75
+ x: (@screen.w - @message.w) / 2,
76
+ y: (@screen.h - @message.h) / 2
77
+ })
78
+
79
+ end
80
+
81
+ # Insert some fake events for testing:
82
+ @window.update_surface()
83
+
84
+ # If you want to see it run, uncomment
85
+ #delay(300)
86
+
87
+ #binding.pry
88
+ begin
89
+ SDL2.push_event(SDL2::Event.cast(enum.next))
90
+ rescue StopIteration
91
+ puts "NO MORE EVENTS"
92
+
93
+ end
94
+
95
+ end
96
+
97
+ end
98
+
99
+ it "rendered the up message" do
100
+ verify(){@upMessage}
101
+ end
102
+
103
+ it "rendered the down message" do
104
+ verify(){@downMessage}
105
+ end
106
+
107
+ it "rendered the left message" do
108
+ verify(){@leftMessage}
109
+ end
110
+
111
+ it "rendered the right message" do
112
+ verify(){@rightMessage}
113
+ end
114
+
115
+
116
+ after do
117
+ quit()
118
+ end
119
+
120
+ end
@@ -0,0 +1,6 @@
1
+ require_relative '../../spec_helper'
2
+
3
+ require 'sdl2'
4
+ require 'sdl2/image'
5
+
6
+ include SDL2
@@ -0,0 +1,21 @@
1
+ require 'approvals'
2
+
3
+ class Approvals::Writers::PngWriter < Approvals::Writers::BinaryWriter
4
+
5
+ WRITE_PROC = Proc.new do |data, file|
6
+ # Bind to prototype
7
+ FileUtils.mkdir_p(File.dirname(file))
8
+ SDL2::Image.save_png(data,file)
9
+ end
10
+
11
+ DETECT_PROC = Proc.new do |subject|
12
+ subject.kind_of? SDL2::Surface
13
+ end
14
+
15
+ def initialize()
16
+ super(format: :png, extension: 'png', write: WRITE_PROC, detect: DETECT_PROC)
17
+ end
18
+
19
+ end
20
+
21
+ Approvals::Writers::PngWriter.instance # Should register writer.
@@ -0,0 +1,23 @@
1
+ require 'approvals'
2
+ require 'approvals/rspec'
3
+ require_relative 'png_writer'
4
+ require 'sdl2/image'
5
+ require 'pry'
6
+
7
+ ImagesPath = File.expand_path('../fixtures/images', __FILE__)
8
+
9
+ def img_path(filename)
10
+ path = File.expand_path(filename, ImagesPath)
11
+ raise "Missing Image File: #{path}!" unless File.exist?(path)
12
+ return path
13
+ end
14
+
15
+ FixturesPath = File.expand_path('../fixtures',__FILE__)
16
+
17
+ def fixture(filename)
18
+ path = File.expand_path(filename, FixturesPath)
19
+ raise "Missing Fixture File: #{path}!" unless File.exist?(path)
20
+ return path
21
+ end
22
+
23
+
@@ -0,0 +1,46 @@
1
+ require_relative '../unit_helper'
2
+
3
+ describe SDL2::Color do
4
+ before :each do
5
+ SDL2.init!(:EVERYTHING)
6
+ @window = SDL2::Window.create(subject.to_s, :CENTERED, :CENTERED, 100, 100)
7
+ @screen = @window.surface
8
+ @color = SDL2::Color.new
9
+ end
10
+
11
+ it 'looks RED when it should' do
12
+ @color.set(255,0,0,255)
13
+ end
14
+
15
+ it 'looks GREEN when it should' do
16
+ @color.set(0,255,0,255)
17
+ end
18
+
19
+ it 'looks BLUE when it should' do
20
+ @color.set(0,0,255,255)
21
+ end
22
+
23
+ it 'should cast hashes properly' do
24
+ @color = SDL2::Color.cast({r: 255, g: 0, b: 0, a: 255})
25
+ expect(@color.r).to eq(255)
26
+ expect(@color.g).to eq(0)
27
+ expect(@color.b).to eq(0)
28
+ expect(@color.a).to eq(255)
29
+ end
30
+
31
+
32
+ it 'should cast arrays properly' do
33
+ @color = SDL2::Color.cast([0, 255, 255, 255])
34
+ expect(@color.r).to eq(0)
35
+ expect(@color.g).to eq(255)
36
+ expect(@color.b).to eq(255)
37
+ expect(@color.a).to eq(255)
38
+ end
39
+
40
+ after :each do
41
+ @screen.fill_rect(@screen.rect, @color)
42
+ @window.update_surface()
43
+ verify(){@screen}
44
+ SDL2.quit()
45
+ end
46
+ end
@@ -0,0 +1,3 @@
1
+ require_relative '../spec_helper'
2
+
3
+ # TODO: Refactor common unit testing needs here.
Binary file
Binary file
Binary file
@@ -0,0 +1,50 @@
1
+ require_relative '../../../test_helper'
2
+
3
+ describe "LazyFoo.net's Examples" do
4
+
5
+ it "runs the clip blitting and sprite sheets example" do
6
+
7
+ require 'sdl2'
8
+ require 'sdl2/image'
9
+ SDL2.init(:EVERYTHING)
10
+
11
+ @window = SDL2::Window.create('Clip blitting and sprite sheets',
12
+ :CENTERED,:CENTERED,640,480)
13
+
14
+ @screen = @window.surface
15
+
16
+ clip = Array.new{SDL2::Rect.new}
17
+
18
+ sprite_rect = Array.new(4)do |idx|
19
+ s = SDL2::Rect.new
20
+ s.x= (idx % 2)* 100
21
+ s.y= (idx / 2) *100
22
+ s.w= 100
23
+ s.h= 100
24
+ s
25
+ end
26
+
27
+ puts sprite_rect[0]
28
+ puts sprite_rect[1]
29
+ puts sprite_rect[2]
30
+ puts sprite_rect[3]
31
+
32
+ dots = @screen.convert(SDL2::Image.load(fixture('sprites.jpg')))
33
+
34
+ @screen.fill_rect(@screen.clip_rect, [0xFF, 0xFF, 0xFF])
35
+
36
+ #binding.pry
37
+ @screen.blit_in(dots, [0,0], sprite_rect[0])
38
+ @screen.blit_in(dots, [540,0], sprite_rect[1])
39
+ @screen.blit_in(dots, [0, 380], sprite_rect[2])
40
+ @screen.blit_in(dots, [540, 380], sprite_rect[3])
41
+
42
+ @window.update_surface
43
+
44
+ Approvals.verify(@screen, format: :bmp, name: "clip blitting and sprite sheets")
45
+
46
+ SDL2.quit()
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,42 @@
1
+ require_relative '../../../test_helper'
2
+
3
+ def path_for(something)
4
+ File.expand_path(something, FIXTURE_DIR)
5
+ end
6
+
7
+ describe "Color Keying" do
8
+ it "runs LazyFoo.net's Color Keying example" do
9
+ require 'sdl2'
10
+ require 'sdl2/image'
11
+
12
+ def load_image(filename)
13
+ loadedImage = SDL2::Image.load!(path_for(filename))
14
+ optimizedImage = @screen.convert(loadedImage)
15
+ loadedImage.free
16
+
17
+ return optimizedImage
18
+ end
19
+
20
+ @window = SDL2::Window.create("Color Keying",:CENTERED,:CENTERED,640,480)
21
+ @screen = @window.surface
22
+
23
+ background = load_image('background.jpg')
24
+
25
+ foo = load_image('foo.jpg')
26
+
27
+ foo.color_key = foo.format.map_rgb([0, 0xFF, 0xFF])
28
+
29
+ @screen.blit_in(background)
30
+ @screen.blit_in(foo)
31
+
32
+ @window.update_surface
33
+
34
+ Approvals.verify(@screen, format: :png, name: "Color Keying Example")
35
+
36
+ background.free
37
+ foo.free
38
+
39
+ SDL2.quit()
40
+ end
41
+
42
+ end