panda_canvas 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Dimitry Solovyov
1
+ Copyright (c) 2010-2011 Dimitry Solovyov
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  of this software and associated documentation files (the "Software"), to deal
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
16
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
17
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
18
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
- THE SOFTWARE.
19
+ THE SOFTWARE.
data/README.rdoc CHANGED
@@ -12,4 +12,4 @@ Panda Canvas is available as a gem:
12
12
 
13
13
  == More
14
14
 
15
- Take a look at the {wiki}[http://github.com/8bitpanda/panda_canvas/wiki].
15
+ Take a look at the {wiki}[http://github.com/dimituri/panda_canvas/wiki].
@@ -15,26 +15,25 @@ module PandaCanvas
15
15
  self.caption = 'Panda Canvas'
16
16
  @block = block
17
17
  @image = TexPlay.create_image(self, width, height)
18
- @clean_room = AnimationCleanRoom.new(@image)
19
- @clean_room.canvas_calls = []
20
- @clean_room.frame = 0
18
+ @agent = DrawingAgent.new(self, @image)
19
+ @agent.frame = 0
21
20
  end
22
21
 
23
22
  # Draws the image in memory.
24
23
  def draw
25
24
  @image.draw(0, 0, 0)
26
- @clean_room.canvas_calls.each {|call| send call[0], *call[1..-1] }
25
+ @agent.canvas_calls.each {|call| send call[0], *call[1..-1] }
27
26
  end
28
27
 
29
28
  # Runs an animation block.
30
29
  def update
31
30
  @image.rect 0, 0, width, height, :color => :black, :fill => true
32
31
  DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
33
- @clean_room.canvas_calls = []
34
- @clean_room.frame += 1
35
- @clean_room.instance_eval &@block
32
+ @agent.canvas_calls = []
33
+ @agent.frame += 1
34
+ @agent.instance_eval &@block
36
35
  end
37
36
 
38
- end
37
+ end # AnimationCanvas
39
38
 
40
- end # PandaCanvas
39
+ end # PandaCanvas
@@ -1,7 +1,9 @@
1
1
  module PandaCanvas
2
2
 
3
- # AnimationCleanRoom is used to capture and store method calls for delayed execution.
4
- class AnimationCleanRoom
3
+ # DrawingAgent is used to capture drawing method calls
4
+ # and pass them to the active window and a TextPlay image
5
+ # in the correct order.
6
+ class DrawingAgent
5
7
 
6
8
  # Array of methods that need to be passed to canvas.
7
9
  attr_accessor :canvas_calls
@@ -9,9 +11,12 @@ module PandaCanvas
9
11
  # Current frame number.
10
12
  attr_accessor :frame
11
13
 
12
- # Initializes the clean object for method call capturing.
14
+ # Initializes the clean object in a given +window+
15
+ # for method call capturing.
13
16
  # TexPlay +image+ is used for TexPlay drawing methods.
14
- def initialize(image)
17
+ def initialize(window, image)
18
+ @canvas_calls = []
19
+ @window = window
15
20
  @image = image
16
21
  end
17
22
 
@@ -21,11 +26,13 @@ module PandaCanvas
21
26
  @canvas_calls << [sym, *args]
22
27
  elsif Gosu::Image.public_instance_methods.include? sym
23
28
  @image.send sym, *args
29
+ elsif @window.respond_to? sym
30
+ @window.send sym, *args
24
31
  else
25
32
  super
26
33
  end
27
34
  end
28
35
 
29
- end # CleanRoom
36
+ end # DrawingAgent
30
37
 
31
- end # PandaCanvas
38
+ end # PandaCanvas
@@ -5,6 +5,16 @@ module PandaCanvas
5
5
 
6
6
  include DrawingMethods
7
7
 
8
+ # Include platform-specific update behavior.
9
+ if RUBY_PLATFORM =~ /mswin|mingw/
10
+ require 'panda_canvas/drawing_with_clean_room'
11
+ include DrawingWithCleanRoom
12
+ else
13
+ require 'fiber'
14
+ require 'panda_canvas/drawing_with_fibers'
15
+ include DrawingWithFibers
16
+ end
17
+
8
18
  # TexPlay image, which is drawn in the window.
9
19
  attr_reader :image
10
20
 
@@ -14,10 +24,8 @@ module PandaCanvas
14
24
  super(width, height, false)
15
25
  self.caption = 'Panda Canvas'
16
26
  @image = TexPlay.create_image(self, width, height)
17
- clean_room = DrawingCleanRoom.new
18
- clean_room.instance_eval(&block)
19
- @calls = clean_room.calls
20
27
  @canvas_calls = []
28
+ prepare &block
21
29
  end
22
30
 
23
31
  # Draws the image in memory.
@@ -26,22 +34,6 @@ module PandaCanvas
26
34
  @canvas_calls.each {|call| send call[0], *call[1..-1] }
27
35
  end
28
36
 
29
- # Runs a range of commands until the next flush.
30
- def update
31
- DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
32
- unless @calls.empty?
33
- flush_index = @calls.index(DrawingCleanRoom::FLUSH)
34
- @calls.slice!(0...flush_index).each do |call|
35
- if DrawingMethods::CANVAS_CALLS.include? call[0]
36
- @canvas_calls << call
37
- else
38
- @image.send call[0], *call[1..-1]
39
- end
40
- end
41
- @calls.shift
42
- end
43
- end
44
-
45
- end # Canvas
37
+ end # DrawingCanvas
46
38
 
47
- end # PandaCanvas
39
+ end # PandaCanvas
@@ -1,6 +1,7 @@
1
1
  module PandaCanvas
2
2
 
3
- # DrawingCleanRoom is used to capture and store method calls for delayed execution.
3
+ # DrawingCleanRoom is used to capture and store method calls
4
+ # for delayed execution.
4
5
  class DrawingCleanRoom
5
6
 
6
7
  # Signature for the +flush+ method.
@@ -30,6 +31,6 @@ module PandaCanvas
30
31
  end
31
32
  end
32
33
 
33
- end # CleanRoom
34
+ end # DrawingCleanRoom
34
35
 
35
- end # PandaCanvas
36
+ end # PandaCanvas
@@ -1,14 +1,17 @@
1
1
  module PandaCanvas
2
2
 
3
+ # DrawingMethods is a set of drawing methods used by PandaCanvas canvases.
3
4
  module DrawingMethods
4
5
 
5
6
  # Signature set for defaults that are executed in each update event.
6
7
  CANVAS_UPDATE = [[:font, 12]]
7
8
 
8
- # Names of calls that need to be sent directly to Canvas instead of the TexPlay image on draw.
9
+ # Names of calls that need to be sent directly to Canvas
10
+ # instead of the TexPlay image on draw.
9
11
  CANVAS_CALLS = [:font, :text, :text_rel].freeze
10
12
 
11
- # Sets the font with name +font_name+ and +height+ in pixels to be used when drawing text.
13
+ # Sets the font with name +font_name+ and +height+ in pixels
14
+ # to be used when drawing text.
12
15
  # All subsequent text drawing calls will use the given font.
13
16
  def font(height, font_name=Gosu::default_font_name)
14
17
  @used_fonts ||= {}
@@ -47,4 +50,4 @@ module PandaCanvas
47
50
 
48
51
  end # DrawingMethods
49
52
 
50
- end # PandaCanvas
53
+ end # PandaCanvas
@@ -0,0 +1,35 @@
1
+ module PandaCanvas
2
+
3
+ # DrawingWithCleanRoom adds behavior to perform canvas updates
4
+ # using method capturing and delayed execution.
5
+ # This is faster, but somewhat ugly and should only be used in environments
6
+ # where resuming fibers in the Gosu draw loop results in a segfault.
7
+ module DrawingWithCleanRoom
8
+
9
+ # Evaluates the drawing block in a DrawingCleanRoom instance
10
+ # and captures drawing method sequence.
11
+ def prepare(&block)
12
+ clean_room = DrawingCleanRoom.new
13
+ clean_room.instance_eval(&block)
14
+ @calls = clean_room.calls
15
+ end
16
+
17
+ # Performs drawing until the next flush using the captured sequence.
18
+ def update
19
+ unless @calls.empty?
20
+ DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
21
+ flush_index = @calls.index(DrawingCleanRoom::FLUSH)
22
+ @calls.slice!(0...flush_index).each do |call|
23
+ if DrawingMethods::CANVAS_CALLS.include? call[0]
24
+ @canvas_calls << call
25
+ else
26
+ @image.send call[0], *call[1..-1]
27
+ end
28
+ end
29
+ @calls.shift
30
+ end
31
+ end
32
+
33
+ end # DrawingWithCleanRoom
34
+
35
+ end # PandaCanvas
@@ -0,0 +1,30 @@
1
+ module PandaCanvas
2
+
3
+ # DrawingWithFibers adds behavior to perform canvas updates
4
+ # using fibers. Test show that this behavior is only suitable for unixes.
5
+ module DrawingWithFibers
6
+
7
+ # Yields control back to calling context.
8
+ def flush
9
+ Fiber.yield
10
+ end
11
+
12
+ # Creates a new fiber that evaluates the given +block+ in this instance.
13
+ def prepare(&block)
14
+ @agent = agent = DrawingAgent.new(self, @image)
15
+ @fiber = Fiber.new { agent.instance_eval &block }
16
+ end
17
+
18
+ # Performs drawing until the next flush by resuming the fiber.
19
+ def update
20
+ if @fiber.alive?
21
+ @agent.canvas_calls = []
22
+ DrawingMethods::CANVAS_UPDATE.each {|call| send call[0], *call[1..-1] }
23
+ @fiber.resume
24
+ @canvas_calls = @agent.canvas_calls
25
+ end
26
+ end
27
+
28
+ end # DrawingWithCleanRoom
29
+
30
+ end # PandaCanvas
@@ -1,6 +1,15 @@
1
1
  module PandaCanvas
2
2
 
3
- # PandaCanvas version.
4
- VERSION = '0.5.0'
3
+ module Version #:nodoc:
5
4
 
6
- end # end PandaCanvas
5
+ MAJOR = 0
6
+ MINOR = 6
7
+ TINY = 0
8
+
9
+ def self.to_s
10
+ [MAJOR, MINOR, TINY].join '.'
11
+ end
12
+
13
+ end
14
+
15
+ end # PandaCanvas
data/lib/panda_canvas.rb CHANGED
@@ -3,8 +3,8 @@ $:.unshift(libdir) unless $:.include?(libdir)
3
3
 
4
4
  require 'texplay'
5
5
  require 'panda_canvas/drawing_methods'
6
+ require 'panda_canvas/drawing_agent'
6
7
  require 'panda_canvas/animation_canvas'
7
- require 'panda_canvas/animation_clean_room'
8
8
  require 'panda_canvas/drawing_canvas'
9
9
  require 'panda_canvas/drawing_clean_room'
10
10
 
@@ -33,4 +33,4 @@ module PandaCanvas
33
33
 
34
34
  end # class << self
35
35
 
36
- end # PandaCanvas
36
+ end # PandaCanvas
metadata CHANGED
@@ -1,12 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: panda_canvas
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 5
8
- - 0
9
- version: 0.5.0
4
+ prerelease:
5
+ version: 0.6.0
10
6
  platform: ruby
11
7
  authors:
12
8
  - Dimitry Solovyov
@@ -14,7 +10,7 @@ autorequire:
14
10
  bindir: bin
15
11
  cert_chain: []
16
12
 
17
- date: 2010-10-24 00:00:00 +03:00
13
+ date: 2011-05-06 00:00:00 +03:00
18
14
  default_executable:
19
15
  dependencies:
20
16
  - !ruby/object:Gem::Dependency
@@ -25,10 +21,6 @@ dependencies:
25
21
  requirements:
26
22
  - - ~>
27
23
  - !ruby/object:Gem::Version
28
- segments:
29
- - 0
30
- - 7
31
- - 24
32
24
  version: 0.7.24
33
25
  type: :runtime
34
26
  version_requirements: *id001
@@ -40,15 +32,11 @@ dependencies:
40
32
  requirements:
41
33
  - - ~>
42
34
  - !ruby/object:Gem::Version
43
- segments:
44
- - 0
45
- - 2
46
- - 981
47
35
  version: 0.2.981
48
36
  type: :runtime
49
37
  version_requirements: *id002
50
38
  description: Educational 2D drawing canvas on top of Gosu and TexPlay.
51
- email:
39
+ email: dimituri@gmail.com
52
40
  executables: []
53
41
 
54
42
  extensions: []
@@ -57,16 +45,18 @@ extra_rdoc_files: []
57
45
 
58
46
  files:
59
47
  - lib/panda_canvas/animation_canvas.rb
60
- - lib/panda_canvas/animation_clean_room.rb
48
+ - lib/panda_canvas/drawing_agent.rb
61
49
  - lib/panda_canvas/drawing_canvas.rb
62
50
  - lib/panda_canvas/drawing_clean_room.rb
63
51
  - lib/panda_canvas/drawing_methods.rb
52
+ - lib/panda_canvas/drawing_with_clean_room.rb
53
+ - lib/panda_canvas/drawing_with_fibers.rb
64
54
  - lib/panda_canvas/version.rb
65
55
  - lib/panda_canvas.rb
66
56
  - LICENSE
67
57
  - README.rdoc
68
58
  has_rdoc: true
69
- homepage:
59
+ homepage: http://github.com/dimituri/panda_canvas
70
60
  licenses: []
71
61
 
72
62
  post_install_message:
@@ -79,25 +69,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
79
69
  requirements:
80
70
  - - ~>
81
71
  - !ruby/object:Gem::Version
82
- segments:
83
- - 1
84
- - 9
85
- - 2
86
72
  version: 1.9.2
87
73
  required_rubygems_version: !ruby/object:Gem::Requirement
88
74
  none: false
89
75
  requirements:
90
76
  - - ">="
91
77
  - !ruby/object:Gem::Version
92
- segments:
93
- - 1
94
- - 3
95
- - 6
96
78
  version: 1.3.6
97
79
  requirements: []
98
80
 
99
81
  rubyforge_project:
100
- rubygems_version: 1.3.7
82
+ rubygems_version: 1.5.2
101
83
  signing_key:
102
84
  specification_version: 3
103
85
  summary: Educational 2D drawing canvas on top of Gosu and TexPlay.