danabr75-ashton 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG +0 -0
  3. data/LICENSE +21 -0
  4. data/README.md +95 -0
  5. data/Rakefile +42 -0
  6. data/examples/bloom_example.rb +59 -0
  7. data/examples/lighting_example.rb +127 -0
  8. data/examples/media/Earth.png +0 -0
  9. data/examples/media/LargeStar.png +0 -0
  10. data/examples/media/SmallStar.png +0 -0
  11. data/examples/media/Star.png +0 -0
  12. data/examples/media/Starfighter.bmp +0 -0
  13. data/examples/media/Starfighter.png +0 -0
  14. data/examples/media/simple.png +0 -0
  15. data/examples/noise_example.rb +94 -0
  16. data/examples/outline_example.rb +86 -0
  17. data/examples/particle_emitter_example.rb +114 -0
  18. data/examples/pixelate_example.rb +52 -0
  19. data/examples/pixelated_texture_example.rb +69 -0
  20. data/examples/radial_blur_example.rb +61 -0
  21. data/examples/shader_image_example.rb +75 -0
  22. data/examples/shockwave_example.rb +75 -0
  23. data/examples/stencil_shader_example.rb +104 -0
  24. data/examples/texture_render_example.rb +54 -0
  25. data/examples/tv_screen_and_static_example.rb +60 -0
  26. data/ext/ashton/GLee.c +18170 -0
  27. data/ext/ashton/GLee.h +17648 -0
  28. data/ext/ashton/ashton.c +42 -0
  29. data/ext/ashton/ashton.h +31 -0
  30. data/ext/ashton/color.c +45 -0
  31. data/ext/ashton/color.h +25 -0
  32. data/ext/ashton/common.h +41 -0
  33. data/ext/ashton/extconf.rb +45 -0
  34. data/ext/ashton/fast_math.c +30 -0
  35. data/ext/ashton/fast_math.h +30 -0
  36. data/ext/ashton/font.c +8 -0
  37. data/ext/ashton/font.h +16 -0
  38. data/ext/ashton/gosu.c +18 -0
  39. data/ext/ashton/gosu.h +19 -0
  40. data/ext/ashton/image.c +8 -0
  41. data/ext/ashton/image.h +16 -0
  42. data/ext/ashton/particle_emitter.c +788 -0
  43. data/ext/ashton/particle_emitter.h +171 -0
  44. data/ext/ashton/pixel_cache.c +237 -0
  45. data/ext/ashton/pixel_cache.h +58 -0
  46. data/ext/ashton/shader.c +9 -0
  47. data/ext/ashton/shader.h +16 -0
  48. data/ext/ashton/texture.c +442 -0
  49. data/ext/ashton/texture.h +63 -0
  50. data/ext/ashton/vendor/gl/include/GL/GL.H +1526 -0
  51. data/ext/ashton/window.c +8 -0
  52. data/ext/ashton/window.h +16 -0
  53. data/lib/ashton.rb +38 -0
  54. data/lib/ashton/gosu_ext/color.rb +25 -0
  55. data/lib/ashton/gosu_ext/font.rb +58 -0
  56. data/lib/ashton/gosu_ext/gosu_module.rb +16 -0
  57. data/lib/ashton/gosu_ext/image.rb +96 -0
  58. data/lib/ashton/gosu_ext/window.rb +79 -0
  59. data/lib/ashton/image_stub.rb +33 -0
  60. data/lib/ashton/lighting/light_source.rb +146 -0
  61. data/lib/ashton/lighting/manager.rb +98 -0
  62. data/lib/ashton/mixins/version_checking.rb +23 -0
  63. data/lib/ashton/particle_emitter.rb +87 -0
  64. data/lib/ashton/pixel_cache.rb +24 -0
  65. data/lib/ashton/shader.rb +386 -0
  66. data/lib/ashton/shaders/bloom.frag +41 -0
  67. data/lib/ashton/shaders/color_inversion.frag +11 -0
  68. data/lib/ashton/shaders/contrast.frag +16 -0
  69. data/lib/ashton/shaders/default.frag +22 -0
  70. data/lib/ashton/shaders/default.vert +14 -0
  71. data/lib/ashton/shaders/fade.frag +14 -0
  72. data/lib/ashton/shaders/grayscale.frag +15 -0
  73. data/lib/ashton/shaders/include/classicnoise2d.glsl +113 -0
  74. data/lib/ashton/shaders/include/classicnoise3d.glsl +177 -0
  75. data/lib/ashton/shaders/include/classicnoise4d.glsl +302 -0
  76. data/lib/ashton/shaders/include/noise2d.glsl +70 -0
  77. data/lib/ashton/shaders/include/noise3d.glsl +102 -0
  78. data/lib/ashton/shaders/include/noise4d.glsl +128 -0
  79. data/lib/ashton/shaders/include/rand.glsl +5 -0
  80. data/lib/ashton/shaders/lighting/distort.frag +57 -0
  81. data/lib/ashton/shaders/lighting/draw_shadows.frag +60 -0
  82. data/lib/ashton/shaders/lighting/shadow_blur.frag +60 -0
  83. data/lib/ashton/shaders/mezzotint.frag +22 -0
  84. data/lib/ashton/shaders/multitexture2.vert +19 -0
  85. data/lib/ashton/shaders/outline.frag +45 -0
  86. data/lib/ashton/shaders/pixelate.frag +48 -0
  87. data/lib/ashton/shaders/radial_blur.frag +63 -0
  88. data/lib/ashton/shaders/sepia.frag +26 -0
  89. data/lib/ashton/shaders/shockwave.frag +38 -0
  90. data/lib/ashton/shaders/signed_distance_field.frag +80 -0
  91. data/lib/ashton/shaders/static.frag +25 -0
  92. data/lib/ashton/shaders/stencil.frag +27 -0
  93. data/lib/ashton/shaders/tv_screen.frag +23 -0
  94. data/lib/ashton/signed_distance_field.rb +151 -0
  95. data/lib/ashton/texture.rb +186 -0
  96. data/lib/ashton/version.rb +3 -0
  97. data/lib/ashton/window_buffer.rb +16 -0
  98. data/spec/ashton/ashton_spec.rb +22 -0
  99. data/spec/ashton/gosu_ext/color_spec.rb +34 -0
  100. data/spec/ashton/gosu_ext/font_spec.rb +57 -0
  101. data/spec/ashton/gosu_ext/gosu_spec.rb +11 -0
  102. data/spec/ashton/gosu_ext/image_spec.rb +66 -0
  103. data/spec/ashton/gosu_ext/window_spec.rb +71 -0
  104. data/spec/ashton/image_stub_spec.rb +46 -0
  105. data/spec/ashton/particle_emitter_spec.rb +123 -0
  106. data/spec/ashton/pixel_cache_spec.rb +153 -0
  107. data/spec/ashton/shader_spec.rb +152 -0
  108. data/spec/ashton/signed_distance_field_spec.rb +163 -0
  109. data/spec/ashton/texture_spec.rb +347 -0
  110. data/spec/helper.rb +12 -0
  111. metadata +309 -0
