salamander 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/salamander +3 -2
- data/lib/salamander.rb +2 -1
- data/lib/salamander/actor.rb +14 -14
- data/lib/salamander/canvas.rb +38 -0
- data/lib/salamander/constants.rb +0 -6
- data/lib/salamander/drawing.rb +1 -1
- data/lib/salamander/drawing/circle.rb +30 -0
- data/lib/salamander/drawing/line.rb +2 -2
- data/lib/salamander/drawing/point.rb +2 -2
- data/lib/salamander/support/radians.rb +4 -4
- data/lib/salamander/version.rb +1 -1
- metadata +4 -2
data/bin/salamander
CHANGED
@@ -7,8 +7,9 @@ class Sketch < Salamander::Actor
|
|
7
7
|
include Salamander::Drawing
|
8
8
|
end
|
9
9
|
|
10
|
-
|
11
|
-
Sketch.draw(
|
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
|
data/lib/salamander.rb
CHANGED
@@ -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
|
data/lib/salamander/actor.rb
CHANGED
@@ -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 (
|
7
|
-
actor = new(
|
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 (
|
17
|
-
@
|
15
|
+
def initialize (canvas)
|
16
|
+
@canvas = canvas
|
18
17
|
|
19
|
-
move_to(
|
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
|
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
|
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
|
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 %
|
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
|
90
|
-
def
|
91
|
-
@
|
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
|
data/lib/salamander/constants.rb
CHANGED
@@ -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",
|
data/lib/salamander/drawing.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
15
|
+
canvas.line(x1, y1, x2, y2, color)
|
16
16
|
end
|
17
17
|
end
|
18
18
|
end
|
@@ -1,11 +1,11 @@
|
|
1
1
|
class Numeric
|
2
|
-
#
|
2
|
+
# Converts from radians to degrees.
|
3
3
|
def radians
|
4
|
-
self
|
4
|
+
self * 180.0 / Math::PI
|
5
5
|
end
|
6
6
|
|
7
|
-
#
|
7
|
+
# Does nothing - input is assumed to be degrees already
|
8
8
|
def degrees
|
9
|
-
self
|
9
|
+
self
|
10
10
|
end
|
11
11
|
end
|
data/lib/salamander/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
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
|