gosu_lighting 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de8a167b3fd936c7d23718dd38eb64b469980341
4
- data.tar.gz: 58bee73381595d46f9d14f6b4de79a55b5b22b37
3
+ metadata.gz: c297ea94f5ee119e5bf4e27197cea5d612153fa0
4
+ data.tar.gz: b46f5beec8128384248da704df4b6d3f441ea642
5
5
  SHA512:
6
- metadata.gz: 220bc50418c93192e53c9a640012a2e3b9b94dfc7004f6719c6e4c5ebd884c73845a0dc80dd3a73d80e91f5e5b3189ba8163c8844381c2c2c1ed8c5f7b4a1acc
7
- data.tar.gz: 5691fb79c38e578537dfb56fd487673e1d8c1fc33396e126e5f6fdbda3201e6590b5574967b4a231a47f4ff612585af97ce9254830c70b7d4cf214593d3fc106
6
+ metadata.gz: 469816d0f3ba5d1c52b261149102e39cb9ef3828dd22bd6576dadf16fea7e0f5a5975df34a9e3cbe0ac4aaf6d581b454dda86792a2515541c2f92d646f8b3698
7
+ data.tar.gz: 018ae898b70ea2fc21b98a704cc8c32fc745ee8a65ca7fb2e10770ce8ed60fa10cf4a59002eee4b0d9404ad95328ad0e755dfde7fa4fe6605bee5361c0069986
@@ -0,0 +1,8 @@
1
+ require 'gosu'
2
+
3
+ require_relative './gosu_lighting/circle'
4
+ require_relative './gosu_lighting/rectangle'
5
+ require_relative './gosu_lighting/source'
6
+
7
+ module GosuLighting
8
+ end
@@ -0,0 +1,7 @@
1
+ module GosuLighting
2
+ module Circle
3
+ def draw_lit source
4
+ self.draw source.shadow_circle self
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module GosuLighting
2
+ module Rectangle
3
+ def draw_lit source
4
+ self.draw source.shadow_rectangle self
5
+ end
6
+
7
+ def center_x
8
+ x + width * 0.5
9
+ end
10
+
11
+ def center_y
12
+ y + height * 0.5
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,143 @@
1
+ require 'gosu'
2
+ require 'glfw'
3
+ require 'opengl'
4
+
5
+ OpenGL.load_dll
6
+ GLFW.load_dll
7
+
8
+ include OpenGL
9
+ include GLFW
10
+
11
+ SHADOW_LENGTH = 20000
12
+
13
+ module GosuLighting
14
+ class Source
15
+ attr_accessor :x, :y, :radius
16
+
17
+ def initialize window, x, y, radius, att_sprite = nil
18
+ @att_sprite = att_sprite || Gosu::Image.new(window, 'light.png', true)
19
+ @window = window
20
+ @x = x
21
+ @y = y
22
+ @radius = radius
23
+ end
24
+
25
+ def shadow_circle circle, depth = 1
26
+ dist = Gosu::distance @x, @y, circle.x, circle.y
27
+ depth = depth + 1.0 - dist / SHADOW_LENGTH
28
+
29
+ bx1, by1, bx2, by2 = endpoints_facing circle.x, circle.y, @x, @y, circle.radius
30
+
31
+ nx1, ny1 = normal @x, @y, bx1, by1
32
+ nx2, ny2 = normal @x, @y, bx2, by2
33
+
34
+ sx1 = bx1 + nx1 * SHADOW_LENGTH
35
+ sy1 = by1 + ny1 * SHADOW_LENGTH
36
+ sx2 = bx2 + nx2 * SHADOW_LENGTH
37
+ sy2 = by2 + ny2 * SHADOW_LENGTH
38
+
39
+ @window.gl depth do
40
+ gl_draw_shadow bx1, by1, sx1, sy1, sx2, sy2, bx2, by2, 0.5
41
+ end
42
+
43
+ return depth
44
+ end
45
+
46
+ def shadow_rectangle rect, depth = 2
47
+ dist = Gosu::distance @x, @y, rect.center_x, rect.center_y
48
+ depth = depth + 1.0 - dist / SHADOW_LENGTH
49
+
50
+ cx1 = cx2 = rect.x
51
+ cy1 = cy2 = rect.y
52
+ if @x < rect.x
53
+ cy1 += rect.height
54
+ elsif @x < rect.x + rect.width
55
+ cy1 += rect.height if @y > rect.center_y
56
+ cy2 = cy1
57
+ else
58
+ cy2 += rect.height
59
+ end
60
+
61
+ if @y < rect.y
62
+ cx2 += rect.width
63
+ elsif @y < rect.y + rect.height
64
+ cx1 += rect.width if @x > rect.center_x
65
+ cx2 = cx1
66
+ else
67
+ cx1 += rect.width
68
+ end
69
+
70
+ nx1, ny1 = normal @x, @y, cx1, cy1
71
+ nx2, ny2 = normal @x, @y, cx2, cy2
72
+
73
+ sx1 = cx1 + nx1 * SHADOW_LENGTH
74
+ sy1 = cy1 + ny1 * SHADOW_LENGTH
75
+ sx2 = cx2 + nx2 * SHADOW_LENGTH
76
+ sy2 = cy2 + ny2 * SHADOW_LENGTH
77
+
78
+ @window.gl depth do
79
+ gl_draw_shadow cx1, cy1, sx1, sy1, sx2, sy2, cx2, cy2, 1.0
80
+ end
81
+
82
+ return depth
83
+ end
84
+
85
+ def draw_attenuation depth
86
+ draw_as_rect @att_sprite, *clip_rect, depth, 0xff999999, :multiply
87
+ end
88
+
89
+ def draw
90
+ @window.clip_to(*clip_rect) do
91
+ yield self
92
+ end
93
+ end
94
+
95
+ private
96
+ def endpoints_facing x1, y1, x2, y2, r
97
+ a = Gosu::angle x1, y1, x2, y2
98
+ x3 = x1 + Gosu::offset_x(a + 90, r)
99
+ y3 = y1 + Gosu::offset_y(a + 90, r)
100
+
101
+ x4 = x1 + Gosu::offset_x(a - 90, r)
102
+ y4 = y1 + Gosu::offset_y(a - 90, r)
103
+
104
+ [x3, y3, x4, y4]
105
+ end
106
+
107
+ def vector x1, y1, x2, y2
108
+ dy = y2 - y1
109
+ dx = x2 - x1
110
+ [dx, dy]
111
+ end
112
+
113
+ def normal x1, y1, x2, y2
114
+ d = Gosu::distance x1, y1, x2, y2
115
+ x, y = vector x1, y1, x2, y2
116
+
117
+ [x/d, y/d]
118
+ end
119
+
120
+ def clip_rect
121
+ [(@x - @radius).to_i, (@y - @radius).to_i, (@radius*2.0).to_i, (@radius*2.0).to_i]
122
+ end
123
+
124
+ def draw_as_rect sprite, x, y, w, h, z, color, mode
125
+ sprite.draw_as_quad x, y, color, x+w, y, color, x+w, y+h, color, x, y+h, color, z, mode
126
+ end
127
+
128
+ def gl_draw_shadow x1, y1, x2, y2, x3, y3, x4, y4, alpha = 0.9
129
+ glDisable GL_DEPTH_TEST
130
+ glEnable GL_BLEND
131
+ glBlendEquationSeparate GL_FUNC_ADD, GL_FUNC_ADD
132
+ glBlendFuncSeparate GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO
133
+
134
+ glBegin GL_QUADS
135
+ glColor4f 0, 0, 0, alpha
136
+ glVertex3f x1, y1, 0
137
+ glVertex3f x2, y2, 0
138
+ glVertex3f x3, y3, 0
139
+ glVertex3f x4, y4, 0
140
+ glEnd
141
+ end
142
+ end
143
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gosu_lighting
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Smith
@@ -86,7 +86,11 @@ email: jellymann@gmail.com
86
86
  executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
- files: []
89
+ files:
90
+ - lib/gosu_lighting.rb
91
+ - lib/gosu_lighting/circle.rb
92
+ - lib/gosu_lighting/rectangle.rb
93
+ - lib/gosu_lighting/source.rb
90
94
  homepage: https://github.com/jellymann/gosu_lighting
91
95
  licenses:
92
96
  - MIT