ashton 0.0.1alpha → 0.0.2alpha

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 (116) hide show
  1. data/LICENSE +21 -21
  2. data/README.md +95 -68
  3. data/Rakefile +41 -23
  4. data/examples/bloom_example.rb +59 -0
  5. data/examples/lighting_example.rb +127 -0
  6. data/examples/media/SmallStar.png +0 -0
  7. data/examples/media/Starfighter.png +0 -0
  8. data/examples/media/simple.png +0 -0
  9. data/examples/noise_example.rb +94 -0
  10. data/examples/outline_example.rb +86 -0
  11. data/examples/particle_emitter_example.rb +114 -0
  12. data/examples/pixelate_example.rb +51 -49
  13. data/examples/pixelated_texture_example.rb +69 -0
  14. data/examples/radial_blur_example.rb +60 -62
  15. data/examples/shader_image_example.rb +74 -41
  16. data/examples/{shockwave2_example.rb → shockwave_example.rb} +74 -75
  17. data/examples/stencil_shader_example.rb +104 -0
  18. data/examples/{framebuffer_example.rb → texture_render_example.rb} +53 -49
  19. data/examples/{tv_screen_and_noise_example.rb → tv_screen_and_static_example.rb} +59 -59
  20. data/ext/ashton/GLee.c +18170 -0
  21. data/ext/ashton/GLee.h +17647 -0
  22. data/ext/ashton/ashton.c +42 -0
  23. data/ext/ashton/ashton.h +31 -0
  24. data/ext/ashton/color.c +45 -0
  25. data/ext/ashton/color.h +25 -0
  26. data/ext/ashton/common.h +41 -0
  27. data/ext/ashton/extconf.rb +42 -0
  28. data/ext/ashton/fast_math.c +30 -0
  29. data/ext/ashton/fast_math.h +30 -0
  30. data/ext/ashton/font.c +8 -0
  31. data/ext/ashton/font.h +16 -0
  32. data/ext/ashton/gosu.c +18 -0
  33. data/ext/ashton/gosu.h +19 -0
  34. data/ext/ashton/image.c +8 -0
  35. data/ext/ashton/image.h +16 -0
  36. data/ext/ashton/particle_emitter.c +788 -0
  37. data/ext/ashton/particle_emitter.h +171 -0
  38. data/ext/ashton/pixel_cache.c +237 -0
  39. data/ext/ashton/pixel_cache.h +58 -0
  40. data/ext/ashton/shader.c +9 -0
  41. data/ext/ashton/shader.h +16 -0
  42. data/ext/ashton/texture.c +442 -0
  43. data/ext/ashton/texture.h +63 -0
  44. data/ext/ashton/window.c +8 -0
  45. data/ext/ashton/window.h +16 -0
  46. data/lib/ashton.rb +38 -26
  47. data/lib/ashton/1.9/ashton.so +0 -0
  48. data/lib/ashton/gosu_ext/color.rb +24 -11
  49. data/lib/ashton/gosu_ext/font.rb +58 -0
  50. data/lib/ashton/gosu_ext/gosu_module.rb +16 -0
  51. data/lib/ashton/gosu_ext/image.rb +95 -31
  52. data/lib/ashton/gosu_ext/window.rb +78 -35
  53. data/lib/ashton/image_stub.rb +32 -36
  54. data/lib/ashton/lighting/light_source.rb +146 -0
  55. data/lib/ashton/lighting/manager.rb +98 -0
  56. data/lib/ashton/mixins/version_checking.rb +23 -0
  57. data/lib/ashton/particle_emitter.rb +87 -0
  58. data/lib/ashton/pixel_cache.rb +24 -0
  59. data/lib/ashton/shader.rb +353 -35
  60. data/lib/ashton/shaders/bloom.frag +41 -0
  61. data/lib/ashton/shaders/color_inversion.frag +11 -0
  62. data/lib/ashton/{post_process → shaders}/contrast.frag +16 -16
  63. data/lib/ashton/{shader → shaders}/default.frag +22 -19
  64. data/lib/ashton/{shader → shaders}/default.vert +13 -13
  65. data/lib/ashton/shaders/fade.frag +14 -0
  66. data/lib/ashton/shaders/grayscale.frag +15 -0
  67. data/lib/ashton/shaders/include/classicnoise2d.glsl +113 -0
  68. data/lib/ashton/shaders/include/classicnoise3d.glsl +177 -0
  69. data/lib/ashton/shaders/include/classicnoise4d.glsl +302 -0
  70. data/lib/ashton/{include/simplex.glsl → shaders/include/noise2d.glsl} +70 -63
  71. data/lib/ashton/shaders/include/noise3d.glsl +102 -0
  72. data/lib/ashton/shaders/include/noise4d.glsl +128 -0
  73. data/lib/ashton/shaders/include/rand.glsl +5 -0
  74. data/lib/ashton/shaders/lighting/distort.frag +57 -0
  75. data/lib/ashton/shaders/lighting/draw_shadows.frag +60 -0
  76. data/lib/ashton/shaders/lighting/shadow_blur.frag +60 -0
  77. data/lib/ashton/shaders/mezzotint.frag +22 -0
  78. data/lib/ashton/shaders/multitexture2.vert +19 -0
  79. data/lib/ashton/shaders/outline.frag +45 -0
  80. data/lib/ashton/{post_process → shaders}/pixelate.frag +48 -48
  81. data/lib/ashton/shaders/radial_blur.frag +63 -0
  82. data/lib/ashton/shaders/sepia.frag +26 -0
  83. data/lib/ashton/{post_process/shockwave2.frag → shaders/shockwave.frag} +38 -35
  84. data/lib/ashton/shaders/signed_distance_field.frag +80 -0
  85. data/lib/ashton/{post_process/noise.frag → shaders/static.frag} +25 -27
  86. data/lib/ashton/shaders/stencil.frag +27 -0
  87. data/lib/ashton/shaders/tv_screen.frag +23 -0
  88. data/lib/ashton/signed_distance_field.rb +151 -0
  89. data/lib/ashton/texture.rb +186 -0
  90. data/lib/ashton/version.rb +2 -2
  91. data/lib/ashton/window_buffer.rb +16 -0
  92. data/spec/ashton/ashton_spec.rb +22 -0
  93. data/spec/ashton/gosu_ext/color_spec.rb +34 -0
  94. data/spec/ashton/gosu_ext/font_spec.rb +57 -0
  95. data/spec/ashton/gosu_ext/gosu_spec.rb +11 -0
  96. data/spec/ashton/gosu_ext/image_spec.rb +66 -0
  97. data/spec/ashton/gosu_ext/window_spec.rb +71 -0
  98. data/spec/ashton/image_stub_spec.rb +46 -0
  99. data/spec/ashton/particle_emitter_spec.rb +123 -0
  100. data/spec/ashton/pixel_cache_spec.rb +153 -0
  101. data/spec/ashton/shader_spec.rb +152 -0
  102. data/spec/ashton/signed_distance_field_spec.rb +163 -0
  103. data/spec/ashton/texture_spec.rb +347 -0
  104. data/spec/helper.rb +12 -0
  105. metadata +159 -28
  106. data/examples/output/README.txt +0 -1
  107. data/lib/ashton/base_shader.rb +0 -172
  108. data/lib/ashton/framebuffer.rb +0 -183
  109. data/lib/ashton/post_process.rb +0 -83
  110. data/lib/ashton/post_process/default.vert +0 -9
  111. data/lib/ashton/post_process/fade.frag +0 -11
  112. data/lib/ashton/post_process/mezzotint.frag +0 -24
  113. data/lib/ashton/post_process/radial_blur.frag +0 -31
  114. data/lib/ashton/post_process/sepia.frag +0 -19
  115. data/lib/ashton/post_process/shockwave.frag +0 -40
  116. data/lib/ashton/post_process/tv_screen.frag +0 -32
