texplay 0.2.721-i386-mingw32

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 (42) hide show
  1. data/CHANGELOG +119 -0
  2. data/README.markdown +41 -0
  3. data/Rakefile +66 -0
  4. data/examples/common.rb +8 -0
  5. data/examples/example_alpha_blend.rb +31 -0
  6. data/examples/example_bezier.rb +42 -0
  7. data/examples/example_color_control.rb +69 -0
  8. data/examples/example_color_transform.rb +68 -0
  9. data/examples/example_dup.rb +75 -0
  10. data/examples/example_each.rb +42 -0
  11. data/examples/example_effect.rb +35 -0
  12. data/examples/example_fill.rb +44 -0
  13. data/examples/example_fill_old.rb +49 -0
  14. data/examples/example_fluent.rb +31 -0
  15. data/examples/example_gen_eval.rb +34 -0
  16. data/examples/example_hash_arguments.rb +47 -0
  17. data/examples/example_lsystem.rb +61 -0
  18. data/examples/example_melt.rb +27 -0
  19. data/examples/example_polyline.rb +43 -0
  20. data/examples/example_scale.rb +29 -0
  21. data/examples/example_simple.rb +38 -0
  22. data/examples/example_splice.rb +27 -0
  23. data/examples/example_sync.rb +60 -0
  24. data/examples/example_turtle.rb +40 -0
  25. data/examples/example_weird.rb +29 -0
  26. data/examples/media/bird.png +0 -0
  27. data/examples/media/empty2.png +0 -0
  28. data/examples/media/gob.png +0 -0
  29. data/examples/media/gosu.png +0 -0
  30. data/examples/media/green.png +0 -0
  31. data/examples/media/logo.png +0 -0
  32. data/examples/media/maria.png +0 -0
  33. data/examples/media/rose.bmp +0 -0
  34. data/examples/media/sand1.png +0 -0
  35. data/examples/media/sunset.png +0 -0
  36. data/examples/media/texplay.png +0 -0
  37. data/lib/1.8/texplay.so +0 -0
  38. data/lib/1.9/texplay.so +0 -0
  39. data/lib/texplay/version.rb +3 -0
  40. data/lib/texplay-contrib.rb +171 -0
  41. data/lib/texplay.rb +137 -0
  42. metadata +115 -0
