salamander 0.1.2 → 0.1.3

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.
@@ -7,8 +7,9 @@ class Sketch < Salamander::Actor
7
7
  include Salamander::Drawing
8
8
  end
9
9
 
10
- screen = Salamander.setup(512, 512)
11
- Sketch.draw(screen, ARGV[0])
10
+ canvas = Salamander.setup(512, 512)
11
+ Sketch.draw(canvas, ARGV[0])
12
+ canvas.redraw
12
13
 
13
14
  while true
14
15
  event = SDL.WaitEvent
@@ -3,6 +3,7 @@ require 'ruby-sdl-ffi/gfx'
3
3
  module Salamander
4
4
  require 'salamander/constants'
5
5
  require 'salamander/actor'
6
+ require 'salamander/canvas'
6
7
  require 'salamander/drawing'
7
8
 
8
9
  # Initialize SDL and create a window
@@ -14,6 +15,6 @@ module Salamander
14
15
 
15
16
  info = SDL.GetVideoInfo
16
17
  flags = (info.hw_available) ? SDL::HWSURFACE : SDL::SWSURFACE
17
- SDL.SetVideoMode(width, height, info.vfmt.BitsPerPixel, flags)
18
+ Canvas.new(SDL.SetVideoMode(width, height, info.vfmt.BitsPerPixel, flags))
18
19
  end
19
20
  end
@@ -3,20 +3,19 @@ module Salamander
3
3
  # Executes code within the context of an Actor.
4
4
  #
5
5
  # If given a filename, reads the contents and evaluates it.
6
- def self.draw (screen, filename=nil, &blk)
7
- actor = new(screen)
6
+ def self.draw (canvas, filename=nil, &blk)
7
+ actor = new(canvas)
8
8
  if filename
9
9
  actor.instance_eval(File.open(filename).read, filename)
10
10
  else
11
11
  actor.instance_eval(&blk)
12
12
  end
13
- SDL.UpdateRect(screen, 0, 0, screen.w, screen.h)
14
13
  end
15
14
 
16
- def initialize (surface)
17
- @surface = surface
15
+ def initialize (canvas)
16
+ @canvas = canvas
18
17
 
19
- move_to(surface.w/2, surface.h/2)
18
+ move_to(canvas.width/2, canvas.height/2)
20
19
  face :north
21
20
  color "#FFFFFF"
22
21
  end
@@ -34,6 +33,7 @@ module Salamander
34
33
  else
35
34
  theta = angle
36
35
  end
36
+ theta = theta / 180.0 * Math::PI
37
37
  x = distance * Math.cos(theta) + @x
38
38
  y = distance * Math.sin(theta) + @y
39
39
  move_to(x, y)
@@ -45,25 +45,25 @@ module Salamander
45
45
  end
46
46
 
47
47
 
48
- # The current angle, in radians.
48
+ # The current angle, in degrees.
49
49
  def angle
50
50
  @angle
51
51
  end
52
52
 
53
53
  # Turn towards a new direction.
54
54
  #
55
- # 'theta' may be :right, :left, :around, or a number of radians. Positive amounts of radians turn right, while negative amounts go to the left.
55
+ # 'theta' may be :right, :left, :around, or a number of degrees. Positive amounts of degrees turn right, while negative amounts go to the left.
56
56
  def turn (theta)
57
57
  theta = DIRECTIONS[theta] if theta.is_a? Symbol
58
58
  face(theta + angle)
59
59
  end
60
60
 
61
- # Face a specific direction
61
+ # Face a specific direction.
62
62
  #
63
- # 'theta' may be :north, :northeast, :east, etc. or a number of radians. 0 radians points east, and increasing amounts go clockwise.
63
+ # 'theta' may be :north, :northeast, :east, etc. or a number of degrees. 0 degrees points east, and increasing amounts go clockwise.
64
64
  def face (theta)
65
65
  theta = COMPASS[theta] if theta.is_a? Symbol
66
- @angle = theta % (2 * Math::PI)
66
+ @angle = theta % 360
67
67
  end
68
68
 
69
69
  # Gets or sets the current drawing color in numeric RGBA format.
@@ -86,9 +86,9 @@ module Salamander
86
86
  end
87
87
 
88
88
 
89
- # The SDL surface being drawn to
90
- def surface
91
- @surface
89
+ # The drawing canvas being used
90
+ def canvas
91
+ @canvas
92
92
  end
93
93
  end
94
94
  end
@@ -0,0 +1,38 @@
1
+ module Salamander
2
+ class Canvas
3
+ attr_reader :surface
4
+
5
+ def initialize (surface)
6
+ @surface = surface
7
+ end
8
+
9
+ def redraw
10
+ SDL.UpdateRect(surface, 0, 0, width, height)
11
+ end
12
+
13
+ def point (x, y, color)
14
+ SDL::Gfx.pixelColor(surface, x.round, y.round, color)
15
+ end
16
+
17
+ def line (x1, y1, x2, y2, color)
18
+ SDL::Gfx.lineColor(surface, x1.round, y1.round, x2.round, y2.round, color)
19
+ end
20
+
21
+ def arc (x, y, radius, left, right, color)
22
+ SDL::Gfx.arcColor(surface, x.round, y.round, radius.round, left.round, right.round, color)
23
+ end
24
+
25
+ def circle (x, y, radius, color)
26
+ SDL::Gfx.circleColor(surface, x.round, y.round, radius.round, color)
27
+ end
28
+
29
+
30
+ def width
31
+ surface.w
32
+ end
33
+
34
+ def height
35
+ surface.h
36
+ end
37
+ end
38
+ end
@@ -4,9 +4,6 @@ module Salamander
4
4
  :left => -90,