@@ -0,0 +1,46 @@
1
+ require_relative "../helper.rb"
2
+
3
+ describe Ashton::ImageStub do
4
+ describe "#initialize" do
5
+ it "should fail if the blob data is not the expected size" do
6
+ lambda do
7
+ described_class.new "\0\0\0", 2, 3
8
+ end.should raise_error ArgumentError, /Expected blob to be 24 bytes/
9
+ end
10
+
11
+ it "should fail width <= 0" do
12
+ lambda do
13
+ described_class.new "\0\0\0\0", 0, 2
14
+ end.should raise_error ArgumentError, /Width must be >= 1 pixel/
15
+ end
16
+
17
+ it "should fail height <= 0" do
18
+ lambda do
19
+ described_class.new "\0\0\0\0", 2, 0
20
+ end.should raise_error ArgumentError, /Height must be >= 1 pixel/
21
+ end
22
+ end
23
+
24
+ context "instantiated" do
25
+ let(:two_by_three_blob) { 24.times.to_a.pack("C*") }
26
+ let(:subject) { described_class.new two_by_three_blob, 2, 3 }
27
+
28
+ describe "#to_blob" do
29
+ it "should return the blob given" do
30
+ subject.to_blob.should eq two_by_three_blob
31
+ end
32
+ end
33
+
34
+ describe "#columns" do
35
+ it "should equal the width of the blob" do
36
+ subject.columns.should eq 2
37
+ end
38
+ end
39
+
40
+ describe "#rows" do
41
+ it "should equal the height of the blob" do
42
+ subject.rows.should eq 3
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,123 @@
1
+ require_relative "../helper.rb"
2
+
3
+ # Values and their defaults. The deviations default to 0.
4
+ FLOATS_WITH_RANGE = {
5
+ angular_velocity: 0.0..0.0,
6
+ center_x: 0.5..0.5,
7
+ center_y: 0.5..0.5,
8
+ fade: 0.0..0.0,
9
+ friction: 0.0..0.0,
10
+ interval: Float::INFINITY..Float::INFINITY,
11
+ scale: 1.0..1.0,
12
+ speed: 0.0..0.0,
13
+ time_to_live: Float::INFINITY..Float::INFINITY,
14
+ zoom: 0.0..0.0,
15
+ }
16
+
17
+ describe Ashton::ParticleEmitter do
18
+ before :all do
19
+ $window ||= Gosu::Window.new 16, 16, false
20
+ # Need not to create a large number of these.
21
+ @emitter = described_class.new 1, 2, 3
22
+ end
23
+
24
+ let(:default_max) { 1000 }
25
+ let(:subject) { @emitter }
26
+
27
+ describe "initialize" do
28
+ [:x, :y, :z].each.with_index(1) do |attr, value|
29
+ it "should set #{attr}" do
30
+ subject.send(attr).should be_kind_of Float
31
+ subject.send(attr).should eq value
32
+ end
33
+ end
34
+
35
+ FLOATS_WITH_RANGE.each_pair do |attr, expected|
36
+ it "should set #{attr}" do
37
+ subject.send(attr).should be_kind_of Range
38
+ subject.send(attr).should eq expected
39
+ end
40
+ end
41
+
42
+ it "should be set max_particles" do
43
+ subject.max_particles.should be_kind_of Fixnum
44
+ subject.max_particles.should eq default_max
45
+ end
46
+
47
+ it "should not have any particles" do
48
+ subject.count.should eq 0
49
+ end
50
+
51
+ it "should have color set to white" do
52
+ subject.color.should eq Gosu::Color::WHITE
53
+ end
54
+ end
55
+
56
+ [:x, :y, :z].each.with_index(1) do |attr, value|
57
+ describe "#{attr}=" do
58
+ it "should set the value of #{attr}" do
59
+ ->{ subject.send "#{attr}=", value + 5.0 }.should change(subject, attr).from(value).to(value + 5)
60
+ end
61
+ end
62
+ end
63
+
64
+ FLOATS_WITH_RANGE.keys.each do |attr|
65
+ describe "#{attr}=" do
66
+ it "should set #{attr} with a number" do
67
+ subject.send("#{attr}=", 42.0).should eq 42.0
68
+ subject.send("#{attr}").should eq 42.0..42.0
69
+ end
70
+
71
+ it "should set #{attr} with a range" do
72
+ subject.send("#{attr}=", 42.0..99.0).should eq 42.0..99.0
73
+ subject.send("#{attr}").should eq 42.0..99.0
74
+ end
75
+ end
76
+ end
77
+
78
+ describe "color=" do
79
+ it "should set color" do
80
+ (subject.color = Gosu::Color::BLACK).should eq Gosu::Color::BLACK
81
+ subject.color.should eq Gosu::Color::BLACK
82
+ end
83
+ end
84
+
85
+ describe "draw" do
86
+ it "should respond to #draw" do
87
+ subject.draw
88
+ end
89
+ end
90
+
91
+ describe "emit" do
92
+ it "should create one more particle" do
93
+ ->{ subject.emit }.should change(subject, :count).from(0).to(1)
94
+ end
95
+
96
+ it "should replace a particle if we are already at max particles" do
97
+ (default_max + 1).times { subject.emit }
98
+ subject.count.should eq default_max
99
+ end
100
+ end
101
+
102
+ describe "update" do
103
+ it "should an appropriate number of particles" do
104
+ subject = described_class.new 1, 2, 3, interval: 0.001
105
+ mock(subject).emit.times 16
106
+ subject.update 1.0 / 60.0 # 16.66667 ms
107
+ end
108
+
109
+ it "should not emit a particle if delta is less than interval" do
110
+ subject = described_class.new 1, 2, 3, interval: 0.020
111
+ dont_allow(subject).emit
112
+ subject.update 1.0 / 60.0 # 16.66667 ms
113
+ end
114
+
115
+ it "should fail given a negative delta" do
116
+ ->{ subject.update -1 }.should raise_error ArgumentError, "delta must be >= 0"
117
+ end
118
+
119
+ it "should fail for a bad delta type" do
120
+ ->{ subject.update "fred" }.should raise_error TypeError
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,153 @@
1
+ require_relative "../helper.rb"
2
+
3
+ describe Ashton::PixelCache do
4
+ before :all do
5
+ $window ||= Gosu::Window.new 16, 16, false
6
+ end
7
+
8
+ let(:testcard_image) { @testcard_image ||= Gosu::Image.new $window, media_path("simple.png") }
9
+ let(:texture) { Ashton::Texture.new testcard_image }
10
+ let(:subject) { described_class.new texture }
11
+
12
+ describe "owner" do
13
+ it "should remember the owner it was created for" do
14
+ subject.owner.should eq texture
15
+ end
16
+ end
17
+
18
+ describe "refresh" do
19
+ it "should respond to refresh" do
20
+ subject.should respond_to :refresh
21
+ end
22
+ end
23
+
24
+ describe "initialize" do
25
+ it "should cache for a texture class" do
26
+ ->{ described_class.new texture }.should_not raise_error TypeError
27
+ end
28
+
29
+ it "should cache for an image class" do
30
+ pending "it not freezing :/"
31
+ ->{ described_class.new testcard_image }.should_not raise_error TypeError
32
+ end
33
+
34
+ it "should fail passed an unexpected class" do
35
+ ->{ described_class.new "frog" }.should raise_error TypeError
36
+ end
37
+ end
38
+
39
+ describe "[]" do
40
+ it "should return the color of the pixel" do
41
+ subject[0, 0].should eq Gosu::Color::WHITE
42
+ subject[0, 1].should eq Gosu::Color::RED
43
+ subject[0, 2].should eq Gosu::Color::GREEN
44
+ subject[0, 3].should eq Gosu::Color::BLUE
45
+ subject[0, 4].should eq Gosu::Color.rgba(255, 255, 255, 153)
46
+ subject[0, 8].should eq Gosu::Color.rgba(0, 0, 0, 0)
47
+ end
48
+
49
+ it "should return a null colour outside the texture" do
50
+ subject[0, -1].should eq Gosu::Color.new 0
51
+ subject[-1, 0].should eq Gosu::Color.new 0
52
+ subject[16, 0].should eq Gosu::Color.new 0
53
+ subject[0, 12].should eq Gosu::Color.new 0
54
+ end
55
+ end
56
+
57
+ describe "rgba" do
58
+ it "should return the appropriate array of values" do
59
+ subject.rgba(0, 0).should eq [255, 255, 255, 255]
60
+ subject.rgba(0, 1).should eq [255, 0, 0, 255]
61
+ subject.rgba(0, 2).should eq [0, 255, 0, 255]
62
+ subject.rgba(0, 3).should eq [0, 0, 255, 255]
63
+ subject.rgba(0, 4).should eq [255, 255, 255, 153]
64
+ subject.rgba(0, 8).should eq [0, 0, 0, 0]
65
+ end
66
+ end
67
+
68
+ describe "red" do
69
+ it "should return the appropriate value" do
70
+ subject.red(0, 0).should eq 255
71
+ subject.red(0, 1).should eq 255
72
+ subject.red(0, 2).should eq 0
73
+ subject.red(0, 3).should eq 0
74
+ subject.red(0, 8).should eq 0
75
+ end
76
+ end
77
+
78
+ describe "green" do
79
+ it "should return the appropriate value" do
80
+ subject.green(0, 0).should eq 255
81
+ subject.green(0, 1).should eq 0
82
+ subject.green(0, 2).should eq 255
83
+ subject.green(0, 3).should eq 0
84
+ subject.green(0, 8).should eq 0
85
+ end
86
+ end
87
+
88
+ describe "blue" do
89
+ it "should return the appropriate value" do
90
+ subject.blue(0, 0).should eq 255
91
+ subject.blue(0, 1).should eq 0
92
+ subject.blue(0, 2).should eq 0
93
+ subject.blue(0, 3).should eq 255
94
+ subject.blue(0, 8).should eq 0
95
+ end
96
+ end
97
+
98
+ describe "alpha" do
99
+ it "should return the appropriate value" do
100
+ subject.alpha(0, 0).should eq 255
101
+ subject.alpha(0, 1).should eq 255
102
+ subject.alpha(0, 2).should eq 255
103
+ subject.alpha(0, 3).should eq 255
104
+ subject.alpha(0, 8).should eq 0
105
+ end
106
+ end
107
+
108
+ describe "transparent?" do
109
+ it "should be false where the buffer is opaque or semi-transparent" do
110
+ subject.transparent?(0, 1).should eq false
111
+ subject.transparent?(0, 5).should eq false
112
+ end
113
+
114
+ it "should be true where the buffer is transparent" do
115
+ subject.transparent?(0, 8).should eq true
116
+ end
117
+ end
118
+
119
+ describe "width" do
120
+ it "should be initially set" do
121
+ subject.width.should eq testcard_image.width
122
+ end
123
+ end
124
+
125
+ describe "height" do
126
+ it "should be initially set" do
127
+ subject.height.should eq testcard_image.height
128
+ end
129
+ end
130
+
131
+ describe "to_blob" do
132
+ it "should create a blob identical to one an equivalent image would create" do
133
+ subject.to_blob.should eq testcard_image.to_blob
134
+ end
135
+ end
136
+
137
+ describe "to_image" do
138
+ let(:image) { subject.to_image }
139
+
140
+ it "should create a Gosu::Image" do
141
+ image.should be_kind_of Gosu::Image
142
+ end
143
+
144
+ it "should create an image of the appropriate size" do
145
+ image.width.should eq testcard_image.width
146
+ image.height.should eq testcard_image.height
147
+ end
148
+
149
+ it "should create an image identical to the one that was drawn into it originally" do
150
+ image.to_blob.should eq testcard_image.to_blob
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,152 @@
1
+ require_relative "../helper.rb"
2
+
3
+ describe Ashton::Shader do
4
+ before :all do
5
+ $window = Gosu::Window.new 16, 16, false
6
+ end
7
+
8
+ let(:subject) { described_class.new}
9
+ let(:program) { subject.instance_variable_get :@program }
10
+
11
+ after :each do
12
+ glUseProgram 0
13
+ end
14
+
15
+ describe "initialize" do
16
+ it "should fail if the built-in fragment shader doesn't exist" do
17
+ ->{ described_class.new fragment: :fish }.should raise_error Ashton::ShaderLoadError
18
+ end
19
+
20
+ it "should fail if the built-in vertex shader doesn't exist" do
21
+ ->{ described_class.new vertex: :fish }.should raise_error Ashton::ShaderLoadError
22
+ end
23
+
24
+ it "should fail if the file-based fragment shader doesn't exist or is bad source" do
25
+ ->{ described_class.new fragment: "/fish.frag" }.should raise_error Ashton::ShaderCompileError
26
+ end
27
+
28
+ it "should fail if the file-based fragment shader doesn't exist or is bad source" do
29
+ ->{ described_class.new vertex: "/fish.vert" }.should raise_error Ashton::ShaderCompileError
30
+ end
31
+
32
+ it "should set uniform values from the options hash" do
33
+ any_instance_of described_class do |shader|
34
+ mock(shader, :[]=).with :frog, 12
35
+ mock(shader, :[]=).with :fish_paste, 15.0
36
+ end
37
+
38
+ described_class.new uniforms: { frog: 12, fish_paste: 15.0 }
39
+ end
40
+
41
+ it "should fail if requested to set nonexistent uniform values in the options hash" do
42
+ ->{ described_class.new uniforms: { frog: 12 }} .should raise_error Ashton::ShaderUniformError
43
+ end
44
+ end
45
+
46
+ describe "dup" do
47
+ it "should create a new object containing the same source" do
48
+ new_shader = subject.dup
49
+ new_shader.vertex_source.should eq subject.vertex_source
50
+ new_shader.fragment_source.should eq subject.fragment_source
51
+ end
52
+ end
53
+
54
+ describe "enable" do
55
+ context "with a block" do
56
+ it "should fail if the shader is already active" do
57
+ glUseProgram program
58
+
59
+ ->{ subject.enable {} }.should raise_error Ashton::ShaderError
60
+ end
61
+
62
+ it "should pass itself into the block" do
63
+ shader = nil
64
+ subject.enable do |s|
65
+ shader = s
66
+ end
67
+
68
+ shader.should eq subject
69
+ end
70
+
71
+ it "should be current within the block?" do
72
+ subject.enable do
73
+ subject.should be_current
74
+ end
75
+ end
76
+
77
+ it "should be not current after the block" do
78
+ subject.enable do
79
+ end
80
+
81
+ subject.should_not be_current
82
+ end
83
+ end
84
+
85
+ context "without a block" do
86
+ it "should fail if the shader is already active" do
87
+ glUseProgram program
88
+
89
+ ->{ subject.enable }.should raise_error Ashton::ShaderError
90
+ end
91
+
92
+ it "should toggle current?" do
93
+ ->{ subject.enable }.should change(subject, :current?).from(false).to true
94
+ end
95
+ end
96
+ end
97
+
98
+ describe "disable" do
99
+ it "should fail if the shader is not active" do
100
+ -> { subject.disable }.should raise_error Ashton::ShaderError
101
+ end
102
+
103
+ it "should toggle current?" do
104
+ subject.enable
105
+ ->{ subject.disable }.should change(subject, :current?).from(true).to false
106
+ end
107
+ end
108
+
109
+ describe "current?" do
110
+ it "should be true if the program is current" do
111
+ glUseProgram program
112
+ subject.should be_current
113
+ end
114
+
115
+ it "should be false if the program isn't current" do
116
+ glUseProgram 0
117
+ subject.should_not be_current
118
+ end
119
+ end
120
+
121
+ describe "image=" do
122
+ pending
123
+ end
124
+
125
+ describe "color=" do
126
+ pending
127
+ end
128
+
129
+ describe "[]=" do
130
+ pending
131
+ end
132
+
133
+ describe "[]" do
134
+ pending "implementation"
135
+ end
136
+
137
+ describe "uniform_location" do
138
+ pending
139
+ end
140
+
141
+ describe "attribute_location" do
142
+ pending
143
+ end
144
+
145
+ describe "compile" do
146
+ pending
147
+ end
148
+
149
+ describe "link" do
150
+ pending
151
+ end
152
+ end