@@ -0,0 +1,31 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'gosu'
4
+ require 'texplay'
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/sunset.png")
10
+ @img.rect 0,0, @img.width - 1, @img.height - 1
11
+
12
+ # test the fluent interface
13
+ @img.
14
+ line(0, 0, 1024, 1024).
15
+ circle(20, 20, 50).
16
+ rect 30, 30, 100, 100
17
+ end
18
+
19
+ def draw
20
+ x = @img.width * rand
21
+ y = @img.height * rand
22
+
23
+ @img.draw 100, 50,1
24
+ end
25
+
26
+ end
27
+
28
+
29
+ w = W.new
30
+ w.show
31
+
@@ -0,0 +1,34 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
10
+
11
+ @width = @img.width
12
+ @height = @img.height
13
+
14
+ # turn alpha blending and filling on
15
+ @img.set_options :alpha_blend => true, :fill => true
16
+ end
17
+
18
+ def draw
19
+
20
+ # Gen_eval lets us use local instance vars within the block
21
+ # even though the block appears to be getting instance_eval'd
22
+ # for more information see gen_eval.c and object2module.c
23
+ @img.paint {
24
+ rect @width * rand, @height * rand, @width * rand, @height * rand,
25
+ :color => [rand, rand ,rand, rand]
26
+ }
27
+
28
+ @img.draw 100, 50,1
29
+ end
30
+ end
31
+
32
+ w = W.new
33
+ w.show
34
+
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
10
+ @tp = Gosu::Image.new(self, "#{Common::MEDIA}/texplay.png")
11
+
12
+
13
+ # put a border on the image
14
+ @img.rect 0,0, @img.width - 1, @img.height - 1
15
+
16
+ # it can be annoying having to specify a bunch of :hash_arguments for every action
17
+ # here is how you specify common hash args that all actions will use (in the same image)
18
+
19
+ # all actions that respond to 'thickness' use a thickness of 8 pixels
20
+ # also set the color to random
21
+ @img.set_options :thickness => 8, :color => :rand
22
+
23
+ @img.rect 100, 100, 200, 200, :fill => false
24
+
25
+ # NOTE: for ngon, the parameters are as follows: x, y, radius, num_sides
26
+ @img.ngon 400, 400, 40, 3
27
+
28
+ # NOTE: the defaults can also be overidden:
29
+ @img.ngon 400, 200, 90, 6, :thickness => 1
30
+
31
+ # you can also delete the defaults
32
+ @img.delete_options
33
+
34
+ # this action will no longer have any default values
35
+ @img.ngon 200, 400, 90, 10
36
+ end
37
+
38
+ def draw
39
+ @img.draw 100, 50,1
40
+ end
41
+
42
+ end
43
+
44
+
45
+ w = W.new
46
+ w.show
47
+
@@ -0,0 +1,61 @@
1
+ require 'common'
2
+ require 'gosu'
3
+
4
+ Dragon = TexPlay::LSystem.new do
5
+ rule "F" => "F"
6
+ rule "X" => "X+YF+"
7
+ rule "Y" => "-FX-Y"
8
+ angle 90
9
+
10
+ atom "FX"
11
+ end
12
+
13
+ Koch = TexPlay::LSystem.new do
14
+ rule "F" => "F-F++F-F"
15
+
16
+ angle 60
17
+
18
+ atom "F"
19
+ end
20
+
21
+ Bush1 = TexPlay::LSystem.new do
22
+ rule "F" => "F[-F]F[+F][F]"
23
+
24
+ angle 20
25
+ atom "F"
26
+ end
27
+
28
+ Bush2 = TexPlay::LSystem.new do
29
+ rule "F" => "FF"
30
+ rule "X" => "F[+X]F[-X]+X"
31
+
32
+ angle 20
33
+ atom "X"
34
+ end
35
+
36
+ Bush3 = TexPlay::LSystem.new do
37
+ rule "F" => "FF"
38
+ rule "X" => "F-[[X]+X]+F[+FX]-X"
39
+
40
+ angle 22.5
41
+ atom "X"
42
+ end
43
+
44
+
45
+
46
+ class W < Gosu::Window
47
+ def initialize
48
+ super(1024, 768, false, 20)
49
+ @img = TexPlay::create_blank_image(self, 500, 500)
50
+ @img.set_options :color => :rand
51
+ @img.lsystem(400, 150, Koch, :order => 8, :line_length => 6)
52
+ #@img.save("dragon.jpg")
53
+ end
54
+
55
+ def draw
56
+ @img.draw(100,0,1)
57
+ end
58
+ end
59
+
60
+ w = W.new
61
+ w.show
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'gosu'
4
+ require 'texplay'
5
+
6
+
7
+ class W < Gosu::Window
8
+ def initialize
9
+ super(1024, 768, false, 20)
10
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/sunset.png")
11
+ end
12
+
13
+ def draw
14
+ x = (@img.width - 100/2) * rand
15
+ y = (@img.height - 100/2) * rand
16
+
17
+ @img.splice @img, x, y + 1, :crop => [x, y, x + 100, y + 100]
18
+
19
+ @img.draw 100, 50,1
20
+ end
21
+
22
+ end
23
+
24
+
25
+ w = W.new
26
+ w.show
27
+
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
10
+
11
+ # put a border on the image
12
+ @img.rect 0,0, @img.width - 1, @img.height - 1
13
+
14
+ points = []
15
+
16
+ # NOTE: TexPlay also accepts points. a 'point' is any object that responds to 'x' or 'y'
17
+ 10.times {
18
+ p = TexPlay::TPPoint.new
19
+ p.x = @img.width * rand
20
+ p.y = @img.height * rand
21
+
22
+ points << p
23
+ }
24
+
25
+
26
+ # what if we want to turn a polyline into a polygon?
27
+ @img.polyline points, :closed => true, :color => :blue
28
+ @img.polyline points, :color => :red
29
+ end
30
+
31
+ def draw
32
+
33
+ # NOTE: (when viewing) the blue line is the extra line added to close the polygon,
34
+ # red lines are original polyline
35
+ @img.draw 100, 50,1
36
+ end
37
+
38
+ end
39
+
40
+
41
+ w = W.new
42
+ w.show
43
+
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'gosu'
4
+ require 'texplay'
5
+
6
+
7
+ class W < Gosu::Window
8
+ def initialize
9
+ super(1024, 768, false, 20)
10
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/logo.png")
11
+ @img2 = TexPlay::create_blank_image(self, 500, 500)
12
+
13
+ @img2.splice_and_scale @img, 0, 50, :factor => 2
14
+ @img2.splice @img, 0, 200
15
+ end
16
+
17
+ def draw
18
+ x = @img.width * rand
19
+ y = @img.height * rand
20
+
21
+ @img2.draw 100, 100,1
22
+ end
23
+
24
+ end
25
+
26
+
27
+ w = W.new
28
+ w.show
29
+
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'gosu'
4
+ require 'texplay'
5
+
6
+
7
+ class W < Gosu::Window
8
+ def initialize
9
+ super(1024, 768, false, 20)
10
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
11
+ @gosu = Gosu::Image.new(self, "#{Common::MEDIA}/gosu.png")
12
+
13
+ # put a border on the image
14
+ @img.rect 0,0, @img.width - 1, @img.height - 1
15
+
16
+ # perform some simple drawing actions
17
+ @img.line 0,0, @img.width - 1, @img.height - 1, :color => :yellow
18
+ @img.circle 400, 100, 40, :fill => true, :color => [rand, rand, rand, 1]
19
+ @img.rect 200, 300, 300, 400, :fill => true, :color => :red
20
+
21
+ @img.ngon 400,300,50, 5, :start_angle => 90
22
+ @img.ngon 300,300,50, 5, :start_angle => 45
23
+
24
+ # NOTE: chroma_key means NOT to splice in that color (pixels with that color are skipped)
25
+ # (chroma_key_not does the opposite, it skips pixels that do NOT have that color)
26
+ @img.splice @gosu, 210, 330, :chroma_key => :alpha
27
+ end
28
+
29
+ def draw
30
+ @img.draw 100, 50,1
31
+ end
32
+
33
+ end
34
+
35
+
36
+ w = W.new
37
+ w.show
38
+
@@ -0,0 +1,27 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/texplay.png")
10
+ @gosu = Gosu::Image.new(self, "#{Common::MEDIA}/gosu.png")
11
+
12
+ @img.splice @gosu, 140,20, :alpha_blend => true
13
+ @img.rect 140,20, 160, 180, :color => [1,1,1,0.5], :alpha_blend => true, :fill => true
14
+
15
+ @img.splice @gosu, 50,20, :chroma_key => :alpha
16
+ end
17
+
18
+ def draw
19
+ @img.draw 100, 50,1
20
+ end
21
+
22
+ end
23
+
24
+
25
+ w = W.new
26
+ w.show
27
+
@@ -0,0 +1,60 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
10
+
11
+ # sets the 'global' color for all actions in this image
12
+ @img.color :red
13
+
14
+ # let's draw the circle without syncing it to gl
15
+ @img.circle 100, 100, 50, :fill => true, :sync_mode => :no_sync
16
+
17
+ # now let's sync half of it to gl
18
+ @img.force_sync [100, 50, 150, 150]
19
+
20
+ # let's draw some lazy shapes
21
+ @img.set_options :sync_mode => :lazy_sync
22
+
23
+ @img.ngon 200, 300, 40, 5, :color => :red
24
+ @img.ngon 280, 300, 40, 6, :color => :green
25
+ @img.ngon 360, 300, 40, 7, :color => :blue
26
+
27
+ # now let's sync the lazy shapes to gl
28
+ @img.paint
29
+
30
+ # NOTE: the lazy drawing (above) is identical to using the following paint block
31
+ # @img.paint {
32
+ # ngon 200, 300, 50, 5
33
+ # ...etc
34
+ # }
35
+
36
+ # end lazy drawing mode
37
+ @img.delete_options
38
+
39
+ # the default sync mode is eager_sync
40
+ # in this mode actions are drawn and sync'd immediately
41
+ # NOTE: TexPlay only syncs the part of the image that changed to gl
42
+ @img.ngon 440, 300, 40, 8, :color => :tyrian
43
+
44
+ # paint blocks can also accept a sync_mode parameter
45
+ # NOTE: the line below will not be visible until you
46
+ # explictly sync it to gl; probably using @img.force_sync
47
+ @img.paint(:sync_mode => :no_sync) {
48
+ line 0, 0, @img.width, @img.height
49
+ }
50
+ end
51
+
52
+ def draw
53
+
54
+ @img.draw 100, 50,1
55
+ end
56
+ end
57
+
58
+ w = W.new
59
+ w.show
60
+
@@ -0,0 +1,40 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/empty2.png")
10
+
11
+ # put a border on the image
12
+ @img.rect 0, 0, @img.width - 1, @img.height - 1
13
+
14
+ @length = 0
15
+ end
16
+
17
+
18
+ # NOTE: turtle is currently written in Ruby so is very slow, look at texplay-contrib.rb for source code.
19
+ def draw
20
+
21
+ # NOTE: putting actions in paint block means they are only sync'd to gl at end of block.
22
+ # compare to calling an action directly (on an @img) where it is sync'd to gl immediately
23
+
24
+ @img.paint {
25
+
26
+ # a 2nd arg of 'true' means to show the turtle
27
+ forward(@length, true, :color => :red)
28
+ turn(89.5)
29
+ @length += 2
30
+ }
31
+
32
+ @img.draw 100, 50,1
33
+ end
34
+
35
+ end
36
+
37
+
38
+ w = W.new
39
+ w.show
40
+
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'common'
3
+ require 'texplay'
4
+
5
+
6
+ class W < Gosu::Window
7
+ def initialize
8
+ super(1024, 768, false, 20)
9
+ @img = Gosu::Image.new(self, "#{Common::MEDIA}/bird.png")
10
+ @green = Gosu::Image.new(self, "#{Common::MEDIA}/gob.png")
11
+ ### @img = Gosu::Image.new(self, "gob.png")
12
+ #@img = TexPlay.create_image(self, 500, 500).fill(0,0, :color => :red)
13
+
14
+ @img.splice @green, 0, 0, :alpha_blend => true
15
+ puts @img.get_pixel 15, 15
16
+ # @img.rect 0, 0, @img.width, @img.height, :texture => @gob, :fill => true, :alpha_blend => true
17
+
18
+ end
19
+
20
+ def draw
21
+ @img.draw 100, 50,1
22
+ end
23
+
24
+ end
25
+
26
+
27
+ w = W.new
28
+ w.show
29
+
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,3 @@
1
+ module TexPlay
2
+ VERSION = "0.2.721"
3
+ end
@@ -0,0 +1,171 @@
1
+ begin
2
+ require 'rubygems'
3
+ rescue LoadError
4
+ end
5
+
6
+ require 'texplay'
7
+
8
+ # to bring in String#each_char for 1.8
9
+ if RUBY_VERSION =~ /1.8/
10
+ require 'jcode'
11
+ end
12
+
13
+ # setup will be executed straight after Gosu::Image instantiation
14
+ TexPlay::on_setup do
15
+ @turtle_pos = TexPlay::TPPoint.new(width / 2, height / 2 )
16
+ @turtle_angle = 0
17
+ end
18
+
19
+
20
+ TexPlay::create_macro(:move_to) do |x, y|
21
+ capture {
22
+ @turtle_pos.x = x
23
+ @turtle_pos.y = y
24
+ }
25
+ end
26
+
27
+ TexPlay::create_macro(:move_rel) do |dx, dy|
28
+ capture {
29
+ @turtle_pos.x += dx
30
+ @turtle_pos.y += dy
31
+ }
32
+ end
33
+
34
+ TexPlay::create_macro(:line_to) do |x, y, *other|
35
+ capture {
36
+ line(@turtle_pos.x, @turtle_pos.y, x, y, *other)
37
+
38
+ @turtle_pos.x, @turtle_pos.y = x, y
39
+ }
40
+ end
41
+
42
+ TexPlay::create_macro(:line_rel) do |dx, dy, *other|
43
+ capture {
44
+ x = @turtle_pos.x + dx
45
+ y = @turtle_pos.y + dy
46
+
47
+ line(@turtle_pos.x, @turtle_pos.y, x, y, *other)
48
+
49
+ @turtle_pos.x, @turtle_pos.y = x, y
50
+ }
51
+ end
52
+
53
+ TexPlay::create_macro(:turn_to) do |a|
54
+ capture {
55
+ @turtle_angle = a
56
+ }
57
+ end
58
+
59
+ TexPlay::create_macro(:turn) do |da|
60
+ capture {
61
+ @turtle_angle += da
62
+ }
63
+ end
64
+
65
+ TexPlay::create_macro(:forward) do |dist, *other|
66
+ capture {
67
+ visible = other.shift
68
+
69
+ radians_per_degree = 0.0174532925199433
70
+
71
+ x = @turtle_pos.x + dist * Math::cos(radians_per_degree * @turtle_angle)
72
+ y = @turtle_pos.y + dist * Math::sin(radians_per_degree * @turtle_angle)
73
+
74
+ if(visible) then
75
+ line_to(x, y, *other)
76
+ else
77
+ move_to(x, y)
78
+ end
79
+ }
80
+ end
81
+
82
+ # L-System code
83
+ # adding LSystem class to TexPlay module
84
+ class TexPlay::LSystem
85
+ def initialize(&block)
86
+ @rules = {}
87
+
88
+ instance_eval(&block) if block
89
+ end
90
+
91
+ def rule(new_rule)
92
+ @rules.merge!(new_rule)
93
+ end
94
+
95
+ def atom(new_atom)
96
+ @atom = new_atom
97
+ end
98
+
99
+ def angle(new_angle=nil)
100
+ return @angle if !new_angle
101
+ @angle = new_angle
102
+ end
103
+
104
+ def produce_string(order)
105
+ order = order[:order]
106
+ string = @atom.dup
107
+
108
+ order.times do
109
+ i = 0
110
+ while(i < string.length)
111
+ sub = @rules[string[i, 1]]
112
+
113
+ string[i] = sub if sub
114
+
115
+ i += sub ? sub.length : 1
116
+ end
117
+ end
118
+
119
+ string
120
+ end
121
+ end
122
+
123
+ # L-System macro
124
+ TexPlay::create_macro(:lsystem) do |x, y, system, options|
125
+ capture {
126
+ theta = system.angle
127
+ turtle_stack = []
128
+ move_to(x, y)
129
+ line_length = options[:line_length] || 1
130
+
131
+ system.produce_string(options).each_char do |v|
132
+
133
+ case v
134
+ when "F"
135
+ forward(line_length, true)
136
+ when "+"
137
+ turn(theta)
138
+ when "-"
139
+ turn(-theta)
140
+ when "["
141
+ turtle_stack.push([@turtle_pos.dup, @turtle_angle])
142
+ when "]"
143
+ @turtle_pos, @turtle_angle = turtle_stack.pop
144
+ end
145
+ end
146
+ }
147
+ end
148
+
149
+ # Scaling
150
+ # uses nearest-neighbour
151
+ TexPlay::create_macro(:splice_and_scale) do |img, cx, cy, *options|
152
+ options = options.first ? options.first : {}
153
+
154
+ options = {
155
+ :color_control => proc do |c1, c2, x, y|
156
+ factor = options[:factor] || 1
157
+ factor_x = options[:factor_x] || factor
158
+ factor_y = options[:factor_y] || factor
159
+
160
+ x = factor_x * (x - cx) + cx
161
+ y = factor_y * (y - cy) + cy
162
+
163
+ rect x, y, x + factor_x, y + factor_y, :color => c2, :fill => true
164
+ :none
165
+ end
166
+ }.merge!(options)
167
+
168
+ splice img, cx, cy, options
169
+
170
+ self
171
+ end