data/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- The MIT License
2
-
3
- Copyright (c) 2012 Bil Bas
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
1
+ The MIT License
2
+
3
+ Copyright (c) 2012 Bil Bas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md CHANGED
@@ -1,68 +1,95 @@
1
- Ashton
2
- ======
3
-
4
- Description
5
- -----------
6
-
7
- Add extra visual effects to the Gosu game-development library, utilising OpenGL shaders (using shader model 1.0, for maximum compatibility) and frame-buffers.
8
-
9
- "Ashton" is named after [Clark Ashton Smith](http://en.wikipedia.org/wiki/Clark_Ashton_Smith), an fantasy/horror author
10
- with a particularly colourful imagination.
11
-
12
- - Author: Bil Bas (Spooner)
13
- - License: MIT
14
-
15
- Usage
16
- -----
17
-
18
- gem install ashton --pre
19
-
20
- Features
21
- --------
22
-
23
- - Gosu::Font
24
- * [TODO] #draw - Added :shader hash option to choose optional shader to use.
25
- * [TODO] #draw_rel - Added :shader hash option to choose optional shader to use.
26
-
27
- - Gosu::Image
28
- * #draw - Added :shader hash option to choose optional shader to use.
29
- * #draw_rot - Added :shader hash option to choose optional shader to use.
30
- * [TODO] #flip!, #flip, #mirror!, #mirror, #scale!, #scale, etc.
31
- * [TODO] #resize (Well, create another image which is smaller/larger).
32
- * [TODO] #to_framebuffer
33
-
34
- - Gosu::Window
35
- * #post_process {} - Draws contents of block into a back-buffer, then applies a shader as it draws that onto the screen.
36
- * [TODO] #to_framebuffer - Copy the contents of the window as a {Ashton::Framebuffer}.
37
- * [TODO] #to_image - Create Gosu::Image from window contents.
38
- * [TODO] #draw_line - Added :shader hash option
39
- * [TODO] #draw_quad - Added :shader hash option
40
- * [TODO] #draw_triangle - Added :shader hash option
41
-
42
- - {Ashton::Shader}
43
- * #use {} - Inside the block, all draw operations are affected by the shader.
44
- * Supports vertex and fragment shaders.
45
-
46
- - {Ashton::PostProcess}
47
- * A specific type of shader to perform full-screen post-processing.
48
- * #process - Used to post-process the entire Gosu::Window at once, after some, or all, drawing is complete.
49
- * Supports fragment shaders.
50
- * [TODO] Includes a small library of example shaders (:blur, :simplex, etc).
51
-
52
- - {Ashton::Framebuffer}
53
- * #use {} - Inside the block, draw operations go into the framebuffer, rather than onto the window.
54
- * #to_image - Convert to Gosu::Image.
55
- * #draw - Draw directly onto a Gosu::Window.
56
- * [TODO] #flip!, #flip - Invert framebuffer's vertical orientation.
57
-
58
- Similar Libraries
59
- -----------------
60
-
61
- - [TexPlay](https://github.com/banister/texplay) - Deals with Gosu::Image manipulation, such as per-pixel editing and drawing. It is compatible with, and complementary to, this gem.
62
-
63
- Credits
64
- -------
65
-
66
- - simplex.glsl - simplex noise implementation - Copyright (C) 2011 Ashima Arts - MIT license.
67
-
68
-
1
+ Ashton
2
+ ======
3
+
4
+ Description
5
+ -----------
6
+
7
+ Add extra visual effects to the Gosu game-development library, utilising OpenGL shaders (using shader model 1.0, for maximum compatibility) and frame-buffers.
8
+
9
+ "Ashton" is named after [Clark Ashton Smith](http://en.wikipedia.org/wiki/Clark_Ashton_Smith), an fantasy/horror author
10
+ with a particularly colourful imagination.
11
+
12
+ - Author: [Bil Bas (Spooner)](http://spooner.github.com/)
13
+ - [Wiki](https://github.com/Spooner/ashton/wiki)
14
+ - License: MIT
15
+
16
+ Usage
17
+ -----
18
+
19
+ gem install ashton --pre
20
+
21
+ Features
22
+ --------
23
+
24
+ * Gosu extensions
25
+ - {Gosu::Color} - Converting to and from opengl values.
26
+
27
+ - {Gosu::Font} - Apply shader to draw operations.
28
+
29
+ - {Gosu::Image} - Apply shader to each draw operation or group of draws. Manipulation, such as flipping and scaling.
30
+
31
+ - {Gosu::Window} - Post-processing with shaders. Converting to image.
32
+
33
+ * Ashton
34
+ - {Ashton::Texture} - Single texture (compared to Gosu::Image which uses a spritesheet) which can be drawn directly onto and drawn to the Window.
35
+
36
+ - {Ashton::Lighting::Manager} - Manages and combines the lighting from {Ashton::Lighting::LightSource} objects.
37
+
38
+ - {Ashton::Lighting::LightSource} - A single light-source that illuminates and whose can be blocked by shadow-casting objects.
39
+
40
+ - {Ashton::ParticleEmitter} - Generates, manages and displays particles.
41
+
42
+ - {Ashton::PixelCache} - Cached image data attached to an {Ashton::Texture} or {Gosu::Image}.
43
+
44
+ - {Ashton::SignedDistanceField} - A signed distance field based on a an image mask.
45
+
46
+ - {Ashton::Shader} - Wrapper around a GLSL shaders, Supports vertex and fragment shaders. Small shader/function library.
47
+
48
+ - {Ashton::WindowBuffer} - Texture that is the same size as the Gosu Window that can capture the contents of the screen.
49
+
50
+ Requirements
51
+ ------------
52
+
53
+ * OSX/Linux: Requires OpenGL library be installed.
54
+
55
+ * {Ashton::Shader} and {Ashton::Lighting} or anything else using shaders, require OpenGL 2.0.
56
+
57
+ Similar Libraries
58
+ -----------------
59
+
60
+ - [TexPlay](https://github.com/banister/texplay) - Deals with Gosu::Image manipulation, such as per-pixel editing and drawing. It is compatible with, and complementary to, this gem.
61
+
62
+ Third party
63
+ -----------
64
+
65
+ - OpenGL static library (in Windows binary gem) and headers.
66
+ - [GLee](http://elf-stone.com/glee.php) source.
67
+
68
+ - Various trivial shaders - "randomly found on the Internet" :$
69
+
70
+ - (Classic and Simplex noise functions)[https://github.com/ashima/webgl-noise/] - Copyright (C) 2011 Ashima Arts - MIT license.
71
+ * classicnoise2d.glsl - 2D Classic Perlin noise implementation - `cnoise(vec2)`
72
+ * classicnoise3d.glsl - 3D Classic Perlin noise implementation - `cnoise(vec3)`
73
+ * classicnoise4d.glsl - 4D Classic Perlin noise implementation - `cnoise(vec4)`
74
+ * noise2d.glsl - 2D Simplex noise implementation - `snoise(vec2)`
75
+ * noise3d.glsl - 3D Simplex noise implementation - `snoise(vec3)`
76
+ * noise4d.glsl - 4D Simplex noise implementation - `snoise(vec4)`
77
+
78
+ - [Bloom filter by myheroics](http://myheroics.wordpress.com/2008/09/04/glsl-bloom-shader/)
79
+ * bloom.frag
80
+
81
+ - [Shockwave by Crystalin](http://empire-defense.crystalin.fr/blog/2d_shock_wave_texture_with_shader)
82
+ * shockwave.frag
83
+
84
+ - [Radial Blur by gamerendering.com](http://www.gamerendering.com/2008/12/20/radial-blur-filter/)
85
+ * radial_blur.frag
86
+
87
+ - Lighting based on, but much optimised from, Catalin Zima's shader based dynamic shadows system.
88
+ * http://www.catalinzima.com/2010/07/my-technique-for-the-shader-based-dynamic-2d-shadows/
89
+ * {Ashton::Lighting::LightSource} and {Ashton::Lighting::Manager} classes.
90
+ * shadow_blur.frag
91
+ * shadow_distory.frag
92
+ * shadow_draw_shadows.frag
93
+
94
+
95
+
data/Rakefile CHANGED
@@ -1,24 +1,42 @@
1
- require 'rake/clean'
2
- require 'rspec/core/rake_task'
3
- require 'yard'
4
- require 'redcloth'
5
- require 'launchy'
6
- require 'rubygems/package_task'
7
-
8
-
9
- spec = Gem::Specification.load Dir['*.gemspec'].first
10
-
11
- Gem::PackageTask.new spec do
12
- end
13
-
14
- YARD::Rake::YardocTask.new
15
-
16
- task :default => :spec
17
-
18
- RSpec::Core::RakeTask.new do |t|
19
- end
20
-
21
- desc "Open yard docs in browser"
22
- task :browse_yard => :yard do
23
- Launchy.open "doc/index.html" rescue nil
1
+ require 'bundler/setup'
2
+
3
+ require 'rake/clean'
4
+ require 'rspec/core/rake_task'
5
+ require 'rake/extensiontask'
6
+ require 'yard'
7
+ require 'redcloth'
8
+ require 'launchy'
9
+
10
+ begin
11
+ require 'devkit' # only used on windows
12
+ rescue LoadError
13
+ end
14
+
15
+
16
+ spec = Gem::Specification.load Dir['*.gemspec'].first
17
+
18
+ Gem::PackageTask.new spec do
19
+ end
20
+
21
+ Rake::ExtensionTask.new 'ashton', spec do |ext|
22
+ RUBY_VERSION =~ /(\d+.\d+)/
23
+ ext.lib_dir = "lib/ashton/#{$1}"
24
+ end
25
+
26
+ YARD::Rake::YardocTask.new
27
+
28
+ task :default => :spec
29
+ task :spec => :compile
30
+
31
+ RSpec::Core::RakeTask.new do |t|
32
+ end
33
+
34
+ desc "Open yard docs in browser"
35
+ task :browse_yard => :yard do
36
+ Launchy.open "doc/index.html" rescue nil
37
+ end
38
+
39
+ desc "Create platform-specific compiled gem"
40
+ task :native_gem do
41
+ Rake::Task["native"].invoke "gem"
24
42
  end
@@ -0,0 +1,59 @@
1
+ # Use of GLSL shader in Gosu to post-process the entire screen.
2
+
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
7
+
8
+ $LOAD_PATH.unshift File.expand_path('../lib/', File.dirname(__FILE__))
9
+ require "ashton"
10
+
11
+ def media_path(file); File.expand_path "media/#{file}", File.dirname(__FILE__) end
12
+
13
+ class TestWindow < Gosu::Window
14
+ def initialize
15
+ super 640, 480, false
16
+ self.caption = "Post-processing with 'bloom' - mouse pos alters glare and power - hold <Space> to disable"
17
+
18
+ @bloom = Ashton::Shader.new fragment: :bloom
19
+
20
+
21
+ @font = Gosu::Font.new self, Gosu::default_font_name, 40
22
+ @star = Gosu::Image.new(self, media_path("LargeStar.png"), true)
23
+ @background = Gosu::Image.new(self, media_path("Earth.png"), true)
24
+
25
+ update # Ensure the values are initially set.
26
+ end
27
+
28
+ def update
29
+ $gosu_blocks.clear if defined? $gosu_blocks # Workaround for Gosu bug (0.7.45)
30
+ @bloom.glare_size = [0.008 * mouse_x / width, 0.0].max
31
+ @bloom.power = [0.5 * mouse_y / height, 0.0].max
32
+ end
33
+
34
+ def needs_cursor?; true end
35
+
36
+ def button_down(id)
37
+ if id == Gosu::KbEscape
38
+ close
39
+ end
40
+ end
41
+
42
+ def draw
43
+ shaders = button_down?(Gosu::KbSpace) ? [] : [@bloom]
44
+ post_process(*shaders) do
45
+ @background.draw 0, 0, 0, width.fdiv(@background.width), height.fdiv(@background.height)
46
+ @star.draw 0, 0, 0
47
+ @star.draw 200, 100, 0, 1, 1, Gosu::Color.rgb(100, 100, 100)
48
+ end
49
+
50
+ # Drawing after the effect isn't processed, which is useful for GUI elements.
51
+ @font.draw_rel "Less glare", 0, height / 2, 0, 0, 0.5
52
+ @font.draw_rel "More glare", width, height / 2, 0, 1, 0.5
53
+
54
+ @font.draw_rel "Less power", width / 2, 0, 0, 0.5, 0
55
+ @font.draw_rel "More power", width / 2, height, 0, 0.5, 1
56
+ end
57
+ end
58
+
59
+ TestWindow.new.show
@@ -0,0 +1,127 @@
1
+ # Use of GLSL shader in Gosu to post-process the entire screen.
2
+
3
+ begin
4
+ require 'rubygems'
5
+ rescue LoadError
6
+ end
7
+
8
+ $LOAD_PATH.unshift File.expand_path('../lib/', File.dirname(__FILE__))
9
+ require "ashton"
10
+
11
+ def media_path(file); File.expand_path "media/#{file}", File.dirname(__FILE__) end
12
+
13
+ class TestWindow < Gosu::Window
14
+ def initialize
15
+ super 640, 480, false
16
+ self.caption = "Shadow-casting - <space> new layout; <LMB> place light; <Arrows> to scroll"
17
+
18
+ @font = Gosu::Font.new self, Gosu::default_font_name, 32
19
+ @background = Gosu::Image.new(self, media_path("Earth.png"), true)
20
+
21
+ @star = Gosu::Image.new(self, media_path("LargeStar.png"), true)
22
+
23
+ # Input: Shadow casters are any object that casts a shadow.
24
+ place_shadow_casters
25
+
26
+ setup_lighting
27
+
28
+ # Perform the initial rendering into the light manager.
29
+ @lighting.update_shadow_casters do
30
+ draw_shadow_casters
31
+ end
32
+
33
+ @debug = false
34
+ @camera_x, @camera_y = 0, 0
35
+ end
36
+
37
+ def setup_lighting
38
+ @lighting = Ashton::Lighting::Manager.new
39
+
40
+ # Add some lights (various methods)
41
+ @lighting.create_light 240, 240, 0, height / 3, color: Gosu::Color::RED
42
+
43
+ light = Ashton::Lighting::LightSource.new 400, 150, 0, height / 5, color: Gosu::Color::GREEN
44
+ @lighting << light
45
+
46
+ @light_mouse = @lighting.create_light mouse_x, mouse_y, 0, height / 2, color: Gosu::Color::GRAY
47
+ end
48
+
49
+ # Creates a new set of objects that cast shadows.
50
+ def place_shadow_casters
51
+ @shadow_casters = Array.new 12 do
52
+ { x: rand() * width, y: rand() * height, angle: rand() * 360 }
53
+ end
54
+ end
55
+
56
+ def update
57
+ $gosu_blocks.clear if defined? $gosu_blocks # workaround for Gosu 0.7.45 bug.
58
+
59
+ @camera_x -= 2 if button_down? Gosu::KbLeft
60
+ @camera_x += 2 if button_down? Gosu::KbRight
61
+ @camera_y -= 2 if button_down? Gosu::KbUp
62
+ @camera_y += 2 if button_down? Gosu::KbDown
63
+
64
+ @lighting.camera_x = @camera_x
65
+ @lighting.camera_y = @camera_y
66
+
67
+ @light_mouse.x, @light_mouse.y = mouse_x + @camera_x, mouse_y + @camera_y
68
+
69
+ @lighting.update_shadow_casters do
70
+ draw_shadow_casters
71
+ end
72
+ end
73
+
74
+ def draw_shadow_casters
75
+ @font.draw "Hello world! Time to get a grip, eh?", 0, 150, 0, 1, 1, Gosu::Color::RED
76
+ @shadow_casters.each do |star|
77
+ @star.draw_rot star[:x], star[:y], 0, star[:angle], 0.5, 0.5, 0.125, 0.125
78
+ end
79
+ end
80
+
81
+ def needs_cursor?
82
+ true
83
+ end
84
+
85
+ def button_down(id)
86
+ case id
87
+ when Gosu::KbEscape
88
+ close
89
+
90
+ when Gosu::KbSpace
91
+ place_shadow_casters
92
+
93
+ when Gosu::MsLeft
94
+ color = Gosu::Color.rgba rand(255), rand(255), rand(255), 127 + rand(128)
95
+ @lighting.create_light mouse_x + @camera_x, mouse_y + @camera_y, 0, height / 16 + rand(height / 2), color: color
96
+
97
+ when Gosu::KbD
98
+ @debug = !@debug
99
+
100
+ when Gosu::KbS
101
+ @lighting.each {|light| light.send :save_buffers }
102
+ end
103
+ end
104
+
105
+ def draw
106
+ translate -@camera_x, -@camera_y do
107
+ @background.draw 0, 0, 0, width.fdiv(@background.width), height.fdiv(@background.height)
108
+
109
+ # ... would draw player and other objects here ...
110
+
111
+ @lighting.draw
112
+
113
+ draw_shadow_casters # These should be drawn above or below the lighting, depending on preference.
114
+
115
+ # Draw the light itself - this isn't managed by the manager.
116
+ @lighting.each do |light|
117
+ pixel.draw_rot light.x, light.y, 0, 0, 0.5, 0.5, 15, 15, light.color, :add
118
+ light.draw_debug if @debug
119
+ end
120
+ end
121
+
122
+ # Drawing after the effect isn't processed, which is useful for GUI elements.
123
+ @font.draw "FPS: #{Gosu::fps} (for #{@lighting.size} lights)", 0, 0, 0
124
+ end
125
+ end
126
+
127
+ TestWindow.new.show