5
5
  :around => 180,
6
6
  }
7
- DIRECTIONS.each_pair do |k, v|
8
- DIRECTIONS[k] = v / 180.0 * Math::PI
9
- end
10
7
 
11
8
  COMPASS = {
12
9
  :east => 0,
@@ -18,9 +15,6 @@ module Salamander
18
15
  :south => 90,
19
16
  :southeast => 45,
20
17
  }
21
- COMPASS.each_pair do |k, v|
22
- COMPASS[k] = v / 180.0 * Math::PI
23
- end
24
18
 
25
19
  COLORS = {
26
20
  :red => "#FF0000",
@@ -1,5 +1,5 @@
1
1
  module Salamander::Drawing
2
- %w{point line shapes}.each do |mod|
2
+ %w{point line shapes circle}.each do |mod|
3
3
  require "salamander/drawing/#{mod}"
4
4
  include Salamander::Drawing.const_get(mod.capitalize)
5
5
  end
@@ -0,0 +1,30 @@
1
+ module Salamander::Drawing
2
+ module Circle
3
+ def arc(rotation, radius)
4
+ direction = (rotation > 0.degrees) ? :right : :left
5
+
6
+ # Enter the center of the arc's circle
7
+ turn direction # Turn towards the center point
8
+ move radius # Move to the center point
9
+ turn :around # Face the first endpoint of the arc
10
+
11
+ # Draw the arc
12
+ x, y = position
13
+ if rotation > 0.degrees
14
+ canvas.arc(x, y, radius, angle, angle+rotation, color)
15
+ else
16
+ canvas.arc(x, y, radius, angle+rotation, angle, color)
17
+ end
18
+
19
+ # Move to the opposite side of the arc
20
+ turn rotation.degrees
21
+ move radius
22
+ turn direction
23
+ end
24
+
25
+ def circle(radius)
26
+ x, y = position
27
+ canvas.circle(x, y, radius, color)
28
+ end
29
+ end
30
+ end
@@ -4,7 +4,7 @@ module Salamander::Drawing
4
4
  def line_to (x2, y2)
5
5
  x1, y1 = position
6
6
  move_to(x2, y2)
7
- SDL::Gfx.aalineColor(surface, x1.round, y1.round, x2.round, y2.round, color)
7
+ canvas.line(x1, y1, x2, y2, color)
8
8
  end
9
9
 
10
10
  # Draws a line 'distance' pixels long from the current position in the current drawing color.
@@ -12,7 +12,7 @@ module Salamander::Drawing
12
12
  x1, y1 = position
13
13
  move distance, direction
14
14
  x2, y2 = position
15
- SDL::Gfx.aalineColor(surface, x1.round, y1.round, x2.round, y2.round, color)
15
+ canvas.line(x1, y1, x2, y2, color)
16
16
  end
17
17
  end
18
18
  end
@@ -1,8 +1,8 @@
1
1
  module Salamander::Drawing
2
2
  module Point
3
3
  def point
4
- x, y = *position
5
- SDL::Gfx.pixelColor(surface, x, y, color)
4
+ x, y = position
5
+ canvas.point(x, y, color)
6
6
  end
7
7
  end
8
8
  end
@@ -1,11 +1,11 @@
1
1
  class Numeric
2
- # Does nothing - input is assumed to be radians already
2
+ # Converts from radians to degrees.
3
3
  def radians
4
- self
4
+ self * 180.0 / Math::PI
5
5
  end
6
6
 
7
- # Converts from degrees to radians.
7
+ # Does nothing - input is assumed to be degrees already
8
8
  def degrees
9
- self / 180.0 * Math::PI
9
+ self
10
10
  end
11
11
  end
@@ -1,3 +1,3 @@
1
1
  module Salamander
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 2
9
- version: 0.1.2
8
+ - 3
9
+ version: 0.1.3
10
10
  platform: ruby
11
11
  authors:
12
12
  - Jonathan Castello
@@ -56,9 +56,11 @@ extra_rdoc_files: []
56
56
 
57
57
  files:
58
58
  - lib/salamander.rb
59
+ - lib/salamander/canvas.rb
59
60
  - lib/salamander/support/radians.rb
60
61
  - lib/salamander/constants.rb
61
62
  - lib/salamander/actor.rb
63
+ - lib/salamander/drawing/circle.rb
62
64
  - lib/salamander/drawing/point.rb
63
65
  - lib/salamander/drawing/line.rb
64
66
  - lib/salamander/drawing/shapes.rb