tung-tea 0.1.5 → 0.2.0
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.
- data/doc/example/circle_alpha.rb +43 -0
- data/doc/example/lines_alpha.rb +29 -0
- data/doc/example/rect_alpha.rb +30 -0
- data/doc/reference.textile +6 -6
- data/lib/tea/c_bitmap.rb +7 -8
- data/lib/tea/m_primitive_drawing.rb +91 -13
- metadata +6 -3
- /data/doc/example/{circles.rb → circle.rb} +0 -0
@@ -0,0 +1,43 @@
|
|
1
|
+
# Test that alpha blending works for circles.
|
2
|
+
# Expected results
|
3
|
+
|
4
|
+
require 'tea'
|
5
|
+
|
6
|
+
puts <<TEST
|
7
|
+
You should see a white rectangle and circles along the edges:
|
8
|
+
|
9
|
+
--b --r -ab
|
10
|
+
|
11
|
+
-ar o-b
|
12
|
+
|
13
|
+
o-r oab oar
|
14
|
+
|
15
|
+
o-- = outline
|
16
|
+
-a- = antialias
|
17
|
+
--b/r = blend/replace
|
18
|
+
|
19
|
+
Press any key to exit.
|
20
|
+
TEST
|
21
|
+
|
22
|
+
Tea.init
|
23
|
+
Tea::Screen.set_mode 400, 300
|
24
|
+
|
25
|
+
Tea::Screen.rect 100, 75, 200, 150, 0xffffffff
|
26
|
+
|
27
|
+
Tea::Screen.circle 100, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :blend
|
28
|
+
Tea::Screen.circle 200, 75, 25, 0x00ff0080, :outline => false, :antialias => false, :mix => :replace
|
29
|
+
Tea::Screen.circle 300, 75, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :blend
|
30
|
+
|
31
|
+
Tea::Screen.circle 100, 150, 25, 0x00ff0080, :outline => false, :antialias => true, :mix => :replace
|
32
|
+
Tea::Screen.circle 300, 150, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :blend
|
33
|
+
|
34
|
+
Tea::Screen.circle 100, 225, 25, 0x00ff0080, :outline => true, :antialias => false, :mix => :replace
|
35
|
+
Tea::Screen.circle 200, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :blend
|
36
|
+
Tea::Screen.circle 300, 225, 25, 0x00ff0080, :outline => true, :antialias => true, :mix => :replace
|
37
|
+
|
38
|
+
Tea::Screen.update
|
39
|
+
|
40
|
+
loop do
|
41
|
+
e = Tea::Event.get(true)
|
42
|
+
break if e.class == Tea::App::Exit || e.class == Tea::Kbd::Down
|
43
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# Test that line alpha mixes properly.
|
2
|
+
# Expected results:
|
3
|
+
#
|
4
|
+
# * half-red background
|
5
|
+
# * green line, translucent
|
6
|
+
# * blue line, translucent
|
7
|
+
# * white line, solid
|
8
|
+
|
9
|
+
require 'tea'
|
10
|
+
|
11
|
+
puts <<TEST
|
12
|
+
You should see for 5 seconds:
|
13
|
+
|
14
|
+
* half-red background
|
15
|
+
* green line, translucent
|
16
|
+
* blue line, translucent
|
17
|
+
* white line, solid
|
18
|
+
TEST
|
19
|
+
|
20
|
+
Tea.init
|
21
|
+
Tea::Screen.set_mode 400, 300
|
22
|
+
|
23
|
+
Tea::Screen.rect 200, 0, 200, 300, 0xff0000ff
|
24
|
+
20.times { |n| Tea::Screen.line 10, 10 + n, 390, 10 + n, 0x00ff0080 }
|
25
|
+
20.times { |n| Tea::Screen.line 10, 100 + n, 390, 100 + n, 0x0000ff80, :mix => :blend }
|
26
|
+
20.times { |n| Tea::Screen.line 10, 200 + n, 390, 200 + n, 0xffffff80, :mix => :replace }
|
27
|
+
|
28
|
+
Tea::Screen.update
|
29
|
+
sleep 5
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# Test that alpha for rectangles works.
|
2
|
+
# Expected results are:
|
3
|
+
#
|
4
|
+
# * center: red rectangle (background)
|
5
|
+
# * top-left: translucent green rectangle
|
6
|
+
# * bottom-left: translucent white rectangle
|
7
|
+
# * bottom-right: solid blue rectangle
|
8
|
+
|
9
|
+
require 'tea'
|
10
|
+
|
11
|
+
puts <<TEST
|
12
|
+
You should see for 5 seconds:
|
13
|
+
|
14
|
+
* center: red rectangle (background)
|
15
|
+
* top-left: translucent green rectangle
|
16
|
+
* bottom-left: translucent white rectangle
|
17
|
+
* bottom-right: solid blue rectangle
|
18
|
+
TEST
|
19
|
+
|
20
|
+
Tea.init
|
21
|
+
Tea::Screen.set_mode 400, 300
|
22
|
+
|
23
|
+
Tea::Screen.rect 100, 75, 200, 150, 0xff0000ff
|
24
|
+
Tea::Screen.rect 0, 0, 200, 150, 0x00ff0080, :mix => :blend
|
25
|
+
Tea::Screen.rect 200, 150, 200, 150, 0x0000ff80, :mix => :replace
|
26
|
+
Tea::Screen.rect 0, 150, 200, 150, 0xffffff80
|
27
|
+
|
28
|
+
Tea::Screen.update
|
29
|
+
|
30
|
+
sleep 5
|
data/doc/reference.textile
CHANGED
@@ -28,9 +28,9 @@ Tea::Blitting:
|
|
28
28
|
Tea::PrimitiveDrawing:
|
29
29
|
* clear()
|
30
30
|
* point(x, y, color)
|
31
|
-
* rect(x, y, w, h, color)
|
32
|
-
* line(x1, y1, x2, y2, color[, {:antialias => false}])
|
33
|
-
* circle(x, y, radius, color[, {:outline => false, :antialias => false}])
|
31
|
+
* rect(x, y, w, h, color[, {:mix => :blend (alt. :replace)}])
|
32
|
+
* line(x1, y1, x2, y2, color[, {:antialias => false, :mix => :blend (alt. :replace)}])
|
33
|
+
* circle(x, y, radius, color[, {:outline => false, :antialias => false, :mix => :blend (alt. :replace)}])
|
34
34
|
|
35
35
|
Note that colours are of the form 0xRRGGBBAA.
|
36
36
|
|
@@ -65,9 +65,9 @@ Tea::Blitting:
|
|
65
65
|
Tea::PrimitiveDrawing:
|
66
66
|
* clear()
|
67
67
|
* point(x, y, color)
|
68
|
-
* rect(x, y, w, h, color)
|
69
|
-
* line(x1, y1, x2, y2, color[, {:antialias => false}])
|
70
|
-
* circle(x, y, radius, color[, {:outline => false, :antialias => false}])
|
68
|
+
* rect(x, y, w, h, color[, {:mix => :blend (alt. :replace)}])
|
69
|
+
* line(x1, y1, x2, y2, color[, {:antialias => false, :mix => :blend (alt. :replace)}])
|
70
|
+
* circle(x, y, radius, color[, {:outline => false, :antialias => false, :mix => :blend (alt. :replace)}])
|
71
71
|
|
72
72
|
Note that colours are of the form 0xRRGGBBAA.
|
73
73
|
|
data/lib/tea/c_bitmap.rb
CHANGED
@@ -74,16 +74,15 @@ module Tea
|
|
74
74
|
raise Tea::Error, "can't create bitmap smaller than 1x1 (#{width}x#{height})", caller
|
75
75
|
end
|
76
76
|
|
77
|
-
# Default to
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
@buffer = SDL::Surface.new(SDL::SWSURFACE | SDL::SRCALPHA,
|
77
|
+
# Default to big endian, as it works better with SGE's anti-aliasing.
|
78
|
+
rmask = 0xff000000
|
79
|
+
gmask = 0x00ff0000
|
80
|
+
bmask = 0x0000ff00
|
81
|
+
amask = 0x000000ff
|
82
|
+
@buffer = SDL::Surface.new(SDL::SWSURFACE,
|
84
83
|
width, height, 32,
|
85
84
|
rmask, gmask, bmask, amask)
|
86
|
-
rect 0, 0, w, h, color
|
85
|
+
rect 0, 0, w, h, color, :mix => :replace
|
87
86
|
|
88
87
|
# Optimise for the screen mode, now or later.
|
89
88
|
if Tea::Screen.mode_set?
|
@@ -39,24 +39,68 @@ module Tea
|
|
39
39
|
end
|
40
40
|
|
41
41
|
# Draw a rectangle of size w * h with the top-left corner at (x, y) with
|
42
|
-
# the given color (0xRRGGBBAA).
|
42
|
+
# the given color (0xRRGGBBAA). Hash arguments that can be used:
|
43
43
|
#
|
44
|
-
#
|
45
|
-
|
46
|
-
|
44
|
+
# +:mix+:: +:blend+ averages the RGB parts the rectangle and destination
|
45
|
+
# colours according to the colour's alpha (default).
|
46
|
+
# +:replace+ writes over the full RGBA parts of the rectangle
|
47
|
+
# area's pixels.
|
48
|
+
#
|
49
|
+
# Raises Tea::Error if w or h are less than 0, or if +:mix+ is given an
|
50
|
+
# unrecognised symbol.
|
51
|
+
def rect(x, y, w, h, color, options=nil)
|
52
|
+
if w < 0 || h < 0 || (options && options[:mix] == :blend && (w < 1 || h < 1))
|
47
53
|
raise Tea::Error, "can't draw rectangle of size #{w}x#{h}", caller
|
48
54
|
end
|
49
|
-
|
55
|
+
|
56
|
+
if options == nil || options[:mix] == nil
|
57
|
+
mix = :blend
|
58
|
+
else
|
59
|
+
unless [:blend, :replace].include?(options[:mix])
|
60
|
+
raise Tea::Error, "invalid mix option \"#{options[:mix]}\"", caller
|
61
|
+
end
|
62
|
+
mix = options[:mix]
|
63
|
+
end
|
64
|
+
|
65
|
+
case mix
|
66
|
+
when :blend
|
67
|
+
r, g, b, a = primitive_hex_to_rgba(color)
|
68
|
+
# draw_rect has an off-by-one error with the width and height, hence the "- 1"s.
|
69
|
+
return if w < 1 || h < 1
|
70
|
+
primitive_buffer.draw_rect x, y, w - 1, h - 1, primitive_rgba_to_color(r, g, b, 255), true, a
|
71
|
+
when :replace
|
72
|
+
primitive_buffer.fill_rect x, y, w, h, primitive_color(color)
|
73
|
+
end
|
50
74
|
end
|
51
75
|
|
52
76
|
# Draw a line from (x1, y1) to (x2, y2) with the given color (0xRRGGBBAA).
|
53
77
|
# Optional hash arguments:
|
54
78
|
#
|
55
79
|
# +:antialias+:: If true, smooth the line with antialiasing.
|
80
|
+
# +:mix+:: +:blend+ averages the RGB parts of the line and
|
81
|
+
# destination colours by the line alpha (default).
|
82
|
+
# +:replace+ writes over the RGBA parts of the line
|
83
|
+
# destination pixels.
|
56
84
|
def line(x1, y1, x2, y2, color, options=nil)
|
57
|
-
|
58
|
-
|
59
|
-
|
85
|
+
if options == nil
|
86
|
+
aa = false
|
87
|
+
mix = :blend
|
88
|
+
else
|
89
|
+
aa = options[:antialias] || false
|
90
|
+
mix = options[:mix] || :blend
|
91
|
+
|
92
|
+
unless [:blend, :replace].include?(mix)
|
93
|
+
raise Tea::Error, "invalid mix option \"#{mix}\"", caller
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
case mix
|
98
|
+
when :blend
|
99
|
+
r, g, b, a = primitive_hex_to_rgba(color)
|
100
|
+
primitive_buffer.draw_line x1, y1, x2, y2, primitive_rgba_to_color(r, g, b, 255), aa, a
|
101
|
+
when :replace
|
102
|
+
primitive_buffer.draw_line x1, y1, x2, y2, primitive_color(color), aa
|
103
|
+
end
|
60
104
|
end
|
61
105
|
|
62
106
|
# Draw a circle centred at (x, y) with the given radius and color
|
@@ -65,18 +109,52 @@ module Tea
|
|
65
109
|
# +:outline+:: If true, do not fill the circle, just draw an outline.
|
66
110
|
# +:antialias+:: If true, smooth the edges of the circle with
|
67
111
|
# antialiasing.
|
112
|
+
# +:mix+:: +:blend+ averages the RGB parts of the circle and
|
113
|
+
# destination colours by the colour's alpha (default).
|
114
|
+
# +:replace+ writes over the RGBA parts of the circle's
|
115
|
+
# destination pixels.
|
68
116
|
#
|
69
|
-
# Raises Tea::Error if the radius is less than 0
|
117
|
+
# Raises Tea::Error if the radius is less than 0, or :mix is given an
|
118
|
+
# unrecognised symbol.
|
70
119
|
def circle(x, y, radius, color, options=nil)
|
71
120
|
if radius < 0
|
72
121
|
raise Tea::Error, "can't draw circle with radius #{radius}", caller
|
73
122
|
end
|
74
123
|
|
75
|
-
if options
|
76
|
-
|
77
|
-
|
124
|
+
if options == nil
|
125
|
+
outline = false
|
126
|
+
antialias = false
|
127
|
+
mix = :blend
|
78
128
|
else
|
79
|
-
|
129
|
+
outline = options[:outline] || false
|
130
|
+
antialias = options[:antialias] || false
|
131
|
+
mix = options[:mix] || :blend
|
132
|
+
|
133
|
+
unless [:blend, :replace].include?(mix)
|
134
|
+
raise Tea::Error, "invalid mix option \"#{mix}\"", caller
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
case mix
|
139
|
+
when :blend
|
140
|
+
r, g, b, a = primitive_hex_to_rgba(color)
|
141
|
+
if !outline && antialias && a < 255
|
142
|
+
# rubysdl can't draw filled antialiased alpha circles for some reason.
|
143
|
+
# Big endian because the SGE-powered circle antialiasing apparently
|
144
|
+
# doesn't like it any other way.
|
145
|
+
ts = SDL::Surface.new(SDL::SWSURFACE, (radius + 1) * 2, (radius + 1) * 2, 32,
|
146
|
+
0xff000000,
|
147
|
+
0x00ff0000,
|
148
|
+
0x0000ff00,
|
149
|
+
0x000000ff)
|
150
|
+
ts.draw_circle radius + 1, radius + 1, radius, ts.map_rgba(r, g, b, a), true, true
|
151
|
+
SDL::Surface.blit ts, 0, 0, ts.w, ts.h, primitive_buffer, x - radius - 1, y - radius - 1
|
152
|
+
else
|
153
|
+
primitive_buffer.draw_circle x, y, radius, primitive_rgba_to_color(r, g, b, 255),
|
154
|
+
!outline, antialias, (a == 255 ? nil : a)
|
155
|
+
end
|
156
|
+
when :replace
|
157
|
+
primitive_buffer.draw_circle x, y, radius, primitive_color(color), !outline, antialias
|
80
158
|
end
|
81
159
|
end
|
82
160
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tung-tea
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tung Nguyen
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-07-
|
12
|
+
date: 2009-07-31 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -37,14 +37,17 @@ files:
|
|
37
37
|
- doc/example/bitmap_draw.rb
|
38
38
|
- doc/example/bitmap_load.rb
|
39
39
|
- doc/example/bitmap_new.rb
|
40
|
-
- doc/example/
|
40
|
+
- doc/example/circle.rb
|
41
|
+
- doc/example/circle_alpha.rb
|
41
42
|
- doc/example/event_app.rb
|
42
43
|
- doc/example/event_keyboard.rb
|
43
44
|
- doc/example/event_mouse.rb
|
44
45
|
- doc/example/init.rb
|
45
46
|
- doc/example/lines.rb
|
47
|
+
- doc/example/lines_alpha.rb
|
46
48
|
- doc/example/point.rb
|
47
49
|
- doc/example/rect.rb
|
50
|
+
- doc/example/rect_alpha.rb
|
48
51
|
- doc/example/screen_set_mode.rb
|
49
52
|
- doc/example/screen_update.rb
|
50
53
|
- doc/example/smile.png
|
File without changes
|